nlib
testing/success/success.cpp
Defines the IsEvenTest and MyString test group. This code writes a very simple test, and a test that uses a test fixture.
All tests succeed.
/*---------------------------------------------------------------------------*
Project: CrossRoad
Copyright (C)2012-2016 Nintendo. All rights reserved.
These coded instructions, statements, and computer programs contain
proprietary information of Nintendo of America Inc. and/or Nintendo
Company Ltd., and are protected by Federal copyright law. They may
not be disclosed to third parties or copied or duplicated in any form,
in whole or in part, without the prior written consent of Nintendo.
*---------------------------------------------------------------------------*/
#include <string.h>
// define NLIB_USE_GTEST to use googletest
// #define NLIB_USE_GTEST
static bool IsEven(int value) { return (value % 2) == 0; }
TEST(IsEvenTest, Positive) {
// expects IsEven() return true
EXPECT_TRUE(IsEven(2));
EXPECT_TRUE(IsEven(1 << 20));
#ifdef INT_MIN
EXPECT_TRUE(IsEven(INT_MIN));
#endif
}
TEST(IsEvenTest, Negative) {
// expects IsEven() return false
EXPECT_FALSE(IsEven(3));
EXPECT_FALSE(IsEven(1 + (1 << 20)));
#ifdef INT_MAX
EXPECT_FALSE(IsEven(INT_MAX));
#endif
}
TEST(IsEvenTest, UsePred) {
// you can pass the arguments of the function to test indirectly by EXPECT_PREDn().
// the richer message will be generated in case of failure.
// the semantics is same to EXPECT_TRUE(IsEven(2)).
EXPECT_PRED1(IsEven, 2);
}
TEST(IsEvenTest, Loop) {
// you can construct messages for failure to concatenate opeartor<<()s.
for (int i = 0; i < 100; i += 2) {
// it is useful to print the index in loops
EXPECT_TRUE(IsEven(i)) << i << " must be even.";
}
for (int i = 1; i < 100; i += 2) {
EXPECT_FALSE(IsEven(i)) << i << " must not be even.";
}
}
TEST(IsEvenTest, DISABLED_NeverSucceed) {
// you can disable tests to add 'DISABLED_' as prefix.
// this is mainly for disabling tests temporarily.
// this is also useful when you don't want to make the test codes run,
// but you want to compile the test codes.
EXPECT_TRUE(IsEven(1));
}
static void ToUpper(char* text) {
char* p = text;
while (*p) {
if (*p >= 'A' && *p <= 'Z') *p += 'a' - 'A';
++p;
}
}
static void ToLower(char* text) {
char* p = text;
while (*p) {
if (*p >= 'a' && *p <= 'z') *p += 'A' - 'a';
++p;
}
}
static void Encrypt(char* text) {
char* p = text;
while (*p) {
++(*p);
++p;
}
}
static void Decrypt(char* text) {
char* p = text;
while (*p) {
--(*p);
++p;
}
}
class MyString : public ::testing::Test {
// defining a test fixture
// you have to define a class inheriting ::testing::Test.
protected:
static void SetUpTestCase() {
// SetUpTestCase() runs only once before all 'MyString' test cases run.
char* p = new char[32];
// 'MyString' test cases will not run if SetUpTestCase() fails.
if (!p) FAIL();
nlib_strcpy(p, 32, "This is a pen.");
original_text = p;
}
static void TearDownTestCase() {
// TearDownTestCase() runs only once after all 'MyString' test cases run.
delete[] original_text;
}
virtual void SetUp() NLIB_OVERRIDE {
// SetUp() runs before each 'MyString' test case.
size_t n = strlen(original_text) + 1;
text = new char[n];
// the test case will not run if SetUp() fails
if (!text) FAIL();
nlib_strcpy(text, n, original_text);
}
virtual void TearDown() NLIB_OVERRIDE {
// TearDown() runs after each 'MyString' test case.
delete[] text;
}
// each test case can use the member functions (they must be protected or public)
void DoToUpper() { ToLower(text); }
void DoToLower() { ToUpper(text); }
void EncryptDecryptTest() {
ASSERT_STREQ("This is a pen.", text);
Encrypt(text);
Decrypt(text);
ASSERT_STREQ("This is a pen.", text);
}
// each test case can access the data members (they must be protected or public)
static const char* original_text;
char* text;
};
const char* MyString::original_text;
TEST_F(MyString, ToUpperTest) {
ASSERT_STREQ("This is a pen.", text);
DoToUpper();
ASSERT_STREQ("THIS IS A PEN.", text);
}
TEST_F(MyString, ToLowerTest) {
ASSERT_STREQ("This is a pen.", text);
DoToLower();
ASSERT_STREQ("this is a pen.", text);
}
TEST_F(MyString, EncTest) {
// the caller function will not return immedidately if some tests in subroutines fail.
// you can use ASSERT_NO_FATAL_FAILURE or EXPECT_NO_FATAL_FAILURE
ASSERT_NO_FATAL_FAILURE({ EncryptDecryptTest(); });
}
NLIB_PATHMAPPER_FORSAMPLE
bool SampleMain(int argc, char** argv) {
InitPathMapperForSample();
char path[512];
char buf[512];
size_t count;
g_PathMapper.ResolvePath(&count, path, "nlibpath:///readwrite/success.xml");
nlib_snprintf(&count, buf, "xml:%s", path);
::testing::GTEST_FLAG(output) = buf;
// RUN_ALL_TESTS returns 0 if all the tests are successful.
// You can use the return value of main() function.
return RUN_ALL_TESTS() == 0;
}
NLIB_MAINFUNC