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