diff --git a/README.md b/README.md index 37c43bb..eeb6073 100644 --- a/README.md +++ b/README.md @@ -5,25 +5,26 @@ A simple one header solution to unit testing for C/C++. -## Usage ## +## Usage Just `#include "utest.h"` in your code! The current supported compilers are gcc, clang and msvc. -The current supported platforms are Linux, Mac OSX and Windows. +The current supported platforms are Linux, macOS and Windows. -## Command Line Options ## +## 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). +* `--list-tests` will list testnames, one per line. Output names can be passed to `--filter`. * `--output=` will output an xunit XML file with the test results (that Jenkins, travis-ci, and appveyor can parse for the test results). -## Design ## +## 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 @@ -37,7 +38,7 @@ [ PASSED ] 1 tests. ``` -## UTEST_MAIN ## +## UTEST_MAIN In one C or C++ file, you must call the macro UTEST_MAIN: @@ -64,7 +65,7 @@ } ``` -## Define a Testcase ## +## Define a Testcase To define a test case to run, you can do the following; @@ -80,7 +81,7 @@ case belongs to, the second being the name of the test. This allows tests to be grouped for convenience. -## Define a Fixtured Testcase ## +## 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. @@ -130,7 +131,7 @@ * You can use EXPECT_* and ASSERT_* macros within the body of both the fixture's setup and teardown macros. -## Define an Indexed Testcase ## +## Define an Indexed Testcase Sometimes you want to use the same fixture _and_ testcase repeatedly, but perhaps subtly change one variable within. This is where indexed testcases come @@ -172,7 +173,7 @@ number of times we should run the test case for that index. It must be a literal. -## Testing Macros ## +## Testing Macros Matching what googletest has, we provide two variants of each of the error checking conditions - ASSERTs and EXPECTs. If an ASSERT fails, the test case @@ -182,7 +183,7 @@ We currently provide the following macros to be used within UTESTs: -### ASSERT_TRUE(x) ### +### ASSERT_TRUE(x) Asserts that x evaluates to true (EG. non-zero). @@ -195,7 +196,7 @@ } ``` -### ASSERT_FALSE(x) ### +### ASSERT_FALSE(x) Asserts that x evaluates to false (EG. zero). @@ -207,7 +208,7 @@ } ``` -### ASSERT_EQ(x, y) ### +### ASSERT_EQ(x, y) Asserts that x and y are equal. @@ -223,7 +224,7 @@ } ``` -### ASSERT_NE(x, y) ### +### ASSERT_NE(x, y) Asserts that x and y are not equal. @@ -239,7 +240,7 @@ } ``` -### ASSERT_LT(x, y) ### +### ASSERT_LT(x, y) Asserts that x is less than y. @@ -255,7 +256,7 @@ } ``` -### ASSERT_LE(x, y) ### +### ASSERT_LE(x, y) Asserts that x is less than or equal to y. @@ -274,7 +275,7 @@ } ``` -### ASSERT_GT(x, y) ### +### ASSERT_GT(x, y) Asserts that x is greater than y. @@ -290,7 +291,7 @@ } ``` -### ASSERT_GE(x, y) ### +### ASSERT_GE(x, y) Asserts that x is greater than or equal to y. @@ -309,7 +310,7 @@ } ``` -### EXPECT_TRUE(x) ### +### EXPECT_TRUE(x) Expects that x evaluates to true (i.e. non-zero). @@ -322,7 +323,7 @@ } ``` -### EXPECT_FALSE(x) ### +### EXPECT_FALSE(x) Expects that x evaluates to false (i.e. zero). @@ -334,7 +335,7 @@ } ``` -### EXPECT_EQ(x, y) ### +### EXPECT_EQ(x, y) Expects that x and y are equal. @@ -350,7 +351,7 @@ } ``` -### EXPECT_NE(x, y) ### +### EXPECT_NE(x, y) Expects that x and y are not equal. @@ -366,7 +367,7 @@ } ``` -### EXPECT_LT(x, y) ### +### EXPECT_LT(x, y) Expects that x is less than y. @@ -382,7 +383,7 @@ } ``` -### EXPECT_LE(x, y) ### +### EXPECT_LE(x, y) Expects that x is less than or equal to y. @@ -401,7 +402,7 @@ } ``` -### EXPECT_GT(x, y) ### +### EXPECT_GT(x, y) Expects that x is greater than y. @@ -417,7 +418,7 @@ } ``` -### EXPECT_GT(x, y) ### +### EXPECT_GT(x, y) Expects that x is greater than or equal to y. @@ -436,7 +437,12 @@ } ``` -## License ## +## Types Supported for Checks + +The library supports asserting on any builtin integer, floating-point, or +pointer type. + +## License This is free and unencumbered software released into the public domain. diff --git a/README.md b/README.md index 37c43bb..eeb6073 100644 --- a/README.md +++ b/README.md @@ -5,25 +5,26 @@ A simple one header solution to unit testing for C/C++. -## Usage ## +## Usage Just `#include "utest.h"` in your code! The current supported compilers are gcc, clang and msvc. -The current supported platforms are Linux, Mac OSX and Windows. +The current supported platforms are Linux, macOS and Windows. -## Command Line Options ## +## 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). +* `--list-tests` will list testnames, one per line. Output names can be passed to `--filter`. * `--output=` will output an xunit XML file with the test results (that Jenkins, travis-ci, and appveyor can parse for the test results). -## Design ## +## 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 @@ -37,7 +38,7 @@ [ PASSED ] 1 tests. ``` -## UTEST_MAIN ## +## UTEST_MAIN In one C or C++ file, you must call the macro UTEST_MAIN: @@ -64,7 +65,7 @@ } ``` -## Define a Testcase ## +## Define a Testcase To define a test case to run, you can do the following; @@ -80,7 +81,7 @@ case belongs to, the second being the name of the test. This allows tests to be grouped for convenience. -## Define a Fixtured Testcase ## +## 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. @@ -130,7 +131,7 @@ * You can use EXPECT_* and ASSERT_* macros within the body of both the fixture's setup and teardown macros. -## Define an Indexed Testcase ## +## Define an Indexed Testcase Sometimes you want to use the same fixture _and_ testcase repeatedly, but perhaps subtly change one variable within. This is where indexed testcases come @@ -172,7 +173,7 @@ number of times we should run the test case for that index. It must be a literal. -## Testing Macros ## +## Testing Macros Matching what googletest has, we provide two variants of each of the error checking conditions - ASSERTs and EXPECTs. If an ASSERT fails, the test case @@ -182,7 +183,7 @@ We currently provide the following macros to be used within UTESTs: -### ASSERT_TRUE(x) ### +### ASSERT_TRUE(x) Asserts that x evaluates to true (EG. non-zero). @@ -195,7 +196,7 @@ } ``` -### ASSERT_FALSE(x) ### +### ASSERT_FALSE(x) Asserts that x evaluates to false (EG. zero). @@ -207,7 +208,7 @@ } ``` -### ASSERT_EQ(x, y) ### +### ASSERT_EQ(x, y) Asserts that x and y are equal. @@ -223,7 +224,7 @@ } ``` -### ASSERT_NE(x, y) ### +### ASSERT_NE(x, y) Asserts that x and y are not equal. @@ -239,7 +240,7 @@ } ``` -### ASSERT_LT(x, y) ### +### ASSERT_LT(x, y) Asserts that x is less than y. @@ -255,7 +256,7 @@ } ``` -### ASSERT_LE(x, y) ### +### ASSERT_LE(x, y) Asserts that x is less than or equal to y. @@ -274,7 +275,7 @@ } ``` -### ASSERT_GT(x, y) ### +### ASSERT_GT(x, y) Asserts that x is greater than y. @@ -290,7 +291,7 @@ } ``` -### ASSERT_GE(x, y) ### +### ASSERT_GE(x, y) Asserts that x is greater than or equal to y. @@ -309,7 +310,7 @@ } ``` -### EXPECT_TRUE(x) ### +### EXPECT_TRUE(x) Expects that x evaluates to true (i.e. non-zero). @@ -322,7 +323,7 @@ } ``` -### EXPECT_FALSE(x) ### +### EXPECT_FALSE(x) Expects that x evaluates to false (i.e. zero). @@ -334,7 +335,7 @@ } ``` -### EXPECT_EQ(x, y) ### +### EXPECT_EQ(x, y) Expects that x and y are equal. @@ -350,7 +351,7 @@ } ``` -### EXPECT_NE(x, y) ### +### EXPECT_NE(x, y) Expects that x and y are not equal. @@ -366,7 +367,7 @@ } ``` -### EXPECT_LT(x, y) ### +### EXPECT_LT(x, y) Expects that x is less than y. @@ -382,7 +383,7 @@ } ``` -### EXPECT_LE(x, y) ### +### EXPECT_LE(x, y) Expects that x is less than or equal to y. @@ -401,7 +402,7 @@ } ``` -### EXPECT_GT(x, y) ### +### EXPECT_GT(x, y) Expects that x is greater than y. @@ -417,7 +418,7 @@ } ``` -### EXPECT_GT(x, y) ### +### EXPECT_GT(x, y) Expects that x is greater than or equal to y. @@ -436,7 +437,12 @@ } ``` -## License ## +## Types Supported for Checks + +The library supports asserting on any builtin integer, floating-point, or +pointer type. + +## License This is free and unencumbered software released into the public domain. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a634c04..e137a37 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -82,7 +82,7 @@ ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(test11.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11 -Wno-c++98-compat" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(test11.cpp PROPERTIES diff --git a/README.md b/README.md index 37c43bb..eeb6073 100644 --- a/README.md +++ b/README.md @@ -5,25 +5,26 @@ A simple one header solution to unit testing for C/C++. -## Usage ## +## Usage Just `#include "utest.h"` in your code! The current supported compilers are gcc, clang and msvc. -The current supported platforms are Linux, Mac OSX and Windows. +The current supported platforms are Linux, macOS and Windows. -## Command Line Options ## +## 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). +* `--list-tests` will list testnames, one per line. Output names can be passed to `--filter`. * `--output=` will output an xunit XML file with the test results (that Jenkins, travis-ci, and appveyor can parse for the test results). -## Design ## +## 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 @@ -37,7 +38,7 @@ [ PASSED ] 1 tests. ``` -## UTEST_MAIN ## +## UTEST_MAIN In one C or C++ file, you must call the macro UTEST_MAIN: @@ -64,7 +65,7 @@ } ``` -## Define a Testcase ## +## Define a Testcase To define a test case to run, you can do the following; @@ -80,7 +81,7 @@ case belongs to, the second being the name of the test. This allows tests to be grouped for convenience. -## Define a Fixtured Testcase ## +## 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. @@ -130,7 +131,7 @@ * You can use EXPECT_* and ASSERT_* macros within the body of both the fixture's setup and teardown macros. -## Define an Indexed Testcase ## +## Define an Indexed Testcase Sometimes you want to use the same fixture _and_ testcase repeatedly, but perhaps subtly change one variable within. This is where indexed testcases come @@ -172,7 +173,7 @@ number of times we should run the test case for that index. It must be a literal. -## Testing Macros ## +## Testing Macros Matching what googletest has, we provide two variants of each of the error checking conditions - ASSERTs and EXPECTs. If an ASSERT fails, the test case @@ -182,7 +183,7 @@ We currently provide the following macros to be used within UTESTs: -### ASSERT_TRUE(x) ### +### ASSERT_TRUE(x) Asserts that x evaluates to true (EG. non-zero). @@ -195,7 +196,7 @@ } ``` -### ASSERT_FALSE(x) ### +### ASSERT_FALSE(x) Asserts that x evaluates to false (EG. zero). @@ -207,7 +208,7 @@ } ``` -### ASSERT_EQ(x, y) ### +### ASSERT_EQ(x, y) Asserts that x and y are equal. @@ -223,7 +224,7 @@ } ``` -### ASSERT_NE(x, y) ### +### ASSERT_NE(x, y) Asserts that x and y are not equal. @@ -239,7 +240,7 @@ } ``` -### ASSERT_LT(x, y) ### +### ASSERT_LT(x, y) Asserts that x is less than y. @@ -255,7 +256,7 @@ } ``` -### ASSERT_LE(x, y) ### +### ASSERT_LE(x, y) Asserts that x is less than or equal to y. @@ -274,7 +275,7 @@ } ``` -### ASSERT_GT(x, y) ### +### ASSERT_GT(x, y) Asserts that x is greater than y. @@ -290,7 +291,7 @@ } ``` -### ASSERT_GE(x, y) ### +### ASSERT_GE(x, y) Asserts that x is greater than or equal to y. @@ -309,7 +310,7 @@ } ``` -### EXPECT_TRUE(x) ### +### EXPECT_TRUE(x) Expects that x evaluates to true (i.e. non-zero). @@ -322,7 +323,7 @@ } ``` -### EXPECT_FALSE(x) ### +### EXPECT_FALSE(x) Expects that x evaluates to false (i.e. zero). @@ -334,7 +335,7 @@ } ``` -### EXPECT_EQ(x, y) ### +### EXPECT_EQ(x, y) Expects that x and y are equal. @@ -350,7 +351,7 @@ } ``` -### EXPECT_NE(x, y) ### +### EXPECT_NE(x, y) Expects that x and y are not equal. @@ -366,7 +367,7 @@ } ``` -### EXPECT_LT(x, y) ### +### EXPECT_LT(x, y) Expects that x is less than y. @@ -382,7 +383,7 @@ } ``` -### EXPECT_LE(x, y) ### +### EXPECT_LE(x, y) Expects that x is less than or equal to y. @@ -401,7 +402,7 @@ } ``` -### EXPECT_GT(x, y) ### +### EXPECT_GT(x, y) Expects that x is greater than y. @@ -417,7 +418,7 @@ } ``` -### EXPECT_GT(x, y) ### +### EXPECT_GT(x, y) Expects that x is greater than or equal to y. @@ -436,7 +437,12 @@ } ``` -## License ## +## Types Supported for Checks + +The library supports asserting on any builtin integer, floating-point, or +pointer type. + +## License This is free and unencumbered software released into the public domain. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a634c04..e137a37 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -82,7 +82,7 @@ ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(test11.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11 -Wno-c++98-compat" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(test11.cpp PROPERTIES diff --git a/test/test.c b/test/test.c index 511c11d..9426c1e 100644 --- a/test/test.c +++ b/test/test.c @@ -120,27 +120,117 @@ }; UTEST_I_SETUP(MyTestI) { - ASSERT_EQ(0, utest_fixture->foo); - ASSERT_EQ(0, utest_fixture->bar); + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); utest_fixture->foo = 42; utest_fixture->bar = utest_index; } UTEST_I_TEARDOWN(MyTestI) { - ASSERT_EQ(13, utest_fixture->foo); + ASSERT_EQ(13u, utest_fixture->foo); ASSERT_EQ(utest_index, utest_fixture->bar); } UTEST_I(MyTestI, c, 2) { - ASSERT_GT(2, utest_fixture->bar); + ASSERT_GT(2u, utest_fixture->bar); utest_fixture->foo = 13; } UTEST_I(MyTestI, c2, 128) { - ASSERT_GT(128, utest_fixture->bar); + ASSERT_GT(128u, utest_fixture->bar); utest_fixture->foo = 13; } +UTEST(c, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Char) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +UTEST(c, VoidPtr) { + void *foo = 0; + EXPECT_NE(foo, (char *)foo + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(c, Array) { EXPECT_NE(data, data + 1); } #ifdef __linux__ #include diff --git a/README.md b/README.md index 37c43bb..eeb6073 100644 --- a/README.md +++ b/README.md @@ -5,25 +5,26 @@ A simple one header solution to unit testing for C/C++. -## Usage ## +## Usage Just `#include "utest.h"` in your code! The current supported compilers are gcc, clang and msvc. -The current supported platforms are Linux, Mac OSX and Windows. +The current supported platforms are Linux, macOS and Windows. -## Command Line Options ## +## 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). +* `--list-tests` will list testnames, one per line. Output names can be passed to `--filter`. * `--output=` will output an xunit XML file with the test results (that Jenkins, travis-ci, and appveyor can parse for the test results). -## Design ## +## 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 @@ -37,7 +38,7 @@ [ PASSED ] 1 tests. ``` -## UTEST_MAIN ## +## UTEST_MAIN In one C or C++ file, you must call the macro UTEST_MAIN: @@ -64,7 +65,7 @@ } ``` -## Define a Testcase ## +## Define a Testcase To define a test case to run, you can do the following; @@ -80,7 +81,7 @@ case belongs to, the second being the name of the test. This allows tests to be grouped for convenience. -## Define a Fixtured Testcase ## +## 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. @@ -130,7 +131,7 @@ * You can use EXPECT_* and ASSERT_* macros within the body of both the fixture's setup and teardown macros. -## Define an Indexed Testcase ## +## Define an Indexed Testcase Sometimes you want to use the same fixture _and_ testcase repeatedly, but perhaps subtly change one variable within. This is where indexed testcases come @@ -172,7 +173,7 @@ number of times we should run the test case for that index. It must be a literal. -## Testing Macros ## +## Testing Macros Matching what googletest has, we provide two variants of each of the error checking conditions - ASSERTs and EXPECTs. If an ASSERT fails, the test case @@ -182,7 +183,7 @@ We currently provide the following macros to be used within UTESTs: -### ASSERT_TRUE(x) ### +### ASSERT_TRUE(x) Asserts that x evaluates to true (EG. non-zero). @@ -195,7 +196,7 @@ } ``` -### ASSERT_FALSE(x) ### +### ASSERT_FALSE(x) Asserts that x evaluates to false (EG. zero). @@ -207,7 +208,7 @@ } ``` -### ASSERT_EQ(x, y) ### +### ASSERT_EQ(x, y) Asserts that x and y are equal. @@ -223,7 +224,7 @@ } ``` -### ASSERT_NE(x, y) ### +### ASSERT_NE(x, y) Asserts that x and y are not equal. @@ -239,7 +240,7 @@ } ``` -### ASSERT_LT(x, y) ### +### ASSERT_LT(x, y) Asserts that x is less than y. @@ -255,7 +256,7 @@ } ``` -### ASSERT_LE(x, y) ### +### ASSERT_LE(x, y) Asserts that x is less than or equal to y. @@ -274,7 +275,7 @@ } ``` -### ASSERT_GT(x, y) ### +### ASSERT_GT(x, y) Asserts that x is greater than y. @@ -290,7 +291,7 @@ } ``` -### ASSERT_GE(x, y) ### +### ASSERT_GE(x, y) Asserts that x is greater than or equal to y. @@ -309,7 +310,7 @@ } ``` -### EXPECT_TRUE(x) ### +### EXPECT_TRUE(x) Expects that x evaluates to true (i.e. non-zero). @@ -322,7 +323,7 @@ } ``` -### EXPECT_FALSE(x) ### +### EXPECT_FALSE(x) Expects that x evaluates to false (i.e. zero). @@ -334,7 +335,7 @@ } ``` -### EXPECT_EQ(x, y) ### +### EXPECT_EQ(x, y) Expects that x and y are equal. @@ -350,7 +351,7 @@ } ``` -### EXPECT_NE(x, y) ### +### EXPECT_NE(x, y) Expects that x and y are not equal. @@ -366,7 +367,7 @@ } ``` -### EXPECT_LT(x, y) ### +### EXPECT_LT(x, y) Expects that x is less than y. @@ -382,7 +383,7 @@ } ``` -### EXPECT_LE(x, y) ### +### EXPECT_LE(x, y) Expects that x is less than or equal to y. @@ -401,7 +402,7 @@ } ``` -### EXPECT_GT(x, y) ### +### EXPECT_GT(x, y) Expects that x is greater than y. @@ -417,7 +418,7 @@ } ``` -### EXPECT_GT(x, y) ### +### EXPECT_GT(x, y) Expects that x is greater than or equal to y. @@ -436,7 +437,12 @@ } ``` -## License ## +## Types Supported for Checks + +The library supports asserting on any builtin integer, floating-point, or +pointer type. + +## License This is free and unencumbered software released into the public domain. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a634c04..e137a37 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -82,7 +82,7 @@ ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(test11.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11 -Wno-c++98-compat" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(test11.cpp PROPERTIES diff --git a/test/test.c b/test/test.c index 511c11d..9426c1e 100644 --- a/test/test.c +++ b/test/test.c @@ -120,27 +120,117 @@ }; UTEST_I_SETUP(MyTestI) { - ASSERT_EQ(0, utest_fixture->foo); - ASSERT_EQ(0, utest_fixture->bar); + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); utest_fixture->foo = 42; utest_fixture->bar = utest_index; } UTEST_I_TEARDOWN(MyTestI) { - ASSERT_EQ(13, utest_fixture->foo); + ASSERT_EQ(13u, utest_fixture->foo); ASSERT_EQ(utest_index, utest_fixture->bar); } UTEST_I(MyTestI, c, 2) { - ASSERT_GT(2, utest_fixture->bar); + ASSERT_GT(2u, utest_fixture->bar); utest_fixture->foo = 13; } UTEST_I(MyTestI, c2, 128) { - ASSERT_GT(128, utest_fixture->bar); + ASSERT_GT(128u, utest_fixture->bar); utest_fixture->foo = 13; } +UTEST(c, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Char) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +UTEST(c, VoidPtr) { + void *foo = 0; + EXPECT_NE(foo, (char *)foo + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(c, Array) { EXPECT_NE(data, data + 1); } #ifdef __linux__ #include diff --git a/test/test.cpp b/test/test.cpp index 1903ad2..bd837d1 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -117,23 +117,109 @@ }; UTEST_I_SETUP(MyTestI) { - ASSERT_EQ(0, utest_fixture->foo); - ASSERT_EQ(0, utest_fixture->bar); + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); utest_fixture->foo = 42; utest_fixture->bar = utest_index; } UTEST_I_TEARDOWN(MyTestI) { - ASSERT_EQ(13, utest_fixture->foo); + ASSERT_EQ(13u, utest_fixture->foo); ASSERT_EQ(utest_index, utest_fixture->bar); } UTEST_I(MyTestI, cpp_1, 2) { - ASSERT_GT(2, utest_fixture->bar); + ASSERT_GT(2u, utest_fixture->bar); utest_fixture->foo = 13; } UTEST_I(MyTestI, cpp_2, 128) { - ASSERT_GT(128, utest_fixture->bar); + ASSERT_GT(128u, utest_fixture->bar); utest_fixture->foo = 13; } + +UTEST(cpp, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Char) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(cpp, Array) { EXPECT_NE(data, data + 1); } diff --git a/README.md b/README.md index 37c43bb..eeb6073 100644 --- a/README.md +++ b/README.md @@ -5,25 +5,26 @@ A simple one header solution to unit testing for C/C++. -## Usage ## +## Usage Just `#include "utest.h"` in your code! The current supported compilers are gcc, clang and msvc. -The current supported platforms are Linux, Mac OSX and Windows. +The current supported platforms are Linux, macOS and Windows. -## Command Line Options ## +## 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). +* `--list-tests` will list testnames, one per line. Output names can be passed to `--filter`. * `--output=` will output an xunit XML file with the test results (that Jenkins, travis-ci, and appveyor can parse for the test results). -## Design ## +## 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 @@ -37,7 +38,7 @@ [ PASSED ] 1 tests. ``` -## UTEST_MAIN ## +## UTEST_MAIN In one C or C++ file, you must call the macro UTEST_MAIN: @@ -64,7 +65,7 @@ } ``` -## Define a Testcase ## +## Define a Testcase To define a test case to run, you can do the following; @@ -80,7 +81,7 @@ case belongs to, the second being the name of the test. This allows tests to be grouped for convenience. -## Define a Fixtured Testcase ## +## 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. @@ -130,7 +131,7 @@ * You can use EXPECT_* and ASSERT_* macros within the body of both the fixture's setup and teardown macros. -## Define an Indexed Testcase ## +## Define an Indexed Testcase Sometimes you want to use the same fixture _and_ testcase repeatedly, but perhaps subtly change one variable within. This is where indexed testcases come @@ -172,7 +173,7 @@ number of times we should run the test case for that index. It must be a literal. -## Testing Macros ## +## Testing Macros Matching what googletest has, we provide two variants of each of the error checking conditions - ASSERTs and EXPECTs. If an ASSERT fails, the test case @@ -182,7 +183,7 @@ We currently provide the following macros to be used within UTESTs: -### ASSERT_TRUE(x) ### +### ASSERT_TRUE(x) Asserts that x evaluates to true (EG. non-zero). @@ -195,7 +196,7 @@ } ``` -### ASSERT_FALSE(x) ### +### ASSERT_FALSE(x) Asserts that x evaluates to false (EG. zero). @@ -207,7 +208,7 @@ } ``` -### ASSERT_EQ(x, y) ### +### ASSERT_EQ(x, y) Asserts that x and y are equal. @@ -223,7 +224,7 @@ } ``` -### ASSERT_NE(x, y) ### +### ASSERT_NE(x, y) Asserts that x and y are not equal. @@ -239,7 +240,7 @@ } ``` -### ASSERT_LT(x, y) ### +### ASSERT_LT(x, y) Asserts that x is less than y. @@ -255,7 +256,7 @@ } ``` -### ASSERT_LE(x, y) ### +### ASSERT_LE(x, y) Asserts that x is less than or equal to y. @@ -274,7 +275,7 @@ } ``` -### ASSERT_GT(x, y) ### +### ASSERT_GT(x, y) Asserts that x is greater than y. @@ -290,7 +291,7 @@ } ``` -### ASSERT_GE(x, y) ### +### ASSERT_GE(x, y) Asserts that x is greater than or equal to y. @@ -309,7 +310,7 @@ } ``` -### EXPECT_TRUE(x) ### +### EXPECT_TRUE(x) Expects that x evaluates to true (i.e. non-zero). @@ -322,7 +323,7 @@ } ``` -### EXPECT_FALSE(x) ### +### EXPECT_FALSE(x) Expects that x evaluates to false (i.e. zero). @@ -334,7 +335,7 @@ } ``` -### EXPECT_EQ(x, y) ### +### EXPECT_EQ(x, y) Expects that x and y are equal. @@ -350,7 +351,7 @@ } ``` -### EXPECT_NE(x, y) ### +### EXPECT_NE(x, y) Expects that x and y are not equal. @@ -366,7 +367,7 @@ } ``` -### EXPECT_LT(x, y) ### +### EXPECT_LT(x, y) Expects that x is less than y. @@ -382,7 +383,7 @@ } ``` -### EXPECT_LE(x, y) ### +### EXPECT_LE(x, y) Expects that x is less than or equal to y. @@ -401,7 +402,7 @@ } ``` -### EXPECT_GT(x, y) ### +### EXPECT_GT(x, y) Expects that x is greater than y. @@ -417,7 +418,7 @@ } ``` -### EXPECT_GT(x, y) ### +### EXPECT_GT(x, y) Expects that x is greater than or equal to y. @@ -436,7 +437,12 @@ } ``` -## License ## +## Types Supported for Checks + +The library supports asserting on any builtin integer, floating-point, or +pointer type. + +## License This is free and unencumbered software released into the public domain. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a634c04..e137a37 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -82,7 +82,7 @@ ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(test11.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11 -Wno-c++98-compat" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(test11.cpp PROPERTIES diff --git a/test/test.c b/test/test.c index 511c11d..9426c1e 100644 --- a/test/test.c +++ b/test/test.c @@ -120,27 +120,117 @@ }; UTEST_I_SETUP(MyTestI) { - ASSERT_EQ(0, utest_fixture->foo); - ASSERT_EQ(0, utest_fixture->bar); + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); utest_fixture->foo = 42; utest_fixture->bar = utest_index; } UTEST_I_TEARDOWN(MyTestI) { - ASSERT_EQ(13, utest_fixture->foo); + ASSERT_EQ(13u, utest_fixture->foo); ASSERT_EQ(utest_index, utest_fixture->bar); } UTEST_I(MyTestI, c, 2) { - ASSERT_GT(2, utest_fixture->bar); + ASSERT_GT(2u, utest_fixture->bar); utest_fixture->foo = 13; } UTEST_I(MyTestI, c2, 128) { - ASSERT_GT(128, utest_fixture->bar); + ASSERT_GT(128u, utest_fixture->bar); utest_fixture->foo = 13; } +UTEST(c, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Char) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +UTEST(c, VoidPtr) { + void *foo = 0; + EXPECT_NE(foo, (char *)foo + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(c, Array) { EXPECT_NE(data, data + 1); } #ifdef __linux__ #include diff --git a/test/test.cpp b/test/test.cpp index 1903ad2..bd837d1 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -117,23 +117,109 @@ }; UTEST_I_SETUP(MyTestI) { - ASSERT_EQ(0, utest_fixture->foo); - ASSERT_EQ(0, utest_fixture->bar); + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); utest_fixture->foo = 42; utest_fixture->bar = utest_index; } UTEST_I_TEARDOWN(MyTestI) { - ASSERT_EQ(13, utest_fixture->foo); + ASSERT_EQ(13u, utest_fixture->foo); ASSERT_EQ(utest_index, utest_fixture->bar); } UTEST_I(MyTestI, cpp_1, 2) { - ASSERT_GT(2, utest_fixture->bar); + ASSERT_GT(2u, utest_fixture->bar); utest_fixture->foo = 13; } UTEST_I(MyTestI, cpp_2, 128) { - ASSERT_GT(128, utest_fixture->bar); + ASSERT_GT(128u, utest_fixture->bar); utest_fixture->foo = 13; } + +UTEST(cpp, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Char) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(cpp, Array) { EXPECT_NE(data, data + 1); } diff --git a/test/test11.cpp b/test/test11.cpp index d9228ae..fa548f8 100644 --- a/test/test11.cpp +++ b/test/test11.cpp @@ -117,23 +117,114 @@ }; UTEST_I_SETUP(MyTestI) { - ASSERT_EQ(0, utest_fixture->foo); - ASSERT_EQ(0, utest_fixture->bar); + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); utest_fixture->foo = 42; utest_fixture->bar = utest_index; } UTEST_I_TEARDOWN(MyTestI) { - ASSERT_EQ(13, utest_fixture->foo); + ASSERT_EQ(13u, utest_fixture->foo); ASSERT_EQ(utest_index, utest_fixture->bar); } UTEST_I(MyTestI, cpp11_1, 2) { - ASSERT_GT(2, utest_fixture->bar); + ASSERT_GT(2u, utest_fixture->bar); utest_fixture->foo = 13; } UTEST_I(MyTestI, cpp11_2, 128) { - ASSERT_GT(128, utest_fixture->bar); + ASSERT_GT(128u, utest_fixture->bar); utest_fixture->foo = 13; } + +UTEST(cpp11, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Char) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +UTEST(cpp11, VoidPtr) { + void *foo = reinterpret_cast(0); + EXPECT_NE(foo, static_cast(foo) + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(cpp11, Array) { EXPECT_NE(data, data + 1); } diff --git a/README.md b/README.md index 37c43bb..eeb6073 100644 --- a/README.md +++ b/README.md @@ -5,25 +5,26 @@ A simple one header solution to unit testing for C/C++. -## Usage ## +## Usage Just `#include "utest.h"` in your code! The current supported compilers are gcc, clang and msvc. -The current supported platforms are Linux, Mac OSX and Windows. +The current supported platforms are Linux, macOS and Windows. -## Command Line Options ## +## 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). +* `--list-tests` will list testnames, one per line. Output names can be passed to `--filter`. * `--output=` will output an xunit XML file with the test results (that Jenkins, travis-ci, and appveyor can parse for the test results). -## Design ## +## 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 @@ -37,7 +38,7 @@ [ PASSED ] 1 tests. ``` -## UTEST_MAIN ## +## UTEST_MAIN In one C or C++ file, you must call the macro UTEST_MAIN: @@ -64,7 +65,7 @@ } ``` -## Define a Testcase ## +## Define a Testcase To define a test case to run, you can do the following; @@ -80,7 +81,7 @@ case belongs to, the second being the name of the test. This allows tests to be grouped for convenience. -## Define a Fixtured Testcase ## +## 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. @@ -130,7 +131,7 @@ * You can use EXPECT_* and ASSERT_* macros within the body of both the fixture's setup and teardown macros. -## Define an Indexed Testcase ## +## Define an Indexed Testcase Sometimes you want to use the same fixture _and_ testcase repeatedly, but perhaps subtly change one variable within. This is where indexed testcases come @@ -172,7 +173,7 @@ number of times we should run the test case for that index. It must be a literal. -## Testing Macros ## +## Testing Macros Matching what googletest has, we provide two variants of each of the error checking conditions - ASSERTs and EXPECTs. If an ASSERT fails, the test case @@ -182,7 +183,7 @@ We currently provide the following macros to be used within UTESTs: -### ASSERT_TRUE(x) ### +### ASSERT_TRUE(x) Asserts that x evaluates to true (EG. non-zero). @@ -195,7 +196,7 @@ } ``` -### ASSERT_FALSE(x) ### +### ASSERT_FALSE(x) Asserts that x evaluates to false (EG. zero). @@ -207,7 +208,7 @@ } ``` -### ASSERT_EQ(x, y) ### +### ASSERT_EQ(x, y) Asserts that x and y are equal. @@ -223,7 +224,7 @@ } ``` -### ASSERT_NE(x, y) ### +### ASSERT_NE(x, y) Asserts that x and y are not equal. @@ -239,7 +240,7 @@ } ``` -### ASSERT_LT(x, y) ### +### ASSERT_LT(x, y) Asserts that x is less than y. @@ -255,7 +256,7 @@ } ``` -### ASSERT_LE(x, y) ### +### ASSERT_LE(x, y) Asserts that x is less than or equal to y. @@ -274,7 +275,7 @@ } ``` -### ASSERT_GT(x, y) ### +### ASSERT_GT(x, y) Asserts that x is greater than y. @@ -290,7 +291,7 @@ } ``` -### ASSERT_GE(x, y) ### +### ASSERT_GE(x, y) Asserts that x is greater than or equal to y. @@ -309,7 +310,7 @@ } ``` -### EXPECT_TRUE(x) ### +### EXPECT_TRUE(x) Expects that x evaluates to true (i.e. non-zero). @@ -322,7 +323,7 @@ } ``` -### EXPECT_FALSE(x) ### +### EXPECT_FALSE(x) Expects that x evaluates to false (i.e. zero). @@ -334,7 +335,7 @@ } ``` -### EXPECT_EQ(x, y) ### +### EXPECT_EQ(x, y) Expects that x and y are equal. @@ -350,7 +351,7 @@ } ``` -### EXPECT_NE(x, y) ### +### EXPECT_NE(x, y) Expects that x and y are not equal. @@ -366,7 +367,7 @@ } ``` -### EXPECT_LT(x, y) ### +### EXPECT_LT(x, y) Expects that x is less than y. @@ -382,7 +383,7 @@ } ``` -### EXPECT_LE(x, y) ### +### EXPECT_LE(x, y) Expects that x is less than or equal to y. @@ -401,7 +402,7 @@ } ``` -### EXPECT_GT(x, y) ### +### EXPECT_GT(x, y) Expects that x is greater than y. @@ -417,7 +418,7 @@ } ``` -### EXPECT_GT(x, y) ### +### EXPECT_GT(x, y) Expects that x is greater than or equal to y. @@ -436,7 +437,12 @@ } ``` -## License ## +## Types Supported for Checks + +The library supports asserting on any builtin integer, floating-point, or +pointer type. + +## License This is free and unencumbered software released into the public domain. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a634c04..e137a37 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -82,7 +82,7 @@ ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(test11.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11 -Wno-c++98-compat" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(test11.cpp PROPERTIES diff --git a/test/test.c b/test/test.c index 511c11d..9426c1e 100644 --- a/test/test.c +++ b/test/test.c @@ -120,27 +120,117 @@ }; UTEST_I_SETUP(MyTestI) { - ASSERT_EQ(0, utest_fixture->foo); - ASSERT_EQ(0, utest_fixture->bar); + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); utest_fixture->foo = 42; utest_fixture->bar = utest_index; } UTEST_I_TEARDOWN(MyTestI) { - ASSERT_EQ(13, utest_fixture->foo); + ASSERT_EQ(13u, utest_fixture->foo); ASSERT_EQ(utest_index, utest_fixture->bar); } UTEST_I(MyTestI, c, 2) { - ASSERT_GT(2, utest_fixture->bar); + ASSERT_GT(2u, utest_fixture->bar); utest_fixture->foo = 13; } UTEST_I(MyTestI, c2, 128) { - ASSERT_GT(128, utest_fixture->bar); + ASSERT_GT(128u, utest_fixture->bar); utest_fixture->foo = 13; } +UTEST(c, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Char) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(c, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +UTEST(c, VoidPtr) { + void *foo = 0; + EXPECT_NE(foo, (char *)foo + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(c, Array) { EXPECT_NE(data, data + 1); } #ifdef __linux__ #include diff --git a/test/test.cpp b/test/test.cpp index 1903ad2..bd837d1 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -117,23 +117,109 @@ }; UTEST_I_SETUP(MyTestI) { - ASSERT_EQ(0, utest_fixture->foo); - ASSERT_EQ(0, utest_fixture->bar); + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); utest_fixture->foo = 42; utest_fixture->bar = utest_index; } UTEST_I_TEARDOWN(MyTestI) { - ASSERT_EQ(13, utest_fixture->foo); + ASSERT_EQ(13u, utest_fixture->foo); ASSERT_EQ(utest_index, utest_fixture->bar); } UTEST_I(MyTestI, cpp_1, 2) { - ASSERT_GT(2, utest_fixture->bar); + ASSERT_GT(2u, utest_fixture->bar); utest_fixture->foo = 13; } UTEST_I(MyTestI, cpp_2, 128) { - ASSERT_GT(128, utest_fixture->bar); + ASSERT_GT(128u, utest_fixture->bar); utest_fixture->foo = 13; } + +UTEST(cpp, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Char) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(cpp, Array) { EXPECT_NE(data, data + 1); } diff --git a/test/test11.cpp b/test/test11.cpp index d9228ae..fa548f8 100644 --- a/test/test11.cpp +++ b/test/test11.cpp @@ -117,23 +117,114 @@ }; UTEST_I_SETUP(MyTestI) { - ASSERT_EQ(0, utest_fixture->foo); - ASSERT_EQ(0, utest_fixture->bar); + ASSERT_EQ(0u, utest_fixture->foo); + ASSERT_EQ(0u, utest_fixture->bar); utest_fixture->foo = 42; utest_fixture->bar = utest_index; } UTEST_I_TEARDOWN(MyTestI) { - ASSERT_EQ(13, utest_fixture->foo); + ASSERT_EQ(13u, utest_fixture->foo); ASSERT_EQ(utest_index, utest_fixture->bar); } UTEST_I(MyTestI, cpp11_1, 2) { - ASSERT_GT(2, utest_fixture->bar); + ASSERT_GT(2u, utest_fixture->bar); utest_fixture->foo = 13; } UTEST_I(MyTestI, cpp11_2, 128) { - ASSERT_GT(128, utest_fixture->bar); + ASSERT_GT(128u, utest_fixture->bar); utest_fixture->foo = 13; } + +UTEST(cpp11, Float) { + float a = 1; + float b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Double) { + double a = 1; + double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, LongDouble) { + long double a = 1; + long double b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Char) { + signed char a = 1; + signed char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, UChar) { + unsigned char a = 1; + unsigned char b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Short) { + short a = 1; + short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, UShort) { + unsigned short a = 1; + unsigned short b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Int) { + int a = 1; + int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, UInt) { + unsigned int a = 1; + unsigned int b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Long) { + long a = 1; + long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, ULong) { + unsigned long a = 1; + unsigned long b = 2; + EXPECT_NE(a, b); + ASSERT_NE(a, b); +} + +UTEST(cpp11, Ptr) { + char foo = 42; + EXPECT_NE(&foo, &foo + 1); +} + +UTEST(cpp11, VoidPtr) { + void *foo = reinterpret_cast(0); + EXPECT_NE(foo, static_cast(foo) + 1); +} + +static const int data[4] = {42, 13, 6, -53}; + +UTEST(cpp11, Array) { EXPECT_NE(data, data + 1); } diff --git a/utest.h b/utest.h index a62eb9e..96c7476 100644 --- a/utest.h +++ b/utest.h @@ -50,10 +50,12 @@ #endif #if defined(_MSC_VER) -typedef __int64 int64_t; -typedef unsigned __int64 uint64_t; +typedef __int64 utest_int64_t; +typedef unsigned __int64 utest_uint64_t; #else #include +typedef int64_t utest_int64_t; +typedef uint64_t utest_uint64_t; #endif #include @@ -172,13 +174,13 @@ #define UTEST_COLOUR_OUTPUT() (isatty(STDOUT_FILENO)) #endif -static UTEST_INLINE int64_t utest_ns(void) { +static UTEST_INLINE utest_int64_t utest_ns(void) { #ifdef _MSC_VER LARGE_INTEGER counter; LARGE_INTEGER frequency; QueryPerformanceCounter(&counter); QueryPerformanceFrequency(&frequency); - return UTEST_CAST(int64_t, + return UTEST_CAST(utest_int64_t, (counter.QuadPart * 1000000000) / frequency.QuadPart); #elif defined(__linux) struct timespec ts; @@ -188,9 +190,9 @@ #else syscall(SYS_clock_gettime, cid, &ts); #endif - return UTEST_CAST(int64_t, ts.tv_sec) * 1000 * 1000 * 1000 + ts.tv_nsec; + return UTEST_CAST(utest_int64_t, ts.tv_sec) * 1000 * 1000 * 1000 + ts.tv_nsec; #elif __APPLE__ - return UTEST_CAST(int64_t, mach_absolute_time()); + return UTEST_CAST(utest_int64_t, mach_absolute_time()); #endif } @@ -295,6 +297,11 @@ UTEST_PRINTF("%lu", i); } +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(const void *p); +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(const void *p) { + UTEST_PRINTF("%p", p); +} + /* long long is a c++11 extension */ @@ -347,9 +354,23 @@ #endif #if defined(__cplusplus) && (__cplusplus >= 201103L) -#define UTEST_TYPEOF(x) decltype(x) +#define UTEST_AUTO(x) auto +#elif !defined(__cplusplus) + +#if defined(__clang__) +/* clang-format off */ +/* had to disable clang-format here because it malforms the pragmas */ +#define UTEST_AUTO(x) \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wgnu-auto-type\"") __auto_type \ + _Pragma("clang diagnostic pop") +/* clang-format on */ #else -#define UTEST_TYPEOF(x) typeof(x) +#define UTEST_AUTO(x) __auto_type +#endif + +#else +#define UTEST_AUTO(x) typeof(x + 0) #endif #if defined(__clang__) @@ -358,10 +379,12 @@ _Pragma("clang diagnostic push") \ _Pragma("clang diagnostic ignored \"-Wlanguage-extension-token\"") \ _Pragma("clang diagnostic ignored \"-Wc++98-compat-pedantic\"") \ - UTEST_TYPEOF(y) xEval = (x); \ - UTEST_TYPEOF(y) yEval = (y); \ - _Pragma("clang diagnostic pop") if (!((xEval)cond(yEval))) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + _Pragma("clang diagnostic ignored \"-Wfloat-equal\"") \ + UTEST_AUTO(x) xEval = (x); \ + UTEST_AUTO(y) yEval = (y); \ + if (!((xEval)cond(yEval))) { \ + _Pragma("clang diagnostic pop") \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : "); \ utest_type_printer(xEval); \ UTEST_PRINTF("\n"); \ @@ -374,8 +397,8 @@ #elif defined(__GNUC__) #define UTEST_EXPECT(x, y, cond) \ { \ - UTEST_TYPEOF(y) xEval = (x); \ - UTEST_TYPEOF(y) yEval = (y); \ + UTEST_AUTO(x) xEval = (x); \ + UTEST_AUTO(y) yEval = (y); \ if (!((xEval)cond(yEval))) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : "); \ @@ -442,10 +465,12 @@ _Pragma("clang diagnostic push") \ _Pragma("clang diagnostic ignored \"-Wlanguage-extension-token\"") \ _Pragma("clang diagnostic ignored \"-Wc++98-compat-pedantic\"") \ - UTEST_TYPEOF(y) xEval = (x); \ - UTEST_TYPEOF(y) yEval = (y); \ - _Pragma("clang diagnostic pop") if (!((xEval)cond(yEval))) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + _Pragma("clang diagnostic ignored \"-Wfloat-equal\"") \ + UTEST_AUTO(x) xEval = (x); \ + UTEST_AUTO(y) yEval = (y); \ + if (!((xEval)cond(yEval))) { \ + _Pragma("clang diagnostic pop") \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : "); \ utest_type_printer(xEval); \ UTEST_PRINTF("\n"); \ @@ -459,8 +484,8 @@ #elif defined(__GNUC__) #define UTEST_ASSERT(x, y, cond) \ { \ - UTEST_TYPEOF(y) xEval = (x); \ - UTEST_TYPEOF(y) yEval = (y); \ + UTEST_AUTO(x) xEval = (x); \ + UTEST_AUTO(y) yEval = (y); \ if (!((xEval)cond(yEval))) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : "); \ @@ -618,7 +643,7 @@ } \ UTEST_INITIALIZER(utest_register_##FIXTURE##_##NAME##_##INDEX) { \ size_t i; \ - uint64_t iUp; \ + utest_uint64_t iUp; \ for (i = 0; i < (INDEX); i++) { \ const size_t index = utest_state.tests_length++; \ const char *name_part = #FIXTURE "." #NAME; \ @@ -632,7 +657,7 @@ 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); \ + iUp = UTEST_CAST(utest_uint64_t, i); \ UTEST_SNPRINTF(name, name_size, "%s/%" UTEST_PRIu64, name_part, iUp); \ } \ } \ @@ -737,12 +762,12 @@ UTEST_WEAK int utest_main(int argc, const char *const argv[]); UTEST_WEAK int utest_main(int argc, const char *const argv[]) { - uint64_t failed = 0; + utest_uint64_t failed = 0; size_t index = 0; size_t *failed_testcases = UTEST_NULL; size_t failed_testcases_length = 0; const char *filter = UTEST_NULL; - uint64_t ran_tests = 0; + utest_uint64_t ran_tests = 0; enum colours { RESET, GREEN, RED }; @@ -798,21 +823,21 @@ } printf("%s[==========]%s Running %" UTEST_PRIu64 " test cases.\n", - colours[GREEN], colours[RESET], UTEST_CAST(uint64_t, ran_tests)); + colours[GREEN], colours[RESET], UTEST_CAST(utest_uint64_t, ran_tests)); if (utest_state.output) { fprintf(utest_state.output, "\n"); fprintf(utest_state.output, "\n", - UTEST_CAST(uint64_t, ran_tests)); + UTEST_CAST(utest_uint64_t, ran_tests)); fprintf(utest_state.output, "\n", - UTEST_CAST(uint64_t, ran_tests)); + UTEST_CAST(utest_uint64_t, ran_tests)); } for (index = 0; index < utest_state.tests_length; index++) { int result = 0; - int64_t ns = 0; + utest_int64_t ns = 0; if (utest_should_filter_test(filter, utest_state.tests[index].name)) { continue;