Max OS 0.3
Loading...
Searching...
No Matches
test.h
Go to the documentation of this file.
1
9#ifndef MAXOS_TESTS_TEST_H
10#define MAXOS_TESTS_TEST_H
11
12#include <common/string.h>
13#include <common/logger.h>
14#include <common/macros.h>
15
17#define MAXOS_CONDITIONAL_TEST(name, type) \
18 auto CONCATENATE(test_, __COUNTER__) = new ConditionalTest(#name, type, []()
19
20namespace MaxOS::tests {
21
26 enum class TestStatus {
27 PASS,
28 FAIL,
29 SKIP
30 };
31
36 enum class TestType {
37 COMMON,
38 DRIVER,
39 FILESYSTEM,
40 GUI,
41 HARDWARE_COMMUNICATION,
42 MEMORY,
43 NETWORK,
44 PROCESSES,
45 RUNTIME,
46 SYSTEM,
47 MAX_TEST_TYPES
48 };
49
51 constexpr const char* TEST_TYPE_STRINGS[] = {
52 "COMMON",
53 "DRIVER",
54 "FILESYSTEM",
55 "GUI",
56 "HARDWARE_COMMUNICATION",
57 "MEMORY",
58 "NETWORK",
59 "PROCESSES",
60 "RUNTIME",
61 "SYSTEM",
62 "UNKNOWN"
63 };
64
69 class Test {
70
71 private:
72 string m_name;
73 TestType m_type;
74
75 public:
76 Test(const string& name, TestType type);
77 virtual ~Test();
78
80
86 virtual TestStatus execute() = 0;
87
88 const string& name() const;
89 TestType type() const;
90 };
91
101 template<typename T1, typename T2> bool compare(T1 const& actual, T2 const& expected) {
102
103 // Direct comparison
104 if (actual == expected)
105 return true;
106
107 // Report what failed
108 Logger::WARNING() << "Test failed comparison: expected [" << expected << "], got [" << actual << "]\n";
109 return false;
110 }
111
116 class ConditionalTest : public Test {
117 private:
118 bool (*m_condition)();
119
120 public:
121 ConditionalTest(const string& name, TestType type, bool (*condition)());
122 ~ConditionalTest() override;
123
124 TestStatus execute() override;
125 };
126
134
135 private:
136 inline static common::Vector<Test*> s_tests = common::Vector<Test*>();
137
138 public:
139
140 static void test_kernel();
141
142 static void run_all_tests();
143 static void run_all_types(TestType type);
144
145 static void add_all_tests();
146 static void add_test(Test* test);
147
148 };
149}
150
151#endif //MAXOS_TESTS_TEST_H
static Logger WARNING()
Gets active logger set to WARNING level.
Definition logger.cpp:205
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
A test that only checks a condition to determine pass/fail.
Definition test.h:116
TestStatus execute() override
Executes the conditional test based on the provided condition function.
Definition test.cpp:101
A class to run all tests.
Definition test.h:133
static void test_kernel()
Setups the TestRunner and runs all kernel tests.
Definition test.cpp:152
static void run_all_types(TestType type)
Runs all tests of a specific type.
Definition test.cpp:127
static void run_all_tests()
Runs all registered tests.
Definition test.cpp:116
static void add_all_tests()
Adds all predefined tests to the test runner.
Definition test.cpp:145
static void add_test(Test *test)
Adds a test to the test runner.
Definition test.cpp:138
A basic test class.
Definition test.h:69
const string & name() const
Gets the name of the test.
Definition test.cpp:68
TestStatus run()
Runs the test and returns the result.
Definition test.cpp:33
virtual TestStatus execute()=0
Executes the test and returns the status of the test.
TestType type() const
Gets the type of the test.
Definition test.cpp:77
Defines a Logger class for logging messages with different severity levels to multiple output streams...
Defines a String class for dynamically sized strings with various operations.
constexpr const char * TEST_TYPE_STRINGS[]
String representations of the TestType enum.
Definition test.h:51
TestType
The different types of tests.
Definition test.h:36
TestStatus
The possible outcomes of a test.
Definition test.h:26
bool compare(T1 const &actual, T2 const &expected)
Compares two values and logs a warning if they are not equal.
Definition test.h:101