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_ &&
123 strcmp(testcase_name_, rhs.testcase_name_) == 0;
125 bool IsTypedTest()
const {
return index_ >= 0; }
126 int GetTypedTestVariant()
const {
return index_ < 0 ? 0 : index_; }
127 struct TestSortPred {
128 bool operator()(TestInfo* lhs, TestInfo* rhs) {
129 return lhs->TestCaseNameLess(*rhs);
134 static TestInfo& AddTestInfo_(
const char* test_case,
const char* test_name,
int index,
135 SetUpTestCaseFunc setup_func,
136 TearDownTestCaseFunc teardown_func);
139 friend class FollowMessageTerminator;
140 const char* testcase_name_;
141 const char* testname_;
143 TestFactoryBase* factory_;
144 void (*setup_testcase_)();
145 void (*teardown_testcase_)();
147 double testfactory_space_[2];
151 template <
class TestClass>
152 TestInfo* TestInfo::AddTestInfo(
const char* test_case,
const char* test_name,
int index,
153 SetUpTestCaseFunc setup_func, TearDownTestCaseFunc teardown_func) {
158 TestInfo& info = AddTestInfo_(test_case, test_name, index, setup_func, teardown_func);
159 void* p = &info.testfactory_space_[0];
160 new (p) TestFactory<TestClass>();
166 char* tmpptr =
reinterpret_cast<char*
>(&info.testfactory_space_[0]);
167 info.factory_ =
reinterpret_cast<TestFactoryBase*
>(tmpptr);
175 static UnitTest* GetInstance();
180 const TestInfo* current_test_info()
const {
return current_; }
203 friend class TestInfo;
205 friend class FollowMessageTerminator;
206 UnitTest() : current_(
nullptr) {}
207 virtual ~UnitTest() {}
214 static void SetUpTestCase() {}
215 static void TearDownTestCase() {}
220 void RecordProperty(
const char* key,
const char* value);
221 void RecordProperty(
const char* key,
int value);
224 virtual void SetUp() {}
225 virtual void TearDown() {}
226 virtual void TestBody() = 0;
234 explicit AssertionResult(
bool result) : is_success_(result), builder_() {}
235 ~AssertionResult() {}
236 AssertionResult(
const AssertionResult& rhs)
237 : is_success_(rhs.is_success_),
238 builder_(const_cast<detail_func::DummyStream&>(rhs.builder_),
242 operator bool()
const {
return is_success_; }
243 AssertionResult operator!()
const {
return AssertionResult(!is_success_); }
244 const char* c_str()
const {
return builder_.c_str(); }
247 AssertionResult& operator<<(
const T& rhs) {
251 AssertionResult& operator<<(AssertionResult& rhs) {
252 is_success_ = rhs.is_success_;
253 builder_.assign(rhs.builder_,
move_tag());
259 detail_func::DummyStream builder_;
260 AssertionResult& operator=(
const AssertionResult&);
266 ScopedTrace(AssertionResult& msg,
const char* file,
int line);
275 explicit FollowMessageTerminator(
bool flag);
281 void operator=(
const AssertionResult& msg)
const;
287 inline bool suppress_warning_(
bool b) {
return b; }
288 NLIB_VIS_PUBLIC AssertionResult BooleanFailMessage(
const char* file,
int line,
const char* expr,
289 const char* actual,
const char* expected);
290 #define NLIB_TESTING_BOOLEAN_IMPL_(expr, expected, actual, iffail) \ 294 if (::nlib_ns::testing::suppress_warning_(expr)) \ 298 ::nlib_ns::testing::BooleanFailMessage(__FILE__, __LINE__, #expr, #actual, #expected) 300 NLIB_VIS_PUBLIC AssertionResult CompFailMessage(
const char* file,
int line,
const char* expr1,
301 const char* expr2,
const char* val1,
302 const char* val2,
const char* op);
303 #define NLIB_TESTING_COMP_IMPL_(func_name, op) \ 304 template<class T1, class T2> \ 305 AssertionResult Comp##func_name(const char* expr1, const char* expr2, const T1& val1, \ 306 const T2& val2, const char* file, int line) { \ 307 if (val1 op val2) { \ 308 return AssertionResult(true); \ 310 detail_func::DummyStream val1str, val2str; \ 313 return CompFailMessage(file, line, expr1, expr2, val1str.c_str(), \ 314 val2str.c_str(), #op); \ 317 NLIB_VIS_PUBLIC AssertionResult Comp##func_name(const char* expr1, const char* expr2, \ 318 int64_t val1, int64_t val2, \ 319 const char* file, int line) 321 NLIB_TESTING_COMP_IMPL_(EQ, ==);
322 NLIB_TESTING_COMP_IMPL_(NE, !=);
323 NLIB_TESTING_COMP_IMPL_(LE, <=);
324 NLIB_TESTING_COMP_IMPL_(LT, <);
325 NLIB_TESTING_COMP_IMPL_(GE, >=);
326 NLIB_TESTING_COMP_IMPL_(GT, >);
328 #undef NLIB_TESTING_COMP_IMPL_ 330 NLIB_VIS_PUBLIC AssertionResult CompStrEq(
const char* expr1,
const char* expr2,
const char* val1,
331 const char* val2,
const char* file,
int line);
332 NLIB_VIS_PUBLIC AssertionResult CompStrNe(
const char* expr1,
const char* expr2,
const char* val1,
333 const char* val2,
const char* file,
int line);
335 NLIB_VIS_PUBLIC AssertionResult CompStrEq(
const char* expr1,
const char* expr2,
const wchar_t* val1,
336 const wchar_t* val2,
const char* file,
int line);
337 NLIB_VIS_PUBLIC AssertionResult CompStrNe(
const char* expr1,
const char* expr2,
const wchar_t* val1,
338 const wchar_t* val2,
const char* file,
int line);
340 NLIB_VIS_PUBLIC AssertionResult CompStrCaseEq(
const char* expr1,
const char* expr2,
341 const char* val1,
const char* val2,
342 const char* file,
int line);
343 NLIB_VIS_PUBLIC AssertionResult CompStrCaseNe(
const char* expr1,
const char* expr2,
344 const char* val1,
const char* val2,
345 const char* file,
int line);
347 NLIB_VIS_PUBLIC AssertionResult CompStrCaseEq(
const char* expr1,
const char* expr2,
348 const wchar_t* val1,
const wchar_t* val2,
349 const char* file,
int line);
350 NLIB_VIS_PUBLIC AssertionResult CompStrCaseNe(
const char* expr1,
const char* expr2,
351 const wchar_t* val1,
const wchar_t* val2,
352 const char* file,
int line);
354 NLIB_VIS_PUBLIC AssertionResult CompFloatEq(
const char* expr1,
const char* expr2,
float val1,
355 float val2,
const char* file,
int line);
356 NLIB_VIS_PUBLIC AssertionResult CompDoubleEq(
const char* expr1,
const char* expr2,
double val1,
357 double val2,
const char* file,
int line);
358 NLIB_VIS_PUBLIC AssertionResult NearDouble(
double val1,
double val2,
double abs_error);
362 #define NLIB_TESTING_CONCAT_TOKEN_(a, b) NLIB_TESTING_CONCAT_TOKEN_IMPL_(a, b) 363 #define NLIB_TESTING_CONCAT_TOKEN_IMPL_(a, b) a##b 365 #define NLIB_TESTING_FATAL_ return ::nlib_ns::testing::FollowMessageTerminator(true) 366 #define NLIB_TESTING_NONFATAL_ ::nlib_ns::testing::FollowMessageTerminator(false) 368 #define NLIB_TESTING_ASSERT_HELPER_(func, val1, val2) \ 372 if (::nlib_ns::testing::AssertionResult nlib_ar_tmp_ = \ 373 func(#val1, #val2, val1, val2, __FILE__, __LINE__)) \ 376 NLIB_TESTING_FATAL_ = nlib_ar_tmp_ 378 #define NLIB_TESTING_EXPECT_HELPER_(func, val1, val2) \ 382 if (::nlib_ns::testing::AssertionResult nlib_ar_tmp_ = \ 383 func(#val1, #val2, val1, val2, __FILE__, __LINE__)) \ 386 NLIB_TESTING_NONFATAL_ = nlib_ar_tmp_ 388 #define NLIB_TESTING_CLASS_NAME_(test_case, test_name) test_case##_##test_name##_Test 390 #define NLIB_TEST_(test_case, test_name, parent) \ 391 class NLIB_TESTING_CLASS_NAME_(test_case, test_name) \ 394 NLIB_TESTING_CLASS_NAME_(test_case, test_name)() {} \ 396 virtual void TestBody(); \ 397 static ::nlib_ns::testing::TestInfo* info_; \ 398 NLIB_DISALLOW_COPY_AND_ASSIGN(NLIB_TESTING_CLASS_NAME_(test_case, test_name)); \ 400 ::nlib_ns::testing::TestInfo* NLIB_TESTING_CLASS_NAME_(test_case, test_name)::info_ = \ 401 ::nlib_ns::testing::TestInfo::AddTestInfo< \ 402 NLIB_TESTING_CLASS_NAME_(test_case, test_name)>( \ 403 #test_case, #test_name, -1, parent::SetUpTestCase, parent::TearDownTestCase); \ 404 void NLIB_TESTING_CLASS_NAME_(test_case, test_name)::TestBody() 406 NLIB_VIS_PUBLIC AssertionResult& NearFailMessage(AssertionResult* ar,
const char* file,
int line,
407 const char* expr1,
const char* expr2,
408 const char* abs_error_expr,
double val1,
409 double val2,
double abs_error);
412 FloatLE(
const char* expr1,
const char* expr2,
float val1,
float val2);
414 DoubleLE(
const char* expr1,
const char* expr2,
double val1,
double val2);
419 #define NLIB_TESTING_FLAG(flag_name) NLIB_FLAGS_##flag_name 436 #define TEST(test_case_name, specific_test_name) \ 437 NLIB_TEST_(test_case_name, specific_test_name, ::nlib_ns::testing::Test) 438 #define TEST_F(test_fixture_name, specific_test_name) \ 439 NLIB_TEST_(test_fixture_name, specific_test_name, test_fixture_name) 441 #define FRIEND_TEST(test_case, test_name) \ 442 friend class NLIB_TESTING_CLASS_NAME_(test_case, test_name) 444 #define RUN_ALL_TESTS() ::nlib_ns::testing::TestInfo::Run() 447 return ::nlib_ns::testing::FollowMessageTerminator(true) = \ 448 ::nlib_ns::testing::AssertionResult(false) 451 #define ASSERT_TRUE(expr) NLIB_TESTING_BOOLEAN_IMPL_(expr, true, false, NLIB_TESTING_FATAL_) 452 #define ASSERT_FALSE(expr) NLIB_TESTING_BOOLEAN_IMPL_(!(expr), false, true, NLIB_TESTING_FATAL_) 453 #define EXPECT_TRUE(expr) NLIB_TESTING_BOOLEAN_IMPL_(expr, true, false, NLIB_TESTING_NONFATAL_) 454 #define EXPECT_FALSE(expr) NLIB_TESTING_BOOLEAN_IMPL_(!(expr), false, true, NLIB_TESTING_NONFATAL_) 456 #define ASSERT_EQ(expected, actual) \ 457 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompEQ, expected, actual) 458 #define ASSERT_NE(expected, actual) \ 459 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompNE, expected, actual) 460 #define ASSERT_LE(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompLE, val1, val2) 461 #define ASSERT_LT(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompLT, val1, val2) 462 #define ASSERT_GE(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompGE, val1, val2) 463 #define ASSERT_GT(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompGT, val1, val2) 465 #define EXPECT_EQ(expected, actual) \ 466 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompEQ, expected, actual) 467 #define EXPECT_NE(expected, actual) \ 468 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompNE, expected, actual) 469 #define EXPECT_LE(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompLE, val1, val2) 470 #define EXPECT_LT(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompLT, val1, val2) 471 #define EXPECT_GE(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompGE, val1, val2) 472 #define EXPECT_GT(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompGT, val1, val2) 474 #define ASSERT_STREQ(expected, actual) \ 475 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrEq, expected, actual) 476 #define ASSERT_STRNE(expected, actual) \ 477 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrNe, expected, actual) 479 #define EXPECT_STREQ(expected, actual) \ 480 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrEq, expected, actual) 481 #define EXPECT_STRNE(expected, actual) \ 482 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrNe, expected, actual) 484 #define ASSERT_STRCASEEQ(expected, actual) \ 485 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrCaseEq, expected, actual) 486 #define ASSERT_STRCASENE(expected, actual) \ 487 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrCaseNe, expected, actual) 489 #define EXPECT_STRCASEEQ(expected, actual) \ 490 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrCaseEq, expected, actual) 491 #define EXPECT_STRCASENE(expected, actual) \ 492 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrCaseNe, expected, actual) 498 #define ASSERT_FLOAT_EQ(expected, actual) \ 499 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompFloatEq, expected, actual) 500 #define ASSERT_DOUBLE_EQ(expected, actual) \ 501 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompDoubleEq, expected, actual) 503 #define EXPECT_FLOAT_EQ(expected, actual) \ 504 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompFloatEq, expected, actual) 505 #define EXPECT_DOUBLE_EQ(expected, actual) \ 506 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompDoubleEq, expected, actual) 508 #define ASSERT_NEAR(val1, val2, abs_error) \ 512 if (::nlib_ns::testing::AssertionResult nlib_ar_tmp_ = \ 513 ::nlib_ns::testing::NearDouble(val1, val2, abs_error)) \ 516 NLIB_TESTING_FATAL_ = ::nlib_ns::testing::NearFailMessage( \ 517 &nlib_ar_tmp_, __FILE__, __LINE__, #val1, #val2, #abs_error, val1, val2, abs_error) 519 #define EXPECT_NEAR(val1, val2, abs_error) \ 523 if (::nlib_ns::testing::AssertionResult nlib_ar_tmp_ = \ 524 ::nlib_ns::testing::NearDouble(val1, val2, abs_error)) \ 527 NLIB_TESTING_NONFATAL_ = ::nlib_ns::testing::NearFailMessage( \ 528 &nlib_ar_tmp_ , __FILE__, __LINE__, #val1, #val2, #abs_error, val1, val2, abs_error) 530 #define SCOPED_TRACE(msg) \ 531 ::nlib_ns::testing::ScopedTrace NLIB_TESTING_CONCAT_TOKEN_(nlib_scopedtrace_, __LINE__)( \ 532 ::nlib_ns::testing::AssertionResult(true) << (msg), __FILE__, __LINE__) 534 #define ASSERT_NO_FATAL_FAILURE(statement) \ 542 if (!::nlib_ns::testing::UnitTest::GetInstance() \ 543 ->current_test_info() \ 545 ->HasFatalFaiulre()) \ 548 NLIB_TESTING_FATAL_ = ::nlib_ns::testing::AssertionResult(false) \ 549 << "Expected: " #statement \ 550 " doesn't generate new fatal failures.\n" \ 551 " Actual: it does.\n" 553 #define EXPECT_NO_FATAL_FAILURE(statement) \ 561 if (!::nlib_ns::testing::UnitTest::GetInstance() \ 562 ->current_test_info() \ 564 ->HasFatalFaiulre()) \ 567 NLIB_TESTING_NONFATAL_ = ::nlib_ns::testing::AssertionResult(false) \ 568 << "Expected: " #statement \ 569 " doesn't generate new fatal failures.\n" \ 570 " Actual: it does.\n" 572 #define GTEST_FLAG(name) NLIB_FLAGS_gtest_##name 577 #if defined(_MSC_VER) && defined(nx_testing_EXPORTS) 578 #undef NLIB_VIS_PUBLIC 579 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 582 #endif // INCLUDE_NN_NLIB_TESTING_NTEST_H_ #define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
Controls display when tests fails.
AssertionResult FloatLE(const char *expr1, const char *expr2, float val1, float val2)
Determines if val1 <= val2. [ASSERT|EXPECT] Used by including in PRED_FORMAT2.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
virtual void SetUp()
Writes the process to execute only once before all tests.
Defines the class for handling command line strings.
Singleton of the type for implicitly constructing an instance based on a static variable before calli...
#define NLIB_FLAGS_DECLARE_bool(opt_name)
Enables the use of an NLIB_FLAGS_option name that was defined in a different place.
An empty structure indicating that an argument to a function needs to be moved.
void InitNintendoTest(int *argc, wchar_t **argv)
Processes command-line options for the test program.
Environment * AddGlobalTestEnvironment(Environment *env)
Passes the pointer to Environment and registers the code to run before and after all tests...
A file that contains the configuration information for each development environment.
virtual void TearDown()
Writes the process to execute only once after all tests.
#define NLIB_STATIC_ASSERT(exp)
Defines a static assertion. Uses static_assert if it is available for use.
#define NLIB_FLAGS_DECLARE_string(opt_name)
Enables the use of an NLIB_FLAGS_option name that was defined in a different place.
Defines the SetUp and TearDown functions that inherit this class and are run globally.
AssertionResult DoubleLE(const char *expr1, const char *expr2, double val1, double val2)
Determines if val1 <= val2. [ASSERT|EXPECT] Used by including in PRED_FORMAT2.
#define NLIB_FLAGS_DECLARE_int32(opt_name)
Enables the use of an NLIB_FLAGS_option name that was defined in a different place.