valFuzz 1.2.0
Loading...
Searching...
No Matches
test.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#include <valfuzz/test.hpp>
7
8namespace valfuzz
9{
10
11std::deque<valfuzz::test_pair> &get_tests()
12{
13 static std::deque<valfuzz::test_pair> registered_tests = {};
14 return registered_tests;
15}
16
17long unsigned int get_num_tests()
18{
19 auto &tests = get_tests();
20 return tests.size();
21}
22
23std::atomic<bool> &get_has_failed_once()
24{
25#if __cplusplus >= 202002L // C++20
26 constinit
27#endif
28 static std::atomic<bool>
29 has_failed_once = false;
30 return has_failed_once;
31}
32
33std::function<void()> &get_function_execute_before()
34{
35 static std::function<void()> function_execute_before = []() {};
36 return function_execute_before;
37}
38
39std::function<void()> &get_function_execute_after()
40{
41 static std::function<void()> function_execute_after = []() {};
42 return function_execute_after;
43}
44
45void set_function_execute_before(std::function<void()> f)
46{
47 auto &function_execute_before = get_function_execute_before();
48 function_execute_before = f;
49}
50
51void set_function_execute_after(std::function<void()> f)
52{
53 auto &function_execute_after = get_function_execute_after();
54 function_execute_after = f;
55}
56
57void set_has_failed_once(bool has_failed_once)
58{
59 auto &has_failed_once_ref = get_has_failed_once();
60 has_failed_once_ref = has_failed_once;
61}
62
63void add_test(const std::string &name, test_function test)
64{
65 auto &tests = get_tests();
66 std::lock_guard<std::mutex> lock(get_tests_mutex());
67 tests.push_back({name, test});
68}
69
70std::optional<valfuzz::test_pair> pop_test_or_null()
71{
72 auto &tests = get_tests();
73 auto &tests_mutex = get_tests_mutex();
74
75 std::lock_guard<std::mutex> lock(tests_mutex);
76 if (tests.empty())
77 {
78 return std::nullopt;
79 }
80 valfuzz::test_pair test = tests.front();
81 tests.pop_front();
82 return test;
83}
84
85void run_one_test(const std::string &name)
86{
87 auto &tests = get_tests();
88 bool found = false;
89 for (auto &test : tests)
90 {
91 if (test.first == name)
92 {
93 {
94 std::lock_guard<std::mutex> lock(get_stream_mutex());
95 std::cout << "Running test: " << test.first << "\n";
96 }
97 found = true;
98 test.second(test.first);
99 break;
100 }
101 }
102 if (!found)
103 {
104 std::lock_guard<std::mutex> lock(get_stream_mutex());
105 std::cout << "Test \"" << name << "\" not found\n";
106 std::exit(1);
107 }
108}
109
111{
112 auto test = std::optional<valfuzz::test_pair>{};
113 while ((test = pop_test_or_null()).has_value())
114 {
115 // run task
116 if (get_verbose())
117 {
118 std::lock_guard<std::mutex> lock(get_stream_mutex());
119 std::cout << "Running test: \"" << test.value().first << "\"\n";
120 }
121 test.value().second(test.value().first);
122 }
123}
124
126{
127 if (get_is_threaded())
128 {
129 auto &thread_pool = get_thread_pool();
130 // spawn threads
131 for (long unsigned int i = 0;
132 i < get_max_num_threads() && i < get_num_tests(); i++)
133 {
134 thread_pool.push_back(std::thread(_run_tests));
135 }
136 for (auto &thread : get_thread_pool())
137 {
138 thread.join();
139 }
140 }
141 else
142 {
143 _run_tests();
144 }
145}
146
147} // namespace valfuzz
std::deque< valfuzz::test_pair > & get_tests()
Definition test.cpp:11
std::function< void()> & get_function_execute_after()
Definition test.cpp:39
std::optional< valfuzz::test_pair > pop_test_or_null()
Definition test.cpp:70
std::atomic< long unsigned int > & get_max_num_threads()
Definition common.cpp:30
void set_function_execute_before(std::function< void()> f)
Definition test.cpp:45
void _run_tests()
Definition test.cpp:110
long unsigned int get_num_tests()
Definition test.cpp:17
std::vector< std::thread > & get_thread_pool()
Definition common.cpp:59
void run_tests()
Definition test.cpp:125
std::mutex & get_tests_mutex()
Definition common.cpp:40
std::atomic< bool > & get_verbose()
Definition common.cpp:20
void run_one_test(const std::string &name)
Definition test.cpp:85
void set_function_execute_after(std::function< void()> f)
Definition test.cpp:51
void set_has_failed_once(bool has_failed_once)
Definition test.cpp:57
std::pair< std::string, test_function > test_pair
Definition common.hpp:19
std::atomic< bool > & get_is_threaded()
Definition common.cpp:49
std::function< void(std::string)> test_function
Definition common.hpp:18
std::atomic< bool > & get_has_failed_once()
Definition test.cpp:23
std::function< void()> & get_function_execute_before()
Definition test.cpp:33
void add_test(const std::string &name, test_function test)
Definition test.cpp:63
std::mutex & get_stream_mutex()
Definition common.cpp:11