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