2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2010 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/misc.h>
21 #include <grub/test.h>
23 struct grub_test_failure
25 /* The next failure. */
26 struct grub_test_failure
*next
;
27 struct grub_test_failure
**prev
;
29 /* The test source file name. */
32 /* The test function name. */
35 /* The test call line number. */
38 /* The test failure message. */
41 typedef struct grub_test_failure
*grub_test_failure_t
;
43 grub_test_t grub_test_list
;
44 static grub_test_failure_t failure_list
;
46 static grub_test_failure_t
47 failure_start(const char *file
, const char *funp
, grub_uint32_t line
);
48 static grub_test_failure_t
49 failure_start(const char *file
, const char *funp
, grub_uint32_t line
)
51 grub_test_failure_t failure
;
53 failure
= (grub_test_failure_t
) grub_malloc (sizeof (*failure
));
57 failure
->file
= grub_strdup (file
? : "<unknown_file>");
64 failure
->funp
= grub_strdup (funp
? : "<unknown_function>");
67 grub_free(failure
->file
);
74 failure
->message
= NULL
;
80 failure_append_vtext(grub_test_failure_t failure
, const char *fmt
, va_list args
);
82 failure_append_vtext(grub_test_failure_t failure
, const char *fmt
, va_list args
)
84 char *msg
= grub_xvasprintf(fmt
, args
);
87 char *oldmsg
= failure
->message
;
89 failure
->message
= grub_xasprintf("%s%s", oldmsg
, msg
);
95 failure
->message
= msg
;
100 failure_append_text(grub_test_failure_t failure
, const char *fmt
, ...)
105 failure_append_vtext(failure
, fmt
, args
);
110 add_failure (const char *file
,
112 grub_uint32_t line
, const char *fmt
, va_list args
)
114 grub_test_failure_t failure
= failure_start(file
, funp
, line
);
115 failure_append_text(failure
, fmt
, args
);
116 grub_list_push (GRUB_AS_LIST_P (&failure_list
), GRUB_AS_LIST (failure
));
122 grub_test_failure_t item
;
127 failure_list
= item
->next
;
129 grub_free (item
->message
);
132 grub_free (item
->funp
);
135 grub_free (item
->file
);
143 grub_test_nonzero (int cond
,
145 const char *funp
, grub_uint32_t line
, const char *fmt
, ...)
153 add_failure (file
, funp
, line
, fmt
, ap
);
158 grub_test_assert_helper (int cond
, const char *file
, const char *funp
,
159 grub_uint32_t line
, const char *condstr
,
160 const char *fmt
, ...)
163 grub_test_failure_t failure
;
168 failure
= failure_start(file
, funp
, line
);
169 failure_append_text(failure
, "assert failed: %s ", condstr
);
173 failure_append_vtext(failure
, fmt
, ap
);
177 grub_list_push (GRUB_AS_LIST_P (&failure_list
), GRUB_AS_LIST (failure
));
181 grub_test_register (const char *name
, void (*test_main
) (void))
185 test
= (grub_test_t
) grub_malloc (sizeof (*test
));
189 test
->name
= grub_strdup (name
);
190 test
->main
= test_main
;
192 grub_list_push (GRUB_AS_LIST_P (&grub_test_list
), GRUB_AS_LIST (test
));
196 grub_test_unregister (const char *name
)
200 test
= (grub_test_t
) grub_named_list_find
201 (GRUB_AS_NAMED_LIST (grub_test_list
), name
);
205 grub_list_remove (GRUB_AS_LIST (test
));
208 grub_free (test
->name
);
215 grub_test_run (grub_test_t test
)
217 grub_test_failure_t failure
;
221 grub_printf ("%s:\n", test
->name
);
222 FOR_LIST_ELEMENTS (failure
, failure_list
)
223 grub_printf (" %s:%s:%u: %s\n",
224 (failure
->file
? : "<unknown_file>"),
225 (failure
->funp
? : "<unknown_function>"),
226 failure
->line
, (failure
->message
? : "<no message>"));
230 grub_printf ("%s: PASS\n", test
->name
);
231 return GRUB_ERR_NONE
;
235 grub_printf ("%s: FAIL\n", test
->name
);
237 return GRUB_ERR_TEST_FAILURE
;