9ns: ensure the parent of a rename target is a directory
[akaros.git] / kern / src / ktest / ktest.c
1 #include <stdbool.h>
2 #include <ktest.h>
3 #include <sys/queue.h>
4
5 /* Global string used to report info about the last completed test */
6 char ktest_msg[1024];
7
8 /* Global linked list used to store registered ktest suites */
9 SLIST_HEAD(suiteq, ktest_suite);
10 static struct suiteq ktest_suiteq = SLIST_HEAD_INITIALIZER(ktest_suiteq);
11
12 void register_ktest_suite(struct ktest_suite *suite)
13 {
14         SLIST_INSERT_HEAD(&ktest_suiteq, suite, link);
15 }
16
17 void run_registered_ktest_suites()
18 {
19         struct ktest_suite *suite = NULL;
20
21         SLIST_FOREACH(suite, &ktest_suiteq, link) {
22                 run_ktest_suite(suite);
23         }
24 }
25
26 void run_ktest_suite(struct ktest_suite *suite)
27 {
28         printk("<-- BEGIN_KERNEL_%s_TESTS -->\n", suite->name);
29
30         for (int i=0; i<suite->num_ktests; i++) {
31                 struct ktest *test = &suite->ktests[i];
32                 if (test->enabled) {
33                         uint64_t start = read_tsc();
34                         bool result = test->func();
35                         uint64_t end = read_tsc();
36                         uint64_t et_us = tsc2usec(end - start) % 1000000;
37                         uint64_t et_s = tsc2sec(end - start);
38                         char fmt[] = "\t%s   [%s](%llu.%06llus)   %s\n";
39
40                         if (result) {
41                                 printk(fmt, "PASSED", test->name, et_s, et_us,
42                                        "");
43                         } else {
44                                 printk(fmt, "FAILED", test->name, et_s, et_us,
45                                        ktest_msg);
46                         }
47                         /* Some older tests disable IRQs */
48                         enable_irq();
49                 } else {
50                         printk("\tDISABLED [%s]\n", test->name);
51                 }
52         }
53
54         printk("<-- END_KERNEL_%s_TESTS -->\n", suite->name);
55 }