1 mike 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 mike 1.1 **==============================================================================
23 */
24
25 //==============================================================================
26 //
27 // ut.h : provides helper macro/classes for unit-test framework
28 //
29 //==============================================================================
30
31 #ifndef _ut_ut_h
32 #define _ut_ut_h
33
34 #include <common.h>
35 #include <string>
36 #include <cassert>
37 #include <cstdio>
38 #include <sstream>
39
40 // A constant 'false' value that can be used in a condition with getting the
41 // 'conditional expression is constant' warning from from Windows.
42 inline bool __UT_False() { return false; }
43 mike 1.1 #define UT_FALSE __UT_False()
44
45 //==============================================================================
46 //
47 // Test framework macros:
48 //
49 //==============================================================================
50
51 #define UT_ASSERT(cond) \
52 do \
53 { \
54 if(!(cond)) \
55 throw(ut::UnittestException(__FILE__, __LINE__, __FUNCTION__, \
56 #cond)); \
57 } \
58 while (__UT_False())
59
60 #define UT_ASSERT_EQUAL(v1, v2) \
61 do \
62 { \
63 if((v1) != (v2)) \
64 mike 1.1 { \
65 std::stringstream ss; \
66 ss << #v1; \
67 ss << " == "; \
68 ss << #v2; \
69 ss << "; {"; \
70 ss << v1; \
71 ss << " == "; \
72 ss << v2; \
73 ss << "}"; \
74 \
75 throw(ut::UnittestException(__FILE__, __LINE__, __FUNCTION__, \
76 ss.str().c_str())); \
77 }\
78 } \
79 while (__UT_False())
80
81
82 #define UT_ASSERT_NOT_EQUAL(v1, v2) \
83 do \
84 { \
85 mike 1.1 if((v1) == (v2)) \
86 { \
87 std::stringstream ss; \
88 ss << #v1; \
89 ss << " != "; \
90 ss << #v2; \
91 ss << "; {"; \
92 ss << v1; \
93 ss << " != "; \
94 ss << v2; \
95 ss << "}"; \
96 \
97 throw(ut::UnittestException(__FILE__, __LINE__, __FUNCTION__, \
98 ss.str().c_str())); \
99 }\
100 } \
101 while (__UT_False())
102
103 void __UT_TEST(
104 const char* testName,
105 void (*setUp)(),
106 mike 1.1 void (*cleanUp)(),
107 void (*test)());
108
109 #define UT_TEST(test) __UT_TEST(#test, setUp, cleanup, test)
110
111 #define UT_ASSERT_FAILED_MSG(message) \
112 throw(ut::UnittestException(__FILE__, __LINE__, __FUNCTION__, (message)))
113
114 #define UT_ENTRY_POINT(FUNC) static ut::TestRegister __entry(FUNC)
115
116 #define UT_WARNING(text) ut::testWarning(text)
117
118 //==============================================================================
119 //
120 // Test framework functions:
121 //
122 //==============================================================================
123
124 namespace ut
125 {
126 // internal part
127 mike 1.1 struct UnittestException
128 {
129 std::string m_file;
130 std::string m_function;
131 std::string m_text;
132 std::string m_testcase;
133 int m_line;
134
135 UnittestException(const char* file, int line, const char* function,
136 const char* text ) : m_file(file), m_function(function),
137 m_text(text), m_line(line)
138 {
139 }
140 };
141
142 typedef void (*MODULE_TEST_CALLBACK) ();
143
144 // interanl callbacks - used by macros
145 void registerCallback(MODULE_TEST_CALLBACK pfn);
146 unsigned int testStarted(const char* name);
147 void testCompleted(const char* name);
148 mike 1.1 void testFailed(const UnittestException& ex);
149
150 void testWarning(const char* text);
151
152 ///////////////////////////////
153 // registration helper
154 struct TestRegister
155 {
156 TestRegister(MODULE_TEST_CALLBACK pfn) { registerCallback(pfn); }
157 };
158
159 bool testGetAttr(const std::string& name, std::string& value);
160 };
161
162 #include "utility.h"
163 #include "strutil.h"
164
165 #endif /* _ut_ut_h */
|