1 | /*
|
---|
2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
3 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
4 | * Copyright (c) 2007 Martin Decky
|
---|
5 | * All rights reserved.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | *
|
---|
11 | * - Redistributions of source code must retain the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer in the
|
---|
15 | * documentation and/or other materials provided with the distribution.
|
---|
16 | * - The name of the author may not be used to endorse or promote products
|
---|
17 | * derived from this software without specific prior written permission.
|
---|
18 | *
|
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /** @addtogroup tester
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 | /**
|
---|
35 | * @file
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <assert.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <stddef.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <str.h>
|
---|
43 | #include <io/log.h>
|
---|
44 | #include <types/casting.h>
|
---|
45 | #include "tester.h"
|
---|
46 |
|
---|
47 | bool test_quiet;
|
---|
48 | int test_argc;
|
---|
49 | char **test_argv;
|
---|
50 |
|
---|
51 | test_t tests[] = {
|
---|
52 | #include "thread/deadlock.def"
|
---|
53 | #include "thread/thread1.def"
|
---|
54 | #include "thread/setjmp1.def"
|
---|
55 | #include "print/print1.def"
|
---|
56 | #include "print/print2.def"
|
---|
57 | #include "print/print3.def"
|
---|
58 | #include "print/print4.def"
|
---|
59 | #include "print/print5.def"
|
---|
60 | #include "print/print6.def"
|
---|
61 | #include "console/console1.def"
|
---|
62 | #include "stdio/stdio1.def"
|
---|
63 | #include "stdio/stdio2.def"
|
---|
64 | #include "stdio/logger1.def"
|
---|
65 | #include "stdio/logger2.def"
|
---|
66 | #include "fault/fault1.def"
|
---|
67 | #include "fault/fault2.def"
|
---|
68 | #include "fault/fault3.def"
|
---|
69 | #include "float/float1.def"
|
---|
70 | #include "float/float2.def"
|
---|
71 | #include "vfs/vfs1.def"
|
---|
72 | #include "ipc/readwrite.def"
|
---|
73 | #include "ipc/sharein.def"
|
---|
74 | #include "ipc/starve.def"
|
---|
75 | #include "loop/loop1.def"
|
---|
76 | #include "mm/malloc1.def"
|
---|
77 | #include "mm/malloc2.def"
|
---|
78 | #include "mm/malloc3.def"
|
---|
79 | #include "mm/mapping1.def"
|
---|
80 | #include "mm/pager1.def"
|
---|
81 | #include "hw/serial/serial1.def"
|
---|
82 | #include "chardev/chardev1.def"
|
---|
83 | { NULL, NULL, NULL, false }
|
---|
84 | };
|
---|
85 |
|
---|
86 | static bool run_test(test_t *test)
|
---|
87 | {
|
---|
88 | /* Execute the test */
|
---|
89 | const char *ret = test->entry();
|
---|
90 |
|
---|
91 | if (ret == NULL) {
|
---|
92 | printf("\nTest passed\n");
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 |
|
---|
96 | printf("\n%s\n", ret);
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 |
|
---|
100 | static int run_safe_tests(void)
|
---|
101 | {
|
---|
102 | test_t *test;
|
---|
103 | unsigned int i = 0;
|
---|
104 | unsigned int n = 0;
|
---|
105 |
|
---|
106 | char *failed_names = NULL;
|
---|
107 |
|
---|
108 | printf("\n*** Running all safe tests ***\n\n");
|
---|
109 |
|
---|
110 | for (test = tests; test->name != NULL; test++) {
|
---|
111 | if (!test->safe)
|
---|
112 | continue;
|
---|
113 |
|
---|
114 | printf("%s (%s)\n", test->name, test->desc);
|
---|
115 | if (run_test(test)) {
|
---|
116 | i++;
|
---|
117 | continue;
|
---|
118 | }
|
---|
119 |
|
---|
120 | if (!failed_names) {
|
---|
121 | failed_names = str_dup(test->name);
|
---|
122 | } else {
|
---|
123 | char *f = NULL;
|
---|
124 | asprintf(&f, "%s, %s", failed_names, test->name);
|
---|
125 | if (!f) {
|
---|
126 | printf("Out of memory.\n");
|
---|
127 | abort();
|
---|
128 | }
|
---|
129 | free(failed_names);
|
---|
130 | failed_names = f;
|
---|
131 | }
|
---|
132 | n++;
|
---|
133 | }
|
---|
134 |
|
---|
135 | printf("\nCompleted, %u tests run, %u passed.\n", i + n, i);
|
---|
136 | if (failed_names)
|
---|
137 | printf("Failed tests: %s\n", failed_names);
|
---|
138 |
|
---|
139 | return n;
|
---|
140 | }
|
---|
141 |
|
---|
142 | static void list_tests(void)
|
---|
143 | {
|
---|
144 | size_t len = 0;
|
---|
145 | test_t *test;
|
---|
146 | for (test = tests; test->name != NULL; test++) {
|
---|
147 | if (str_length(test->name) > len)
|
---|
148 | len = str_length(test->name);
|
---|
149 | }
|
---|
150 |
|
---|
151 | assert(can_cast_size_t_to_int(len) && "test name length overflow");
|
---|
152 |
|
---|
153 | for (test = tests; test->name != NULL; test++)
|
---|
154 | printf("%-*s %s%s\n", (int) len, test->name, test->desc,
|
---|
155 | (test->safe ? "" : " (unsafe)"));
|
---|
156 |
|
---|
157 | printf("%-*s Run all safe tests\n", (int) len, "*");
|
---|
158 | }
|
---|
159 |
|
---|
160 | int main(int argc, char *argv[])
|
---|
161 | {
|
---|
162 | if (argc < 2) {
|
---|
163 | printf("Usage:\n\n");
|
---|
164 | printf("%s <test> [args ...]\n\n", argv[0]);
|
---|
165 | list_tests();
|
---|
166 | return 0;
|
---|
167 | }
|
---|
168 |
|
---|
169 | log_init("tester");
|
---|
170 |
|
---|
171 | test_quiet = false;
|
---|
172 | test_argc = argc - 2;
|
---|
173 | test_argv = argv + 2;
|
---|
174 |
|
---|
175 | if (str_cmp(argv[1], "*") == 0) {
|
---|
176 | return run_safe_tests();
|
---|
177 | }
|
---|
178 |
|
---|
179 | test_t *test;
|
---|
180 | for (test = tests; test->name != NULL; test++) {
|
---|
181 | if (str_cmp(argv[1], test->name) == 0) {
|
---|
182 | return (run_test(test) ? 0 : -1);
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | printf("Unknown test \"%s\"\n", argv[1]);
|
---|
187 | return -2;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /** @}
|
---|
191 | */
|
---|