nlib
testing.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_TESTING_TESTING_H_
17 #define INCLUDE_NN_NLIB_TESTING_TESTING_H_
18 
19 #include "nn/nlib/Config.h"
20 
21 #ifndef NLIB_USE_GTEST
22 #include "nn/nlib/testing/NTest.h"
23 #include "nn/nlib/testing/Pred.h"
27 
28 namespace testing = ::nlib_ns::testing;
29 NLIB_NAMESPACE_BEGIN
30 namespace testing {
31 inline void InitGoogleTest(int* argc, char** argv) { InitNintendoTest(argc, argv); }
32 inline void InitGoogleTest(int* argc, wchar_t** argv) { InitNintendoTest(argc, argv); }
33 #define NLIB_TESTING_OSTREAM ::nlib_ns::testing::detail_func::DummyStream
34 } // namespace testing
35 NLIB_NAMESPACE_END
36 #else
37 // See: https://code.google.com/p/googletest/
38 #include "gtest/gtest.h"
39 
40 #define NLIB_TESTING_OSTREAM std::ostream
41 namespace testing {
42 inline void InitNintendoTest(int* argc, char** argv) { InitGoogleTest(argc, argv); }
43 inline void InitNintendoTest(int* argc, wchar_t** argv) { InitGoogleTest(argc, argv); }
44 }
45 
46 #endif
47 
48 #if 1
49 #define EXPECT_ERR(e1, e2) EXPECT_STREQ(nlib_error_string(e1), nlib_error_string(e2))
50 #define ASSERT_ERR(e1, e2) ASSERT_STREQ(nlib_error_string(e1), nlib_error_string(e2))
51 #define EXPECT_NOERR(e) EXPECT_STREQ("OK", nlib_error_string(e))
52 #define ASSERT_NOERR(e) ASSERT_STREQ("OK", nlib_error_string(e))
53 #else
54 NLIB_NAMESPACE_BEGIN
55 namespace testing {
56 class NlibErrHelper {
57  public:
58  NlibErrHelper(errno_t e1, errno_t e2) NLIB_NOEXCEPT : e1_(e1), e2_(e2) {}
59  ~NlibErrHelper() NLIB_NOEXCEPT {}
60  NLIB_SAFE_BOOL(NlibErrHelper, (e1_ == e2_));
61  errno_t GetE1() NLIB_NOEXCEPT { return e1_; }
62  errno_t GetE2() NLIB_NOEXCEPT { return e2_; }
63  private:
64  errno_t e1_;
65  errno_t e2_;
66 };
67 } // namespace testing
68 NLIB_NAMESPACE_END
69 
70 #define NLIB_CONCAT_(sym, l) sym##l
71 #define NLIB_CONCAT(sym, l) NLIB_CONCAT_(sym, l)
72 
73 #define EXPECT_ERR(e1, e2) \
74  switch (0) \
75  case 0: \
76  default: \
77  if (::nlib_ns::testing::NlibErrHelper NLIB_CONCAT(noerr, l) = \
78  ::nlib_ns::testing::NlibErrHelper(e1, e2)) \
79  ; \
80  else \
81  EXPECT_STREQ(nlib_error_string(NLIB_CONCAT(noerr, l).GetE1()), \
82  nlib_error_string(NLIB_CONCAT(noerr, l).GetE2()))
83 
84 
85 #define ASSERT_ERR(e1, e2) \
86  switch (0) \
87  case 0: \
88  default: \
89  if (::nlib_ns::testing::NlibErrHelper NLIB_CONCAT(noerr, l) = \
90  ::nlib_ns::testing::NlibErrHelper(e1, e2)) \
91  ; \
92  else \
93  ASSERT_STREQ(nlib_error_string(NLIB_CONCAT(noerr, l).GetE1()), \
94  nlib_error_string(NLIB_CONCAT(noerr, l).GetE2()))
95 
96 #define EXPECT_NOERR(e) \
97  switch (0) \
98  case 0: \
99  default: \
100  if (::nlib_ns::testing::NlibErrHelper NLIB_CONCAT(noerr, l) = \
101  ::nlib_ns::testing::NlibErrHelper(0, e)) \
102  ; \
103  else \
104  EXPECT_STREQ("OK", nlib_error_string(NLIB_CONCAT(noerr, l).GetE2()))
105 
106 #define ASSERT_NOERR(e) \
107  switch (0) \
108  case 0: \
109  default: \
110  if (::nlib_ns::testing::NlibErrHelper NLIB_CONCAT(noerr, l) = \
111  ::nlib_ns::testing::NlibErrHelper(0, e)) \
112  ; \
113  else \
114  ASSERT_STREQ("OK", nlib_error_string(NLIB_CONCAT(noerr, l).GetE2()))
115 
116 #endif
117 
118 #if defined(_MSC_VER) && defined(_PREFAST_)
119 // for /analyze option
120 #include "nn/nlib/Cstring.h"
121 struct MsvcAnalyzeHelper_ {
122  template<class T>
123  MsvcAnalyzeHelper_& operator<<(T x) { return *this; }
124  operator int() const { return 1; }
125 };
126 #undef FAIL
127 #define FAIL() throw MsvcAnalyzeHelper_()
128 #undef SUCCEED
129 #define SUCCEED()
130 
131 #undef ASSERT_TRUE
132 #define ASSERT_TRUE(e) if (!(e)) throw MsvcAnalyzeHelper_()
133 #undef ASSERT_FALSE
134 #define ASSERT_FALSE(e) if (e) throw MsvcAnalyzeHelper_()
135 #undef EXPECT_TRUE
136 #define EXPECT_TRUE(e) if (!(e)) throw MsvcAnalyzeHelper_()
137 #undef EXPECT_FALSE
138 #define EXPECT_FALSE(e) if (e) throw MsvcAnalyzeHelper_()
139 
140 #undef ASSERT_EQ
141 #define ASSERT_EQ(a, b) if (!((a) == (b))) throw MsvcAnalyzeHelper_()
142 #undef EXPECT_EQ
143 #define EXPECT_EQ(a, b) if (!((a) == (b))) throw MsvcAnalyzeHelper_()
144 
145 #undef ASSERT_NE
146 #define ASSERT_NE(a, b) if ((a) == (b)) throw MsvcAnalyzeHelper_()
147 #undef EXPECT_NE
148 #define EXPECT_NE(a, b) if ((a) == (b)) throw MsvcAnalyzeHelper_()
149 
150 #undef ASSERT_LE
151 #define ASSERT_LE(a, b) if ((a) > (b)) throw MsvcAnalyzeHelper_()
152 #undef EXPECT_LE
153 #define EXPECT_LE(a, b) if ((a) > (b)) throw MsvcAnalyzeHelper_()
154 
155 #undef ASSERT_LT
156 #define ASSERT_LT(a, b) if ((a) >= (b)) throw MsvcAnalyzeHelper_()
157 #undef EXPECT_LT
158 #define EXPECT_LT(a, b) if ((a) >= (b)) throw MsvcAnalyzeHelper_()
159 
160 #undef ASSERT_GE
161 #define ASSERT_GE(a, b) if ((a) < (b)) throw MsvcAnalyzeHelper_()
162 #undef EXPECT_GE
163 #define EXPECT_GE(a, b) if ((a) < (b)) throw MsvcAnalyzeHelper_()
164 
165 #undef ASSERT_GT
166 #define ASSERT_GT(a, b) if ((a) <= (b)) throw MsvcAnalyzeHelper_()
167 #undef EXPECT_GT
168 #define EXPECT_GT(a, b) if ((a) <= (b)) throw MsvcAnalyzeHelper_()
169 
170 #undef ASSERT_STREQ
171 #define ASSERT_STREQ(a, b) if (NLIB_NS::StrCmp((a), (b)) != 0) throw MsvcAnalyzeHelper_()
172 #undef EXPECT_STREQ
173 #define EXPECT_STREQ(a, b) if (NLIB_NS::StrCmp((a), (b)) != 0) throw MsvcAnalyzeHelper_()
174 
175 #undef ASSERT_STRNE
176 #define ASSERT_STRNE(a, b) if (NLIB_NS::StrCmp((a), (b)) == 0) throw MsvcAnalyzeHelper_()
177 #undef EXPECT_STRNE
178 #define EXPECT_STRNE(a, b) if (NLIB_NS::StrCmp((a), (b)) == 0) throw MsvcAnalyzeHelper_()
179 
180 #undef ASSERT_STRCASEEQ
181 #define ASSERT_STRCASEEQ(a, b) if (NLIB_NS::StrCmp((a), (b)) != 0) throw MsvcAnalyzeHelper_()
182 #undef EXPECT_STRCASEEQ
183 #define EXPECT_STRCASEEQ(a, b) if (NLIB_NS::StrCmp((a), (b)) != 0) throw MsvcAnalyzeHelper_()
184 
185 #undef ASSERT_STRCASENE
186 #define ASSERT_STRCASENE(a, b) if (NLIB_NS::StrCmp((a), (b)) == 0) throw MsvcAnalyzeHelper_()
187 #undef EXPECT_STRCASENE
188 #define EXPECT_STRCASENE(a, b) if (NLIB_NS::StrCmp((a), (b)) == 0) throw MsvcAnalyzeHelper_()
189 
190 #undef ASSERT_FLOAT_EQ
191 #define ASSERT_FLOAT_EQ(a, b) if ((a) != (b)) throw MsvcAnalyzeHelper_()
192 #undef EXPECT_FLOAT_EQ
193 #define EXPECT_FLOAT_EQ(a, b) if ((a) != (b)) throw MsvcAnalyzeHelper_()
194 
195 #undef ASSERT_DOUBLE_EQ
196 #define ASSERT_DOUBLE_EQ(a, b) if ((a) != (b)) throw MsvcAnalyzeHelper_()
197 #undef EXPECT_DOUBLE_EQ
198 #define EXPECT_DOUBLE_EQ(a, b) if ((a) != (b)) throw MsvcAnalyzeHelper_()
199 
200 #undef ASSERT_NEAR
201 #define ASSERT_NEAR(a, b, c) if (!((a) == (b))) throw MsvcAnalyzeHelper_()
202 #undef EXPECT_NEAR
203 #define EXPECT_NEAR(a, b, c) if (!((a) == (b))) throw MsvcAnalyzeHelper_()
204 
205 #endif
206 
207 
208 #endif // INCLUDE_NN_NLIB_TESTING_TESTING_H_
Controls display when tests fails.
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Definition: Config.h:194
Defines a test that parameterizes values.
void InitNintendoTest(int *argc, char **argv)
Processes command-line options for the test program.
Defines a typed test.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
A file that contains the configuration information for each development environment.
Defines the macro for a basic, simple test.
Wraps functions like strlen and strcpy so they can be safely used.
Defines a predicate assertion.
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37