diff --git a/README.md b/README.md index ca471f4..6d7a200 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,19 @@ The current supported compilers are gcc, clang and msvc. -The current tested compiler versions are gcc 4.8.2, clang 3.5 and MSVC 18.0.21005.1. +The current supported platforms are Linux, Mac OSX and Windows. + +## Command Line Options ## + +utest.h supports some command line options: + +* --help to output the help message +* --filter= will filter the test cases to run (useful for re-running one particular offending test case). +* --output= will output an xunit XML file with the test results (that Jenkins, travis-ci, and appveyor can parse for the test results). ## Design ## -UTest is a single header library to enable all the fun of unit testing in C and C++. The library has been designed to provide an output similar to Google's googletest framework; +UTest is a single header library to enable all the fun of unit testing in C and C++. The library has been designed to provide an output similar to Google's googletest framework: [==========] Running 1 test cases. [ RUN ] foo.bar @@ -24,25 +32,125 @@ [==========] 1 test cases ran. [ PASSED ] 1 tests. +## UTEST_MAIN ## + +In one C or C++ file, you must call the macro UTEST_MAIN: + + UTEST_MAIN(); + +This will call into utest.h, instantiate all the testcases and run the unit test framework. + +Alternatively, if you want to write your own main and call into utest.h, you can instead, in one C or C++ file call: + + UTEST_STATE(); + +And then when you are ready to call into the utest.h framework do: + + int main(int argc, const char *const argv[]) { + // do your own thing + return utest_main(argc, argv); + } + +## Define a Testcase ## + To define a test case to run, you can do the following; #include "utest.h" - TESTCASE(foo, bar) { + UTEST(foo, bar) { ASSERT_TRUE(1); } -The TESTCASE macro takes two parameters - the first being the set that the test case belongs to, the second being the name of the test. This allows tests to be grouped for conveience. +The UTEST macro takes two parameters - the first being the set that the test case belongs to, the second being the name of the test. This allows tests to be grouped for conveience. + +## Define a Fixtured Testcase ## + +A fixtured testcase is one in which there is a struct that is instantiated that can be shared across multiple testcases. + + struct MyTestFixture { + char c; + int i; + float f; + }; + + UTEST_F_SETUP(MyTestFixture) { + utest_fixture->c = 'a'; + utest_fixture->i = 42; + utest_fixture->f = 3.14f; + + // we can even assert and expect in setup! + ASSERT_EQ(42, utest_fixture->i); + EXPECT_TRUE(true); + } + + UTEST_F_TEARDOWN(MyTestFixture) { + // and also assert and expect in teardown! + ASSERT_EQ(13, utest_fixture->i); + } + + UTEST_F(MyTestFixture, a) { + utest_fixture->i = 13; + // teardown will succeed because i is 13... + } + + UTEST_F(MyTestFixture, b) { + utest_fixture->i = 83; + // teardown will fail because i is not 13! + } + +Some things to note that were demonstrated above: +* We have this new implicit variable within our macros - utest_fixture. This is a pointer to the struct you decidedw as your fixture (so MyTestFixture in the above code). +* Instead of specifying a testcase set (like we do with the UTEST macro), we instead specify the name of the fixture struct we are use. +* Every fixture has to have a UTEST_F_SETUP and UTEST_F_TEARDOWN macro - even if they do nothing in the body of them. +* Multiple testcases (UTEST_F's) can use the same fixture. +* You can use EXPECT_* and ASSERT_* macros within the body of both the fixture's setup and teardown macros. + +## Define an Indexed Testcase ## + +Sometimes you want to use the same fixture _and_ testcase repeatedly, but prehaps subtly change one variable within. This is where indexed testcases come in. + + struct MyTestIndexedFixture{ + bool x; + bool y; + }; + + UTEST_I_SETUP(MyTestIndexedFixture) { + if (utest_index < 30) { + utest_fixture->x = utest_index & 1; + utest_fixture->y = (utest_index + 1) & 1; + } + } + + UTEST_I_TEARDOWN(MyTestIndexedFixture) { + EXPECT_LE(0, utest_index); + } + + UTEST_I(MyTestIndexedFixture, a, 2) { + ASSERT_TRUE(utest_fixture->x | utest_fixture->y); + } + + UTEST_I(MyTestIndexedFixture, b, 42) { + // this will fail when the index is >= 30 + ASSERT_TRUE(utest_fixture->x | utest_fixture->y); + } + +Note: +* We use UTEST_I_* as the prefix for the setup and teardown functions now. +* We use UTEST_I to declare the testcases. +* We have access to a new variable utest_index in our setup and teardown functions, that we can use to slightly vary our fixture. +* We provide a number as the third parameter of the UTEST_I macro - this is the number of times we should run the test case for that index. It must be a literal. + +## Testing Macros ## Matching what googletest has, we provide two variants of each of the error checking conditions - ASSERT's and EXPECT's. If an ASSERT fails, the test case will cease execution, and utest.h will continue with the next test case to be run. If an EXPECT fails, the remainder of the test case will still be executed, allowing for further checks to be carried out. -We currently provide the following macros to be used within TESTCASE's. +We currently provide the following macros to be used within UTEST's. ### ASSERT_TRUE(x) ### Asserts that x evaluates to true (EG. non-zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 1; ASSERT_TRUE(i); // pass! ASSERT_TRUE(42); // pass! @@ -53,7 +161,7 @@ Asserts that x evaluates to false (EG. zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 0; ASSERT_FALSE(i); // pass! ASSERT_FALSE(1); // fail! @@ -63,7 +171,7 @@ Asserts that x and y are equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 42; ASSERT_EQ(a, b); // pass! @@ -77,7 +185,7 @@ Asserts that x and y are not equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_NE(a, b); // pass! @@ -91,7 +199,7 @@ Asserts that x is less than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; ASSERT_LT(a, b); // pass! @@ -105,7 +213,7 @@ Asserts that x is less than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; ASSERT_LE(a, b); // pass! @@ -122,7 +230,7 @@ Asserts that x is greater than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_GT(a, b); // pass! @@ -136,7 +244,7 @@ Asserts that x is greater than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_GE(a, b); // pass! @@ -153,7 +261,7 @@ Expects that x evaluates to true (EG. non-zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 1; EXPECT_TRUE(i); // pass! EXPECT_TRUE(42); // pass! @@ -164,7 +272,7 @@ Expects that x evaluates to false (EG. zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 0; EXPECT_FALSE(i); // pass! EXPECT_FALSE(1); // fail! @@ -174,7 +282,7 @@ Expects that x and y are equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 42; EXPECT_EQ(a, b); // pass! @@ -188,7 +296,7 @@ Expects that x and y are not equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_NE(a, b); // pass! @@ -202,7 +310,7 @@ Expects that x is less than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; EXPECT_LT(a, b); // pass! @@ -216,7 +324,7 @@ Expects that x is less than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; EXPECT_LE(a, b); // pass! @@ -233,7 +341,7 @@ Expects that x is greater than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_GT(a, b); // pass! @@ -247,7 +355,7 @@ Expects that x is greater than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_GE(a, b); // pass! diff --git a/README.md b/README.md index ca471f4..6d7a200 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,19 @@ The current supported compilers are gcc, clang and msvc. -The current tested compiler versions are gcc 4.8.2, clang 3.5 and MSVC 18.0.21005.1. +The current supported platforms are Linux, Mac OSX and Windows. + +## Command Line Options ## + +utest.h supports some command line options: + +* --help to output the help message +* --filter= will filter the test cases to run (useful for re-running one particular offending test case). +* --output= will output an xunit XML file with the test results (that Jenkins, travis-ci, and appveyor can parse for the test results). ## Design ## -UTest is a single header library to enable all the fun of unit testing in C and C++. The library has been designed to provide an output similar to Google's googletest framework; +UTest is a single header library to enable all the fun of unit testing in C and C++. The library has been designed to provide an output similar to Google's googletest framework: [==========] Running 1 test cases. [ RUN ] foo.bar @@ -24,25 +32,125 @@ [==========] 1 test cases ran. [ PASSED ] 1 tests. +## UTEST_MAIN ## + +In one C or C++ file, you must call the macro UTEST_MAIN: + + UTEST_MAIN(); + +This will call into utest.h, instantiate all the testcases and run the unit test framework. + +Alternatively, if you want to write your own main and call into utest.h, you can instead, in one C or C++ file call: + + UTEST_STATE(); + +And then when you are ready to call into the utest.h framework do: + + int main(int argc, const char *const argv[]) { + // do your own thing + return utest_main(argc, argv); + } + +## Define a Testcase ## + To define a test case to run, you can do the following; #include "utest.h" - TESTCASE(foo, bar) { + UTEST(foo, bar) { ASSERT_TRUE(1); } -The TESTCASE macro takes two parameters - the first being the set that the test case belongs to, the second being the name of the test. This allows tests to be grouped for conveience. +The UTEST macro takes two parameters - the first being the set that the test case belongs to, the second being the name of the test. This allows tests to be grouped for conveience. + +## Define a Fixtured Testcase ## + +A fixtured testcase is one in which there is a struct that is instantiated that can be shared across multiple testcases. + + struct MyTestFixture { + char c; + int i; + float f; + }; + + UTEST_F_SETUP(MyTestFixture) { + utest_fixture->c = 'a'; + utest_fixture->i = 42; + utest_fixture->f = 3.14f; + + // we can even assert and expect in setup! + ASSERT_EQ(42, utest_fixture->i); + EXPECT_TRUE(true); + } + + UTEST_F_TEARDOWN(MyTestFixture) { + // and also assert and expect in teardown! + ASSERT_EQ(13, utest_fixture->i); + } + + UTEST_F(MyTestFixture, a) { + utest_fixture->i = 13; + // teardown will succeed because i is 13... + } + + UTEST_F(MyTestFixture, b) { + utest_fixture->i = 83; + // teardown will fail because i is not 13! + } + +Some things to note that were demonstrated above: +* We have this new implicit variable within our macros - utest_fixture. This is a pointer to the struct you decidedw as your fixture (so MyTestFixture in the above code). +* Instead of specifying a testcase set (like we do with the UTEST macro), we instead specify the name of the fixture struct we are use. +* Every fixture has to have a UTEST_F_SETUP and UTEST_F_TEARDOWN macro - even if they do nothing in the body of them. +* Multiple testcases (UTEST_F's) can use the same fixture. +* You can use EXPECT_* and ASSERT_* macros within the body of both the fixture's setup and teardown macros. + +## Define an Indexed Testcase ## + +Sometimes you want to use the same fixture _and_ testcase repeatedly, but prehaps subtly change one variable within. This is where indexed testcases come in. + + struct MyTestIndexedFixture{ + bool x; + bool y; + }; + + UTEST_I_SETUP(MyTestIndexedFixture) { + if (utest_index < 30) { + utest_fixture->x = utest_index & 1; + utest_fixture->y = (utest_index + 1) & 1; + } + } + + UTEST_I_TEARDOWN(MyTestIndexedFixture) { + EXPECT_LE(0, utest_index); + } + + UTEST_I(MyTestIndexedFixture, a, 2) { + ASSERT_TRUE(utest_fixture->x | utest_fixture->y); + } + + UTEST_I(MyTestIndexedFixture, b, 42) { + // this will fail when the index is >= 30 + ASSERT_TRUE(utest_fixture->x | utest_fixture->y); + } + +Note: +* We use UTEST_I_* as the prefix for the setup and teardown functions now. +* We use UTEST_I to declare the testcases. +* We have access to a new variable utest_index in our setup and teardown functions, that we can use to slightly vary our fixture. +* We provide a number as the third parameter of the UTEST_I macro - this is the number of times we should run the test case for that index. It must be a literal. + +## Testing Macros ## Matching what googletest has, we provide two variants of each of the error checking conditions - ASSERT's and EXPECT's. If an ASSERT fails, the test case will cease execution, and utest.h will continue with the next test case to be run. If an EXPECT fails, the remainder of the test case will still be executed, allowing for further checks to be carried out. -We currently provide the following macros to be used within TESTCASE's. +We currently provide the following macros to be used within UTEST's. ### ASSERT_TRUE(x) ### Asserts that x evaluates to true (EG. non-zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 1; ASSERT_TRUE(i); // pass! ASSERT_TRUE(42); // pass! @@ -53,7 +161,7 @@ Asserts that x evaluates to false (EG. zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 0; ASSERT_FALSE(i); // pass! ASSERT_FALSE(1); // fail! @@ -63,7 +171,7 @@ Asserts that x and y are equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 42; ASSERT_EQ(a, b); // pass! @@ -77,7 +185,7 @@ Asserts that x and y are not equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_NE(a, b); // pass! @@ -91,7 +199,7 @@ Asserts that x is less than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; ASSERT_LT(a, b); // pass! @@ -105,7 +213,7 @@ Asserts that x is less than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; ASSERT_LE(a, b); // pass! @@ -122,7 +230,7 @@ Asserts that x is greater than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_GT(a, b); // pass! @@ -136,7 +244,7 @@ Asserts that x is greater than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_GE(a, b); // pass! @@ -153,7 +261,7 @@ Expects that x evaluates to true (EG. non-zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 1; EXPECT_TRUE(i); // pass! EXPECT_TRUE(42); // pass! @@ -164,7 +272,7 @@ Expects that x evaluates to false (EG. zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 0; EXPECT_FALSE(i); // pass! EXPECT_FALSE(1); // fail! @@ -174,7 +282,7 @@ Expects that x and y are equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 42; EXPECT_EQ(a, b); // pass! @@ -188,7 +296,7 @@ Expects that x and y are not equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_NE(a, b); // pass! @@ -202,7 +310,7 @@ Expects that x is less than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; EXPECT_LT(a, b); // pass! @@ -216,7 +324,7 @@ Expects that x is less than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; EXPECT_LE(a, b); // pass! @@ -233,7 +341,7 @@ Expects that x is greater than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_GT(a, b); // pass! @@ -247,7 +355,7 @@ Expects that x is greater than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_GE(a, b); // pass! diff --git a/appveyor.yml b/appveyor.yml index d833fc9..87d140e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -6,8 +6,10 @@ environment: matrix: - - VSVERSION: Visual Studio 11 - - VSVERSION: Visual Studio 12 + - VSVERSION: Visual Studio 10 2010 + - VSVERSION: Visual Studio 11 2012 + - VSVERSION: Visual Studio 12 2013 + - VSVERSION: Visual Studio 14 2015 platform: - Win32 diff --git a/README.md b/README.md index ca471f4..6d7a200 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,19 @@ The current supported compilers are gcc, clang and msvc. -The current tested compiler versions are gcc 4.8.2, clang 3.5 and MSVC 18.0.21005.1. +The current supported platforms are Linux, Mac OSX and Windows. + +## Command Line Options ## + +utest.h supports some command line options: + +* --help to output the help message +* --filter= will filter the test cases to run (useful for re-running one particular offending test case). +* --output= will output an xunit XML file with the test results (that Jenkins, travis-ci, and appveyor can parse for the test results). ## Design ## -UTest is a single header library to enable all the fun of unit testing in C and C++. The library has been designed to provide an output similar to Google's googletest framework; +UTest is a single header library to enable all the fun of unit testing in C and C++. The library has been designed to provide an output similar to Google's googletest framework: [==========] Running 1 test cases. [ RUN ] foo.bar @@ -24,25 +32,125 @@ [==========] 1 test cases ran. [ PASSED ] 1 tests. +## UTEST_MAIN ## + +In one C or C++ file, you must call the macro UTEST_MAIN: + + UTEST_MAIN(); + +This will call into utest.h, instantiate all the testcases and run the unit test framework. + +Alternatively, if you want to write your own main and call into utest.h, you can instead, in one C or C++ file call: + + UTEST_STATE(); + +And then when you are ready to call into the utest.h framework do: + + int main(int argc, const char *const argv[]) { + // do your own thing + return utest_main(argc, argv); + } + +## Define a Testcase ## + To define a test case to run, you can do the following; #include "utest.h" - TESTCASE(foo, bar) { + UTEST(foo, bar) { ASSERT_TRUE(1); } -The TESTCASE macro takes two parameters - the first being the set that the test case belongs to, the second being the name of the test. This allows tests to be grouped for conveience. +The UTEST macro takes two parameters - the first being the set that the test case belongs to, the second being the name of the test. This allows tests to be grouped for conveience. + +## Define a Fixtured Testcase ## + +A fixtured testcase is one in which there is a struct that is instantiated that can be shared across multiple testcases. + + struct MyTestFixture { + char c; + int i; + float f; + }; + + UTEST_F_SETUP(MyTestFixture) { + utest_fixture->c = 'a'; + utest_fixture->i = 42; + utest_fixture->f = 3.14f; + + // we can even assert and expect in setup! + ASSERT_EQ(42, utest_fixture->i); + EXPECT_TRUE(true); + } + + UTEST_F_TEARDOWN(MyTestFixture) { + // and also assert and expect in teardown! + ASSERT_EQ(13, utest_fixture->i); + } + + UTEST_F(MyTestFixture, a) { + utest_fixture->i = 13; + // teardown will succeed because i is 13... + } + + UTEST_F(MyTestFixture, b) { + utest_fixture->i = 83; + // teardown will fail because i is not 13! + } + +Some things to note that were demonstrated above: +* We have this new implicit variable within our macros - utest_fixture. This is a pointer to the struct you decidedw as your fixture (so MyTestFixture in the above code). +* Instead of specifying a testcase set (like we do with the UTEST macro), we instead specify the name of the fixture struct we are use. +* Every fixture has to have a UTEST_F_SETUP and UTEST_F_TEARDOWN macro - even if they do nothing in the body of them. +* Multiple testcases (UTEST_F's) can use the same fixture. +* You can use EXPECT_* and ASSERT_* macros within the body of both the fixture's setup and teardown macros. + +## Define an Indexed Testcase ## + +Sometimes you want to use the same fixture _and_ testcase repeatedly, but prehaps subtly change one variable within. This is where indexed testcases come in. + + struct MyTestIndexedFixture{ + bool x; + bool y; + }; + + UTEST_I_SETUP(MyTestIndexedFixture) { + if (utest_index < 30) { + utest_fixture->x = utest_index & 1; + utest_fixture->y = (utest_index + 1) & 1; + } + } + + UTEST_I_TEARDOWN(MyTestIndexedFixture) { + EXPECT_LE(0, utest_index); + } + + UTEST_I(MyTestIndexedFixture, a, 2) { + ASSERT_TRUE(utest_fixture->x | utest_fixture->y); + } + + UTEST_I(MyTestIndexedFixture, b, 42) { + // this will fail when the index is >= 30 + ASSERT_TRUE(utest_fixture->x | utest_fixture->y); + } + +Note: +* We use UTEST_I_* as the prefix for the setup and teardown functions now. +* We use UTEST_I to declare the testcases. +* We have access to a new variable utest_index in our setup and teardown functions, that we can use to slightly vary our fixture. +* We provide a number as the third parameter of the UTEST_I macro - this is the number of times we should run the test case for that index. It must be a literal. + +## Testing Macros ## Matching what googletest has, we provide two variants of each of the error checking conditions - ASSERT's and EXPECT's. If an ASSERT fails, the test case will cease execution, and utest.h will continue with the next test case to be run. If an EXPECT fails, the remainder of the test case will still be executed, allowing for further checks to be carried out. -We currently provide the following macros to be used within TESTCASE's. +We currently provide the following macros to be used within UTEST's. ### ASSERT_TRUE(x) ### Asserts that x evaluates to true (EG. non-zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 1; ASSERT_TRUE(i); // pass! ASSERT_TRUE(42); // pass! @@ -53,7 +161,7 @@ Asserts that x evaluates to false (EG. zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 0; ASSERT_FALSE(i); // pass! ASSERT_FALSE(1); // fail! @@ -63,7 +171,7 @@ Asserts that x and y are equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 42; ASSERT_EQ(a, b); // pass! @@ -77,7 +185,7 @@ Asserts that x and y are not equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_NE(a, b); // pass! @@ -91,7 +199,7 @@ Asserts that x is less than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; ASSERT_LT(a, b); // pass! @@ -105,7 +213,7 @@ Asserts that x is less than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; ASSERT_LE(a, b); // pass! @@ -122,7 +230,7 @@ Asserts that x is greater than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_GT(a, b); // pass! @@ -136,7 +244,7 @@ Asserts that x is greater than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_GE(a, b); // pass! @@ -153,7 +261,7 @@ Expects that x evaluates to true (EG. non-zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 1; EXPECT_TRUE(i); // pass! EXPECT_TRUE(42); // pass! @@ -164,7 +272,7 @@ Expects that x evaluates to false (EG. zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 0; EXPECT_FALSE(i); // pass! EXPECT_FALSE(1); // fail! @@ -174,7 +282,7 @@ Expects that x and y are equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 42; EXPECT_EQ(a, b); // pass! @@ -188,7 +296,7 @@ Expects that x and y are not equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_NE(a, b); // pass! @@ -202,7 +310,7 @@ Expects that x is less than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; EXPECT_LT(a, b); // pass! @@ -216,7 +324,7 @@ Expects that x is less than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; EXPECT_LE(a, b); // pass! @@ -233,7 +341,7 @@ Expects that x is greater than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_GT(a, b); // pass! @@ -247,7 +355,7 @@ Expects that x is greater than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_GE(a, b); // pass! diff --git a/appveyor.yml b/appveyor.yml index d833fc9..87d140e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -6,8 +6,10 @@ environment: matrix: - - VSVERSION: Visual Studio 11 - - VSVERSION: Visual Studio 12 + - VSVERSION: Visual Studio 10 2010 + - VSVERSION: Visual Studio 11 2012 + - VSVERSION: Visual Studio 12 2013 + - VSVERSION: Visual Studio 14 2015 platform: - Win32 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fd3cfbc..b911fba 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -36,15 +36,31 @@ ) if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") - set_source_files_properties(test.c test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Werror" + set_source_files_properties(main.c test.c PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu89" ) elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") - set_source_files_properties(main.c test.c test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + set_source_files_properties(main.c test.c PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=gnu89" ) elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") - set_source_files_properties(main.c test.c test.cpp PROPERTIES + set_source_files_properties(main.c test.c PROPERTIES + COMPILE_FLAGS "/Wall /WX /wd4514" + ) +else() + message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") +endif() + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set_source_files_properties(test.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set_source_files_properties(test.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + set_source_files_properties(test.cpp PROPERTIES COMPILE_FLAGS "/Wall /WX /wd4514" ) else() diff --git a/README.md b/README.md index ca471f4..6d7a200 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,19 @@ The current supported compilers are gcc, clang and msvc. -The current tested compiler versions are gcc 4.8.2, clang 3.5 and MSVC 18.0.21005.1. +The current supported platforms are Linux, Mac OSX and Windows. + +## Command Line Options ## + +utest.h supports some command line options: + +* --help to output the help message +* --filter= will filter the test cases to run (useful for re-running one particular offending test case). +* --output= will output an xunit XML file with the test results (that Jenkins, travis-ci, and appveyor can parse for the test results). ## Design ## -UTest is a single header library to enable all the fun of unit testing in C and C++. The library has been designed to provide an output similar to Google's googletest framework; +UTest is a single header library to enable all the fun of unit testing in C and C++. The library has been designed to provide an output similar to Google's googletest framework: [==========] Running 1 test cases. [ RUN ] foo.bar @@ -24,25 +32,125 @@ [==========] 1 test cases ran. [ PASSED ] 1 tests. +## UTEST_MAIN ## + +In one C or C++ file, you must call the macro UTEST_MAIN: + + UTEST_MAIN(); + +This will call into utest.h, instantiate all the testcases and run the unit test framework. + +Alternatively, if you want to write your own main and call into utest.h, you can instead, in one C or C++ file call: + + UTEST_STATE(); + +And then when you are ready to call into the utest.h framework do: + + int main(int argc, const char *const argv[]) { + // do your own thing + return utest_main(argc, argv); + } + +## Define a Testcase ## + To define a test case to run, you can do the following; #include "utest.h" - TESTCASE(foo, bar) { + UTEST(foo, bar) { ASSERT_TRUE(1); } -The TESTCASE macro takes two parameters - the first being the set that the test case belongs to, the second being the name of the test. This allows tests to be grouped for conveience. +The UTEST macro takes two parameters - the first being the set that the test case belongs to, the second being the name of the test. This allows tests to be grouped for conveience. + +## Define a Fixtured Testcase ## + +A fixtured testcase is one in which there is a struct that is instantiated that can be shared across multiple testcases. + + struct MyTestFixture { + char c; + int i; + float f; + }; + + UTEST_F_SETUP(MyTestFixture) { + utest_fixture->c = 'a'; + utest_fixture->i = 42; + utest_fixture->f = 3.14f; + + // we can even assert and expect in setup! + ASSERT_EQ(42, utest_fixture->i); + EXPECT_TRUE(true); + } + + UTEST_F_TEARDOWN(MyTestFixture) { + // and also assert and expect in teardown! + ASSERT_EQ(13, utest_fixture->i); + } + + UTEST_F(MyTestFixture, a) { + utest_fixture->i = 13; + // teardown will succeed because i is 13... + } + + UTEST_F(MyTestFixture, b) { + utest_fixture->i = 83; + // teardown will fail because i is not 13! + } + +Some things to note that were demonstrated above: +* We have this new implicit variable within our macros - utest_fixture. This is a pointer to the struct you decidedw as your fixture (so MyTestFixture in the above code). +* Instead of specifying a testcase set (like we do with the UTEST macro), we instead specify the name of the fixture struct we are use. +* Every fixture has to have a UTEST_F_SETUP and UTEST_F_TEARDOWN macro - even if they do nothing in the body of them. +* Multiple testcases (UTEST_F's) can use the same fixture. +* You can use EXPECT_* and ASSERT_* macros within the body of both the fixture's setup and teardown macros. + +## Define an Indexed Testcase ## + +Sometimes you want to use the same fixture _and_ testcase repeatedly, but prehaps subtly change one variable within. This is where indexed testcases come in. + + struct MyTestIndexedFixture{ + bool x; + bool y; + }; + + UTEST_I_SETUP(MyTestIndexedFixture) { + if (utest_index < 30) { + utest_fixture->x = utest_index & 1; + utest_fixture->y = (utest_index + 1) & 1; + } + } + + UTEST_I_TEARDOWN(MyTestIndexedFixture) { + EXPECT_LE(0, utest_index); + } + + UTEST_I(MyTestIndexedFixture, a, 2) { + ASSERT_TRUE(utest_fixture->x | utest_fixture->y); + } + + UTEST_I(MyTestIndexedFixture, b, 42) { + // this will fail when the index is >= 30 + ASSERT_TRUE(utest_fixture->x | utest_fixture->y); + } + +Note: +* We use UTEST_I_* as the prefix for the setup and teardown functions now. +* We use UTEST_I to declare the testcases. +* We have access to a new variable utest_index in our setup and teardown functions, that we can use to slightly vary our fixture. +* We provide a number as the third parameter of the UTEST_I macro - this is the number of times we should run the test case for that index. It must be a literal. + +## Testing Macros ## Matching what googletest has, we provide two variants of each of the error checking conditions - ASSERT's and EXPECT's. If an ASSERT fails, the test case will cease execution, and utest.h will continue with the next test case to be run. If an EXPECT fails, the remainder of the test case will still be executed, allowing for further checks to be carried out. -We currently provide the following macros to be used within TESTCASE's. +We currently provide the following macros to be used within UTEST's. ### ASSERT_TRUE(x) ### Asserts that x evaluates to true (EG. non-zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 1; ASSERT_TRUE(i); // pass! ASSERT_TRUE(42); // pass! @@ -53,7 +161,7 @@ Asserts that x evaluates to false (EG. zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 0; ASSERT_FALSE(i); // pass! ASSERT_FALSE(1); // fail! @@ -63,7 +171,7 @@ Asserts that x and y are equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 42; ASSERT_EQ(a, b); // pass! @@ -77,7 +185,7 @@ Asserts that x and y are not equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_NE(a, b); // pass! @@ -91,7 +199,7 @@ Asserts that x is less than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; ASSERT_LT(a, b); // pass! @@ -105,7 +213,7 @@ Asserts that x is less than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; ASSERT_LE(a, b); // pass! @@ -122,7 +230,7 @@ Asserts that x is greater than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_GT(a, b); // pass! @@ -136,7 +244,7 @@ Asserts that x is greater than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_GE(a, b); // pass! @@ -153,7 +261,7 @@ Expects that x evaluates to true (EG. non-zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 1; EXPECT_TRUE(i); // pass! EXPECT_TRUE(42); // pass! @@ -164,7 +272,7 @@ Expects that x evaluates to false (EG. zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 0; EXPECT_FALSE(i); // pass! EXPECT_FALSE(1); // fail! @@ -174,7 +282,7 @@ Expects that x and y are equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 42; EXPECT_EQ(a, b); // pass! @@ -188,7 +296,7 @@ Expects that x and y are not equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_NE(a, b); // pass! @@ -202,7 +310,7 @@ Expects that x is less than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; EXPECT_LT(a, b); // pass! @@ -216,7 +324,7 @@ Expects that x is less than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; EXPECT_LE(a, b); // pass! @@ -233,7 +341,7 @@ Expects that x is greater than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_GT(a, b); // pass! @@ -247,7 +355,7 @@ Expects that x is greater than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_GE(a, b); // pass! diff --git a/appveyor.yml b/appveyor.yml index d833fc9..87d140e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -6,8 +6,10 @@ environment: matrix: - - VSVERSION: Visual Studio 11 - - VSVERSION: Visual Studio 12 + - VSVERSION: Visual Studio 10 2010 + - VSVERSION: Visual Studio 11 2012 + - VSVERSION: Visual Studio 12 2013 + - VSVERSION: Visual Studio 14 2015 platform: - Win32 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fd3cfbc..b911fba 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -36,15 +36,31 @@ ) if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") - set_source_files_properties(test.c test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Werror" + set_source_files_properties(main.c test.c PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu89" ) elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") - set_source_files_properties(main.c test.c test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + set_source_files_properties(main.c test.c PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=gnu89" ) elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") - set_source_files_properties(main.c test.c test.cpp PROPERTIES + set_source_files_properties(main.c test.c PROPERTIES + COMPILE_FLAGS "/Wall /WX /wd4514" + ) +else() + message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") +endif() + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set_source_files_properties(test.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set_source_files_properties(test.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + set_source_files_properties(test.cpp PROPERTIES COMPILE_FLAGS "/Wall /WX /wd4514" ) else() diff --git a/test/test.c b/test/test.c index ac4330a..4766e74 100644 --- a/test/test.c +++ b/test/test.c @@ -1,85 +1,136 @@ -// This is free and unencumbered software released into the public domain. -// -// Anyone is free to copy, modify, publish, use, compile, sell, or -// distribute this software, either in source code form or as a compiled -// binary, for any purpose, commercial or non-commercial, and by any -// means. -// -// In jurisdictions that recognize copyright laws, the author or authors -// of this software dedicate any and all copyright interest in the -// software to the public domain. We make this dedication for the benefit -// of the public at large and to the detriment of our heirs and -// successors. We intend this dedication to be an overt act of -// relinquishment in perpetuity of all present and future rights to this -// software under copyright law. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// -// For more information, please refer to +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to +*/ #include "utest.h" #ifdef _MSC_VER -#pragma warning(push) - -// disable 'conditional expression is constant' - our examples below use this! +/* disable 'conditional expression is constant' - our examples below use this! */ #pragma warning(disable : 4127) +#pragma #endif -TESTCASE(c, ASSERT_TRUE) { ASSERT_TRUE(1); } +UTEST(c, ASSERT_TRUE) { ASSERT_TRUE(1); } -TESTCASE(c, ASSERT_FALSE) { ASSERT_FALSE(0); } +UTEST(c, ASSERT_FALSE) { ASSERT_FALSE(0); } -TESTCASE(c, ASSERT_EQ) { ASSERT_EQ(1, 1); } +UTEST(c, ASSERT_EQ) { ASSERT_EQ(1, 1); } -TESTCASE(c, ASSERT_NE) { ASSERT_NE(1, 2); } +UTEST(c, ASSERT_NE) { ASSERT_NE(1, 2); } -TESTCASE(c, ASSERT_LT) { ASSERT_LT(1, 2); } +UTEST(c, ASSERT_LT) { ASSERT_LT(1, 2); } -TESTCASE(c, ASSERT_LE) { +UTEST(c, ASSERT_LE) { ASSERT_LE(1, 1); ASSERT_LE(1, 2); } -TESTCASE(c, ASSERT_GT) { ASSERT_GT(2, 1); } +UTEST(c, ASSERT_GT) { ASSERT_GT(2, 1); } -TESTCASE(c, ASSERT_GE) { +UTEST(c, ASSERT_GE) { ASSERT_GE(1, 1); ASSERT_GE(2, 1); } -TESTCASE(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } +UTEST(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } -TESTCASE(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } +UTEST(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } -TESTCASE(c, EXPECT_TRUE) { EXPECT_TRUE(1); } +UTEST(c, EXPECT_TRUE) { EXPECT_TRUE(1); } -TESTCASE(c, EXPECT_FALSE) { EXPECT_FALSE(0); } +UTEST(c, EXPECT_FALSE) { EXPECT_FALSE(0); } -TESTCASE(c, EXPECT_EQ) { EXPECT_EQ(1, 1); } +UTEST(c, EXPECT_EQ) { EXPECT_EQ(1, 1); } -TESTCASE(c, EXPECT_NE) { EXPECT_NE(1, 2); } +UTEST(c, EXPECT_NE) { EXPECT_NE(1, 2); } -TESTCASE(c, EXPECT_LT) { EXPECT_LT(1, 2); } +UTEST(c, EXPECT_LT) { EXPECT_LT(1, 2); } -TESTCASE(c, EXPECT_LE) { +UTEST(c, EXPECT_LE) { EXPECT_LE(1, 1); EXPECT_LE(1, 2); } -TESTCASE(c, EXPECT_GT) { EXPECT_GT(2, 1); } +UTEST(c, EXPECT_GT) { EXPECT_GT(2, 1); } -TESTCASE(c, EXPECT_GE) { +UTEST(c, EXPECT_GE) { EXPECT_GE(1, 1); EXPECT_GE(2, 1); } -TESTCASE(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } +UTEST(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } -TESTCASE(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } +UTEST(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +struct MyTestF { + int foo; +}; + +UTEST_F_SETUP(MyTestF) { + ASSERT_EQ(0, utest_fixture->foo); + utest_fixture->foo = 42; +} + +UTEST_F_TEARDOWN(MyTestF) { + ASSERT_EQ(13, utest_fixture->foo); +} + +UTEST_F(MyTestF, c) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, c2) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +struct MyTestI { + size_t foo; + size_t bar; +}; + +UTEST_I_SETUP(MyTestI) { + ASSERT_EQ(0, utest_fixture->foo); + ASSERT_EQ(0, utest_fixture->bar); + utest_fixture->foo = 42; + utest_fixture->bar = utest_index; +} + +UTEST_I_TEARDOWN(MyTestI) { + ASSERT_EQ(13, utest_fixture->foo); + ASSERT_EQ(utest_index, utest_fixture->bar); +} + +UTEST_I(MyTestI, c, 2) { + ASSERT_GT(2, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, c2, 128) { + ASSERT_GT(128, utest_fixture->bar); + utest_fixture->foo = 13; +} diff --git a/README.md b/README.md index ca471f4..6d7a200 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,19 @@ The current supported compilers are gcc, clang and msvc. -The current tested compiler versions are gcc 4.8.2, clang 3.5 and MSVC 18.0.21005.1. +The current supported platforms are Linux, Mac OSX and Windows. + +## Command Line Options ## + +utest.h supports some command line options: + +* --help to output the help message +* --filter= will filter the test cases to run (useful for re-running one particular offending test case). +* --output= will output an xunit XML file with the test results (that Jenkins, travis-ci, and appveyor can parse for the test results). ## Design ## -UTest is a single header library to enable all the fun of unit testing in C and C++. The library has been designed to provide an output similar to Google's googletest framework; +UTest is a single header library to enable all the fun of unit testing in C and C++. The library has been designed to provide an output similar to Google's googletest framework: [==========] Running 1 test cases. [ RUN ] foo.bar @@ -24,25 +32,125 @@ [==========] 1 test cases ran. [ PASSED ] 1 tests. +## UTEST_MAIN ## + +In one C or C++ file, you must call the macro UTEST_MAIN: + + UTEST_MAIN(); + +This will call into utest.h, instantiate all the testcases and run the unit test framework. + +Alternatively, if you want to write your own main and call into utest.h, you can instead, in one C or C++ file call: + + UTEST_STATE(); + +And then when you are ready to call into the utest.h framework do: + + int main(int argc, const char *const argv[]) { + // do your own thing + return utest_main(argc, argv); + } + +## Define a Testcase ## + To define a test case to run, you can do the following; #include "utest.h" - TESTCASE(foo, bar) { + UTEST(foo, bar) { ASSERT_TRUE(1); } -The TESTCASE macro takes two parameters - the first being the set that the test case belongs to, the second being the name of the test. This allows tests to be grouped for conveience. +The UTEST macro takes two parameters - the first being the set that the test case belongs to, the second being the name of the test. This allows tests to be grouped for conveience. + +## Define a Fixtured Testcase ## + +A fixtured testcase is one in which there is a struct that is instantiated that can be shared across multiple testcases. + + struct MyTestFixture { + char c; + int i; + float f; + }; + + UTEST_F_SETUP(MyTestFixture) { + utest_fixture->c = 'a'; + utest_fixture->i = 42; + utest_fixture->f = 3.14f; + + // we can even assert and expect in setup! + ASSERT_EQ(42, utest_fixture->i); + EXPECT_TRUE(true); + } + + UTEST_F_TEARDOWN(MyTestFixture) { + // and also assert and expect in teardown! + ASSERT_EQ(13, utest_fixture->i); + } + + UTEST_F(MyTestFixture, a) { + utest_fixture->i = 13; + // teardown will succeed because i is 13... + } + + UTEST_F(MyTestFixture, b) { + utest_fixture->i = 83; + // teardown will fail because i is not 13! + } + +Some things to note that were demonstrated above: +* We have this new implicit variable within our macros - utest_fixture. This is a pointer to the struct you decidedw as your fixture (so MyTestFixture in the above code). +* Instead of specifying a testcase set (like we do with the UTEST macro), we instead specify the name of the fixture struct we are use. +* Every fixture has to have a UTEST_F_SETUP and UTEST_F_TEARDOWN macro - even if they do nothing in the body of them. +* Multiple testcases (UTEST_F's) can use the same fixture. +* You can use EXPECT_* and ASSERT_* macros within the body of both the fixture's setup and teardown macros. + +## Define an Indexed Testcase ## + +Sometimes you want to use the same fixture _and_ testcase repeatedly, but prehaps subtly change one variable within. This is where indexed testcases come in. + + struct MyTestIndexedFixture{ + bool x; + bool y; + }; + + UTEST_I_SETUP(MyTestIndexedFixture) { + if (utest_index < 30) { + utest_fixture->x = utest_index & 1; + utest_fixture->y = (utest_index + 1) & 1; + } + } + + UTEST_I_TEARDOWN(MyTestIndexedFixture) { + EXPECT_LE(0, utest_index); + } + + UTEST_I(MyTestIndexedFixture, a, 2) { + ASSERT_TRUE(utest_fixture->x | utest_fixture->y); + } + + UTEST_I(MyTestIndexedFixture, b, 42) { + // this will fail when the index is >= 30 + ASSERT_TRUE(utest_fixture->x | utest_fixture->y); + } + +Note: +* We use UTEST_I_* as the prefix for the setup and teardown functions now. +* We use UTEST_I to declare the testcases. +* We have access to a new variable utest_index in our setup and teardown functions, that we can use to slightly vary our fixture. +* We provide a number as the third parameter of the UTEST_I macro - this is the number of times we should run the test case for that index. It must be a literal. + +## Testing Macros ## Matching what googletest has, we provide two variants of each of the error checking conditions - ASSERT's and EXPECT's. If an ASSERT fails, the test case will cease execution, and utest.h will continue with the next test case to be run. If an EXPECT fails, the remainder of the test case will still be executed, allowing for further checks to be carried out. -We currently provide the following macros to be used within TESTCASE's. +We currently provide the following macros to be used within UTEST's. ### ASSERT_TRUE(x) ### Asserts that x evaluates to true (EG. non-zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 1; ASSERT_TRUE(i); // pass! ASSERT_TRUE(42); // pass! @@ -53,7 +161,7 @@ Asserts that x evaluates to false (EG. zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 0; ASSERT_FALSE(i); // pass! ASSERT_FALSE(1); // fail! @@ -63,7 +171,7 @@ Asserts that x and y are equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 42; ASSERT_EQ(a, b); // pass! @@ -77,7 +185,7 @@ Asserts that x and y are not equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_NE(a, b); // pass! @@ -91,7 +199,7 @@ Asserts that x is less than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; ASSERT_LT(a, b); // pass! @@ -105,7 +213,7 @@ Asserts that x is less than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; ASSERT_LE(a, b); // pass! @@ -122,7 +230,7 @@ Asserts that x is greater than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_GT(a, b); // pass! @@ -136,7 +244,7 @@ Asserts that x is greater than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_GE(a, b); // pass! @@ -153,7 +261,7 @@ Expects that x evaluates to true (EG. non-zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 1; EXPECT_TRUE(i); // pass! EXPECT_TRUE(42); // pass! @@ -164,7 +272,7 @@ Expects that x evaluates to false (EG. zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 0; EXPECT_FALSE(i); // pass! EXPECT_FALSE(1); // fail! @@ -174,7 +282,7 @@ Expects that x and y are equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 42; EXPECT_EQ(a, b); // pass! @@ -188,7 +296,7 @@ Expects that x and y are not equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_NE(a, b); // pass! @@ -202,7 +310,7 @@ Expects that x is less than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; EXPECT_LT(a, b); // pass! @@ -216,7 +324,7 @@ Expects that x is less than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; EXPECT_LE(a, b); // pass! @@ -233,7 +341,7 @@ Expects that x is greater than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_GT(a, b); // pass! @@ -247,7 +355,7 @@ Expects that x is greater than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_GE(a, b); // pass! diff --git a/appveyor.yml b/appveyor.yml index d833fc9..87d140e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -6,8 +6,10 @@ environment: matrix: - - VSVERSION: Visual Studio 11 - - VSVERSION: Visual Studio 12 + - VSVERSION: Visual Studio 10 2010 + - VSVERSION: Visual Studio 11 2012 + - VSVERSION: Visual Studio 12 2013 + - VSVERSION: Visual Studio 14 2015 platform: - Win32 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fd3cfbc..b911fba 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -36,15 +36,31 @@ ) if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") - set_source_files_properties(test.c test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Werror" + set_source_files_properties(main.c test.c PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu89" ) elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") - set_source_files_properties(main.c test.c test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + set_source_files_properties(main.c test.c PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=gnu89" ) elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") - set_source_files_properties(main.c test.c test.cpp PROPERTIES + set_source_files_properties(main.c test.c PROPERTIES + COMPILE_FLAGS "/Wall /WX /wd4514" + ) +else() + message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") +endif() + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set_source_files_properties(test.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set_source_files_properties(test.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + set_source_files_properties(test.cpp PROPERTIES COMPILE_FLAGS "/Wall /WX /wd4514" ) else() diff --git a/test/test.c b/test/test.c index ac4330a..4766e74 100644 --- a/test/test.c +++ b/test/test.c @@ -1,85 +1,136 @@ -// This is free and unencumbered software released into the public domain. -// -// Anyone is free to copy, modify, publish, use, compile, sell, or -// distribute this software, either in source code form or as a compiled -// binary, for any purpose, commercial or non-commercial, and by any -// means. -// -// In jurisdictions that recognize copyright laws, the author or authors -// of this software dedicate any and all copyright interest in the -// software to the public domain. We make this dedication for the benefit -// of the public at large and to the detriment of our heirs and -// successors. We intend this dedication to be an overt act of -// relinquishment in perpetuity of all present and future rights to this -// software under copyright law. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// -// For more information, please refer to +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to +*/ #include "utest.h" #ifdef _MSC_VER -#pragma warning(push) - -// disable 'conditional expression is constant' - our examples below use this! +/* disable 'conditional expression is constant' - our examples below use this! */ #pragma warning(disable : 4127) +#pragma #endif -TESTCASE(c, ASSERT_TRUE) { ASSERT_TRUE(1); } +UTEST(c, ASSERT_TRUE) { ASSERT_TRUE(1); } -TESTCASE(c, ASSERT_FALSE) { ASSERT_FALSE(0); } +UTEST(c, ASSERT_FALSE) { ASSERT_FALSE(0); } -TESTCASE(c, ASSERT_EQ) { ASSERT_EQ(1, 1); } +UTEST(c, ASSERT_EQ) { ASSERT_EQ(1, 1); } -TESTCASE(c, ASSERT_NE) { ASSERT_NE(1, 2); } +UTEST(c, ASSERT_NE) { ASSERT_NE(1, 2); } -TESTCASE(c, ASSERT_LT) { ASSERT_LT(1, 2); } +UTEST(c, ASSERT_LT) { ASSERT_LT(1, 2); } -TESTCASE(c, ASSERT_LE) { +UTEST(c, ASSERT_LE) { ASSERT_LE(1, 1); ASSERT_LE(1, 2); } -TESTCASE(c, ASSERT_GT) { ASSERT_GT(2, 1); } +UTEST(c, ASSERT_GT) { ASSERT_GT(2, 1); } -TESTCASE(c, ASSERT_GE) { +UTEST(c, ASSERT_GE) { ASSERT_GE(1, 1); ASSERT_GE(2, 1); } -TESTCASE(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } +UTEST(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } -TESTCASE(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } +UTEST(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } -TESTCASE(c, EXPECT_TRUE) { EXPECT_TRUE(1); } +UTEST(c, EXPECT_TRUE) { EXPECT_TRUE(1); } -TESTCASE(c, EXPECT_FALSE) { EXPECT_FALSE(0); } +UTEST(c, EXPECT_FALSE) { EXPECT_FALSE(0); } -TESTCASE(c, EXPECT_EQ) { EXPECT_EQ(1, 1); } +UTEST(c, EXPECT_EQ) { EXPECT_EQ(1, 1); } -TESTCASE(c, EXPECT_NE) { EXPECT_NE(1, 2); } +UTEST(c, EXPECT_NE) { EXPECT_NE(1, 2); } -TESTCASE(c, EXPECT_LT) { EXPECT_LT(1, 2); } +UTEST(c, EXPECT_LT) { EXPECT_LT(1, 2); } -TESTCASE(c, EXPECT_LE) { +UTEST(c, EXPECT_LE) { EXPECT_LE(1, 1); EXPECT_LE(1, 2); } -TESTCASE(c, EXPECT_GT) { EXPECT_GT(2, 1); } +UTEST(c, EXPECT_GT) { EXPECT_GT(2, 1); } -TESTCASE(c, EXPECT_GE) { +UTEST(c, EXPECT_GE) { EXPECT_GE(1, 1); EXPECT_GE(2, 1); } -TESTCASE(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } +UTEST(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } -TESTCASE(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } +UTEST(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +struct MyTestF { + int foo; +}; + +UTEST_F_SETUP(MyTestF) { + ASSERT_EQ(0, utest_fixture->foo); + utest_fixture->foo = 42; +} + +UTEST_F_TEARDOWN(MyTestF) { + ASSERT_EQ(13, utest_fixture->foo); +} + +UTEST_F(MyTestF, c) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, c2) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +struct MyTestI { + size_t foo; + size_t bar; +}; + +UTEST_I_SETUP(MyTestI) { + ASSERT_EQ(0, utest_fixture->foo); + ASSERT_EQ(0, utest_fixture->bar); + utest_fixture->foo = 42; + utest_fixture->bar = utest_index; +} + +UTEST_I_TEARDOWN(MyTestI) { + ASSERT_EQ(13, utest_fixture->foo); + ASSERT_EQ(utest_index, utest_fixture->bar); +} + +UTEST_I(MyTestI, c, 2) { + ASSERT_GT(2, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, c2, 128) { + ASSERT_GT(128, utest_fixture->bar); + utest_fixture->foo = 13; +} diff --git a/test/test.cpp b/test/test.cpp index 4d7ef3a..b655f4c 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -26,60 +26,108 @@ #include "utest.h" #ifdef _MSC_VER -#pragma warning(push) - // disable 'conditional expression is constant' - our examples below use this! #pragma warning(disable : 4127) #endif -TESTCASE(cpp, ASSERT_TRUE) { ASSERT_TRUE(1); } +UTEST(cpp, ASSERT_TRUE) { ASSERT_TRUE(1); } -TESTCASE(cpp, ASSERT_FALSE) { ASSERT_FALSE(0); } +UTEST(cpp, ASSERT_FALSE) { ASSERT_FALSE(0); } -TESTCASE(cpp, ASSERT_EQ) { ASSERT_EQ(1, 1); } +UTEST(cpp, ASSERT_EQ) { ASSERT_EQ(1, 1); } -TESTCASE(cpp, ASSERT_NE) { ASSERT_NE(1, 2); } +UTEST(cpp, ASSERT_NE) { ASSERT_NE(1, 2); } -TESTCASE(cpp, ASSERT_LT) { ASSERT_LT(1, 2); } +UTEST(cpp, ASSERT_LT) { ASSERT_LT(1, 2); } -TESTCASE(cpp, ASSERT_LE) { +UTEST(cpp, ASSERT_LE) { ASSERT_LE(1, 1); ASSERT_LE(1, 2); } -TESTCASE(cpp, ASSERT_GT) { ASSERT_GT(2, 1); } +UTEST(cpp, ASSERT_GT) { ASSERT_GT(2, 1); } -TESTCASE(cpp, ASSERT_GE) { +UTEST(cpp, ASSERT_GE) { ASSERT_GE(1, 1); ASSERT_GE(2, 1); } -TESTCASE(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } +UTEST(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } -TESTCASE(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } +UTEST(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } -TESTCASE(cpp, EXPECT_TRUE) { EXPECT_TRUE(1); } +UTEST(cpp, EXPECT_TRUE) { EXPECT_TRUE(1); } -TESTCASE(cpp, EXPECT_FALSE) { EXPECT_FALSE(0); } +UTEST(cpp, EXPECT_FALSE) { EXPECT_FALSE(0); } -TESTCASE(cpp, EXPECT_EQ) { EXPECT_EQ(1, 1); } +UTEST(cpp, EXPECT_EQ) { EXPECT_EQ(1, 1); } -TESTCASE(cpp, EXPECT_NE) { EXPECT_NE(1, 2); } +UTEST(cpp, EXPECT_NE) { EXPECT_NE(1, 2); } -TESTCASE(cpp, EXPECT_LT) { EXPECT_LT(1, 2); } +UTEST(cpp, EXPECT_LT) { EXPECT_LT(1, 2); } -TESTCASE(cpp, EXPECT_LE) { +UTEST(cpp, EXPECT_LE) { EXPECT_LE(1, 1); EXPECT_LE(1, 2); } -TESTCASE(cpp, EXPECT_GT) { EXPECT_GT(2, 1); } +UTEST(cpp, EXPECT_GT) { EXPECT_GT(2, 1); } -TESTCASE(cpp, EXPECT_GE) { +UTEST(cpp, EXPECT_GE) { EXPECT_GE(1, 1); EXPECT_GE(2, 1); } -TESTCASE(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } +UTEST(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } -TESTCASE(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } +UTEST(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +struct MyTestF { + int foo; +}; + +UTEST_F_SETUP(MyTestF) { + ASSERT_EQ(0, utest_fixture->foo); + utest_fixture->foo = 42; +} + +UTEST_F_TEARDOWN(MyTestF) { + ASSERT_EQ(13, utest_fixture->foo); +} + +UTEST_F(MyTestF, cpp) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, cpp2) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +struct MyTestI { + size_t foo; + size_t bar; +}; + +UTEST_I_SETUP(MyTestI) { + ASSERT_EQ(0, utest_fixture->foo); + ASSERT_EQ(0, utest_fixture->bar); + utest_fixture->foo = 42; + utest_fixture->bar = utest_index; +} + +UTEST_I_TEARDOWN(MyTestI) { + ASSERT_EQ(13, utest_fixture->foo); + ASSERT_EQ(utest_index, utest_fixture->bar); +} + +UTEST_I(MyTestI, cpp, 2) { + ASSERT_GT(2, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, cpp2, 128) { + ASSERT_GT(128, utest_fixture->bar); + utest_fixture->foo = 13; +} diff --git a/README.md b/README.md index ca471f4..6d7a200 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,19 @@ The current supported compilers are gcc, clang and msvc. -The current tested compiler versions are gcc 4.8.2, clang 3.5 and MSVC 18.0.21005.1. +The current supported platforms are Linux, Mac OSX and Windows. + +## Command Line Options ## + +utest.h supports some command line options: + +* --help to output the help message +* --filter= will filter the test cases to run (useful for re-running one particular offending test case). +* --output= will output an xunit XML file with the test results (that Jenkins, travis-ci, and appveyor can parse for the test results). ## Design ## -UTest is a single header library to enable all the fun of unit testing in C and C++. The library has been designed to provide an output similar to Google's googletest framework; +UTest is a single header library to enable all the fun of unit testing in C and C++. The library has been designed to provide an output similar to Google's googletest framework: [==========] Running 1 test cases. [ RUN ] foo.bar @@ -24,25 +32,125 @@ [==========] 1 test cases ran. [ PASSED ] 1 tests. +## UTEST_MAIN ## + +In one C or C++ file, you must call the macro UTEST_MAIN: + + UTEST_MAIN(); + +This will call into utest.h, instantiate all the testcases and run the unit test framework. + +Alternatively, if you want to write your own main and call into utest.h, you can instead, in one C or C++ file call: + + UTEST_STATE(); + +And then when you are ready to call into the utest.h framework do: + + int main(int argc, const char *const argv[]) { + // do your own thing + return utest_main(argc, argv); + } + +## Define a Testcase ## + To define a test case to run, you can do the following; #include "utest.h" - TESTCASE(foo, bar) { + UTEST(foo, bar) { ASSERT_TRUE(1); } -The TESTCASE macro takes two parameters - the first being the set that the test case belongs to, the second being the name of the test. This allows tests to be grouped for conveience. +The UTEST macro takes two parameters - the first being the set that the test case belongs to, the second being the name of the test. This allows tests to be grouped for conveience. + +## Define a Fixtured Testcase ## + +A fixtured testcase is one in which there is a struct that is instantiated that can be shared across multiple testcases. + + struct MyTestFixture { + char c; + int i; + float f; + }; + + UTEST_F_SETUP(MyTestFixture) { + utest_fixture->c = 'a'; + utest_fixture->i = 42; + utest_fixture->f = 3.14f; + + // we can even assert and expect in setup! + ASSERT_EQ(42, utest_fixture->i); + EXPECT_TRUE(true); + } + + UTEST_F_TEARDOWN(MyTestFixture) { + // and also assert and expect in teardown! + ASSERT_EQ(13, utest_fixture->i); + } + + UTEST_F(MyTestFixture, a) { + utest_fixture->i = 13; + // teardown will succeed because i is 13... + } + + UTEST_F(MyTestFixture, b) { + utest_fixture->i = 83; + // teardown will fail because i is not 13! + } + +Some things to note that were demonstrated above: +* We have this new implicit variable within our macros - utest_fixture. This is a pointer to the struct you decidedw as your fixture (so MyTestFixture in the above code). +* Instead of specifying a testcase set (like we do with the UTEST macro), we instead specify the name of the fixture struct we are use. +* Every fixture has to have a UTEST_F_SETUP and UTEST_F_TEARDOWN macro - even if they do nothing in the body of them. +* Multiple testcases (UTEST_F's) can use the same fixture. +* You can use EXPECT_* and ASSERT_* macros within the body of both the fixture's setup and teardown macros. + +## Define an Indexed Testcase ## + +Sometimes you want to use the same fixture _and_ testcase repeatedly, but prehaps subtly change one variable within. This is where indexed testcases come in. + + struct MyTestIndexedFixture{ + bool x; + bool y; + }; + + UTEST_I_SETUP(MyTestIndexedFixture) { + if (utest_index < 30) { + utest_fixture->x = utest_index & 1; + utest_fixture->y = (utest_index + 1) & 1; + } + } + + UTEST_I_TEARDOWN(MyTestIndexedFixture) { + EXPECT_LE(0, utest_index); + } + + UTEST_I(MyTestIndexedFixture, a, 2) { + ASSERT_TRUE(utest_fixture->x | utest_fixture->y); + } + + UTEST_I(MyTestIndexedFixture, b, 42) { + // this will fail when the index is >= 30 + ASSERT_TRUE(utest_fixture->x | utest_fixture->y); + } + +Note: +* We use UTEST_I_* as the prefix for the setup and teardown functions now. +* We use UTEST_I to declare the testcases. +* We have access to a new variable utest_index in our setup and teardown functions, that we can use to slightly vary our fixture. +* We provide a number as the third parameter of the UTEST_I macro - this is the number of times we should run the test case for that index. It must be a literal. + +## Testing Macros ## Matching what googletest has, we provide two variants of each of the error checking conditions - ASSERT's and EXPECT's. If an ASSERT fails, the test case will cease execution, and utest.h will continue with the next test case to be run. If an EXPECT fails, the remainder of the test case will still be executed, allowing for further checks to be carried out. -We currently provide the following macros to be used within TESTCASE's. +We currently provide the following macros to be used within UTEST's. ### ASSERT_TRUE(x) ### Asserts that x evaluates to true (EG. non-zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 1; ASSERT_TRUE(i); // pass! ASSERT_TRUE(42); // pass! @@ -53,7 +161,7 @@ Asserts that x evaluates to false (EG. zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 0; ASSERT_FALSE(i); // pass! ASSERT_FALSE(1); // fail! @@ -63,7 +171,7 @@ Asserts that x and y are equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 42; ASSERT_EQ(a, b); // pass! @@ -77,7 +185,7 @@ Asserts that x and y are not equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_NE(a, b); // pass! @@ -91,7 +199,7 @@ Asserts that x is less than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; ASSERT_LT(a, b); // pass! @@ -105,7 +213,7 @@ Asserts that x is less than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; ASSERT_LE(a, b); // pass! @@ -122,7 +230,7 @@ Asserts that x is greater than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_GT(a, b); // pass! @@ -136,7 +244,7 @@ Asserts that x is greater than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; ASSERT_GE(a, b); // pass! @@ -153,7 +261,7 @@ Expects that x evaluates to true (EG. non-zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 1; EXPECT_TRUE(i); // pass! EXPECT_TRUE(42); // pass! @@ -164,7 +272,7 @@ Expects that x evaluates to false (EG. zero). - TESTCASE(foo, bar) { + UTEST(foo, bar) { int i = 0; EXPECT_FALSE(i); // pass! EXPECT_FALSE(1); // fail! @@ -174,7 +282,7 @@ Expects that x and y are equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 42; EXPECT_EQ(a, b); // pass! @@ -188,7 +296,7 @@ Expects that x and y are not equal. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_NE(a, b); // pass! @@ -202,7 +310,7 @@ Expects that x is less than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; EXPECT_LT(a, b); // pass! @@ -216,7 +324,7 @@ Expects that x is less than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 13; int b = 42; EXPECT_LE(a, b); // pass! @@ -233,7 +341,7 @@ Expects that x is greater than y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_GT(a, b); // pass! @@ -247,7 +355,7 @@ Expects that x is greater than or equal to y. - TESTCASE(foo, bar) { + UTEST(foo, bar) { int a = 42; int b = 13; EXPECT_GE(a, b); // pass! diff --git a/appveyor.yml b/appveyor.yml index d833fc9..87d140e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -6,8 +6,10 @@ environment: matrix: - - VSVERSION: Visual Studio 11 - - VSVERSION: Visual Studio 12 + - VSVERSION: Visual Studio 10 2010 + - VSVERSION: Visual Studio 11 2012 + - VSVERSION: Visual Studio 12 2013 + - VSVERSION: Visual Studio 14 2015 platform: - Win32 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fd3cfbc..b911fba 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -36,15 +36,31 @@ ) if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") - set_source_files_properties(test.c test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Werror" + set_source_files_properties(main.c test.c PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu89" ) elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") - set_source_files_properties(main.c test.c test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + set_source_files_properties(main.c test.c PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=gnu89" ) elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") - set_source_files_properties(main.c test.c test.cpp PROPERTIES + set_source_files_properties(main.c test.c PROPERTIES + COMPILE_FLAGS "/Wall /WX /wd4514" + ) +else() + message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") +endif() + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set_source_files_properties(test.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set_source_files_properties(test.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + set_source_files_properties(test.cpp PROPERTIES COMPILE_FLAGS "/Wall /WX /wd4514" ) else() diff --git a/test/test.c b/test/test.c index ac4330a..4766e74 100644 --- a/test/test.c +++ b/test/test.c @@ -1,85 +1,136 @@ -// This is free and unencumbered software released into the public domain. -// -// Anyone is free to copy, modify, publish, use, compile, sell, or -// distribute this software, either in source code form or as a compiled -// binary, for any purpose, commercial or non-commercial, and by any -// means. -// -// In jurisdictions that recognize copyright laws, the author or authors -// of this software dedicate any and all copyright interest in the -// software to the public domain. We make this dedication for the benefit -// of the public at large and to the detriment of our heirs and -// successors. We intend this dedication to be an overt act of -// relinquishment in perpetuity of all present and future rights to this -// software under copyright law. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// -// For more information, please refer to +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to +*/ #include "utest.h" #ifdef _MSC_VER -#pragma warning(push) - -// disable 'conditional expression is constant' - our examples below use this! +/* disable 'conditional expression is constant' - our examples below use this! */ #pragma warning(disable : 4127) +#pragma #endif -TESTCASE(c, ASSERT_TRUE) { ASSERT_TRUE(1); } +UTEST(c, ASSERT_TRUE) { ASSERT_TRUE(1); } -TESTCASE(c, ASSERT_FALSE) { ASSERT_FALSE(0); } +UTEST(c, ASSERT_FALSE) { ASSERT_FALSE(0); } -TESTCASE(c, ASSERT_EQ) { ASSERT_EQ(1, 1); } +UTEST(c, ASSERT_EQ) { ASSERT_EQ(1, 1); } -TESTCASE(c, ASSERT_NE) { ASSERT_NE(1, 2); } +UTEST(c, ASSERT_NE) { ASSERT_NE(1, 2); } -TESTCASE(c, ASSERT_LT) { ASSERT_LT(1, 2); } +UTEST(c, ASSERT_LT) { ASSERT_LT(1, 2); } -TESTCASE(c, ASSERT_LE) { +UTEST(c, ASSERT_LE) { ASSERT_LE(1, 1); ASSERT_LE(1, 2); } -TESTCASE(c, ASSERT_GT) { ASSERT_GT(2, 1); } +UTEST(c, ASSERT_GT) { ASSERT_GT(2, 1); } -TESTCASE(c, ASSERT_GE) { +UTEST(c, ASSERT_GE) { ASSERT_GE(1, 1); ASSERT_GE(2, 1); } -TESTCASE(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } +UTEST(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } -TESTCASE(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } +UTEST(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } -TESTCASE(c, EXPECT_TRUE) { EXPECT_TRUE(1); } +UTEST(c, EXPECT_TRUE) { EXPECT_TRUE(1); } -TESTCASE(c, EXPECT_FALSE) { EXPECT_FALSE(0); } +UTEST(c, EXPECT_FALSE) { EXPECT_FALSE(0); } -TESTCASE(c, EXPECT_EQ) { EXPECT_EQ(1, 1); } +UTEST(c, EXPECT_EQ) { EXPECT_EQ(1, 1); } -TESTCASE(c, EXPECT_NE) { EXPECT_NE(1, 2); } +UTEST(c, EXPECT_NE) { EXPECT_NE(1, 2); } -TESTCASE(c, EXPECT_LT) { EXPECT_LT(1, 2); } +UTEST(c, EXPECT_LT) { EXPECT_LT(1, 2); } -TESTCASE(c, EXPECT_LE) { +UTEST(c, EXPECT_LE) { EXPECT_LE(1, 1); EXPECT_LE(1, 2); } -TESTCASE(c, EXPECT_GT) { EXPECT_GT(2, 1); } +UTEST(c, EXPECT_GT) { EXPECT_GT(2, 1); } -TESTCASE(c, EXPECT_GE) { +UTEST(c, EXPECT_GE) { EXPECT_GE(1, 1); EXPECT_GE(2, 1); } -TESTCASE(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } +UTEST(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } -TESTCASE(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } +UTEST(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +struct MyTestF { + int foo; +}; + +UTEST_F_SETUP(MyTestF) { + ASSERT_EQ(0, utest_fixture->foo); + utest_fixture->foo = 42; +} + +UTEST_F_TEARDOWN(MyTestF) { + ASSERT_EQ(13, utest_fixture->foo); +} + +UTEST_F(MyTestF, c) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, c2) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +struct MyTestI { + size_t foo; + size_t bar; +}; + +UTEST_I_SETUP(MyTestI) { + ASSERT_EQ(0, utest_fixture->foo); + ASSERT_EQ(0, utest_fixture->bar); + utest_fixture->foo = 42; + utest_fixture->bar = utest_index; +} + +UTEST_I_TEARDOWN(MyTestI) { + ASSERT_EQ(13, utest_fixture->foo); + ASSERT_EQ(utest_index, utest_fixture->bar); +} + +UTEST_I(MyTestI, c, 2) { + ASSERT_GT(2, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, c2, 128) { + ASSERT_GT(128, utest_fixture->bar); + utest_fixture->foo = 13; +} diff --git a/test/test.cpp b/test/test.cpp index 4d7ef3a..b655f4c 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -26,60 +26,108 @@ #include "utest.h" #ifdef _MSC_VER -#pragma warning(push) - // disable 'conditional expression is constant' - our examples below use this! #pragma warning(disable : 4127) #endif -TESTCASE(cpp, ASSERT_TRUE) { ASSERT_TRUE(1); } +UTEST(cpp, ASSERT_TRUE) { ASSERT_TRUE(1); } -TESTCASE(cpp, ASSERT_FALSE) { ASSERT_FALSE(0); } +UTEST(cpp, ASSERT_FALSE) { ASSERT_FALSE(0); } -TESTCASE(cpp, ASSERT_EQ) { ASSERT_EQ(1, 1); } +UTEST(cpp, ASSERT_EQ) { ASSERT_EQ(1, 1); } -TESTCASE(cpp, ASSERT_NE) { ASSERT_NE(1, 2); } +UTEST(cpp, ASSERT_NE) { ASSERT_NE(1, 2); } -TESTCASE(cpp, ASSERT_LT) { ASSERT_LT(1, 2); } +UTEST(cpp, ASSERT_LT) { ASSERT_LT(1, 2); } -TESTCASE(cpp, ASSERT_LE) { +UTEST(cpp, ASSERT_LE) { ASSERT_LE(1, 1); ASSERT_LE(1, 2); } -TESTCASE(cpp, ASSERT_GT) { ASSERT_GT(2, 1); } +UTEST(cpp, ASSERT_GT) { ASSERT_GT(2, 1); } -TESTCASE(cpp, ASSERT_GE) { +UTEST(cpp, ASSERT_GE) { ASSERT_GE(1, 1); ASSERT_GE(2, 1); } -TESTCASE(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } +UTEST(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } -TESTCASE(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } +UTEST(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } -TESTCASE(cpp, EXPECT_TRUE) { EXPECT_TRUE(1); } +UTEST(cpp, EXPECT_TRUE) { EXPECT_TRUE(1); } -TESTCASE(cpp, EXPECT_FALSE) { EXPECT_FALSE(0); } +UTEST(cpp, EXPECT_FALSE) { EXPECT_FALSE(0); } -TESTCASE(cpp, EXPECT_EQ) { EXPECT_EQ(1, 1); } +UTEST(cpp, EXPECT_EQ) { EXPECT_EQ(1, 1); } -TESTCASE(cpp, EXPECT_NE) { EXPECT_NE(1, 2); } +UTEST(cpp, EXPECT_NE) { EXPECT_NE(1, 2); } -TESTCASE(cpp, EXPECT_LT) { EXPECT_LT(1, 2); } +UTEST(cpp, EXPECT_LT) { EXPECT_LT(1, 2); } -TESTCASE(cpp, EXPECT_LE) { +UTEST(cpp, EXPECT_LE) { EXPECT_LE(1, 1); EXPECT_LE(1, 2); } -TESTCASE(cpp, EXPECT_GT) { EXPECT_GT(2, 1); } +UTEST(cpp, EXPECT_GT) { EXPECT_GT(2, 1); } -TESTCASE(cpp, EXPECT_GE) { +UTEST(cpp, EXPECT_GE) { EXPECT_GE(1, 1); EXPECT_GE(2, 1); } -TESTCASE(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } +UTEST(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } -TESTCASE(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } +UTEST(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +struct MyTestF { + int foo; +}; + +UTEST_F_SETUP(MyTestF) { + ASSERT_EQ(0, utest_fixture->foo); + utest_fixture->foo = 42; +} + +UTEST_F_TEARDOWN(MyTestF) { + ASSERT_EQ(13, utest_fixture->foo); +} + +UTEST_F(MyTestF, cpp) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, cpp2) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +struct MyTestI { + size_t foo; + size_t bar; +}; + +UTEST_I_SETUP(MyTestI) { + ASSERT_EQ(0, utest_fixture->foo); + ASSERT_EQ(0, utest_fixture->bar); + utest_fixture->foo = 42; + utest_fixture->bar = utest_index; +} + +UTEST_I_TEARDOWN(MyTestI) { + ASSERT_EQ(13, utest_fixture->foo); + ASSERT_EQ(utest_index, utest_fixture->bar); +} + +UTEST_I(MyTestI, cpp, 2) { + ASSERT_GT(2, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, cpp2, 128) { + ASSERT_GT(128, utest_fixture->bar); + utest_fixture->foo = 13; +} diff --git a/utest.h b/utest.h index ea45bb2..229d06e 100644 --- a/utest.h +++ b/utest.h @@ -1,40 +1,62 @@ -// The latest version of this library is available on GitHub; -// https://github.com/sheredom/utest.h +/* + The latest version of this library is available on GitHub; + https://github.com/sheredom/utest.h +*/ -// This is free and unencumbered software released into the public domain. -// -// Anyone is free to copy, modify, publish, use, compile, sell, or -// distribute this software, either in source code form or as a compiled -// binary, for any purpose, commercial or non-commercial, and by any -// means. -// -// In jurisdictions that recognize copyright laws, the author or authors -// of this software dedicate any and all copyright interest in the -// software to the public domain. We make this dedication for the benefit -// of the public at large and to the detriment of our heirs and -// successors. We intend this dedication to be an overt act of -// relinquishment in perpetuity of all present and future rights to this -// software under copyright law. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// -// For more information, please refer to +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to +*/ #ifndef SHEREDOM_UTEST_H_INCLUDED #define SHEREDOM_UTEST_H_INCLUDED #ifdef _MSC_VER +/* + Disable warning about not inlining 'inline' functions. + TODO: We'll fix this later by not using fprintf within our macros, and + instead use snprintf to a realloc'ed buffer. +*/ +#pragma warning(disable : 4710) + +/* + Disable warning about inlining functions that are not marked 'inline'. + TODO: add a UTEST_NOINLINE onto the macro generated functions to fix this. +*/ +#pragma warning(disable : 4711) #pragma warning(push, 1) #endif -#include +#if defined(_MSC_VER) +#define int64_t __int64 +#define uint64_t unsigned __int64 +#else #include +#endif + +#include #include #include #include @@ -59,23 +81,25 @@ #elif defined(__linux__) -// slightly obscure include here - we need to include glibc's features.h, but -// we don't want to just include a header that might not be defined for other -// c libraries like musl. Instead we include limits.h, which we know on all -// glibc distributions includes features.h +/* + slightly obscure include here - we need to include glibc's features.h, but + we don't want to just include a header that might not be defined for other + c libraries like musl. Instead we include limits.h, which we know on all + glibc distributions includes features.h +*/ #include #if defined(__GLIBC__) && defined(__GLIBC_MINOR__) #include #if ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__))) -// glibc is version 2.17 or above, so we can just use clock_gettime +/* glibc is version 2.17 or above, so we can just use clock_gettime */ #define UTEST_USE_CLOCKGETTIME -#else // ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__))) -#include +#else #include -#endif // ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__))) -#endif // defined(__GLIBC__) && defined(__GLIBC_MINOR__) +#include +#endif +#endif #elif defined(__APPLE__) #include @@ -93,8 +117,22 @@ static void __cdecl f(void) #else #if defined(__linux__) -#define __STDC_FORMAT_MACROS 1 +#if defined(__clang__) +#if __has_warning("-Wreserved-id-macro") +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wreserved-id-macro" #endif +#endif + +#define __STDC_FORMAT_MACROS 1 + +#if defined(__clang__) +#if __has_warning("-Wreserved-id-macro") +#pragma clang diagnostic pop +#endif +#endif +#endif + #include #define UTEST_PRId64 PRId64 @@ -138,104 +176,153 @@ #endif } -typedef void (*utest_testcase_t)(int *); +typedef void (*utest_testcase_t)(int *, size_t); + +struct utest_test_state_s { + utest_testcase_t func; + size_t index; + char *name; +}; struct utest_state_s { - utest_testcase_t *testcases; - const char **testcase_names; - size_t testcases_length; - const char *filter; + struct utest_test_state_s *tests; + size_t tests_length; + FILE *output; }; +/* extern to the global state utest needs to execute */ +UTEST_EXTERN struct utest_state_s utest_state; + #if defined(_MSC_VER) #define UTEST_WEAK __forceinline #else #define UTEST_WEAK __attribute__((weak)) #endif +#if defined(_MSC_VER) +#define UTEST_UNUSED +#else +#define UTEST_UNUSED __attribute__((unused)) +#endif + +#define UTEST_PRINTF0(FORMAT) \ + if (utest_state.output) { \ + fprintf(utest_state.output, FORMAT); \ + } \ + printf(FORMAT) + +#define UTEST_PRINTF1(FORMAT, P0) \ + if (utest_state.output) { \ + fprintf(utest_state.output, FORMAT, P0); \ + } \ + printf(FORMAT, P0) + +#define UTEST_PRINTF2(FORMAT, P0, P1) \ + if (utest_state.output) { \ + fprintf(utest_state.output, FORMAT, P0, P1); \ + } \ + printf(FORMAT, P0, P1) + +#ifdef _MSC_VER +#define UTEST_SNPRINTF(BUFFER, N, ...) _snprintf_s(BUFFER, N, N, __VA_ARGS__) +#else +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wvariadic-macros" +#endif +#define UTEST_SNPRINTF(...) snprintf(__VA_ARGS__) +#ifdef __clang__ +#pragma clang diagnostic pop +#endif +#endif + #if defined(__cplusplus) -// if we are using c++ we can use overloaded methods (its in the language) +/* if we are using c++ we can use overloaded methods (its in the language) */ #define UTEST_OVERLOADABLE #elif defined(__clang__) -// otherwise, if we are using clang with c we can use the overloadable attribute +/* otherwise, if we are using clang with c - use the overloadable attribute */ #define UTEST_OVERLOADABLE __attribute__((overloadable)) #endif #if defined(UTEST_OVERLOADABLE) UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(float f); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(float f) { - printf("%f", f); + UTEST_PRINTF1("%f", UTEST_CAST(double, f)); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(double d); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(double d) { - printf("%f", d); + UTEST_PRINTF1("%f", d); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long double d); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long double d) { - printf("%Lf", d); + UTEST_PRINTF1("%Lf", d); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(int i) { - printf("%d", i); + UTEST_PRINTF1("%d", i); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned int i) { - printf("%u", i); + UTEST_PRINTF1("%u", i); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long int i) { - printf("%ld", i); + UTEST_PRINTF1("%ld", i); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long unsigned int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long unsigned int i) { - printf("%lu", i); + UTEST_PRINTF1("%lu", i); } -// long long is a c++11 extension -// TODO: grok for c++11 version here -#if !defined(__cplusplus) +/* + long long is a c++11 extension + TODO: grok for c++11 version here +*/ +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long int i) { - printf("%lld", i); + UTEST_PRINTF1("%lld", i); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long unsigned int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long unsigned int i) { - printf("%llu", i); + UTEST_PRINTF1("%llu", i); } #endif #else -// we don't have the ability to print the values we got, so we create a macro to -// tell our users we can't do anything fancy -#define utest_type_printer(...) printf("undef") +/* + we don't have the ability to print the values we got, so we create a macro + to tell our users we can't do anything fancy +*/ +#define utest_type_printer(...) UTEST_PRINTF0("undef") #endif #define UTEST_EXPECT(x, y, cond) \ if (!((x)cond(y))) { \ - printf("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ *utest_result = 1; \ } #define EXPECT_TRUE(x) \ if (!(x)) { \ - printf("%s:%u: Failure\n", __FILE__, __LINE__); \ - printf(" Expected : true\n"); \ - printf(" Actual : %s\n", (x) ? "true" : "false"); \ + UTEST_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF0(" Expected : true\n"); \ + UTEST_PRINTF1(" Actual : %s\n", (x) ? "true" : "false"); \ *utest_result = 1; \ } #define EXPECT_FALSE(x) \ if (x) { \ - printf("%s:%u: Failure\n", __FILE__, __LINE__); \ - printf(" Expected : false\n"); \ - printf(" Actual : %s\n", (x) ? "true" : "false"); \ + UTEST_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF0(" Expected : false\n"); \ + UTEST_PRINTF1(" Actual : %s\n", (x) ? "true" : "false"); \ *utest_result = 1; \ } @@ -248,35 +335,42 @@ #define EXPECT_STREQ(x, y) \ if (0 != strcmp(x, y)) { \ - printf("%s:%u: Failure\n", __FILE__, __LINE__); \ - printf(" Expected : \"%s\"\n", x); \ - printf(" Actual : \"%s\"\n", y); \ + UTEST_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF1(" Expected : \"%s\"\n", x); \ + UTEST_PRINTF1(" Actual : \"%s\"\n", y); \ *utest_result = 1; \ } #define EXPECT_STRNE(x, y) \ if (0 == strcmp(x, y)) { \ - printf("%s:%u: Failure\n", __FILE__, __LINE__); \ - printf(" Expected : \"%s\"\n", x); \ - printf(" Actual : \"%s\"\n", y); \ + UTEST_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF1(" Expected : \"%s\"\n", x); \ + UTEST_PRINTF1(" Actual : \"%s\"\n", y); \ *utest_result = 1; \ } #define UTEST_ASSERT(x, y, cond) \ - UTEST_EXPECT(x, y, cond); \ if (!((x)cond(y))) { \ + UTEST_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ + *utest_result = 1; \ return; \ } #define ASSERT_TRUE(x) \ - EXPECT_TRUE(x); \ if (!(x)) { \ + UTEST_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF0(" Expected : true\n"); \ + UTEST_PRINTF1(" Actual : %s\n", (x) ? "true" : "false"); \ + *utest_result = 1; \ return; \ } #define ASSERT_FALSE(x) \ - EXPECT_FALSE(x); \ if (x) { \ + UTEST_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF0(" Expected : false\n"); \ + UTEST_PRINTF1(" Actual : %s\n", (x) ? "true" : "false"); \ + *utest_result = 1; \ return; \ } @@ -290,37 +384,135 @@ #define ASSERT_STREQ(x, y) \ EXPECT_STREQ(x, y); \ if (0 != strcmp(x, y)) { \ + UTEST_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF1(" Expected : \"%s\"\n", x); \ + UTEST_PRINTF1(" Actual : \"%s\"\n", y); \ + *utest_result = 1; \ return; \ } #define ASSERT_STRNE(x, y) \ EXPECT_STRNE(x, y); \ if (0 == strcmp(x, y)) { \ + UTEST_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF1(" Expected : \"%s\"\n", x); \ + UTEST_PRINTF1(" Actual : \"%s\"\n", y); \ + *utest_result = 1; \ return; \ } -#define TESTCASE(set, name) \ - static void utest_run_##set##_##name(int *utest_result); \ - UTEST_INITIALIZER(utest_register_##set##_##name) { \ - const size_t index = utest_state.testcases_length++; \ - utest_state.testcases = UTEST_PTR_CAST( \ - utest_testcase_t *, \ - realloc(UTEST_PTR_CAST(void *, utest_state.testcases), \ - sizeof(utest_testcase_t) * utest_state.testcases_length)); \ - utest_state.testcases[index] = &utest_run_##set##_##name; \ - utest_state.testcase_names = UTEST_PTR_CAST( \ - const char **, \ - realloc(UTEST_PTR_CAST(void *, utest_state.testcase_names), \ - sizeof(char *) * utest_state.testcases_length)); \ - utest_state.testcase_names[index] = #set "." #name; \ +#define UTEST(SET, NAME) \ + UTEST_EXTERN struct utest_state_s utest_state; \ + static void utest_run_##SET##_##NAME(int *utest_result); \ + static void utest_##SET##_##NAME(int *utest_result, size_t utest_index) { \ + (void) utest_index; \ + utest_run_##SET##_##NAME(utest_result); \ } \ - void utest_run_##set##_##name(int *utest_result) + UTEST_INITIALIZER(utest_register_##SET##_##NAME) { \ + const size_t index = utest_state.tests_length++; \ + const char *name_part = #SET "." #NAME; \ + const size_t name_size = strlen(name_part) + 1; \ + char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ + utest_state.tests = \ + UTEST_PTR_CAST(struct utest_test_state_s *, \ + realloc(UTEST_PTR_CAST(void *, utest_state.tests), \ + sizeof(struct utest_test_state_s) * \ + utest_state.tests_length)); \ + utest_state.tests[index].func = &utest_##SET##_##NAME; \ + utest_state.tests[index].name = name; \ + UTEST_SNPRINTF(name, name_size, "%s", name_part); \ + } \ + void utest_run_##SET##_##NAME(int *utest_result) -// extern to the global state utest needs to execute -UTEST_EXTERN struct utest_state_s utest_state; +#define UTEST_F_SETUP(FIXTURE) \ + static void utest_f_setup_##FIXTURE(int *utest_result, \ + struct FIXTURE *utest_fixture) -UTEST_WEAK int utest_should_filter_test(const char *filter, - const char *testcase); +#define UTEST_F_TEARDOWN(FIXTURE) \ + static void utest_f_teardown_##FIXTURE(int *utest_result, \ + struct FIXTURE *utest_fixture) + +#define UTEST_F(FIXTURE, NAME) \ + UTEST_EXTERN struct utest_state_s utest_state; \ + static void utest_f_setup_##FIXTURE(int *, struct FIXTURE *); \ + static void utest_f_teardown_##FIXTURE(int *, struct FIXTURE *); \ + static void utest_run_##FIXTURE##_##NAME(int *, struct FIXTURE *); \ + static void utest_f_##FIXTURE##_##NAME(int *utest_result, \ + size_t utest_index) { \ + struct FIXTURE fixture; \ + (void) utest_index; \ + memset(&fixture, 0, sizeof(fixture)); \ + utest_f_setup_##FIXTURE(utest_result, &fixture); \ + if (0 != *utest_result) { \ + return; \ + } \ + utest_run_##FIXTURE##_##NAME(utest_result, &fixture); \ + utest_f_teardown_##FIXTURE(utest_result, &fixture); \ + } \ + UTEST_INITIALIZER(utest_register_##FIXTURE##_##NAME) { \ + const size_t index = utest_state.tests_length++; \ + const char *name_part = #FIXTURE "." #NAME; \ + const size_t name_size = strlen(name_part) + 1; \ + char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ + utest_state.tests = \ + UTEST_PTR_CAST(struct utest_test_state_s *, \ + realloc(UTEST_PTR_CAST(void *, utest_state.tests), \ + sizeof(struct utest_test_state_s) * \ + utest_state.tests_length)); \ + utest_state.tests[index].func = &utest_f_##FIXTURE##_##NAME; \ + utest_state.tests[index].name = name; \ + UTEST_SNPRINTF(name, name_size, "%s", name_part); \ + } \ + void utest_run_##FIXTURE##_##NAME(int *utest_result, \ + struct FIXTURE *utest_fixture) + +#define UTEST_I_SETUP(FIXTURE) \ + static void utest_i_setup_##FIXTURE( \ + int *utest_result, struct FIXTURE *utest_fixture, size_t utest_index) + +#define UTEST_I_TEARDOWN(FIXTURE) \ + static void utest_i_teardown_##FIXTURE( \ + int *utest_result, struct FIXTURE *utest_fixture, size_t utest_index) + +#define UTEST_I(FIXTURE, NAME, INDEX) \ + UTEST_EXTERN struct utest_state_s utest_state; \ + static void utest_run_##FIXTURE##_##NAME##_##INDEX(int *, struct FIXTURE *); \ + static void utest_i_##FIXTURE##_##NAME##_##INDEX(int *utest_result, \ + size_t index) { \ + struct FIXTURE fixture; \ + memset(&fixture, 0, sizeof(fixture)); \ + utest_i_setup_##FIXTURE(utest_result, &fixture, index); \ + if (0 != *utest_result) { \ + return; \ + } \ + utest_run_##FIXTURE##_##NAME##_##INDEX(utest_result, &fixture); \ + utest_i_teardown_##FIXTURE(utest_result, &fixture, index); \ + } \ + UTEST_INITIALIZER(utest_register_##FIXTURE##_##NAME##_##INDEX) { \ + size_t i; \ + uint64_t iUp; \ + for (i = 0; i < (INDEX); i++) { \ + const size_t index = utest_state.tests_length++; \ + const char *name_part = #FIXTURE "." #NAME; \ + const size_t name_size = strlen(name_part) + 32; \ + char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ + utest_state.tests = \ + UTEST_PTR_CAST(struct utest_test_state_s *, \ + realloc(UTEST_PTR_CAST(void *, utest_state.tests), \ + sizeof(struct utest_test_state_s) * \ + utest_state.tests_length)); \ + utest_state.tests[index].func = &utest_i_##FIXTURE##_##NAME##_##INDEX; \ + utest_state.tests[index].index = i; \ + utest_state.tests[index].name = name; \ + iUp = UTEST_CAST(uint64_t, i); \ + UTEST_SNPRINTF(name, name_size, "%s/%" UTEST_PRIu64, name_part, iUp); \ + } \ + } \ + void utest_run_##FIXTURE##_##NAME##_##INDEX(int *utest_result, \ + struct FIXTURE *utest_fixture) + +UTEST_WEAK +int utest_should_filter_test(const char *filter, const char *testcase); UTEST_WEAK int utest_should_filter_test(const char *filter, const char *testcase) { if (filter) { @@ -330,27 +522,29 @@ while (('\0' != *filter_cur) && ('\0' != *testcase_cur)) { if ('*' == *filter_cur) { - // store the position of the wildcard + /* store the position of the wildcard */ filter_wildcard = filter_cur; - // skip the wildcard character + /* skip the wildcard character */ filter_cur++; while (('\0' != *filter_cur) && ('\0' != *testcase_cur)) { if ('*' == *filter_cur) { - // we found another wildcard (filter is something like *foo*) so we - // exit the current loop, and return to the parent loop to handle - // the wildcard case + /* + we found another wildcard (filter is something like *foo*) so we + exit the current loop, and return to the parent loop to handle + the wildcard case + */ break; } else if (*filter_cur != *testcase_cur) { - // otherwise our filter didn't match, so reset it + /* otherwise our filter didn't match, so reset it */ filter_cur = filter_wildcard; } - // move testcase along + /* move testcase along */ testcase_cur++; - // move filter along + /* move filter along */ filter_cur++; } @@ -358,16 +552,16 @@ return 0; } - // if the testcase has been exhausted, we don't have a match! + /* if the testcase has been exhausted, we don't have a match! */ if ('\0' == *testcase_cur) { return 1; } } else { if (*testcase_cur != *filter_cur) { - // test case doesn't match filter + /* test case doesn't match filter */ return 1; } else { - // move our filter and testcase forward + /* move our filter and testcase forward */ testcase_cur++; filter_cur++; } @@ -377,7 +571,7 @@ if (('\0' != *filter_cur) || (('\0' != *testcase_cur) && ((filter == filter_cur) || ('*' != filter_cur[-1])))) { - // we have a mismatch! + /* we have a mismatch! */ return 1; } } @@ -385,27 +579,70 @@ return 0; } +static UTEST_INLINE int utest_strncmp(const char *a, const char *b, size_t n) { + /* strncmp breaks on Wall / Werror on gcc/clang, so we avoid using it */ + unsigned i; + + for (i = 0; i < n; i++) { + if (a[i] < b[i]) { + return -1; + } else if (a[i] > b[i]) { + return 1; + } + } + + return 0; +} + +static UTEST_INLINE FILE *utest_fopen(const char *filename, const char *mode) { +#ifdef _MSC_VER + FILE *file; + if (0 == fopen_s(&file, filename, mode)) { + return file; + } else { + return 0; + } +#else + return fopen(filename, mode); +#endif +} + UTEST_WEAK int utest_main(int argc, const char *const argv[]); UTEST_WEAK int utest_main(int argc, const char *const argv[]) { - size_t failed = 0; + uint64_t failed = 0; size_t index = 0; size_t *failed_testcases = 0; size_t failed_testcases_length = 0; const char *filter = 0; - size_t ran_tests = 0; + uint64_t ran_tests = 0; - // loop through all arguments looking for our options + /* loop through all arguments looking for our options */ for (index = 1; index < UTEST_CAST(size_t, argc); index++) { + const char help_str[] = "--help"; const char filter_str[] = "--filter="; + const char output_str[] = "--output="; - if (0 < strcmp(argv[index], filter_str)) { - // user wants to filter what test cases run! + if (0 == utest_strncmp(argv[index], help_str, strlen(help_str))) { + printf("utest.h - the single file unit testing solution for C/C++!\n" + "Command line Options:\n" + " --help Show this message and exit.\n" + " --filter= Filter the test cases to run (EG. MyTest*.a " + "would run MyTestCase.a but not MyTestCase.b).\n" + " --output= Output an xunit XML file to the file " + "specified in .\n"); + goto cleanup; + } else if (0 == + utest_strncmp(argv[index], filter_str, strlen(filter_str))) { + /* user wants to filter what test cases run! */ filter = argv[index] + strlen(filter_str); + } else if (0 == + utest_strncmp(argv[index], output_str, strlen(output_str))) { + utest_state.output = utest_fopen(argv[index] + strlen(output_str), "w+"); } } - for (index = 0; index < utest_state.testcases_length; index++) { - if (utest_should_filter_test(filter, utest_state.testcase_names[index])) { + for (index = 0; index < utest_state.tests_length; index++) { + if (utest_should_filter_test(filter, utest_state.tests[index].name)) { continue; } @@ -415,19 +652,39 @@ printf("\033[32m[==========]\033[0m Running %" UTEST_PRIu64 " test cases.\n", UTEST_CAST(uint64_t, ran_tests)); - for (index = 0; index < utest_state.testcases_length; index++) { + if (utest_state.output) { + fprintf(utest_state.output, "\n"); + fprintf(utest_state.output, + "\n", + UTEST_CAST(uint64_t, ran_tests)); + fprintf(utest_state.output, + "\n", + UTEST_CAST(uint64_t, ran_tests)); + } + + for (index = 0; index < utest_state.tests_length; index++) { int result = 0; int64_t ns = 0; - if (utest_should_filter_test(filter, utest_state.testcase_names[index])) { + if (utest_should_filter_test(filter, utest_state.tests[index].name)) { continue; } - printf("\033[32m[ RUN ]\033[0m %s\n", - utest_state.testcase_names[index]); + printf("\033[32m[ RUN ]\033[0m %s\n", utest_state.tests[index].name); + + if (utest_state.output) { + fprintf(utest_state.output, "", + utest_state.tests[index].name); + } + ns = utest_ns(); - utest_state.testcases[index](&result); + utest_state.tests[index].func(&result, utest_state.tests[index].index); ns = utest_ns() - ns; + + if (utest_state.output) { + fprintf(utest_state.output, "\n"); + } + if (0 != result) { const size_t failed_testcase_index = failed_testcases_length++; failed_testcases = UTEST_PTR_CAST( @@ -436,46 +693,66 @@ failed_testcases[failed_testcase_index] = index; failed++; printf("\033[31m[ FAILED ]\033[0m %s (%" UTEST_PRId64 "ns)\n", - utest_state.testcase_names[index], ns); + utest_state.tests[index].name, ns); } else { printf("\033[32m[ OK ]\033[0m %s (%" UTEST_PRId64 "ns)\n", - utest_state.testcase_names[index], ns); + utest_state.tests[index].name, ns); } } + printf("\033[32m[==========]\033[0m %" UTEST_PRIu64 " test cases ran.\n", - UTEST_CAST(uint64_t, ran_tests)); + ran_tests); printf("\033[32m[ PASSED ]\033[0m %" UTEST_PRIu64 " tests.\n", - UTEST_CAST(uint64_t, ran_tests - failed)); + ran_tests - failed); + if (0 != failed) { printf("\033[31m[ FAILED ]\033[0m %" UTEST_PRIu64 " tests, listed below:\n", - UTEST_CAST(uint64_t, failed)); + failed); for (index = 0; index < failed_testcases_length; index++) { printf("\033[31m[ FAILED ]\033[0m %s\n", - utest_state.testcase_names[failed_testcases[index]]); + utest_state.tests[failed_testcases[index]].name); } } + + if (utest_state.output) { + fprintf(utest_state.output, "\n\n"); + } + +cleanup: + for (index = 0; index < utest_state.tests_length; index++) { + free(UTEST_PTR_CAST(void *, utest_state.tests[index].name)); + } + free(UTEST_PTR_CAST(void *, failed_testcases)); - free(UTEST_PTR_CAST(void *, utest_state.testcases)); - free(UTEST_PTR_CAST(void *, utest_state.testcase_names)); + free(UTEST_PTR_CAST(void *, utest_state.tests)); + + if (utest_state.output) { + fclose(utest_state.output); + } + return UTEST_CAST(int, failed); } -// we need, in exactly one source file, define the global struct that will hold -// the data we need to run utest. This macro allows the user to declare the -// data without having to use the UTEST_MAIN macro, thus allowing them to write -// their own main() function. -#define UTEST_STATE() struct utest_state_s utest_state +/* + we need, in exactly one source file, define the global struct that will hold + the data we need to run utest. This macro allows the user to declare the + data without having to use the UTEST_MAIN macro, thus allowing them to write + their own main() function. +*/ +#define UTEST_STATE() struct utest_state_s utest_state = {0, 0, 0} -// define a main() function to call into utest.h and start executing tests! A -// user can optionally not use this macro, and instead define their own main() -// function and manually call utest_main. The user must, in exactly one source -// file, use the UTEST_STATE macro to declare a global struct variable that -// utest requires. +/* + define a main() function to call into utest.h and start executing tests! A + user can optionally not use this macro, and instead define their own main() + function and manually call utest_main. The user must, in exactly one source + file, use the UTEST_STATE macro to declare a global struct variable that + utest requires. +*/ #define UTEST_MAIN() \ UTEST_STATE(); \ int main(int argc, const char *const argv[]) { \ return utest_main(argc, argv); \ } -#endif // SHEREDOM_UTEST_H_INCLUDED +#endif /* SHEREDOM_UTEST_H_INCLUDED */