Initial import
[sliver-openvswitch.git] / datapath / unit.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007 The Board of Trustees of The Leland Stanford Junior Univer
4 sity
5  */
6
7 #include <linux/autoconf.h>
8 #include <linux/module.h>
9 #include <linux/string.h>
10 #include <linux/version.h>
11 #include <linux/errno.h>
12
13 #include "unit.h"
14
15 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
16 static char run[1024];
17 module_param_string(run, run, sizeof run, 0);
18 MODULE_PARM_DESC(run_tests, "run=\"test1,[test2,...]\"\n");
19 #else
20 static char *run;
21 MODULE_PARM(run, "s");
22 #endif
23
24 static int test_failed;
25 static const char *test_name;
26
27 void unit_fail_function(const char *function, const char *msg, ...) 
28 {
29         va_list args;
30
31         printk("%s: FAIL: %s: ", test_name, function);
32         va_start(args, msg);
33         vprintk(msg, args);
34         va_end(args);
35         printk("\n");
36         test_failed = 1;
37 }
38
39 int unit_failed(void) 
40 {
41         return test_failed;
42 }
43
44 static int run_test(const char *name, size_t len)
45 {
46         static const struct test {
47                 const char *name;
48                 void (*func)(void);
49         } tests[] = {
50 #define UNIT_TEST(NAME) {#NAME, run_##NAME},
51                 UNIT_TESTS
52 #undef UNIT_TEST
53         };
54
55         const struct test *p;
56
57         for (p = tests; p < &tests[ARRAY_SIZE(tests)]; p++)
58                 if (len == strlen(p->name)
59                         && !memcmp(name, p->name, len)) {
60                         test_name = p->name;
61                         test_failed = 0;
62                         p->func();
63                         printk("%s: %s\n", test_name,
64                                                 test_failed ? "FAIL" : "PASS");
65                         return !test_failed;
66                 }
67         printk("unknown unit test %.*s\n", (int) len, name);
68         return 0;
69 }
70
71 int unit_init(void)
72 {
73         int n_pass = 0, n_fail = 0;
74         char *p = run ?: "";
75         for (;;) {
76                 static const char white_space[] = " \t\r\n\v,";
77                 int len;
78
79                 p += strspn(p, white_space);
80                 if (!*p)
81                         break;
82
83                 len = strcspn(p, white_space);
84                 if (run_test(p, len))
85                         n_pass++;
86                 else
87                         n_fail++;
88                 p += len;
89         }
90
91         if (n_pass + n_fail == 0)
92                 printk("no tests specified (use run=\"test1 [test2...]\")\n");
93         else
94                 printk("%d tests passed, %d failed\n", n_pass, n_fail);
95
96         return -ENODEV;
97 }
98
99 module_init(unit_init);
100 MODULE_LICENSE("GPL");