Finalize arg, env, aux migration (1/3) (CXX) (BB)
[akaros.git] / kern / include / test_infrastructure.h
1 #ifndef ROS_INC_TEST_INFRASTRUCTURE_H
2 #define ROS_INC_TEST_INFRASTRUCTURE_H
3
4 /*
5  * Header file with infrastructure needed for kernel unit tests:
6  *  - Assertion functions.
7  *  - Test structures.
8  */
9
10 #include <stdio.h>
11 #include <kmalloc.h>
12
13 /* Macros for assertions. 
14  * They depend on <stdbool.h> and printk() to be included in the source file. 
15  */
16 #define KT_ASSERT_M(message, test)                                               \
17         do {                                                                         \
18                 if (!(test)) {                                                           \
19                         extern char *kern_test_msg;                                          \
20                         char prefix[] = "Assertion failed: ";                                \
21                         int msg_size = sizeof(prefix) + sizeof(message) - 1;                 \
22                         kern_test_msg = (char*) kmalloc(msg_size, 0);                        \
23                         snprintf(kern_test_msg, msg_size, "%s%s", prefix, message);          \
24                         return false;                                                        \
25                 }                                                                        \
26         } while (0)
27
28 #define KT_ASSERT(test)                                                          \
29         do {                                                                         \
30                 if (!(test)) {                                                           \
31                         return false;                                                        \
32                 }                                                                        \
33         } while (0)
34
35
36 /* Postboot kernel tests: tests ran after boot in kernel mode. */
37
38 struct pb_kernel_test {
39         char name[256]; // Name of the test function.
40         bool (*func)(void); // Name of the test function, should be equal to 'name'.
41         bool enabled; // Whether to run or not the test.
42 };
43
44 /* A macro for testing if another macro has been #defined or not.  Can be used
45  * wherever you need a boolean defined.  Returns 0 or 1. */
46 #define is_defined(macro) is_defined_(macro)
47 #define is_defined_test_1 ,
48 #define is_defined_(value) is_defined__(is_defined_test_##value, value)
49 #define is_defined__(comma, value) is_defined___(comma value, 0)
50 #define is_defined___(_, v, ...) v
51
52 /* Macro for registering a postboot kernel test. */
53 #define PB_K_TEST_REG(name, config) \
54         {"test_" #name, test_##name, is_defined(config)}
55
56 extern struct pb_kernel_test pb_kernel_tests[];
57 extern int num_pb_kernel_tests;
58
59 #endif // ROS_INC_TEST_INFRASTRUCTURE_H