The old code gives arguments to a printf function which can't work
correctly, and the compiler complains. * grub-core/tests/example_functional_test.c (example_test): Add missing text. * grub-core/tests/lib/test.c (add_failure): Rewrite. * include/grub/test.h (grub_test_assert_helper): New declaration. (grub_test_assert): Use grub_test_assert_helper.
This commit is contained in:
parent
526ef13d34
commit
2787ae53d6
4 changed files with 103 additions and 12 deletions
11
ChangeLog
11
ChangeLog
|
@ -1,3 +1,14 @@
|
||||||
|
2012-05-27 Peter Jones <pjones@redhat.com>
|
||||||
|
|
||||||
|
The old code gives arguments to a printf function which can't work
|
||||||
|
correctly, and the compiler complains.
|
||||||
|
|
||||||
|
* grub-core/tests/example_functional_test.c (example_test): Add
|
||||||
|
missing text.
|
||||||
|
* grub-core/tests/lib/test.c (add_failure): Rewrite.
|
||||||
|
* include/grub/test.h (grub_test_assert_helper): New declaration.
|
||||||
|
(grub_test_assert): Use grub_test_assert_helper.
|
||||||
|
|
||||||
2012-05-27 Vladimir Serbinenko <phcoder@gmail.com>
|
2012-05-27 Vladimir Serbinenko <phcoder@gmail.com>
|
||||||
|
|
||||||
* grub-core/Makefile.core.def (example_functional_test): Rename to ...
|
* grub-core/Makefile.core.def (example_functional_test): Rename to ...
|
||||||
|
|
|
@ -27,7 +27,7 @@ static void
|
||||||
example_test (void)
|
example_test (void)
|
||||||
{
|
{
|
||||||
/* Check if 1st argument is true and report with default error message. */
|
/* Check if 1st argument is true and report with default error message. */
|
||||||
grub_test_assert (1 == 1);
|
grub_test_assert (1 == 1, "1 equal 1 expected");
|
||||||
|
|
||||||
/* Check if 1st argument is true and report with custom error message. */
|
/* Check if 1st argument is true and report with custom error message. */
|
||||||
grub_test_assert (2 == 2, "2 equal 2 expected");
|
grub_test_assert (2 == 2, "2 equal 2 expected");
|
||||||
|
|
|
@ -43,22 +43,75 @@ typedef struct grub_test_failure *grub_test_failure_t;
|
||||||
grub_test_t grub_test_list;
|
grub_test_t grub_test_list;
|
||||||
static grub_test_failure_t failure_list;
|
static grub_test_failure_t failure_list;
|
||||||
|
|
||||||
static void
|
static grub_test_failure_t
|
||||||
add_failure (const char *file,
|
failure_start(const char *file, const char *funp, grub_uint32_t line);
|
||||||
const char *funp,
|
static grub_test_failure_t
|
||||||
grub_uint32_t line, const char *fmt, va_list args)
|
failure_start(const char *file, const char *funp, grub_uint32_t line)
|
||||||
{
|
{
|
||||||
grub_test_failure_t failure;
|
grub_test_failure_t failure;
|
||||||
|
|
||||||
failure = (grub_test_failure_t) grub_malloc (sizeof (*failure));
|
failure = (grub_test_failure_t) grub_malloc (sizeof (*failure));
|
||||||
if (!failure)
|
if (!failure)
|
||||||
return;
|
return NULL;
|
||||||
|
|
||||||
failure->file = grub_strdup (file ? : "<unknown_file>");
|
failure->file = grub_strdup (file ? : "<unknown_file>");
|
||||||
failure->funp = grub_strdup (funp ? : "<unknown_function>");
|
if (!failure->file)
|
||||||
failure->line = line;
|
{
|
||||||
failure->message = grub_xvasprintf (fmt, args);
|
grub_free(failure);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
failure->funp = grub_strdup (funp ? : "<unknown_function>");
|
||||||
|
if (!failure->funp)
|
||||||
|
{
|
||||||
|
grub_free(failure->file);
|
||||||
|
grub_free(failure);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
failure->line = line;
|
||||||
|
|
||||||
|
failure->message = NULL;
|
||||||
|
|
||||||
|
return failure;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
failure_append_vtext(grub_test_failure_t failure, const char *fmt, va_list args);
|
||||||
|
static void
|
||||||
|
failure_append_vtext(grub_test_failure_t failure, const char *fmt, va_list args)
|
||||||
|
{
|
||||||
|
char *msg = grub_xvasprintf(fmt, args);
|
||||||
|
if (failure->message)
|
||||||
|
{
|
||||||
|
char *oldmsg = failure->message;
|
||||||
|
|
||||||
|
failure->message = grub_xasprintf("%s%s", oldmsg, msg);
|
||||||
|
grub_free(oldmsg);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
failure->message = msg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
failure_append_text(grub_test_failure_t failure, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
|
||||||
|
va_start(args, fmt);
|
||||||
|
failure_append_vtext(failure, fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
add_failure (const char *file,
|
||||||
|
const char *funp,
|
||||||
|
grub_uint32_t line, const char *fmt, va_list args)
|
||||||
|
{
|
||||||
|
grub_test_failure_t failure = failure_start(file, funp, line);
|
||||||
|
failure_append_text(failure, fmt, args);
|
||||||
grub_list_push (GRUB_AS_LIST_P (&failure_list), GRUB_AS_LIST (failure));
|
grub_list_push (GRUB_AS_LIST_P (&failure_list), GRUB_AS_LIST (failure));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,6 +153,29 @@ grub_test_nonzero (int cond,
|
||||||
va_end (ap);
|
va_end (ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
grub_test_assert_helper (int cond, const char *file, const char *funp,
|
||||||
|
grub_uint32_t line, const char *condstr,
|
||||||
|
const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
grub_test_failure_t failure;
|
||||||
|
|
||||||
|
if (cond)
|
||||||
|
return;
|
||||||
|
|
||||||
|
failure = failure_start(file, funp, line);
|
||||||
|
failure_append_text(failure, "assert failed: %s ", condstr);
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
|
||||||
|
failure_append_vtext(failure, fmt, ap);
|
||||||
|
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
grub_list_push (GRUB_AS_LIST_P (&failure_list), GRUB_AS_LIST (failure));
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
grub_test_register (const char *name, void (*test_main) (void))
|
grub_test_register (const char *name, void (*test_main) (void))
|
||||||
{
|
{
|
||||||
|
|
|
@ -54,10 +54,14 @@ void grub_test_nonzero (int cond, const char *file,
|
||||||
__attribute__ ((format (printf, 5, 6)));
|
__attribute__ ((format (printf, 5, 6)));
|
||||||
|
|
||||||
/* Macro to fill in location details and an optional error message. */
|
/* Macro to fill in location details and an optional error message. */
|
||||||
|
void grub_test_assert_helper (int cond, const char *file,
|
||||||
|
const char *func, grub_uint32_t line,
|
||||||
|
const char *condstr, const char *fmt, ...)
|
||||||
|
__attribute__ ((format (printf, 6, 7)));
|
||||||
|
|
||||||
#define grub_test_assert(cond, ...) \
|
#define grub_test_assert(cond, ...) \
|
||||||
grub_test_nonzero(cond, GRUB_FILE, __FUNCTION__, __LINE__, \
|
grub_test_assert_helper(cond, GRUB_FILE, __FUNCTION__, __LINE__, \
|
||||||
## __VA_ARGS__, \
|
#cond, ## __VA_ARGS__);
|
||||||
"assert failed: %s", #cond)
|
|
||||||
|
|
||||||
void grub_unit_test_init (void);
|
void grub_unit_test_init (void);
|
||||||
void grub_unit_test_fini (void);
|
void grub_unit_test_fini (void);
|
||||||
|
|
Loading…
Reference in a new issue