Handle pending signals even when resuming current_uthread
[akaros.git] / kern / include / ktest.h
1 #ifndef ROS_INC_KTEST_H
2 #define ROS_INC_KTEST_H
3
4 /*
5  * Header file with infrastructure needed for kernel unit tests:
6  *  - Assertion functions.
7  *  - Test structures.
8  */
9
10 #include <common.h>
11 #include <stdio.h>
12 #include <stdbool.h>
13 #include <kmalloc.h>
14 #include <arch/arch.h>
15 #include <time.h>
16 #include <sys/queue.h>
17
18 /* Macros for assertions. 
19  */
20 #define KT_ASSERT(test)                                                          \
21         KT_ASSERT_M("", test)
22
23 #define KT_ASSERT_M(message, test)                                               \
24         do {                                                                         \
25                 if (!(test)) {                                                           \
26                         char fmt[] = "Assertion failure in %s() at %s:%d: %s";               \
27                         snprintf(ktest_msg, sizeof(ktest_msg), fmt, __FUNCTION__,            \
28                                  __FILE__, __LINE__, message);                               \
29                         return false;                                                        \
30                 }                                                                        \
31         } while (0)
32
33 struct ktest {
34         char name[256]; // Name of the test function.
35         bool (*func)(void); // Name of the test function, should be equal to 'name'.
36         bool enabled; // Whether to run or not the test.
37 };
38
39 struct ktest_suite {
40     SLIST_ENTRY(ktest_suite) link;
41         char name[256];
42         struct ktest *ktests;
43         int num_ktests;
44 };
45
46 #define KTEST_SUITE(name) \
47         static struct ktest_suite ktest_suite = {{}, name, NULL, 0};
48
49 #define KTEST_REG(name, config) \
50         {"test_" #name, test_##name, is_defined(config)}
51
52 #define REGISTER_KTESTS(ktests, num_ktests)                                      \
53         do {                                                                         \
54                 ktest_suite.ktests = ktests;                                             \
55                 ktest_suite.num_ktests = num_ktests;                                     \
56                 register_ktest_suite(&ktest_suite);                                      \
57         } while (0)
58
59 /* Global string used to report info about the last completed test */
60 extern char ktest_msg[1024];
61
62 void register_ktest_suite(struct ktest_suite *suite);
63 void run_ktest_suite(struct ktest_suite *suite);
64 void run_registered_ktest_suites();
65
66 #endif // ROS_INC_KTEST_H