nlib
Pred.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_PRED_H_
17 #define INCLUDE_NN_NLIB_TESTING_PRED_H_
18 
19 #include "nn/nlib/testing/NTest.h"
20 
21 NLIB_NAMESPACE_BEGIN
22 namespace testing {
23 
24 template <class Pred, class T1>
25 AssertionResult EvalPred1(const char* pred_text, const char* e1, Pred pred, const T1& v1) {
26  return pred(v1) ? AssertionResult(true) : AssertionResult(false)
27  << pred_text << "(" << e1
28  << ") evaluates to false, where"
29  << "\n" << e1 << " evaluates to " << v1;
30 }
31 
32 template <class Pred, class T1, class T2>
33 AssertionResult EvalPred2(const char* pred_text, const char* e1, const char* e2, Pred pred,
34  const T1& v1, const T2& v2) {
35  return pred(v1, v2) ? AssertionResult(true) : AssertionResult(false)
36  << pred_text << "(" << e1 << ", " << e2
37  << ") evaluates to false, where"
38  << "\n" << e1 << " evaluates to " << v1
39  << "\n" << e2 << " evaluates to " << v2;
40 }
41 
42 template <class Pred, class T1, class T2, class T3>
43 AssertionResult EvalPred3(const char* pred_text, const char* e1, const char* e2, const char* e3,
44  Pred pred, const T1& v1, const T2& v2, const T3& v3) {
45  return pred(v1, v2, v3) ? AssertionResult(true)
46  : AssertionResult(false) << pred_text << "(" << e1 << ", " << e2 << ", "
47  << e3 << ") evaluates to false, where"
48  << "\n" << e1 << " evaluates to " << v1 << "\n"
49  << e2 << " evaluates to " << v2 << "\n" << e3
50  << " evaluates to " << v3;
51 }
52 
53 template <class Pred, class T1, class T2, class T3, class T4>
54 AssertionResult EvalPred4(const char* pred_text, const char* e1, const char* e2, const char* e3,
55  const char* e4, Pred pred, const T1& v1, const T2& v2, const T3& v3,
56  const T4& v4) {
57  return pred(v1, v2, v3, v4) ? AssertionResult(true)
58  : AssertionResult(false)
59  << pred_text << "(" << e1 << ", " << e2 << ", " << e3 << ", "
60  << e4 << ") evaluates to false, where"
61  << "\n" << e1 << " evaluates to " << v1 << "\n" << e2
62  << " evaluates to " << v2 << "\n" << e3 << " evaluates to "
63  << v3 << "\n" << e4 << " evaluates to " << v4;
64 }
65 
66 template <class Pred, class T1, class T2, class T3, class T4, class T5>
67 AssertionResult EvalPred5(const char* pred_text, const char* e1, const char* e2, const char* e3,
68  const char* e4, const char* e5, Pred pred, const T1& v1, const T2& v2,
69  const T3& v3, const T4& v4, const T5& v5) {
70  return pred(v1, v2, v3, v4, v5)
71  ? AssertionResult(true)
72  : AssertionResult(false) << pred_text << "(" << e1 << ", " << e2 << ", " << e3
73  << ", " << e4 << ", " << e5 << ") evaluates to false, where"
74  << "\n" << e1 << " evaluates to " << v1 << "\n" << e2
75  << " evaluates to " << v2 << "\n" << e3 << " evaluates to "
76  << v3 << "\n" << e4 << " evaluates to " << v4 << "\n" << e5
77  << " evaluates to " << v5;
78 }
79 
80 } // namespace testing
81 NLIB_NAMESPACE_END
82 
83 #define NLIB_TESTING_PRED1_(pred, v1, iffail) \
84  switch (0) \
85  case 0: \
86  default: \
87  if (::nlib_ns::testing::AssertionResult ar = \
88  ::nlib_ns::testing::EvalPred1(#pred, #v1, pred, v1)) \
89  ; \
90  else \
91  iffail = ar
92 
93 #define NLIB_TESTING_PRED2_(pred, v1, v2, iffail) \
94  switch (0) \
95  case 0: \
96  default: \
97  if (::nlib_ns::testing::AssertionResult ar = \
98  ::nlib_ns::testing::EvalPred2(#pred, #v1, #v2, pred, v1, v2)) \
99  ; \
100  else \
101  iffail = ar
102 
103 #define NLIB_TESTING_PRED3_(pred, v1, v2, v3, iffail) \
104  switch (0) \
105  case 0: \
106  default: \
107  if (::nlib_ns::testing::AssertionResult ar = \
108  ::nlib_ns::testing::EvalPred3(#pred, #v1, #v2, #v3, pred, v1, v2, v3)) \
109  ; \
110  else \
111  iffail = ar
112 
113 #define NLIB_TESTING_PRED4_(pred, v1, v2, v3, v4, iffail) \
114  switch (0) \
115  case 0: \
116  default: \
117  if (::nlib_ns::testing::AssertionResult ar = \
118  ::nlib_ns::testing::EvalPred4(#pred, #v1, #v2, #v3, #v4, pred, v1, v2, v3, v4)) \
119  ; \
120  else \
121  iffail = ar
122 
123 #define NLIB_TESTING_PRED5_(pred, v1, v2, v3, v4, v5, iffail) \
124  switch (0) \
125  case 0: \
126  default: \
127  if (::nlib_ns::testing::AssertionResult ar = ::nlib_ns::testing::EvalPred5( \
128  #pred, #v1, #v2, #v3, #v4, #v5, pred, v1, v2, v3, v4, v5)) \
129  ; \
130  else \
131  iffail = ar
132 
133 #define NLIB_TESTING_PRED_FORMAT1_(pred_format, v1, iffail) \
134  switch (0) \
135  case 0: \
136  default: \
137  if (::nlib_ns::testing::AssertionResult ar = pred_format(#v1, v1)) \
138  ; \
139  else \
140  iffail = ar
141 
142 #define NLIB_TESTING_PRED_FORMAT2_(pred_format, v1, v2, iffail) \
143  switch (0) \
144  case 0: \
145  default: \
146  if (::nlib_ns::testing::AssertionResult ar = pred_format(#v1, #v2, v1, v2)) \
147  ; \
148  else \
149  iffail = ar
150 
151 #define NLIB_TESTING_PRED_FORMAT3_(pred_format, v1, v2, v3, iffail) \
152  switch (0) \
153  case 0: \
154  default: \
155  if (::nlib_ns::testing::AssertionResult ar = pred_format(#v1, #v2, #v3, v1, v2, v3)) \
156  ; \
157  else \
158  iffail = ar
159 
160 #define NLIB_TESTING_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, iffail) \
161  switch (0) \
162  case 0: \
163  default: \
164  if (::nlib_ns::testing::AssertionResult ar = \
165  pred_format(#v1, #v2, #v3, #v4, v1, v2, v3, v4)) \
166  ; \
167  else \
168  iffail = ar
169 
170 #define NLIB_TESTING_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, iffail) \
171  switch (0) \
172  case 0: \
173  default: \
174  if (::nlib_ns::testing::AssertionResult ar = \
175  pred_format(#v1, #v2, #v3, #v4, #v5, v1, v2, v3, v4, v5)) \
176  ; \
177  else \
178  iffail = ar
179 
180 #define ASSERT_PRED1(pred, v1) NLIB_TESTING_PRED1_(pred, v1, NLIB_TESTING_FATAL_)
181 #define EXPECT_PRED1(pred, v1) NLIB_TESTING_PRED1_(pred, v1, NLIB_TESTING_NONFATAL_)
182 #define ASSERT_PRED_FORMAT1(pred_format, v1) \
183  NLIB_TESTING_PRED_FORMAT1_(pred_format, v1, NLIB_TESTING_FATAL_)
184 #define EXPECT_PRED_FORMAT1(pred_format, v1) \
185  NLIB_TESTING_PRED_FORMAT1_(pred_format, v1, NLIB_TESTING_NONFATAL_)
186 
187 #define ASSERT_PRED2(pred, v1, v2) NLIB_TESTING_PRED2_(pred, v1, v2, NLIB_TESTING_FATAL_)
188 #define EXPECT_PRED2(pred, v1, v2) NLIB_TESTING_PRED2_(pred, v1, v2, NLIB_TESTING_NONFATAL_)
189 #define ASSERT_PRED_FORMAT2(pred_format, v1, v2) \
190  NLIB_TESTING_PRED_FORMAT2_(pred_format, v1, v2, NLIB_TESTING_FATAL_)
191 #define EXPECT_PRED_FORMAT2(pred_format, v1, v2) \
192  NLIB_TESTING_PRED_FORMAT2_(pred_format, v1, v2, NLIB_TESTING_NONFATAL_)
193 
194 #define ASSERT_PRED3(pred, v1, v2, v3) NLIB_TESTING_PRED3_(pred, v1, v2, v3, NLIB_TESTING_FATAL_)
195 #define EXPECT_PRED3(pred, v1, v2, v3) NLIB_TESTING_PRED3_(pred, v1, v2, v3, NLIB_TESTING_NONFATAL_)
196 #define ASSERT_PRED_FORMAT3(pred_format, v1, v2, v3) \
197  NLIB_TESTING_PRED_FORMAT3_(pred_format, v1, v2, v3, NLIB_TESTING_FATAL_)
198 #define EXPECT_PRED_FORMAT3(pred_format, v1, v2, v3) \
199  NLIB_TESTING_PRED_FORMAT3_(pred_format, v1, v2, v3, NLIB_TESTING_NONFATAL_)
200 
201 #define ASSERT_PRED4(pred, v1, v2, v3, v4) \
202  NLIB_TESTING_PRED4_(pred, v1, v2, v3, v4, NLIB_TESTING_FATAL_)
203 #define EXPECT_PRED4(pred, v1, v2, v3, v4) \
204  NLIB_TESTING_PRED4_(pred, v1, v2, v3, v4, NLIB_TESTING_NONFATAL_)
205 #define ASSERT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \
206  NLIB_TESTING_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, NLIB_TESTING_FATAL_)
207 #define EXPECT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \
208  NLIB_TESTING_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, NLIB_TESTING_NONFATAL_)
209 
210 #define ASSERT_PRED5(pred, v1, v2, v3, v4, v5) \
211  NLIB_TESTING_PRED5_(pred, v1, v2, v3, v4, v5, NLIB_TESTING_FATAL_)
212 #define EXPECT_PRED5(pred, v1, v2, v3, v4, v5) \
213  NLIB_TESTING_PRED5_(pred, v1, v2, v3, v4, v5, NLIB_TESTING_NONFATAL_)
214 #define ASSERT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \
215  NLIB_TESTING_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, NLIB_TESTING_FATAL_)
216 #define EXPECT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \
217  NLIB_TESTING_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, NLIB_TESTING_NONFATAL_)
218 
219 #endif // INCLUDE_NN_NLIB_TESTING_PRED_H_
基本的な単体テスト用マクロが定義されています。