16 #ifndef INCLUDE_NN_NLIB_TESTING_NTEST_H_ 17 #define INCLUDE_NN_NLIB_TESTING_NTEST_H_ 27 #if defined(_MSC_VER) && defined(nx_testing_EXPORTS) 28 #undef NLIB_VIS_PUBLIC 29 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT 51 bool HasFatalFaiulre()
const {
return is_fatal_error; }
52 bool HasNonfatalFailure()
const {
return is_non_fatal_error; }
57 bool is_non_fatal_error;
69 virtual ~TestFactoryBase() {}
70 virtual Test* Create() = 0;
79 template<
class TestClass>
80 class TestFactory :
public TestFactoryBase {
96 const
char* test_case_name()
const {
return testcase_name_; }
97 const char* name()
const {
return testname_; }
98 const TestResult* result()
const {
return &result_; }
102 typedef void (*SetUpTestCaseFunc)();
103 typedef void (*TearDownTestCaseFunc)();
104 template<
class TestClass>
105 static TestInfo* AddTestInfo(
const char* test_case,
const char* test_name,
int index,
106 SetUpTestCaseFunc setup_func, TearDownTestCaseFunc teardown_func);
109 static const int kMaxTestList = 4096;
110 static TestInfo testlist_[kMaxTestList];
111 static int testlist_idx_;
115 bool TestCaseNameLess(
const TestInfo& rhs)
const {
116 int tmp = strcmp(testcase_name_, rhs.testcase_name_);
117 if (tmp < 0)
return true;
118 if (tmp > 0)
return false;
119 return index_ < rhs.index_;
121 bool TestCaseNameEq(
const TestInfo& rhs)
const {
122 return index_ == rhs.index_ && strcmp(testcase_name_, rhs.testcase_name_) == 0;
124 bool IsTypedTest()
const {
return index_ >= 0; }
125 int GetTypedTestVariant()
const {
return index_ < 0 ? 0 : index_; }
126 struct TestSortPred {
127 bool operator()(TestInfo* lhs, TestInfo* rhs) {
return lhs->TestCaseNameLess(*rhs); }
131 static TestInfo& AddTestInfo_(
const char* test_case,
const char* test_name,
int index,
132 SetUpTestCaseFunc setup_func, TearDownTestCaseFunc teardown_func);
135 friend class FollowMessageTerminator;
136 const char* testcase_name_;
137 const char* testname_;
138 TestFactoryBase* factory_;
139 void (*setup_testcase_)();
140 void (*teardown_testcase_)();
143 double testfactory_space_[2];
147 template<
class TestClass>
148 TestInfo* TestInfo::AddTestInfo(
const char* test_case,
const char* test_name,
int index,
149 SetUpTestCaseFunc setup_func, TearDownTestCaseFunc teardown_func) {
154 TestInfo& info = AddTestInfo_(test_case, test_name, index, setup_func, teardown_func);
155 void* p = &info.testfactory_space_[0];
156 new (p) TestFactory<TestClass>();
162 char* tmpptr =
reinterpret_cast<char*
>(&info.testfactory_space_[0]);
163 info.factory_ =
reinterpret_cast<TestFactoryBase*
>(tmpptr);
171 static UnitTest* GetInstance();
176 const TestInfo* current_test_info()
const {
return current_; }
199 friend class TestInfo;
200 friend class SimpleSingleton<UnitTest>;
201 friend class FollowMessageTerminator;
203 virtual ~UnitTest() {}
210 static void SetUpTestCase() {}
211 static void TearDownTestCase() {}
216 void RecordProperty(
const char* key,
const char* value);
217 void RecordProperty(
const char* key,
int value);
220 virtual void SetUp() {}
221 virtual void TearDown() {}
222 virtual void TestBody() = 0;
230 explicit AssertionResult(
bool result) : is_success_(result), builder_() {}
231 ~AssertionResult() {}
232 AssertionResult(
const AssertionResult& rhs) :
233 is_success_(rhs.is_success_),
234 builder_(const_cast<detail_func::DummyStream&>(rhs.builder_), move_tag()) {
237 operator bool()
const {
return is_success_; }
238 AssertionResult operator!()
const {
return AssertionResult(!is_success_); }
239 const char* c_str()
const {
return builder_.c_str(); }
242 AssertionResult& operator<<(
const T& rhs) {
246 AssertionResult& operator<<(AssertionResult& rhs) {
247 is_success_ = rhs.is_success_;
248 builder_.assign(rhs.builder_, move_tag());
254 detail_func::DummyStream builder_;
255 AssertionResult& operator=(
const AssertionResult&);
261 ScopedTrace(AssertionResult& msg,
const char* file,
int line);
270 explicit FollowMessageTerminator(
bool flag);
276 void operator=(
const AssertionResult& msg)
const;
282 NLIB_VIS_PUBLIC AssertionResult BooleanFailMessage(
const char* file,
int line,
const char* expr,
283 const char* actual,
const char* msg,
284 const char* expected);
285 #define NLIB_TESTING_BOOLEAN_IMPL_(expr, expected, actual, iffail) \ 289 if (const ::nlib_ns::testing::AssertionResult ar = \ 290 ::nlib_ns::testing::AssertionResult(expr)) \ 293 iffail = ::nlib_ns::testing::BooleanFailMessage(__FILE__, __LINE__, #expr, #actual, \ 294 ar.c_str(), #expected) 296 NLIB_VIS_PUBLIC AssertionResult CompFailMessage(
const char* file,
int line,
const char* expr1,
297 const char* expr2,
const char* val1,
298 const char* val2,
const char* op);
299 #define NLIB_TESTING_COMP_IMPL_(func_name, op) \ 300 template<class T1, class T2> \ 301 AssertionResult Comp##func_name(const char* expr1, const char* expr2, const T1& val1, \ 302 const T2& val2, const char* file, int line) { \ 303 if (val1 op val2) { \ 304 return AssertionResult(true); \ 306 detail_func::DummyStream val1str, val2str; \ 309 return CompFailMessage(file, line, expr1, expr2, val1str.c_str(), val2str.c_str(), \ 313 NLIB_VIS_PUBLIC AssertionResult Comp##func_name(const char* expr1, const char* expr2, \ 314 int64_t val1, int64_t val2, const char* file, \ 317 NLIB_TESTING_COMP_IMPL_(EQ, ==);
318 NLIB_TESTING_COMP_IMPL_(NE, !=);
319 NLIB_TESTING_COMP_IMPL_(LE, <=);
320 NLIB_TESTING_COMP_IMPL_(LT, <);
321 NLIB_TESTING_COMP_IMPL_(GE, >=);
322 NLIB_TESTING_COMP_IMPL_(GT, >);
324 #undef NLIB_TESTING_COMP_IMPL_ 326 NLIB_VIS_PUBLIC AssertionResult CompStrEq(
const char* expr1,
const char* expr2,
const char* val1,
327 const char* val2,
const char* file,
int line);
328 NLIB_VIS_PUBLIC AssertionResult CompStrNe(
const char* expr1,
const char* expr2,
const char* val1,
329 const char* val2,
const char* file,
int line);
331 NLIB_VIS_PUBLIC AssertionResult CompStrEq(
const char* expr1,
const char* expr2,
const wchar_t* val1,
332 const wchar_t* val2,
const char* file,
int line);
333 NLIB_VIS_PUBLIC AssertionResult CompStrNe(
const char* expr1,
const char* expr2,
const wchar_t* val1,
334 const wchar_t* val2,
const char* file,
int line);
336 NLIB_VIS_PUBLIC AssertionResult CompStrEq(
const char* expr1,
const char* expr2,
338 const char* file,
int line);
339 NLIB_VIS_PUBLIC AssertionResult CompStrNe(
const char* expr1,
const char* expr2,
341 const char* file,
int line);
343 NLIB_VIS_PUBLIC AssertionResult CompStrEq(
const char* expr1,
const char* expr2,
345 const char* file,
int line);
346 NLIB_VIS_PUBLIC AssertionResult CompStrNe(
const char* expr1,
const char* expr2,
348 const char* file,
int line);
350 NLIB_VIS_PUBLIC AssertionResult CompStrCaseEq(
const char* expr1,
const char* expr2,
351 const char* val1,
const char* val2,
const char* file,
353 NLIB_VIS_PUBLIC AssertionResult CompStrCaseNe(
const char* expr1,
const char* expr2,
354 const char* val1,
const char* val2,
const char* file,
357 NLIB_VIS_PUBLIC AssertionResult CompStrCaseEq(
const char* expr1,
const char* expr2,
358 const wchar_t* val1,
const wchar_t* val2,
359 const char* file,
int line);
360 NLIB_VIS_PUBLIC AssertionResult CompStrCaseNe(
const char* expr1,
const char* expr2,
361 const wchar_t* val1,
const wchar_t* val2,
362 const char* file,
int line);
364 NLIB_VIS_PUBLIC AssertionResult CompFloatEq(
const char* expr1,
const char* expr2,
float val1,
365 float val2,
const char* file,
int line);
366 NLIB_VIS_PUBLIC AssertionResult CompDoubleEq(
const char* expr1,
const char* expr2,
double val1,
367 double val2,
const char* file,
int line);
368 NLIB_VIS_PUBLIC AssertionResult NearDouble(
double val1,
double val2,
double abs_error);
372 #define NLIB_TESTING_CONCAT_TOKEN_(a, b) NLIB_TESTING_CONCAT_TOKEN_IMPL_(a, b) 373 #define NLIB_TESTING_CONCAT_TOKEN_IMPL_(a, b) a##b 375 #define NLIB_TESTING_FATAL_ return ::nlib_ns::testing::FollowMessageTerminator(true) 376 #define NLIB_TESTING_NONFATAL_ ::nlib_ns::testing::FollowMessageTerminator(false) 378 #define NLIB_TESTING_ASSERT_HELPER_(func, val1, val2) \ 382 if (::nlib_ns::testing::AssertionResult nlib_ar_tmp_ = \ 383 func(#val1, #val2, val1, val2, __FILE__, __LINE__)) \ 386 NLIB_TESTING_FATAL_ = nlib_ar_tmp_ 388 #define NLIB_TESTING_EXPECT_HELPER_(func, val1, val2) \ 392 if (::nlib_ns::testing::AssertionResult nlib_ar_tmp_ = \ 393 func(#val1, #val2, val1, val2, __FILE__, __LINE__)) \ 396 NLIB_TESTING_NONFATAL_ = nlib_ar_tmp_ 398 #define NLIB_TESTING_CLASS_NAME_(test_case, test_name) test_case##_##test_name##_Test 400 #define NLIB_TEST_(test_case, test_name, parent) \ 401 class NLIB_TESTING_CLASS_NAME_(test_case, test_name) : public parent { \ 403 NLIB_TESTING_CLASS_NAME_(test_case, test_name)() {} \ 406 virtual void TestBody(); \ 407 static ::nlib_ns::testing::TestInfo* info_; \ 408 NLIB_DISALLOW_COPY_AND_ASSIGN(NLIB_TESTING_CLASS_NAME_(test_case, test_name)); \ 410 ::nlib_ns::testing::TestInfo* NLIB_TESTING_CLASS_NAME_(test_case, test_name)::info_ = \ 411 ::nlib_ns::testing::TestInfo::AddTestInfo<NLIB_TESTING_CLASS_NAME_(test_case, test_name)>( \ 412 #test_case, #test_name, -1, parent::SetUpTestCase, parent::TearDownTestCase); \ 413 void NLIB_TESTING_CLASS_NAME_(test_case, test_name)::TestBody() 416 NearFailMessage(AssertionResult* ar,
const char* file,
int line,
const char* expr1,
417 const char* expr2,
const char* abs_error_expr,
double val1,
double val2,
428 #define NLIB_TESTING_FLAG(flag_name) NLIB_FLAGS_##flag_name 445 #define TEST(test_case_name, specific_test_name) \ 446 NLIB_TEST_(test_case_name, specific_test_name, ::nlib_ns::testing::Test) 447 #define TEST_F(test_fixture_name, specific_test_name) \ 448 NLIB_TEST_(test_fixture_name, specific_test_name, test_fixture_name) 450 #define FRIEND_TEST(test_case, test_name) \ 451 friend class NLIB_TESTING_CLASS_NAME_(test_case, test_name) 453 #define RUN_ALL_TESTS() ::nlib_ns::testing::TestInfo::Run() 456 return ::nlib_ns::testing::FollowMessageTerminator(true) = \ 457 ::nlib_ns::testing::AssertionResult(false) 460 #define ASSERT_TRUE(expr) NLIB_TESTING_BOOLEAN_IMPL_(expr, true, false, NLIB_TESTING_FATAL_) 461 #define ASSERT_FALSE(expr) NLIB_TESTING_BOOLEAN_IMPL_(!(expr), false, true, NLIB_TESTING_FATAL_) 462 #define EXPECT_TRUE(expr) NLIB_TESTING_BOOLEAN_IMPL_(expr, true, false, NLIB_TESTING_NONFATAL_) 463 #define EXPECT_FALSE(expr) NLIB_TESTING_BOOLEAN_IMPL_(!(expr), false, true, NLIB_TESTING_NONFATAL_) 465 #define ASSERT_EQ(expected, actual) \ 466 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompEQ, expected, actual) 467 #define ASSERT_NE(expected, actual) \ 468 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompNE, expected, actual) 469 #define ASSERT_LE(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompLE, val1, val2) 470 #define ASSERT_LT(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompLT, val1, val2) 471 #define ASSERT_GE(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompGE, val1, val2) 472 #define ASSERT_GT(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompGT, val1, val2) 474 #define EXPECT_EQ(expected, actual) \ 475 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompEQ, expected, actual) 476 #define EXPECT_NE(expected, actual) \ 477 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompNE, expected, actual) 478 #define EXPECT_LE(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompLE, val1, val2) 479 #define EXPECT_LT(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompLT, val1, val2) 480 #define EXPECT_GE(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompGE, val1, val2) 481 #define EXPECT_GT(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompGT, val1, val2) 483 #define ASSERT_STREQ(expected, actual) \ 484 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrEq, expected, actual) 485 #define ASSERT_STRNE(expected, actual) \ 486 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrNe, expected, actual) 488 #define EXPECT_STREQ(expected, actual) \ 489 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrEq, expected, actual) 490 #define EXPECT_STRNE(expected, actual) \ 491 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrNe, expected, actual) 493 #define ASSERT_STRCASEEQ(expected, actual) \ 494 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrCaseEq, expected, actual) 495 #define ASSERT_STRCASENE(expected, actual) \ 496 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrCaseNe, expected, actual) 498 #define EXPECT_STRCASEEQ(expected, actual) \ 499 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrCaseEq, expected, actual) 500 #define EXPECT_STRCASENE(expected, actual) \ 501 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrCaseNe, expected, actual) 507 #define ASSERT_FLOAT_EQ(expected, actual) \ 508 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompFloatEq, expected, actual) 509 #define ASSERT_DOUBLE_EQ(expected, actual) \ 510 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompDoubleEq, expected, actual) 512 #define EXPECT_FLOAT_EQ(expected, actual) \ 513 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompFloatEq, expected, actual) 514 #define EXPECT_DOUBLE_EQ(expected, actual) \ 515 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompDoubleEq, expected, actual) 517 #define ASSERT_NEAR(val1, val2, abs_error) \ 521 if (::nlib_ns::testing::AssertionResult nlib_ar_tmp_ = \ 522 ::nlib_ns::testing::NearDouble(val1, val2, abs_error)) \ 525 NLIB_TESTING_FATAL_ = \ 526 ::nlib_ns::testing::NearFailMessage(&nlib_ar_tmp_, __FILE__, __LINE__, #val1, \ 527 #val2, #abs_error, val1, val2, abs_error) 529 #define EXPECT_NEAR(val1, val2, abs_error) \ 533 if (::nlib_ns::testing::AssertionResult nlib_ar_tmp_ = \ 534 ::nlib_ns::testing::NearDouble(val1, val2, abs_error)) \ 537 NLIB_TESTING_NONFATAL_ = \ 538 ::nlib_ns::testing::NearFailMessage(&nlib_ar_tmp_, __FILE__, __LINE__, #val1, \ 539 #val2, #abs_error, val1, val2, abs_error) 541 #define SCOPED_TRACE(msg) \ 542 ::nlib_ns::testing::ScopedTrace NLIB_TESTING_CONCAT_TOKEN_(nlib_scopedtrace_, __LINE__)( \ 543 ::nlib_ns::testing::AssertionResult(true) << (msg), __FILE__, __LINE__) 545 #define ASSERT_NO_FATAL_FAILURE(statement) \ 553 if (!::nlib_ns::testing::UnitTest::GetInstance() \ 554 ->current_test_info() \ 556 ->HasFatalFaiulre()) \ 559 NLIB_TESTING_FATAL_ = ::nlib_ns::testing::AssertionResult(false) \ 560 << "Expected: " #statement \ 561 " doesn't generate new fatal failures.\n" \ 562 " Actual: it does.\n" 564 #define EXPECT_NO_FATAL_FAILURE(statement) \ 572 if (!::nlib_ns::testing::UnitTest::GetInstance() \ 573 ->current_test_info() \ 575 ->HasFatalFaiulre()) \ 578 NLIB_TESTING_NONFATAL_ = ::nlib_ns::testing::AssertionResult(false) \ 579 << "Expected: " #statement \ 580 " doesn't generate new fatal failures.\n" \ 581 " Actual: it does.\n" 583 #define GTEST_FLAG(name) NLIB_FLAGS_gtest_##name 588 #if defined(_MSC_VER) && defined(nx_testing_EXPORTS) 589 #undef NLIB_VIS_PUBLIC 590 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 593 #endif // INCLUDE_NN_NLIB_TESTING_NTEST_H_ #define NLIB_OVERRIDE
利用可能であればoverrideが定義されます。そうでない場合は空文字列です。
AssertionResult FloatLE(const char *expr1, const char *expr2, float val1, float val2)
val1 <= val2を検証します。[ASSERT|EXPECT]_PRED_FORMAT2に組み込んで利用します。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
virtual void SetUp()
全てのテストの前に1回だけ実行される処理を記述します。
コマンドライン文字列を扱うためのクラスが定義されています。
#define NLIB_FLAGS_DECLARE_bool(opt_name)
別の場所で定義された、NLIB_FLAGS_オプション名、を利用できるようにします。
void InitNintendoTest(int *argc, wchar_t **argv)
テストプログラムのコマンドラインオプションを処理します。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Environment * AddGlobalTestEnvironment(Environment *env)
Environment へのポインタを渡して、全てのテストの前と後に実行されるコードを登録します。 ...
virtual void TearDown()
全てのテストの後に1回だけ実行される処理を記述します。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
#define NLIB_STATIC_ASSERT(exp)
静的アサートが定義されます。利用可能であればstatic_assertを利用します。
#define NLIB_FLAGS_DECLARE_string(opt_name)
別の場所で定義された、NLIB_FLAGS_オプション名、を利用できるようにします。
このクラスを継承してグローバルに実行されるSetUp()とTearDown()を定義します。
AssertionResult DoubleLE(const char *expr1, const char *expr2, double val1, double val2)
val1 <= val2を検証します。[ASSERT|EXPECT]_PRED_FORMAT2に組み込んで利用します。
#define NLIB_FLAGS_DECLARE_int32(opt_name)
別の場所で定義された、NLIB_FLAGS_オプション名、を利用できるようにします。