PatchworkOS
69292a3
A non-POSIX operating system.
Theme:
Default
Round
Robot
Loading...
Searching...
No Matches
test.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <
kernel/log/log.h
>
4
#include <
kernel/sched/clock.h
>
5
#include <
time.h
>
6
7
#ifndef _TESTING_
8
#error "This file is only meant to be used for testing"
9
#endif
10
11
/**
12
* @brief Kernel Test Framework.
13
* @defgroup kernel_utils_test Test
14
* @ingroup kernel_utils
15
*
16
* @{
17
*/
18
19
/**
20
* @brief Type of a test function.
21
*/
22
typedef
uint64_t
(*
test_func_t
)(void);
23
24
/**
25
* @brief Structure representing a test case.
26
* @struct test_t
27
*/
28
typedef
struct
test
29
{
30
const
char
*
name
;
31
test_func_t
func
;
32
}
test_t
;
33
34
/**
35
* @brief Run all registered tests in the `._tests` section.
36
*/
37
#define TEST_ALL() \
38
do \
39
{ \
40
extern test_t _tests_start; \
41
extern test_t _tests_end; \
42
const test_t* test = &_tests_start; \
43
while (test < &_tests_end) \
44
{ \
45
LOG_INFO("running test '%s'\n", test->name); \
46
clock_t start = clock_uptime(); \
47
uint64_t result = test->func(); \
48
clock_t end = clock_uptime(); \
49
if (result == ERR) \
50
{ \
51
LOG_ERR("test '%s' FAILED in %llu ms\n", test->name, (end - start) / (CLOCKS_PER_MS)); \
52
panic(NULL, "test failure"); \
53
} \
54
else \
55
{ \
56
LOG_INFO("test '%s' passed in %llu ms\n", test->name, (end - start) / (CLOCKS_PER_MS)); \
57
} \
58
test++; \
59
} \
60
} while (0)
61
62
/**
63
* @brief Define a test function to be run by `TEST_ALL()`.
64
*
65
* This will register the test within the current module or if used in the kernel, the kernel itself.
66
*
67
* Any module that wants to use the testing framework must call `TEST_ALL()` on its own.
68
*
69
* @param name The name of the test function.
70
*/
71
#define TEST_DEFINE(_name) \
72
uint64_t _test_func_##_name(void); \
73
const test_t __test_##_name __attribute__((used, section("._tests"))) = { \
74
.name = #_name, \
75
.func = _test_func_##_name, \
76
}; \
77
uint64_t _test_func_##_name(void)
78
79
/**
80
* @brief Assert a condition in a test.
81
*/
82
#define TEST_ASSERT(cond) \
83
do \
84
{ \
85
if (!(cond)) \
86
{ \
87
LOG_ERR("TEST_ASSERT failed '%s' at %s:%d\n", #cond, __FILE__, __LINE__); \
88
return ERR; \
89
} \
90
} while (0)
91
92
/** @} */
test_func_t
uint64_t(* test_func_t)(void)
Type of a test function.
Definition
test.h:22
clock.h
log.h
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
test_t
Structure representing a test case.
Definition
test.h:29
test_t::name
const char * name
Definition
test.h:30
test_t::func
test_func_t func
Definition
test.h:31
time.h
include
kernel
utils
test.h
Generated on Thu Jan 15 2026 15:55:29 for PatchworkOS by
1.9.8