Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / tests / lib / test.c
blob3000fc88d24c5b4da15bed96caef806564203316
1 /*
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/>.
19 #include <grub/mm.h>
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. */
30 char *file;
32 /* The test function name. */
33 char *funp;
35 /* The test call line number. */
36 grub_uint32_t line;
38 /* The test failure message. */
39 char *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));
54 if (!failure)
55 return NULL;
57 failure->file = grub_strdup (file ? : "<unknown_file>");
58 if (!failure->file)
60 grub_free(failure);
61 return NULL;
64 failure->funp = grub_strdup (funp ? : "<unknown_function>");
65 if (!failure->funp)
67 grub_free(failure->file);
68 grub_free(failure);
69 return NULL;
72 failure->line = line;
74 failure->message = NULL;
76 return failure;
79 static void
80 failure_append_vtext(grub_test_failure_t failure, const char *fmt, va_list args);
81 static void
82 failure_append_vtext(grub_test_failure_t failure, const char *fmt, va_list args)
84 char *msg = grub_xvasprintf(fmt, args);
85 if (failure->message)
87 char *oldmsg = failure->message;
89 failure->message = grub_xasprintf("%s%s", oldmsg, msg);
90 grub_free (oldmsg);
91 grub_free (msg);
93 else
95 failure->message = msg;
99 static void
100 failure_append_text(grub_test_failure_t failure, const char *fmt, ...)
102 va_list args;
104 va_start(args, fmt);
105 failure_append_vtext(failure, fmt, args);
106 va_end(args);
109 static void
110 add_failure (const char *file,
111 const char *funp,
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));
119 static void
120 free_failures (void)
122 grub_test_failure_t item;
124 while (failure_list)
126 item = failure_list;
127 failure_list = item->next;
128 if (item->message)
129 grub_free (item->message);
131 if (item->funp)
132 grub_free (item->funp);
134 if (item->file)
135 grub_free (item->file);
137 grub_free (item);
139 failure_list = 0;
142 void
143 grub_test_nonzero (int cond,
144 const char *file,
145 const char *funp, grub_uint32_t line, const char *fmt, ...)
147 va_list ap;
149 if (cond)
150 return;
152 va_start (ap, fmt);
153 add_failure (file, funp, line, fmt, ap);
154 va_end (ap);
157 void
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, ...)
162 va_list ap;
163 grub_test_failure_t failure;
165 if (cond)
166 return;
168 failure = failure_start(file, funp, line);
169 failure_append_text(failure, "assert failed: %s ", condstr);
171 va_start(ap, fmt);
173 failure_append_vtext(failure, fmt, ap);
175 va_end(ap);
177 grub_list_push (GRUB_AS_LIST_P (&failure_list), GRUB_AS_LIST (failure));
180 void
181 grub_test_register (const char *name, void (*test_main) (void))
183 grub_test_t test;
185 test = (grub_test_t) grub_malloc (sizeof (*test));
186 if (!test)
187 return;
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));
195 void
196 grub_test_unregister (const char *name)
198 grub_test_t test;
200 test = (grub_test_t) grub_named_list_find
201 (GRUB_AS_NAMED_LIST (grub_test_list), name);
203 if (test)
205 grub_list_remove (GRUB_AS_LIST (test));
207 if (test->name)
208 grub_free (test->name);
210 grub_free (test);
215 grub_test_run (grub_test_t test)
217 grub_test_failure_t failure;
219 test->main ();
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>"));
228 if (!failure_list)
230 grub_printf ("%s: PASS\n", test->name);
231 return GRUB_ERR_NONE;
233 else
235 grub_printf ("%s: FAIL\n", test->name);
236 free_failures ();
237 return GRUB_ERR_TEST_FAILURE;