1fcd613
From e51968bcd089db5efd5e33043e6e23592f696371 Mon Sep 17 00:00:00 2001
1fcd613
From: Peter Jones <pjones@redhat.com>
1fcd613
Date: Wed, 2 Mar 2011 13:13:44 -0500
1fcd613
Subject: [PATCH] Make get_test_assert() correctly format its output.
1fcd613
1fcd613
The old code gives arguments to a printf function which can't work
1fcd613
correctly, and the compiler complains.
1fcd613
---
1fcd613
 grub-core/tests/example_functional_test.c |    2 +-
1fcd613
 grub-core/tests/lib/test.c                |   88 +++++++++++++++++++++++++++--
1fcd613
 include/grub/test.h                       |   10 ++-
1fcd613
 3 files changed, 90 insertions(+), 10 deletions(-)
1fcd613
1fcd613
diff --git a/grub-core/tests/example_functional_test.c b/grub-core/tests/example_functional_test.c
1fcd613
index 5259881..0c69749 100644
1fcd613
--- a/grub-core/tests/example_functional_test.c
1fcd613
+++ b/grub-core/tests/example_functional_test.c
1fcd613
@@ -24,7 +24,7 @@ static void
1fcd613
 example_test (void)
1fcd613
 {
1fcd613
   /* Check if 1st argument is true and report with default error message.  */
1fcd613
-  grub_test_assert (1 == 1);
1fcd613
+  grub_test_assert (1 == 1, "1 equal 1 expected");
1fcd613
 
1fcd613
   /* Check if 1st argument is true and report with custom error message.  */
1fcd613
   grub_test_assert (2 == 2, "2 equal 2 expected");
1fcd613
diff --git a/grub-core/tests/lib/test.c b/grub-core/tests/lib/test.c
1fcd613
index 06d78b7..8453d5b 100644
1fcd613
--- a/grub-core/tests/lib/test.c
1fcd613
+++ b/grub-core/tests/lib/test.c
1fcd613
@@ -42,22 +42,75 @@ typedef struct grub_test_failure *grub_test_failure_t;
1fcd613
 grub_test_t grub_test_list;
1fcd613
 static grub_test_failure_t failure_list;
1fcd613
 
1fcd613
-static void
1fcd613
-add_failure (const char *file,
1fcd613
-	     const char *funp,
1fcd613
-	     grub_uint32_t line, const char *fmt, va_list args)
1fcd613
+static grub_test_failure_t
1fcd613
+failure_start(const char *file, const char *funp, grub_uint32_t line);
1fcd613
+static grub_test_failure_t
1fcd613
+failure_start(const char *file, const char *funp, grub_uint32_t line)
1fcd613
 {
1fcd613
   grub_test_failure_t failure;
1fcd613
 
1fcd613
   failure = (grub_test_failure_t) grub_malloc (sizeof (*failure));
1fcd613
   if (!failure)
1fcd613
-    return;
1fcd613
+    return NULL;
1fcd613
 
1fcd613
   failure->file = grub_strdup (file ? : "<unknown_file>");
1fcd613
+  if (!failure->file)
1fcd613
+    {
1fcd613
+      grub_free(failure);
1fcd613
+      return NULL;
1fcd613
+    }
1fcd613
+
1fcd613
   failure->funp = grub_strdup (funp ? : "<unknown_function>");
1fcd613
+  if (!failure->funp)
1fcd613
+    {
1fcd613
+      grub_free(failure->file);
1fcd613
+      grub_free(failure);
1fcd613
+      return NULL;
1fcd613
+    }
1fcd613
+
1fcd613
   failure->line = line;
1fcd613
-  failure->message = grub_xvasprintf (fmt, args);
1fcd613
 
1fcd613
+  failure->message = NULL;
1fcd613
+
1fcd613
+  return failure;
1fcd613
+}
1fcd613
+
1fcd613
+static void
1fcd613
+failure_append_vtext(grub_test_failure_t failure, const char *fmt, va_list args);
1fcd613
+static void
1fcd613
+failure_append_vtext(grub_test_failure_t failure, const char *fmt, va_list args)
1fcd613
+{
1fcd613
+  char *msg = grub_xvasprintf(fmt, args);
1fcd613
+  if (failure->message)
1fcd613
+    {
1fcd613
+      char *oldmsg = failure->message;
1fcd613
+
1fcd613
+      failure->message = grub_xasprintf("%s%s", oldmsg, msg);
1fcd613
+      grub_free(oldmsg);
1fcd613
+    }
1fcd613
+  else
1fcd613
+    {
1fcd613
+      failure->message = msg;
1fcd613
+    }
1fcd613
+}
1fcd613
+
1fcd613
+static void
1fcd613
+failure_append_text(grub_test_failure_t failure, const char *fmt, ...)
1fcd613
+{
1fcd613
+  va_list args;
1fcd613
+
1fcd613
+  va_start(args, fmt);
1fcd613
+  failure_append_vtext(failure, fmt, args);
1fcd613
+  va_end(args);
1fcd613
+}
1fcd613
+
1fcd613
+static void
1fcd613
+add_failure (const char *file,
1fcd613
+	     const char *funp,
1fcd613
+	     grub_uint32_t line, const char *fmt, va_list args)
1fcd613
+{
1fcd613
+  grub_test_failure_t failure = failure_start(file, funp, line);
1fcd613
+  failure_append_text(failure, fmt, args);
1fcd613
   grub_list_push (GRUB_AS_LIST_P (&failure_list), GRUB_AS_LIST (failure));
1fcd613
 }
1fcd613
 
1fcd613
@@ -100,6 +153,29 @@ grub_test_nonzero (int cond,
1fcd613
 }
1fcd613
 
1fcd613
 void
1fcd613
+grub_test_assert_helper (int cond, const char *file, const char *funp,
1fcd613
+			 grub_uint32_t line, const char *condstr,
1fcd613
+			 const char *fmt, ...)
1fcd613
+{
1fcd613
+  va_list ap;
1fcd613
+  grub_test_failure_t failure;
1fcd613
+
1fcd613
+  if (cond)
1fcd613
+    return;
1fcd613
+
1fcd613
+  failure = failure_start(file, funp, line);
1fcd613
+  failure_append_text(failure, "assert failed: %s ", condstr);
1fcd613
+
1fcd613
+  va_start(ap, fmt);
1fcd613
+
1fcd613
+  failure_append_vtext(failure, fmt, ap);
1fcd613
+
1fcd613
+  va_end(ap);
1fcd613
+
1fcd613
+  grub_list_push (GRUB_AS_LIST_P (&failure_list), GRUB_AS_LIST (failure));
1fcd613
+}
1fcd613
+
1fcd613
+void
1fcd613
 grub_test_register (const char *name, void (*test_main) (void))
1fcd613
 {
1fcd613
   grub_test_t test;
1fcd613
diff --git a/include/grub/test.h b/include/grub/test.h
1fcd613
index 336d3b6..77a7598 100644
1fcd613
--- a/include/grub/test.h
1fcd613
+++ b/include/grub/test.h
1fcd613
@@ -53,10 +53,14 @@ void grub_test_nonzero (int cond, const char *file,
1fcd613
   __attribute__ ((format (printf, 5, 6)));
1fcd613
 
1fcd613
 /* Macro to fill in location details and an optional error message.  */
1fcd613
+void grub_test_assert_helper (int cond, const char *file,
1fcd613
+			     const char *func, grub_uint32_t line,
1fcd613
+			     const char *condstr, const char *fmt, ...)
1fcd613
+  __attribute__ ((format (printf, 6, 7)));
1fcd613
+
1fcd613
 #define grub_test_assert(cond, ...)				\
1fcd613
-  grub_test_nonzero(cond, GRUB_FILE, __FUNCTION__, __LINE__,	\
1fcd613
-		    ## __VA_ARGS__,				\
1fcd613
-		    "assert failed: %s", #cond)
1fcd613
+  grub_test_assert_helper(cond, GRUB_FILE, __FUNCTION__, __LINE__,	\
1fcd613
+			  #cond, ## __VA_ARGS__);
1fcd613
 
1fcd613
 /* Macro to define a unit test.  */
1fcd613
 #define GRUB_UNIT_TEST(name, funp)		\
1fcd613
-- 
1fcd613
1.7.3.1
1fcd613