3 #ifndef INCLUDE_NN_NLIB_TESTING_NTEST_H_ 4 #define INCLUDE_NN_NLIB_TESTING_NTEST_H_ 14 #if defined(_MSC_VER) && defined(nx_testing_EXPORTS) 15 #undef NLIB_VIS_PUBLIC 16 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT 38 bool HasFatalFaiulre()
const {
return is_fatal_error; }
39 bool HasNonfatalFailure()
const {
return is_non_fatal_error; }
44 bool is_non_fatal_error;
56 virtual ~TestFactoryBase() {}
57 virtual Test* Create() = 0;
66 template <
class TestClass>
67 class TestFactory :
public TestFactoryBase {
83 const char* test_case_name()
const {
return testcase_name_; }
84 const char* name()
const {
return testname_; }
85 const TestResult* result()
const {
return &result_; }
89 typedef void (*SetUpTestCaseFunc)();
90 typedef void (*TearDownTestCaseFunc)();
91 template <
class TestClass>
92 static TestInfo* AddTestInfo(
const char* test_case,
const char* test_name,
int index,
93 SetUpTestCaseFunc setup_func, TearDownTestCaseFunc teardown_func);
96 static const int kMaxTestList = 4096;
97 static TestInfo testlist_[kMaxTestList];
98 static int testlist_idx_;
102 bool TestCaseNameLess(
const TestInfo& rhs)
const {
103 int tmp = strcmp(testcase_name_, rhs.testcase_name_);
104 if (tmp < 0)
return true;
105 if (tmp > 0)
return false;
106 return index_ < rhs.index_;
108 bool TestCaseNameEq(
const TestInfo& rhs)
const {
109 return index_ == rhs.index_ &&
110 strcmp(testcase_name_, rhs.testcase_name_) == 0;
112 bool IsTypedTest()
const {
return index_ >= 0; }
113 int GetTypedTestVariant()
const {
return index_ < 0 ? 0 : index_; }
114 struct TestSortPred {
115 bool operator()(TestInfo* lhs, TestInfo* rhs) {
116 return lhs->TestCaseNameLess(*rhs);
121 static TestInfo& AddTestInfo_(
const char* test_case,
const char* test_name,
int index,
122 SetUpTestCaseFunc setup_func,
123 TearDownTestCaseFunc teardown_func);
126 friend class FollowMessageTerminator;
127 const char* testcase_name_;
128 const char* testname_;
130 TestFactoryBase* factory_;
131 void (*setup_testcase_)();
132 void (*teardown_testcase_)();
134 double testfactory_space_[2];
138 template <
class TestClass>
139 TestInfo* TestInfo::AddTestInfo(
const char* test_case,
const char* test_name,
int index,
140 SetUpTestCaseFunc setup_func, TearDownTestCaseFunc teardown_func) {
145 TestInfo& info = AddTestInfo_(test_case, test_name, index, setup_func, teardown_func);
146 void* p = &info.testfactory_space_[0];
147 new (p) TestFactory<TestClass>();
153 char* tmpptr =
reinterpret_cast<char*
>(&info.testfactory_space_[0]);
154 info.factory_ =
reinterpret_cast<TestFactoryBase*
>(tmpptr);
162 static UnitTest* GetInstance();
167 const TestInfo* current_test_info()
const {
return current_; }
190 friend class TestInfo;
192 friend class FollowMessageTerminator;
193 UnitTest() : current_(NULL) {}
194 virtual ~UnitTest() {}
201 static void SetUpTestCase() {}
202 static void TearDownTestCase() {}
207 void RecordProperty(
const char* key,
const char* value);
208 void RecordProperty(
const char* key,
int value);
211 virtual void SetUp() {}
212 virtual void TearDown() {}
213 virtual void TestBody() = 0;
221 explicit AssertionResult(
bool result) : is_success_(result), builder_() {}
222 ~AssertionResult() {}
223 AssertionResult(
const AssertionResult& rhs)
224 : is_success_(rhs.is_success_), builder_() {
226 builder_.swap(const_cast<AssertionResult&>(rhs).builder_);
228 operator bool()
const {
return is_success_; }
229 AssertionResult operator!()
const {
return AssertionResult(!is_success_); }
230 const char* c_str()
const {
return builder_.c_str(); }
233 AssertionResult& operator<<(
const T& rhs) {
237 AssertionResult& operator<<(AssertionResult& rhs) {
238 is_success_ = rhs.is_success_;
239 builder_.swap(rhs.builder_);
245 detail_func::DummyStream builder_;
246 AssertionResult& operator=(
const AssertionResult&);
252 ScopedTrace(AssertionResult& msg,
const char* file,
int line);
261 explicit FollowMessageTerminator(
bool flag);
267 void operator=(
const AssertionResult& msg)
const;
273 inline bool suppress_warning_(
bool b) {
return b; }
274 NLIB_VIS_PUBLIC AssertionResult BooleanFailMessage(
const char* file,
int line,
const char* expr,
275 const char* actual,
const char* expected);
276 #define NLIB_TESTING_BOOLEAN_IMPL_(expr, expected, actual, iffail) \ 280 if (::nlib_ns::testing::suppress_warning_(expr)) \ 284 ::nlib_ns::testing::BooleanFailMessage(__FILE__, __LINE__, #expr, #actual, #expected) 286 NLIB_VIS_PUBLIC AssertionResult CompFailMessage(
const char* file,
int line,
const char* expr1,
287 const char* expr2,
const char* val1,
288 const char* val2,
const char* op);
289 #define NLIB_TESTING_COMP_IMPL_(func_name, op) \ 290 template<class T1, class T2> \ 291 AssertionResult Comp##func_name(const char* expr1, const char* expr2, const T1& val1, \ 292 const T2& val2, const char* file, int line) { \ 293 if (val1 op val2) { \ 294 return AssertionResult(true); \ 296 detail_func::DummyStream val1str, val2str; \ 299 return CompFailMessage(file, line, expr1, expr2, val1str.c_str(), \ 300 val2str.c_str(), #op); \ 303 NLIB_VIS_PUBLIC AssertionResult Comp##func_name(const char* expr1, const char* expr2, \ 304 int64_t val1, int64_t val2, \ 305 const char* file, int line) 307 NLIB_TESTING_COMP_IMPL_(EQ, ==);
308 NLIB_TESTING_COMP_IMPL_(NE, !=);
309 NLIB_TESTING_COMP_IMPL_(LE, <=);
310 NLIB_TESTING_COMP_IMPL_(LT, <);
311 NLIB_TESTING_COMP_IMPL_(GE, >=);
312 NLIB_TESTING_COMP_IMPL_(GT, >);
314 #undef NLIB_TESTING_COMP_IMPL_ 316 NLIB_VIS_PUBLIC AssertionResult CompStrEq(
const char* expr1,
const char* expr2,
const char* val1,
317 const char* val2,
const char* file,
int line);
318 NLIB_VIS_PUBLIC AssertionResult CompStrNe(
const char* expr1,
const char* expr2,
const char* val1,
319 const char* val2,
const char* file,
int line);
321 NLIB_VIS_PUBLIC AssertionResult CompStrEq(
const char* expr1,
const char* expr2,
const wchar_t* val1,
322 const wchar_t* val2,
const char* file,
int line);
323 NLIB_VIS_PUBLIC AssertionResult CompStrNe(
const char* expr1,
const char* expr2,
const wchar_t* val1,
324 const wchar_t* val2,
const char* file,
int line);
326 NLIB_VIS_PUBLIC AssertionResult CompStrCaseEq(
const char* expr1,
const char* expr2,
327 const char* val1,
const char* val2,
328 const char* file,
int line);
329 NLIB_VIS_PUBLIC AssertionResult CompStrCaseNe(
const char* expr1,
const char* expr2,
330 const char* val1,
const char* val2,
331 const char* file,
int line);
333 NLIB_VIS_PUBLIC AssertionResult CompStrCaseEq(
const char* expr1,
const char* expr2,
334 const wchar_t* val1,
const wchar_t* val2,
335 const char* file,
int line);
336 NLIB_VIS_PUBLIC AssertionResult CompStrCaseNe(
const char* expr1,
const char* expr2,
337 const wchar_t* val1,
const wchar_t* val2,
338 const char* file,
int line);
340 NLIB_VIS_PUBLIC AssertionResult CompFloatEq(
const char* expr1,
const char* expr2,
float val1,
341 float val2,
const char* file,
int line);
342 NLIB_VIS_PUBLIC AssertionResult CompDoubleEq(
const char* expr1,
const char* expr2,
double val1,
343 double val2,
const char* file,
int line);
344 NLIB_VIS_PUBLIC AssertionResult NearDouble(
double val1,
double val2,
double abs_error);
348 #define NLIB_TESTING_CONCAT_TOKEN_(a, b) NLIB_TESTING_CONCAT_TOKEN_IMPL_(a, b) 349 #define NLIB_TESTING_CONCAT_TOKEN_IMPL_(a, b) a##b 351 #define NLIB_TESTING_FATAL_ return ::nlib_ns::testing::FollowMessageTerminator(true) 352 #define NLIB_TESTING_NONFATAL_ ::nlib_ns::testing::FollowMessageTerminator(false) 354 #define NLIB_TESTING_ASSERT_HELPER_(func, val1, val2) \ 358 if (::nlib_ns::testing::AssertionResult ar = \ 359 func(#val1, #val2, val1, val2, __FILE__, __LINE__)) \ 362 NLIB_TESTING_FATAL_ = ar 364 #define NLIB_TESTING_EXPECT_HELPER_(func, val1, val2) \ 368 if (::nlib_ns::testing::AssertionResult ar = \ 369 func(#val1, #val2, val1, val2, __FILE__, __LINE__)) \ 372 NLIB_TESTING_NONFATAL_ = ar 374 #define NLIB_TESTING_CLASS_NAME_(test_case, test_name) test_case##_##test_name##_Test 376 #define NLIB_TEST_(test_case, test_name, parent) \ 377 class NLIB_TESTING_CLASS_NAME_(test_case, test_name) \ 380 NLIB_TESTING_CLASS_NAME_(test_case, test_name)() {} \ 382 virtual void TestBody(); \ 383 static ::nlib_ns::testing::TestInfo* info_; \ 384 NLIB_DISALLOW_COPY_AND_ASSIGN(NLIB_TESTING_CLASS_NAME_(test_case, test_name)); \ 386 ::nlib_ns::testing::TestInfo* NLIB_TESTING_CLASS_NAME_(test_case, test_name)::info_ = \ 387 ::nlib_ns::testing::TestInfo::AddTestInfo< \ 388 NLIB_TESTING_CLASS_NAME_(test_case, test_name)>( \ 389 #test_case, #test_name, -1, parent::SetUpTestCase, parent::TearDownTestCase); \ 390 void NLIB_TESTING_CLASS_NAME_(test_case, test_name)::TestBody() 392 NLIB_VIS_PUBLIC AssertionResult& NearFailMessage(AssertionResult* ar,
const char* file,
int line,
393 const char* expr1,
const char* expr2,
394 const char* abs_error_expr,
double val1,
395 double val2,
double abs_error);
398 FloatLE(
const char* expr1,
const char* expr2,
float val1,
float val2);
400 DoubleLE(
const char* expr1,
const char* expr2,
double val1,
double val2);
405 #define NLIB_TESTING_FLAG(flag_name) NLIB_FLAGS_##flag_name 422 #define TEST(test_case_name, specific_test_name) \ 423 NLIB_TEST_(test_case_name, specific_test_name, ::nlib_ns::testing::Test) 424 #define TEST_F(test_fixture_name, specific_test_name) \ 425 NLIB_TEST_(test_fixture_name, specific_test_name, test_fixture_name) 427 #define FRIEND_TEST(test_case, test_name) \ 428 friend class NLIB_TESTING_CLASS_NAME_(test_case, test_name) 430 #define RUN_ALL_TESTS() ::nlib_ns::testing::TestInfo::Run() 433 return ::nlib_ns::testing::FollowMessageTerminator(true) = \ 434 ::nlib_ns::testing::AssertionResult(false) 437 #define ASSERT_TRUE(expr) NLIB_TESTING_BOOLEAN_IMPL_(expr, true, false, NLIB_TESTING_FATAL_) 438 #define ASSERT_FALSE(expr) NLIB_TESTING_BOOLEAN_IMPL_(!(expr), false, true, NLIB_TESTING_FATAL_) 439 #define EXPECT_TRUE(expr) NLIB_TESTING_BOOLEAN_IMPL_(expr, true, false, NLIB_TESTING_NONFATAL_) 440 #define EXPECT_FALSE(expr) NLIB_TESTING_BOOLEAN_IMPL_(!(expr), false, true, NLIB_TESTING_NONFATAL_) 442 #define ASSERT_EQ(expected, actual) \ 443 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompEQ, expected, actual) 444 #define ASSERT_NE(expected, actual) \ 445 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompNE, expected, actual) 446 #define ASSERT_LE(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompLE, val1, val2) 447 #define ASSERT_LT(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompLT, val1, val2) 448 #define ASSERT_GE(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompGE, val1, val2) 449 #define ASSERT_GT(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompGT, val1, val2) 451 #define EXPECT_EQ(expected, actual) \ 452 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompEQ, expected, actual) 453 #define EXPECT_NE(expected, actual) \ 454 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompNE, expected, actual) 455 #define EXPECT_LE(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompLE, val1, val2) 456 #define EXPECT_LT(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompLT, val1, val2) 457 #define EXPECT_GE(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompGE, val1, val2) 458 #define EXPECT_GT(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompGT, val1, val2) 460 #define ASSERT_STREQ(expected, actual) \ 461 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrEq, expected, actual) 462 #define ASSERT_STRNE(expected, actual) \ 463 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrNe, expected, actual) 465 #define EXPECT_STREQ(expected, actual) \ 466 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrEq, expected, actual) 467 #define EXPECT_STRNE(expected, actual) \ 468 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrNe, expected, actual) 470 #define ASSERT_STRCASEEQ(expected, actual) \ 471 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrCaseEq, expected, actual) 472 #define ASSERT_STRCASENE(expected, actual) \ 473 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrCaseNe, expected, actual) 475 #define EXPECT_STRCASEEQ(expected, actual) \ 476 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrCaseEq, expected, actual) 477 #define EXPECT_STRCASENE(expected, actual) \ 478 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrCaseNe, expected, actual) 484 #define ASSERT_FLOAT_EQ(expected, actual) \ 485 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompFloatEq, expected, actual) 486 #define ASSERT_DOUBLE_EQ(expected, actual) \ 487 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompDoubleEq, expected, actual) 489 #define EXPECT_FLOAT_EQ(expected, actual) \ 490 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompFloatEq, expected, actual) 491 #define EXPECT_DOUBLE_EQ(expected, actual) \ 492 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompDoubleEq, expected, actual) 494 #define ASSERT_NEAR(val1, val2, abs_error) \ 498 if (::nlib_ns::testing::AssertionResult ar = \ 499 ::nlib_ns::testing::NearDouble(val1, val2, abs_error)) \ 502 NLIB_TESTING_FATAL_ = ::nlib_ns::testing::NearFailMessage( \ 503 &ar, __FILE__, __LINE__, #val1, #val2, #abs_error, val1, val2, abs_error) 505 #define EXPECT_NEAR(val1, val2, abs_error) \ 509 if (::nlib_ns::testing::AssertionResult ar = \ 510 ::nlib_ns::testing::NearDouble(val1, val2, abs_error)) \ 513 NLIB_TESTING_NONFATAL_ = ::nlib_ns::testing::NearFailMessage( \ 514 &ar, __FILE__, __LINE__, #val1, #val2, #abs_error, val1, val2, abs_error) 516 #define SCOPED_TRACE(msg) \ 517 ::nlib_ns::testing::ScopedTrace NLIB_TESTING_CONCAT_TOKEN_(nlib_scopedtrace_, __LINE__)( \ 518 ::nlib_ns::testing::AssertionResult(true) << (msg), __FILE__, __LINE__) 520 #define ASSERT_NO_FATAL_FAILURE(statement) \ 528 if (!::nlib_ns::testing::UnitTest::GetInstance() \ 529 ->current_test_info() \ 531 ->HasFatalFaiulre()) \ 534 NLIB_TESTING_FATAL_ = ::nlib_ns::testing::AssertionResult(false) \ 535 << "Expected: " #statement \ 536 " doesn't generate new fatal failures.\n" \ 537 " Actual: it does.\n" 539 #define EXPECT_NO_FATAL_FAILURE(statement) \ 547 if (!::nlib_ns::testing::UnitTest::GetInstance() \ 548 ->current_test_info() \ 550 ->HasFatalFaiulre()) \ 553 NLIB_TESTING_NONFATAL_ = ::nlib_ns::testing::AssertionResult(false) \ 554 << "Expected: " #statement \ 555 " doesn't generate new fatal failures.\n" \ 556 " Actual: it does.\n" 558 #define GTEST_FLAG(name) NLIB_FLAGS_gtest_##name 563 #if defined(_MSC_VER) && defined(nx_testing_EXPORTS) 564 #undef NLIB_VIS_PUBLIC 565 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 568 #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回だけ実行される処理を記述します。
コマンドライン文字列を扱うためのクラスが定義されています。
main関数の実行前にstatic変数により暗黙的にインスタンスを構築するタイプのシングルトンです。 ...
#define NLIB_FLAGS_DECLARE_bool(opt_name)
別の場所で定義された、NLIB_FLAGS_オプション名、を利用できるようにします。
void InitNintendoTest(int *argc, wchar_t **argv)
テストプログラムのコマンドラインオプションを処理します。
Environment * AddGlobalTestEnvironment(Environment *env)
Environment へのポインタを渡して、全てのテストの前と後に実行されるコードを登録します。 ...
virtual void TearDown()
全てのテストの後に1回だけ実行される処理を記述します。
#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_オプション名、を利用できるようにします。