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_(NULL) {}
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_), builder_() {
239 builder_.swap(const_cast<AssertionResult&>(rhs).builder_);
241 operator bool()
const {
return is_success_; }
242 AssertionResult operator!()
const {
return AssertionResult(!is_success_); }
243 const char* c_str()
const {
return builder_.c_str(); }
246 AssertionResult& operator<<(
const T& rhs) {
250 AssertionResult& operator<<(AssertionResult& rhs) {
251 is_success_ = rhs.is_success_;
252 builder_.swap(rhs.builder_);
258 detail_func::DummyStream builder_;
259 AssertionResult& operator=(
const AssertionResult&);
265 ScopedTrace(AssertionResult& msg,
const char* file,
int line);
274 explicit FollowMessageTerminator(
bool flag);
280 void operator=(
const AssertionResult& msg)
const;
286 inline bool suppress_warning_(
bool b) {
return b; }
287 NLIB_VIS_PUBLIC AssertionResult BooleanFailMessage(
const char* file,
int line,
const char* expr,
288 const char* actual,
const char* expected);
289 #define NLIB_TESTING_BOOLEAN_IMPL_(expr, expected, actual, iffail) \ 293 if (::nlib_ns::testing::suppress_warning_(expr)) \ 297 ::nlib_ns::testing::BooleanFailMessage(__FILE__, __LINE__, #expr, #actual, #expected) 299 NLIB_VIS_PUBLIC AssertionResult CompFailMessage(
const char* file,
int line,
const char* expr1,
300 const char* expr2,
const char* val1,
301 const char* val2,
const char* op);
302 #define NLIB_TESTING_COMP_IMPL_(func_name, op) \ 303 template<class T1, class T2> \ 304 AssertionResult Comp##func_name(const char* expr1, const char* expr2, const T1& val1, \ 305 const T2& val2, const char* file, int line) { \ 306 if (val1 op val2) { \ 307 return AssertionResult(true); \ 309 detail_func::DummyStream val1str, val2str; \ 312 return CompFailMessage(file, line, expr1, expr2, val1str.c_str(), \ 313 val2str.c_str(), #op); \ 316 NLIB_VIS_PUBLIC AssertionResult Comp##func_name(const char* expr1, const char* expr2, \ 317 int64_t val1, int64_t val2, \ 318 const char* file, int line) 320 NLIB_TESTING_COMP_IMPL_(EQ, ==);
321 NLIB_TESTING_COMP_IMPL_(NE, !=);
322 NLIB_TESTING_COMP_IMPL_(LE, <=);
323 NLIB_TESTING_COMP_IMPL_(LT, <);
324 NLIB_TESTING_COMP_IMPL_(GE, >=);
325 NLIB_TESTING_COMP_IMPL_(GT, >);
327 #undef NLIB_TESTING_COMP_IMPL_ 329 NLIB_VIS_PUBLIC AssertionResult CompStrEq(
const char* expr1,
const char* expr2,
const char* val1,
330 const char* val2,
const char* file,
int line);
331 NLIB_VIS_PUBLIC AssertionResult CompStrNe(
const char* expr1,
const char* expr2,
const char* val1,
332 const char* val2,
const char* file,
int line);
334 NLIB_VIS_PUBLIC AssertionResult CompStrEq(
const char* expr1,
const char* expr2,
const wchar_t* val1,
335 const wchar_t* val2,
const char* file,
int line);
336 NLIB_VIS_PUBLIC AssertionResult CompStrNe(
const char* expr1,
const char* expr2,
const wchar_t* val1,
337 const wchar_t* val2,
const char* file,
int line);
339 NLIB_VIS_PUBLIC AssertionResult CompStrCaseEq(
const char* expr1,
const char* expr2,
340 const char* val1,
const char* val2,
341 const char* file,
int line);
342 NLIB_VIS_PUBLIC AssertionResult CompStrCaseNe(
const char* expr1,
const char* expr2,
343 const char* val1,
const char* val2,
344 const char* file,
int line);
346 NLIB_VIS_PUBLIC AssertionResult CompStrCaseEq(
const char* expr1,
const char* expr2,
347 const wchar_t* val1,
const wchar_t* val2,
348 const char* file,
int line);
349 NLIB_VIS_PUBLIC AssertionResult CompStrCaseNe(
const char* expr1,
const char* expr2,
350 const wchar_t* val1,
const wchar_t* val2,
351 const char* file,
int line);
353 NLIB_VIS_PUBLIC AssertionResult CompFloatEq(
const char* expr1,
const char* expr2,
float val1,
354 float val2,
const char* file,
int line);
355 NLIB_VIS_PUBLIC AssertionResult CompDoubleEq(
const char* expr1,
const char* expr2,
double val1,
356 double val2,
const char* file,
int line);
357 NLIB_VIS_PUBLIC AssertionResult NearDouble(
double val1,
double val2,
double abs_error);
361 #define NLIB_TESTING_CONCAT_TOKEN_(a, b) NLIB_TESTING_CONCAT_TOKEN_IMPL_(a, b) 362 #define NLIB_TESTING_CONCAT_TOKEN_IMPL_(a, b) a##b 364 #define NLIB_TESTING_FATAL_ return ::nlib_ns::testing::FollowMessageTerminator(true) 365 #define NLIB_TESTING_NONFATAL_ ::nlib_ns::testing::FollowMessageTerminator(false) 367 #define NLIB_TESTING_ASSERT_HELPER_(func, val1, val2) \ 371 if (::nlib_ns::testing::AssertionResult ar = \ 372 func(#val1, #val2, val1, val2, __FILE__, __LINE__)) \ 375 NLIB_TESTING_FATAL_ = ar 377 #define NLIB_TESTING_EXPECT_HELPER_(func, val1, val2) \ 381 if (::nlib_ns::testing::AssertionResult ar = \ 382 func(#val1, #val2, val1, val2, __FILE__, __LINE__)) \ 385 NLIB_TESTING_NONFATAL_ = ar 387 #define NLIB_TESTING_CLASS_NAME_(test_case, test_name) test_case##_##test_name##_Test 389 #define NLIB_TEST_(test_case, test_name, parent) \ 390 class NLIB_TESTING_CLASS_NAME_(test_case, test_name) \ 393 NLIB_TESTING_CLASS_NAME_(test_case, test_name)() {} \ 395 virtual void TestBody(); \ 396 static ::nlib_ns::testing::TestInfo* info_; \ 397 NLIB_DISALLOW_COPY_AND_ASSIGN(NLIB_TESTING_CLASS_NAME_(test_case, test_name)); \ 399 ::nlib_ns::testing::TestInfo* NLIB_TESTING_CLASS_NAME_(test_case, test_name)::info_ = \ 400 ::nlib_ns::testing::TestInfo::AddTestInfo< \ 401 NLIB_TESTING_CLASS_NAME_(test_case, test_name)>( \ 402 #test_case, #test_name, -1, parent::SetUpTestCase, parent::TearDownTestCase); \ 403 void NLIB_TESTING_CLASS_NAME_(test_case, test_name)::TestBody() 405 NLIB_VIS_PUBLIC AssertionResult& NearFailMessage(AssertionResult* ar,
const char* file,
int line,
406 const char* expr1,
const char* expr2,
407 const char* abs_error_expr,
double val1,
408 double val2,
double abs_error);
411 FloatLE(
const char* expr1,
const char* expr2,
float val1,
float val2);
413 DoubleLE(
const char* expr1,
const char* expr2,
double val1,
double val2);
418 #define NLIB_TESTING_FLAG(flag_name) NLIB_FLAGS_##flag_name 435 #define TEST(test_case_name, specific_test_name) \ 436 NLIB_TEST_(test_case_name, specific_test_name, ::nlib_ns::testing::Test) 437 #define TEST_F(test_fixture_name, specific_test_name) \ 438 NLIB_TEST_(test_fixture_name, specific_test_name, test_fixture_name) 440 #define FRIEND_TEST(test_case, test_name) \ 441 friend class NLIB_TESTING_CLASS_NAME_(test_case, test_name) 443 #define RUN_ALL_TESTS() ::nlib_ns::testing::TestInfo::Run() 446 return ::nlib_ns::testing::FollowMessageTerminator(true) = \ 447 ::nlib_ns::testing::AssertionResult(false) 450 #define ASSERT_TRUE(expr) NLIB_TESTING_BOOLEAN_IMPL_(expr, true, false, NLIB_TESTING_FATAL_) 451 #define ASSERT_FALSE(expr) NLIB_TESTING_BOOLEAN_IMPL_(!(expr), false, true, NLIB_TESTING_FATAL_) 452 #define EXPECT_TRUE(expr) NLIB_TESTING_BOOLEAN_IMPL_(expr, true, false, NLIB_TESTING_NONFATAL_) 453 #define EXPECT_FALSE(expr) NLIB_TESTING_BOOLEAN_IMPL_(!(expr), false, true, NLIB_TESTING_NONFATAL_) 455 #define ASSERT_EQ(expected, actual) \ 456 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompEQ, expected, actual) 457 #define ASSERT_NE(expected, actual) \ 458 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompNE, expected, actual) 459 #define ASSERT_LE(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompLE, val1, val2) 460 #define ASSERT_LT(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompLT, val1, val2) 461 #define ASSERT_GE(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompGE, val1, val2) 462 #define ASSERT_GT(val1, val2) NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompGT, val1, val2) 464 #define EXPECT_EQ(expected, actual) \ 465 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompEQ, expected, actual) 466 #define EXPECT_NE(expected, actual) \ 467 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompNE, expected, actual) 468 #define EXPECT_LE(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompLE, val1, val2) 469 #define EXPECT_LT(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompLT, val1, val2) 470 #define EXPECT_GE(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompGE, val1, val2) 471 #define EXPECT_GT(val1, val2) NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompGT, val1, val2) 473 #define ASSERT_STREQ(expected, actual) \ 474 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrEq, expected, actual) 475 #define ASSERT_STRNE(expected, actual) \ 476 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrNe, expected, actual) 478 #define EXPECT_STREQ(expected, actual) \ 479 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrEq, expected, actual) 480 #define EXPECT_STRNE(expected, actual) \ 481 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrNe, expected, actual) 483 #define ASSERT_STRCASEEQ(expected, actual) \ 484 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrCaseEq, expected, actual) 485 #define ASSERT_STRCASENE(expected, actual) \ 486 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompStrCaseNe, expected, actual) 488 #define EXPECT_STRCASEEQ(expected, actual) \ 489 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrCaseEq, expected, actual) 490 #define EXPECT_STRCASENE(expected, actual) \ 491 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompStrCaseNe, expected, actual) 497 #define ASSERT_FLOAT_EQ(expected, actual) \ 498 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompFloatEq, expected, actual) 499 #define ASSERT_DOUBLE_EQ(expected, actual) \ 500 NLIB_TESTING_ASSERT_HELPER_(::nlib_ns::testing::CompDoubleEq, expected, actual) 502 #define EXPECT_FLOAT_EQ(expected, actual) \ 503 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompFloatEq, expected, actual) 504 #define EXPECT_DOUBLE_EQ(expected, actual) \ 505 NLIB_TESTING_EXPECT_HELPER_(::nlib_ns::testing::CompDoubleEq, expected, actual) 507 #define ASSERT_NEAR(val1, val2, abs_error) \ 511 if (::nlib_ns::testing::AssertionResult ar = \ 512 ::nlib_ns::testing::NearDouble(val1, val2, abs_error)) \ 515 NLIB_TESTING_FATAL_ = ::nlib_ns::testing::NearFailMessage( \ 516 &ar, __FILE__, __LINE__, #val1, #val2, #abs_error, val1, val2, abs_error) 518 #define EXPECT_NEAR(val1, val2, abs_error) \ 522 if (::nlib_ns::testing::AssertionResult ar = \ 523 ::nlib_ns::testing::NearDouble(val1, val2, abs_error)) \ 526 NLIB_TESTING_NONFATAL_ = ::nlib_ns::testing::NearFailMessage( \ 527 &ar, __FILE__, __LINE__, #val1, #val2, #abs_error, val1, val2, abs_error) 529 #define SCOPED_TRACE(msg) \ 530 ::nlib_ns::testing::ScopedTrace NLIB_TESTING_CONCAT_TOKEN_(nlib_scopedtrace_, __LINE__)( \ 531 ::nlib_ns::testing::AssertionResult(true) << (msg), __FILE__, __LINE__) 533 #define ASSERT_NO_FATAL_FAILURE(statement) \ 541 if (!::nlib_ns::testing::UnitTest::GetInstance() \ 542 ->current_test_info() \ 544 ->HasFatalFaiulre()) \ 547 NLIB_TESTING_FATAL_ = ::nlib_ns::testing::AssertionResult(false) \ 548 << "Expected: " #statement \ 549 " doesn't generate new fatal failures.\n" \ 550 " Actual: it does.\n" 552 #define EXPECT_NO_FATAL_FAILURE(statement) \ 560 if (!::nlib_ns::testing::UnitTest::GetInstance() \ 561 ->current_test_info() \ 563 ->HasFatalFaiulre()) \ 566 NLIB_TESTING_NONFATAL_ = ::nlib_ns::testing::AssertionResult(false) \ 567 << "Expected: " #statement \ 568 " doesn't generate new fatal failures.\n" \ 569 " Actual: it does.\n" 571 #define GTEST_FLAG(name) NLIB_FLAGS_gtest_##name 576 #if defined(_MSC_VER) && defined(nx_testing_EXPORTS) 577 #undef NLIB_VIS_PUBLIC 578 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 581 #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.
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.