nlib
testing.h
[詳解]
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) {
32  InitNintendoTest(argc, argv);
33 }
34 inline void InitGoogleTest(int* argc, wchar_t** argv) {
35  InitNintendoTest(argc, argv);
36 }
37 #define NLIB_TESTING_OSTREAM ::nlib_ns::testing::detail_func::DummyStream
38 } // namespace testing
39 NLIB_NAMESPACE_END
40 #else
41 // See: https://code.google.com/p/googletest/
42 #include "gtest/gtest.h"
43 
44 #define NLIB_TESTING_OSTREAM std::ostream
45 namespace testing {
46 inline void InitNintendoTest(int* argc, char** argv) {
47  InitGoogleTest(argc, argv);
48 }
49 inline void InitNintendoTest(int* argc, wchar_t** argv) {
50  InitGoogleTest(argc, argv);
51 }
52 } // namespace testing
53 
54 #endif
55 
56 #if 1
57 #define EXPECT_ERR(e1, e2) EXPECT_STREQ(nlib_error_string(e1), nlib_error_string(e2))
58 #define ASSERT_ERR(e1, e2) ASSERT_STREQ(nlib_error_string(e1), nlib_error_string(e2))
59 #define EXPECT_NOERR(e) EXPECT_STREQ("OK", nlib_error_string(e))
60 #define ASSERT_NOERR(e) ASSERT_STREQ("OK", nlib_error_string(e))
61 #else
62 NLIB_NAMESPACE_BEGIN
63 namespace testing {
64 class NlibErrHelper {
65  public:
66  NlibErrHelper(errno_t e1, errno_t e2) NLIB_NOEXCEPT : e1_(e1), e2_(e2) {}
67  ~NlibErrHelper() NLIB_NOEXCEPT {}
68  NLIB_SAFE_BOOL(NlibErrHelper, (e1_ == e2_));
69  errno_t GetE1() NLIB_NOEXCEPT { return e1_; }
70  errno_t GetE2() NLIB_NOEXCEPT { return e2_; }
71 
72  private:
73  errno_t e1_;
74  errno_t e2_;
75 };
76 } // namespace testing
77 NLIB_NAMESPACE_END
78 
79 #define NLIB_CONCAT_(sym, l) sym##l
80 #define NLIB_CONCAT(sym, l) NLIB_CONCAT_(sym, l)
81 
82 #define EXPECT_ERR(e1, e2) \
83  switch (0) \
84  case 0: \
85  default: \
86  if (::nlib_ns::testing::NlibErrHelper NLIB_CONCAT(noerr, l) = \
87  ::nlib_ns::testing::NlibErrHelper(e1, e2)) \
88  ; \
89  else \
90  EXPECT_STREQ(nlib_error_string(NLIB_CONCAT(noerr, l).GetE1()), \
91  nlib_error_string(NLIB_CONCAT(noerr, l).GetE2()))
92 
93 #define ASSERT_ERR(e1, e2) \
94  switch (0) \
95  case 0: \
96  default: \
97  if (::nlib_ns::testing::NlibErrHelper NLIB_CONCAT(noerr, l) = \
98  ::nlib_ns::testing::NlibErrHelper(e1, e2)) \
99  ; \
100  else \
101  ASSERT_STREQ(nlib_error_string(NLIB_CONCAT(noerr, l).GetE1()), \
102  nlib_error_string(NLIB_CONCAT(noerr, l).GetE2()))
103 
104 #define EXPECT_NOERR(e) \
105  switch (0) \
106  case 0: \
107  default: \
108  if (::nlib_ns::testing::NlibErrHelper NLIB_CONCAT(noerr, l) = \
109  ::nlib_ns::testing::NlibErrHelper(0, e)) \
110  ; \
111  else \
112  EXPECT_STREQ("OK", nlib_error_string(NLIB_CONCAT(noerr, l).GetE2()))
113 
114 #define ASSERT_NOERR(e) \
115  switch (0) \
116  case 0: \
117  default: \
118  if (::nlib_ns::testing::NlibErrHelper NLIB_CONCAT(noerr, l) = \
119  ::nlib_ns::testing::NlibErrHelper(0, e)) \
120  ; \
121  else \
122  ASSERT_STREQ("OK", nlib_error_string(NLIB_CONCAT(noerr, l).GetE2()))
123 
124 #endif
125 
126 #if defined(_MSC_VER) && defined(_PREFAST_)
127 // for /analyze option
128 #include "nn/nlib/Cstring.h"
129 struct MsvcAnalyzeHelper_ {
130  template<class T>
131  MsvcAnalyzeHelper_& operator<<(T x) {
132  return *this;
133  }
134  operator int() const { return 1; }
135 };
136 #undef FAIL
137 #define FAIL() throw MsvcAnalyzeHelper_()
138 #undef SUCCEED
139 #define SUCCEED()
140 
141 #undef ASSERT_TRUE
142 #define ASSERT_TRUE(e) \
143  if (!(e)) throw MsvcAnalyzeHelper_()
144 #undef ASSERT_FALSE
145 #define ASSERT_FALSE(e) \
146  if (e) throw MsvcAnalyzeHelper_()
147 #undef EXPECT_TRUE
148 #define EXPECT_TRUE(e) \
149  if (!(e)) throw MsvcAnalyzeHelper_()
150 #undef EXPECT_FALSE
151 #define EXPECT_FALSE(e) \
152  if (e) throw MsvcAnalyzeHelper_()
153 
154 #undef ASSERT_EQ
155 #define ASSERT_EQ(a, b) \
156  if (!((a) == (b))) throw MsvcAnalyzeHelper_()
157 #undef EXPECT_EQ
158 #define EXPECT_EQ(a, b) \
159  if (!((a) == (b))) throw MsvcAnalyzeHelper_()
160 
161 #undef ASSERT_NE
162 #define ASSERT_NE(a, b) \
163  if ((a) == (b)) throw MsvcAnalyzeHelper_()
164 #undef EXPECT_NE
165 #define EXPECT_NE(a, b) \
166  if ((a) == (b)) throw MsvcAnalyzeHelper_()
167 
168 #undef ASSERT_LE
169 #define ASSERT_LE(a, b) \
170  if ((a) > (b)) throw MsvcAnalyzeHelper_()
171 #undef EXPECT_LE
172 #define EXPECT_LE(a, b) \
173  if ((a) > (b)) throw MsvcAnalyzeHelper_()
174 
175 #undef ASSERT_LT
176 #define ASSERT_LT(a, b) \
177  if ((a) >= (b)) throw MsvcAnalyzeHelper_()
178 #undef EXPECT_LT
179 #define EXPECT_LT(a, b) \
180  if ((a) >= (b)) throw MsvcAnalyzeHelper_()
181 
182 #undef ASSERT_GE
183 #define ASSERT_GE(a, b) \
184  if ((a) < (b)) throw MsvcAnalyzeHelper_()
185 #undef EXPECT_GE
186 #define EXPECT_GE(a, b) \
187  if ((a) < (b)) throw MsvcAnalyzeHelper_()
188 
189 #undef ASSERT_GT
190 #define ASSERT_GT(a, b) \
191  if ((a) <= (b)) throw MsvcAnalyzeHelper_()
192 #undef EXPECT_GT
193 #define EXPECT_GT(a, b) \
194  if ((a) <= (b)) throw MsvcAnalyzeHelper_()
195 
196 #undef ASSERT_STREQ
197 #define ASSERT_STREQ(a, b) \
198  if (NLIB_NS::StrCmp((a), (b)) != 0) throw MsvcAnalyzeHelper_()
199 #undef EXPECT_STREQ
200 #define EXPECT_STREQ(a, b) \
201  if (NLIB_NS::StrCmp((a), (b)) != 0) throw MsvcAnalyzeHelper_()
202 
203 #undef ASSERT_STRNE
204 #define ASSERT_STRNE(a, b) \
205  if (NLIB_NS::StrCmp((a), (b)) == 0) throw MsvcAnalyzeHelper_()
206 #undef EXPECT_STRNE
207 #define EXPECT_STRNE(a, b) \
208  if (NLIB_NS::StrCmp((a), (b)) == 0) throw MsvcAnalyzeHelper_()
209 
210 #undef ASSERT_STRCASEEQ
211 #define ASSERT_STRCASEEQ(a, b) \
212  if (NLIB_NS::StrCmp((a), (b)) != 0) throw MsvcAnalyzeHelper_()
213 #undef EXPECT_STRCASEEQ
214 #define EXPECT_STRCASEEQ(a, b) \
215  if (NLIB_NS::StrCmp((a), (b)) != 0) throw MsvcAnalyzeHelper_()
216 
217 #undef ASSERT_STRCASENE
218 #define ASSERT_STRCASENE(a, b) \
219  if (NLIB_NS::StrCmp((a), (b)) == 0) throw MsvcAnalyzeHelper_()
220 #undef EXPECT_STRCASENE
221 #define EXPECT_STRCASENE(a, b) \
222  if (NLIB_NS::StrCmp((a), (b)) == 0) throw MsvcAnalyzeHelper_()
223 
224 #undef ASSERT_FLOAT_EQ
225 #define ASSERT_FLOAT_EQ(a, b) \
226  if ((a) != (b)) throw MsvcAnalyzeHelper_()
227 #undef EXPECT_FLOAT_EQ
228 #define EXPECT_FLOAT_EQ(a, b) \
229  if ((a) != (b)) throw MsvcAnalyzeHelper_()
230 
231 #undef ASSERT_DOUBLE_EQ
232 #define ASSERT_DOUBLE_EQ(a, b) \
233  if ((a) != (b)) throw MsvcAnalyzeHelper_()
234 #undef EXPECT_DOUBLE_EQ
235 #define EXPECT_DOUBLE_EQ(a, b) \
236  if ((a) != (b)) throw MsvcAnalyzeHelper_()
237 
238 #undef ASSERT_NEAR
239 #define ASSERT_NEAR(a, b, c) \
240  if (!((a) == (b))) throw MsvcAnalyzeHelper_()
241 #undef EXPECT_NEAR
242 #define EXPECT_NEAR(a, b, c) \
243  if (!((a) == (b))) throw MsvcAnalyzeHelper_()
244 
245 #endif
246 
247 #endif // INCLUDE_NN_NLIB_TESTING_TESTING_H_
テスト失敗時の表示を制御します。
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
Definition: Config.h:199
値をパラメータ化したテストを定義しています。
void InitNintendoTest(int *argc, char **argv)
テストプログラムのコマンドラインオプションを処理します。
型付けテストを定義しています。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:109
開発環境別の設定が書かれるファイルです。
基本的な単体テスト用マクロが定義されています。
strlen, strcpy等を安全に使えるようにラップしています。
述語アサーションが定義されています。
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37