55#ifndef INCLUDED_PYSTRING_UNITTEST_H
66#define INCLUDED_PYSTRING_UNITTEST_H
77
8- #include < iostream>
98#include < cmath>
9+ #include < iostream>
1010#include < vector>
1111
1212extern int unit_test_failures;
@@ -15,111 +15,136 @@ void unittest_fail();
1515
1616typedef void (*PYSTRINGTestFunc)();
1717
18- struct PYSTRINGTest
19- {
20- PYSTRINGTest (std::string testgroup, std::string testname, PYSTRINGTestFunc test) :
21- group (testgroup), name(testname), function(test) { };
22- std::string group, name;
23- PYSTRINGTestFunc function;
18+ struct PYSTRINGTest {
19+ PYSTRINGTest (std::string testgroup, std::string testname,
20+ PYSTRINGTestFunc test)
21+ : group(testgroup), name(testname), function(test){ };
22+ std::string group, name;
23+ PYSTRINGTestFunc function;
2424};
2525
2626typedef std::vector<PYSTRINGTest> UnitTests;
2727
28- UnitTests& GetUnitTests ();
28+ UnitTests & GetUnitTests ();
2929
30- struct AddTest { AddTest(PYSTRINGTest&& test); };
30+ struct AddTest {
31+ AddTest (PYSTRINGTest &&test);
32+ };
3133
3234// / PYSTRING_CHECK_* macros checks if the conditions is met, and if not,
3335// / prints an error message indicating the module and line where the
3436// / error occurred, but does NOT abort. This is helpful for unit tests
3537// / where we do not want one failure.
36- #define PYSTRING_CHECK_ASSERT (x ) \
37- ((x) ? ((void )0 ) \
38- : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
39- << " FAILED: " << #x << " \n " ), \
40- (void )++unit_test_failures))
41-
42- #define PYSTRING_CHECK_EQUAL (x,y ) \
43- (((x) == (y)) ? ((void )0 ) \
44- : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
45- << " FAILED: " << #x << " == " << #y << " \n " \
46- << " \t values were '" << (x) << " ' and '" << (y) << " '\n " ), \
47- (void )++unit_test_failures))
48-
49- #define PYSTRING_CHECK_NE (x,y ) \
50- (((x) != (y)) ? ((void )0 ) \
51- : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
52- << " FAILED: " << #x << " != " << #y << " \n " \
53- << " \t values were '" << (x) << " ' and '" << (y) << " '\n " ), \
54- (void )++unit_test_failures))
55-
56- #define PYSTRING_CHECK_LT (x,y ) \
57- (((x) < (y)) ? ((void )0 ) \
58- : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
59- << " FAILED: " << #x << " < " << #y << " \n " \
60- << " \t values were '" << (x) << " ' and '" << (y) << " '\n " ), \
61- (void )++unit_test_failures))
62-
63- #define PYSTRING_CHECK_GT (x,y ) \
64- (((x) > (y)) ? ((void )0 ) \
65- : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
66- << " FAILED: " << #x << " > " << #y << " \n " \
67- << " \t values were '" << (x) << " ' and '" << (y) << " '\n " ), \
68- (void )++unit_test_failures))
69-
70- #define PYSTRING_CHECK_LE (x,y ) \
71- (((x) <= (y)) ? ((void )0 ) \
72- : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
73- << " FAILED: " << #x << " <= " << #y << " \n " \
74- << " \t values were '" << (x) << " ' and '" << (y) << " '\n " ), \
75- (void )++unit_test_failures))
76-
77- #define PYSTRING_CHECK_GE (x,y ) \
78- (((x) >= (y)) ? ((void )0 ) \
79- : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
80- << " FAILED: " << #x << " >= " << #y << " \n " \
81- << " \t values were '" << (x) << " ' and '" << (y) << " '\n " ), \
82- (void )++unit_test_failures))
83-
84- #define PYSTRING_CHECK_CLOSE (x,y,tol ) \
85- ((std::abs((x) - (y)) < tol) ? ((void )0 ) \
86- : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
87- << " FAILED: abs(" << #x << " - " << #y << " ) < " << #tol << " \n " \
88- << " \t values were '" << (x) << " ', '" << (y) << " ' and '" << (tol) << " '\n " ), \
89- (void )++unit_test_failures))
90-
91- #define PYSTRING_CHECK_THOW (S, E ) \
92- try { S; throw " throwanything" ; } catch ( E const & ex ) { } catch (...) { \
93- std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
94- << " FAILED: " << #E << " is expected to be thrown\n " ; \
95- ++unit_test_failures; }
96-
97- #define PYSTRING_CHECK_NO_THOW (S ) \
98- try { S; } catch (...) { \
99- std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
100- << " FAILED: exception thrown from " << #S <<" \n " ; \
101- ++unit_test_failures; }
102-
103- #define PYSTRING_ADD_TEST (group, name ) \
104- static void pystringtest_##group##_##name(); \
105- AddTest pystringaddtest_##group##_##name(PYSTRINGTest(#group, #name, pystringtest_##group##_##name)); \
106- static void pystringtest_##group##_##name()
107-
108- #define PYSTRING_TEST_SETUP () \
109- int unit_test_failures = 0
110-
111- #define PYSTRING_TEST_APP (app ) \
112- std::vector<PYSTRINGTest>& GetUnitTests () { \
113- static std::vector<PYSTRINGTest> pystring_unit_tests; \
114- return pystring_unit_tests; } \
115- AddTest::AddTest (PYSTRINGTest&& test){GetUnitTests ().emplace_back (test);}; \
116- PYSTRING_TEST_SETUP (); \
117- int main (int , char **) { std::cerr << " \n " << #app <<" \n\n " ; \
118- for (size_t i = 0 ; i < GetUnitTests ().size (); ++i) { \
119- int _tmp = unit_test_failures; GetUnitTests ()[i].function (); \
120- std::cerr << " Test [" << GetUnitTests ()[i].group << " ] [" << GetUnitTests ()[i].name << " ] - " ; \
121- std::cerr << (_tmp == unit_test_failures ? " PASSED" : " FAILED" ) << " \n " ; } \
122- std::cerr << " \n " << unit_test_failures << " tests failed\n\n " ; \
123- return unit_test_failures; }
38+ #define PYSTRING_CHECK_ASSERT (x ) \
39+ ((x) ? ((void )0 ) \
40+ : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
41+ << " FAILED: " << #x << " \n " ), \
42+ (void )++unit_test_failures))
43+
44+ #define PYSTRING_CHECK_EQUAL (x, y ) \
45+ (((x) == (y)) ? ((void )0 ) \
46+ : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
47+ << " FAILED: " << #x << " == " << #y << " \n " \
48+ << " \t values were '" << (x) << " ' and '" << (y) \
49+ << " '\n " ), \
50+ (void )++unit_test_failures))
51+
52+ #define PYSTRING_CHECK_NE (x, y ) \
53+ (((x) != (y)) ? ((void )0 ) \
54+ : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
55+ << " FAILED: " << #x << " != " << #y << " \n " \
56+ << " \t values were '" << (x) << " ' and '" << (y) \
57+ << " '\n " ), \
58+ (void )++unit_test_failures))
59+
60+ #define PYSTRING_CHECK_LT (x, y ) \
61+ (((x) < (y)) ? ((void )0 ) \
62+ : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
63+ << " FAILED: " << #x << " < " << #y << " \n " \
64+ << " \t values were '" << (x) << " ' and '" << (y) \
65+ << " '\n " ), \
66+ (void )++unit_test_failures))
67+
68+ #define PYSTRING_CHECK_GT (x, y ) \
69+ (((x) > (y)) ? ((void )0 ) \
70+ : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
71+ << " FAILED: " << #x << " > " << #y << " \n " \
72+ << " \t values were '" << (x) << " ' and '" << (y) \
73+ << " '\n " ), \
74+ (void )++unit_test_failures))
75+
76+ #define PYSTRING_CHECK_LE (x, y ) \
77+ (((x) <= (y)) ? ((void )0 ) \
78+ : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
79+ << " FAILED: " << #x << " <= " << #y << " \n " \
80+ << " \t values were '" << (x) << " ' and '" << (y) \
81+ << " '\n " ), \
82+ (void )++unit_test_failures))
83+
84+ #define PYSTRING_CHECK_GE (x, y ) \
85+ (((x) >= (y)) ? ((void )0 ) \
86+ : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
87+ << " FAILED: " << #x << " >= " << #y << " \n " \
88+ << " \t values were '" << (x) << " ' and '" << (y) \
89+ << " '\n " ), \
90+ (void )++unit_test_failures))
91+
92+ #define PYSTRING_CHECK_CLOSE (x, y, tol ) \
93+ ((std::abs((x) - (y)) < tol) \
94+ ? ((void )0 ) \
95+ : ((std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
96+ << " FAILED: abs(" << #x << " - " << #y << " ) < " << #tol \
97+ << " \n " \
98+ << " \t values were '" << (x) << " ', '" << (y) << " ' and '" \
99+ << (tol) << " '\n " ), \
100+ (void )++unit_test_failures))
101+
102+ #define PYSTRING_CHECK_THOW (S, E ) \
103+ try { \
104+ S; \
105+ throw " throwanything" ; \
106+ } catch (E const &ex) { \
107+ } catch (...) { \
108+ std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
109+ << " FAILED: " << #E << " is expected to be thrown\n " ; \
110+ ++unit_test_failures; \
111+ }
112+
113+ #define PYSTRING_CHECK_NO_THOW (S ) \
114+ try { \
115+ S; \
116+ } catch (...) { \
117+ std::cout << __FILE__ << " :" << __LINE__ << " :\n " \
118+ << " FAILED: exception thrown from " << #S << " \n " ; \
119+ ++unit_test_failures; \
120+ }
121+
122+ #define PYSTRING_ADD_TEST (group, name ) \
123+ static void pystringtest_##group##_##name(); \
124+ AddTest pystringaddtest_##group##_##name( \
125+ PYSTRINGTest (#group, #name, pystringtest_##group##_##name)); \
126+ static void pystringtest_##group##_##name()
127+
128+ #define PYSTRING_TEST_SETUP () int unit_test_failures = 0
129+
130+ #define PYSTRING_TEST_APP (app ) \
131+ std::vector<PYSTRINGTest> &GetUnitTests () { \
132+ static std::vector<PYSTRINGTest> pystring_unit_tests; \
133+ return pystring_unit_tests; \
134+ } \
135+ AddTest::AddTest (PYSTRINGTest &&test) { GetUnitTests ().emplace_back (test); } \
136+ PYSTRING_TEST_SETUP (); \
137+ int main (int , char **) { \
138+ std::cerr << " \n " << #app << " \n\n " ; \
139+ for (size_t i = 0 ; i < GetUnitTests ().size (); ++i) { \
140+ int _tmp = unit_test_failures; \
141+ GetUnitTests ()[i].function (); \
142+ std::cerr << " Test [" << GetUnitTests ()[i].group << " ] [" \
143+ << GetUnitTests ()[i].name << " ] - " ; \
144+ std::cerr << (_tmp == unit_test_failures ? " PASSED" : " FAILED" ) << " \n " ; \
145+ } \
146+ std::cerr << " \n " << unit_test_failures << " tests failed\n\n " ; \
147+ return unit_test_failures; \
148+ }
124149
125150#endif
0 commit comments