stub_analytics_plugin  1.6
Network Optix Video Analytics SDK
test.h
Go to the documentation of this file.
1 #pragma once
2 
7 #include <functional>
8 #include <iostream>
9 #include <sstream>
10 #include <string>
11 
12 #include <nx/kit/utils.h>
13 
14 #if !defined(NX_KIT_API)
15  #define NX_KIT_API
16 #endif
17 
18 namespace nx {
19 namespace kit {
20 namespace test {
21 
22 extern bool NX_KIT_API verbose; //< Use to control additional output of the unit test framework.
23 
24 #define TEST(TEST_CASE, TEST_NAME) \
25  static void test_##TEST_CASE##_##TEST_NAME(); \
26  int unused_##TEST_CASE##_##TEST_NAME /* Not `static const` to suppress "unused" warning. */ = \
27  ::nx::kit::test::detail::regTest( \
28  {#TEST_CASE, #TEST_NAME, #TEST_CASE "." #TEST_NAME, test_##TEST_CASE##_##TEST_NAME, \
29  /*tempDir*/ ""}); \
30  static void test_##TEST_CASE##_##TEST_NAME()
31  // Function body follows the TEST macro.
32 
33 #define ASSERT_TRUE(CONDITION) \
34  ::nx::kit::test::detail::assertBool(true, (CONDITION), #CONDITION, __FILE__, __LINE__)
35 
36 #define ASSERT_TRUE_AT_LINE(LINE, CONDITION) \
37  ::nx::kit::test::detail::assertBool(true, (CONDITION), #CONDITION, __FILE__, (LINE))
38 
39 #define ASSERT_FALSE(CONDITION) \
40  ::nx::kit::test::detail::assertBool(false, (CONDITION), #CONDITION, __FILE__, __LINE__)
41 
42 #define ASSERT_FALSE_AT_LINE(LINE, CONDITION) \
43  ::nx::kit::test::detail::assertBool(false, (CONDITION), #CONDITION, __FILE__, (LINE))
44 
45 #define ASSERT_EQ(EXPECTED, ACTUAL) \
46  ::nx::kit::test::detail::assertEq( \
47  (EXPECTED), #EXPECTED, (ACTUAL), #ACTUAL, __FILE__, __LINE__)
48 
49 #define ASSERT_EQ_AT_LINE(LINE, EXPECTED, ACTUAL) \
50  ::nx::kit::test::detail::assertEq( \
51  (EXPECTED), #EXPECTED, (ACTUAL), #ACTUAL, __FILE__, (LINE))
52 
53 #define ASSERT_STREQ(EXPECTED, ACTUAL) \
54  ::nx::kit::test::detail::assertStrEq( \
55  ::nx::kit::test::detail::toCStr(EXPECTED), #EXPECTED, \
56  ::nx::kit::test::detail::toCStr(ACTUAL), #ACTUAL, __FILE__, __LINE__)
57 
58 #define ASSERT_STREQ_AT_LINE(LINE, EXPECTED, ACTUAL) \
59  ::nx::kit::test::detail::assertStrEq( \
60  ::nx::kit::test::detail::toCStr(EXPECTED), #EXPECTED, \
61  ::nx::kit::test::detail::toCStr(ACTUAL), #ACTUAL, __FILE__, (LINE))
62 
70 NX_KIT_API const char* tempDir();
71 
79 NX_KIT_API const char* staticTempDir();
80 
93 NX_KIT_API int runAllTests(const char* testSuiteName, int argc, const char* const argv[]);
94 
95 NX_KIT_API void createFile(const char* filename, const char* content);
96 
97 //-------------------------------------------------------------------------------------------------
98 // Implementation
99 
100 #if defined(NX_KIT_TEST_KEEP_TEMP_FILES)
101  namespace
102  {
103  static const nx::kit::test::TempFile::KeepFilesInitializer tempFileKeepFilesInitializer;
104  }
105 #endif
106 
107 namespace detail {
108 
109 NX_KIT_API void failEq(
110  const char* expectedValue, const char* expectedExpr,
111  const char* actualValue, const char* actualExpr,
112  const char* file, int line);
113 
114 typedef std::function<void()> TestFunc;
115 
116 struct Test
117 {
118  const char* const testCase;
119  const char* const testName;
120  const char* const testCaseDotName;
121  const TestFunc testFunc;
122  std::string tempDir;
123 };
124 
125 NX_KIT_API int regTest(const Test& test);
126 
127 NX_KIT_API void assertBool(
128  bool expected, bool condition, const char* conditionStr, const char* file, int line);
129 
130 template<typename Expected, typename Actual>
131 void assertEq(
132  const Expected& expected, const char* expectedExpr,
133  const Actual& actual, const char* actualExpr,
134  const char* file, int line)
135 {
136  if (!(expected == actual)) //< Require only operator==().
137  {
138  detail::failEq(
139  utils::toString(expected).c_str(), expectedExpr,
140  utils::toString(actual).c_str(), actualExpr,
141  file, line);
142  }
143 }
144 
145 NX_KIT_API void assertStrEq(
146  const char* expectedValue, const char* expectedExpr,
147  const char* actualValue, const char* actualExpr,
148  const char* file, int line);
149 
150 inline const char* toCStr(const std::string& s)
151 {
152  return s.c_str();
153 }
154 
155 inline const char* toCStr(const char* s)
156 {
157  return s;
158 }
159 
160 } // namespace detail
161 
162 } // namespace test
163 } // namespace kit
164 } // namespace nx
Definition: debug.cpp:12
Definition: test.h:116