Ghidra Decompiler Analysis Engine
test.hh
Go to the documentation of this file.
1 /* ###
2  * IP: GHIDRA
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
29 
30 #include <vector>
31 #include <set>
32 #include <string>
33 #include <iostream>
34 
35 typedef void (*testfunc_t)();
36 
42 struct UnitTest {
43  static std::vector<UnitTest *> tests;
44  std::string name;
46 
51  UnitTest(const std::string &name,testfunc_t func) :
52  name(name), func(func)
53  {
54  tests.push_back(this);
55  }
56 
57  static void run(std::set<std::string> &testNames);
58 };
59 
60 
62 #define TEST(testname) \
63  void testname(); \
64  UnitTest testname##_obj{ #testname, testname }; \
65  void testname()
66 
68 #define ASSERT(test) \
69  if (!(test)) { \
70  std::cerr << " failed at " << __FILE__ << ":" << __LINE__ << " asserting \"" << #test << "\"." << std::endl; \
71  throw 0; \
72  }
73 
75 #define ASSERT_EQUALS(a, b) \
76  if ((a) != (b)) { \
77  std::stringstream ssa, ssb; \
78  ssa << (a); \
79  ssb << (b); \
80  std::cerr << " failed at " << __FILE__ << ":" << __LINE__ << " asserting \"" << ssa.str() \
81  << " == " << ssb.str() << "\"." << std::endl; \
82  throw 0; \
83  }
84 
86 #define ASSERT_NOT_EQUALS(a, b) \
87  if ((a) == (b)) { \
88  std::stringstream ssa, ssb; \
89  ssa << (a); \
90  ssb << (b); \
91  std::cerr << " failed at " << __FILE__ << ":" << __LINE__ << " asserting \"" << ssa.str() \
92  << " != " << ssb.str() << "\"." << std::endl; \
93  throw 0; \
94  }
UnitTest
Simple unit test class.
Definition: test.hh:42
testfunc_t
void(* testfunc_t)()
A unit-test function.
Definition: test.hh:35
UnitTest::name
std::string name
Name of the test.
Definition: test.hh:44
UnitTest::run
static void run(std::set< std::string > &testNames)
Run all the instantiated tests.
Definition: test.cc:24
UnitTest::func
testfunc_t func
Call-back function executing the test.
Definition: test.hh:45
UnitTest::UnitTest
UnitTest(const std::string &name, testfunc_t func)
Constructor.
Definition: test.hh:51
UnitTest::tests
static std::vector< UnitTest * > tests
The collection of test objects.
Definition: test.hh:43