source: mainline/uspace/app/tester/tester.c@ 901b302

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 901b302 was 713ba400, checked in by Vojtech Horky <vojtech.horky@…>, 7 years ago

Refactoring in tester and perf

Use can_cast_size_t_to_int instead of the body of this function.

Also replaced "if" with assert() as the overflow (see diff) can rarely
happen and actually denotes a problem with the test itself (really,
test name longer than MAX_INT is insane).

Notice how the code from kernel was ported to userspace without changes
to the error text that would printed :-).

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