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 #if !defined(NX_KIT_API)
14  #define NX_KIT_API
15 #endif
16 
17 namespace nx {
18 namespace kit {
19 namespace test {
20 
21 #define TEST(TEST_CASE, TEST_NAME) \
22  static void test_##TEST_CASE##_##TEST_NAME(); \
23  static const nx::kit::test::detail::Test testDescriptor_##TEST_CASE##_##TEST_NAME = \
24  {#TEST_CASE, #TEST_NAME, &test_##TEST_CASE##_##TEST_NAME}; \
25  static const int unused_##TEST_CASE##_##TEST_NAME = \
26  nx::kit::test::detail::regTest(testDescriptor_##TEST_CASE##_##TEST_NAME); \
27  static void test_##TEST_CASE##_##TEST_NAME()
28  // Function body follows the TEST macro.
29 
30 #define ASSERT_TRUE(CONDITION) \
31  nx::kit::test::detail::assertBool(true, (CONDITION), #CONDITION, __FILE__, __LINE__)
32 
33 #define ASSERT_FALSE(CONDITION) \
34  nx::kit::test::detail::assertBool(false, (CONDITION), #CONDITION, __FILE__, __LINE__)
35 
36 #define ASSERT_EQ(EXPECTED, ACTUAL) \
37  nx::kit::test::detail::assertEq( \
38  (EXPECTED), #EXPECTED, (ACTUAL), #ACTUAL, __FILE__, __LINE__)
39 
40 #define ASSERT_EQ_AT_LINE(LINE, EXPECTED, ACTUAL) \
41  nx::kit::test::detail::assertEq( \
42  (EXPECTED), #EXPECTED, (ACTUAL), #ACTUAL, __FILE__, (LINE))
43 
44 #define ASSERT_STREQ(EXPECTED, ACTUAL) \
45  nx::kit::test::detail::assertStrEq( \
46  nx::kit::test::detail::toCStr(EXPECTED), #EXPECTED, \
47  nx::kit::test::detail::toCStr(ACTUAL), #ACTUAL, __FILE__, __LINE__)
48 
49 #define ASSERT_STREQ_AT_LINE(LINE, EXPECTED, ACTUAL) \
50  nx::kit::test::detail::assertStrEq( \
51  nx::kit::test::detail::toCStr(EXPECTED), #EXPECTED, \
52  nx::kit::test::detail::toCStr(ACTUAL), #ACTUAL, __FILE__, (LINE))
53 
60 NX_KIT_API const char* tempDir();
61 
74 NX_KIT_API int runAllTests(const char* testSuiteName, int argc, const char* const argv[]);
75 
76 NX_KIT_API void createFile(const char* filename, const char* content);
77 
78 //-------------------------------------------------------------------------------------------------
79 // Implementation
80 
81 #if defined(NX_KIT_TEST_KEEP_TEMP_FILES)
82  namespace
83  {
84  static const nx::kit::test::TempFile::KeepFilesInitializer tempFileKeepFilesInitializer;
85  }
86 #endif
87 
88 namespace detail {
89 
90 NX_KIT_API void failEq(
91  const char* expectedValue, const char* expectedExpr,
92  const char* actualValue, const char* actualExpr,
93  const char* file, int line);
94 
95 typedef std::function<void()> TestFunc;
96 
97 struct Test
98 {
99  std::string testCase;
100  std::string testName;
101  TestFunc testFunc;
102  std::string tempDir;
103 };
104 
105 NX_KIT_API int regTest(const Test& test);
106 
107 NX_KIT_API void assertBool(
108  bool expected, bool condition, const char* conditionStr, const char* file, int line);
109 
110 template<typename Expected, typename Actual>
111 void assertEq(
112  const Expected& expected, const char* expectedExpr,
113  const Actual& actual, const char* actualExpr,
114  const char* file, int line)
115 {
116  if (!(expected == actual)) //< Require only operator==().
117  {
118  std::ostringstream expectedValue;
119  expectedValue << expected;
120  std::ostringstream actualValue;
121  actualValue << actual;
122  detail::failEq(
123  expectedValue.str().c_str(), expectedExpr,
124  actualValue.str().c_str(), actualExpr,
125  file, line);
126  }
127 }
128 
129 NX_KIT_API void assertStrEq(
130  const char* expectedValue, const char* expectedExpr,
131  const char* actualValue, const char* actualExpr,
132  const char* file, int line);
133 
134 inline const char* toCStr(const std::string& s)
135 {
136  return s.c_str();
137 }
138 
139 inline const char* toCStr(const char* s)
140 {
141  return s;
142 }
143 
144 } // namespace detail
145 
146 } // namespace test
147 } // namespace kit
148 } // namespace nx
Definition: debug.cpp:14
Definition: test.h:97