stub_analytics_plugin  1.6
Network Optix Video Analytics SDK
test.h
Go to the documentation of this file.
1 // Copyright 2018-present Network Optix, Inc.
2 #pragma once
3 
8 #include <functional>
9 #include <iostream>
10 #include <sstream>
11 #include <string>
12 
13 #include <nx/kit/utils.h>
14 
15 #if !defined(NX_KIT_API)
16  #define NX_KIT_API
17 #endif
18 
19 namespace nx {
20 namespace kit {
21 namespace test {
22 
23 extern bool NX_KIT_API verbose; //< Use to control additional output of the unit test framework.
24 
25 #define TEST(TEST_CASE, TEST_NAME) \
26  static void test_##TEST_CASE##_##TEST_NAME(); \
27  static const nx::kit::test::detail::Test testDescriptor_##TEST_CASE##_##TEST_NAME = \
28  {#TEST_CASE, #TEST_NAME, &test_##TEST_CASE##_##TEST_NAME, /*tempDir*/ ""}; \
29  static const int unused_##TEST_CASE##_##TEST_NAME = \
30  nx::kit::test::detail::regTest(testDescriptor_##TEST_CASE##_##TEST_NAME); \
31  static void test_##TEST_CASE##_##TEST_NAME()
32  // Function body follows the TEST macro.
33 
34 #define ASSERT_TRUE(CONDITION) \
35  nx::kit::test::detail::assertBool(true, (CONDITION), #CONDITION, __FILE__, __LINE__)
36 
37 #define ASSERT_TRUE_AT_LINE(LINE, CONDITION) \
38  nx::kit::test::detail::assertBool(true, (CONDITION), #CONDITION, __FILE__, (LINE))
39 
40 #define ASSERT_FALSE(CONDITION) \
41  nx::kit::test::detail::assertBool(false, (CONDITION), #CONDITION, __FILE__, __LINE__)
42 
43 #define ASSERT_FALSE_AT_LINE(LINE, CONDITION) \
44  nx::kit::test::detail::assertBool(false, (CONDITION), #CONDITION, __FILE__, (LINE))
45 
46 #define ASSERT_EQ(EXPECTED, ACTUAL) \
47  nx::kit::test::detail::assertEq( \
48  (EXPECTED), #EXPECTED, (ACTUAL), #ACTUAL, __FILE__, __LINE__)
49 
50 #define ASSERT_EQ_AT_LINE(LINE, EXPECTED, ACTUAL) \
51  nx::kit::test::detail::assertEq( \
52  (EXPECTED), #EXPECTED, (ACTUAL), #ACTUAL, __FILE__, (LINE))
53 
54 #define ASSERT_STREQ(EXPECTED, ACTUAL) \
55  nx::kit::test::detail::assertStrEq( \
56  nx::kit::test::detail::toCStr(EXPECTED), #EXPECTED, \
57  nx::kit::test::detail::toCStr(ACTUAL), #ACTUAL, __FILE__, __LINE__)
58 
59 #define ASSERT_STREQ_AT_LINE(LINE, EXPECTED, ACTUAL) \
60  nx::kit::test::detail::assertStrEq( \
61  nx::kit::test::detail::toCStr(EXPECTED), #EXPECTED, \
62  nx::kit::test::detail::toCStr(ACTUAL), #ACTUAL, __FILE__, (LINE))
63 
71 NX_KIT_API const char* tempDir();
72 
80 NX_KIT_API const char* staticTempDir();
81 
94 NX_KIT_API int runAllTests(const char* testSuiteName, int argc, const char* const argv[]);
95 
96 NX_KIT_API void createFile(const char* filename, const char* content);
97 
98 //-------------------------------------------------------------------------------------------------
99 // Implementation
100 
101 #if defined(NX_KIT_TEST_KEEP_TEMP_FILES)
102  namespace
103  {
104  static const nx::kit::test::TempFile::KeepFilesInitializer tempFileKeepFilesInitializer;
105  }
106 #endif
107 
108 namespace detail {
109 
110 NX_KIT_API void failEq(
111  const char* expectedValue, const char* expectedExpr,
112  const char* actualValue, const char* actualExpr,
113  const char* file, int line);
114 
115 typedef std::function<void()> TestFunc;
116 
117 struct Test
118 {
119  std::string testCase;
120  std::string testName;
121  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:13
Definition: test.h:117