STORMM Source Documentation
Loading...
Searching...
No Matches
unit_test.h
1// -*-c++-*-
2#ifndef STORMM_UNIT_TEST_H
3#define STORMM_UNIT_TEST_H
4
5#include <cmath>
6#include <cstdlib>
7#include <stdexcept>
8#include <string>
9#include <vector>
10#include "copyright.h"
11#include "DataTypes/common_types.h"
12#include "DataTypes/stormm_vector_types.h"
13#include "Parsing/polynumeric.h"
14#include "Parsing/textfile.h"
15#include "Reporting/error_format.h"
16#include "Math/summation.h"
17#include "Math/vector_ops.h"
18#include "approx.h"
19#include "checklist.h"
20#include "file_snapshot.h"
21#include "test_environment.h"
22#include "vector_report.h"
23
30#define CHECK_THROWS(test_code, error_message) {\
31 bool exception_thrown = false;\
32 try {\
33 test_code;\
34 }\
35 catch (std::runtime_error) {\
36 exception_thrown = true;\
37 }\
38 check(exception_thrown, error_message);\
39}
40
50#define CHECK_THROWS_SOFT(test_code, error_message, priority) {\
51 if (priority == stormm::testing::TestPriority::ABORT) {\
52 check(true, error_message, priority);\
53 }\
54 else {\
55 bool exception_thrown = false;\
56 try {\
57 test_code;\
58 }\
59 catch (std::runtime_error) {\
60 exception_thrown = true;\
61 }\
62 check(exception_thrown, error_message, priority);\
63 }\
64}
65
66namespace stormm {
67namespace testing {
68
69using errors::rtErr;
70using errors::terminalFormat;
71using data_types::isScalarType;
72using data_types::isSignedIntegralScalarType;
73using data_types::isUnsignedIntegralScalarType;
74using data_types::isFloatingPointScalarType;
75using stmath::sum;
76using stmath::maxAbsoluteDifference;
77using stmath::maxRelativeDifference;
78using stmath::meanUnsignedError;
79using stmath::relativeRmsError;
80using parse::NumberFormat;
81using parse::polyNumericVector;
82using parse::TextFile;
83
92void section(const std::string &section_name);
93void section(const int section_index);
95
99int unitTestSectionIndex(const std::string &name);
100
104std::string unitTestSectionName(int index);
105
128CheckResult check(const bool statement, const std::string &error_message = "",
129 TestPriority urgency = TestPriority::CRITICAL);
130
131CheckResult check(double lhs, RelationalOperator relationship, const Approx &rhs,
132 const std::string &error_message, TestPriority urgency = TestPriority::CRITICAL);
133
134CheckResult check(const Approx &lhs, RelationalOperator relationship, const double rhs,
135 const std::string &error_message, TestPriority urgency = TestPriority::CRITICAL);
136
137CheckResult check(const double lhs, RelationalOperator relationship, const double rhs,
138 const std::string &error_message, TestPriority urgency = TestPriority::CRITICAL);
139
140CheckResult check(const std::string &lhs, RelationalOperator relationship,
141 const std::string &rhs, const std::string &error_message,
142 TestPriority urgency = TestPriority::CRITICAL);
143
144CheckResult check(const char4 &lhs, RelationalOperator relationship, const char4 &rhs,
145 const std::string &error_message, TestPriority urgency = TestPriority::CRITICAL);
146
147CheckResult check(const std::vector<char4> &lhs, RelationalOperator relationship,
148 const std::vector<char4> &rhs, const std::string &error_message,
149 TestPriority urgency = TestPriority::CRITICAL);
151
158template <typename T>
159CheckResult check(const std::vector<T> &lhs, RelationalOperator relationship,
160 const Approx &rhs, const std::string &error_message,
161 TestPriority urgency = TestPriority::CRITICAL);
162
170template <typename T>
171CheckResult check(const Approx &lhs, const RelationalOperator relationship,
172 const std::vector<T> &rhs, const std::string &error_message,
173 TestPriority urgency = TestPriority::CRITICAL);
174
178template <typename T1, typename T2>
179CheckResult check(const std::vector<T1> &lhs, const RelationalOperator relationship,
180 const std::vector<T2> &rhs, const std::string &error_message,
181 TestPriority urgency = TestPriority::CRITICAL);
182
213CheckResult snapshot(const std::string &filename, const std::vector<PolyNumeric> &content,
214 const std::string &label = std::string(""),
215 double comparison_tolerance = 1.0e-4,
216 const std::string &error_message = std::string(""),
217 SnapshotOperation activity = SnapshotOperation::COMPARE,
218 double output_precision = 1.0e-8,
219 NumberFormat data_format = NumberFormat::STANDARD_REAL,
220 PrintSituation expectation = PrintSituation::OVERWRITE,
221 TestPriority urgency = TestPriority::CRITICAL);
222
223CheckResult snapshot(const std::string &filename, const std::vector<PolyNumeric> &content,
224 const std::string &label = std::string(""),
225 NumberFormat data_format = NumberFormat::STANDARD_REAL,
226 const std::string &error_message = std::string(""),
227 SnapshotOperation activity = SnapshotOperation::COMPARE,
228 double comparison_tolerance = 1.0e-4, double output_precision = 1.0e-8,
229 PrintSituation expectation = PrintSituation::OVERWRITE,
230 TestPriority urgency = TestPriority::CRITICAL);
231
232CheckResult snapshot(const std::string &filename, const TextFile &content,
233 const std::string &label = std::string(""),
234 const std::string &error_message = std::string(""),
235 const SnapshotOperation activity = SnapshotOperation::COMPARE,
236 const PrintSituation expectation = PrintSituation::OVERWRITE,
237 const TestPriority urgency = TestPriority::CRITICAL,
238 int comparison_tolerance = 0);
239
240CheckResult snapshot(const std::string &filename, const std::string &content,
241 const std::string &label = std::string(""),
242 const std::string &error_message = std::string(""),
243 const SnapshotOperation activity = SnapshotOperation::COMPARE,
244 const PrintSituation expectation = PrintSituation::OVERWRITE,
245 const TestPriority urgency = TestPriority::CRITICAL,
246 int comparison_tolerance = 0);
248
252void printTestSummary(TestVerbosity tv);
253
255int countGlobalTestFailures();
256
260std::string getRelationalOperatorString(RelationalOperator ro);
261
262} // namespace testing
263} // namespace stormm
264
267extern stormm::testing::CheckList gbl_test_results;
268
269#include "unit_test.tpp"
270
271#endif
Structure for translating a text file into a compact, rapidly parsable vector of characters in CPU RA...
Definition textfile.h:45
Class for handling comparisons of floating-point results versus expected values in unit tests....
Definition approx.h:23
Object for storing a series of test results, with labels.
Definition checklist.h:14