diff --git a/README.md b/README.md index 2db2732..37c43bb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ -# utest.h # +# 🧪 utest.h [![Build status](https://ci.appveyor.com/api/projects/status/i2u3a0pw4pxprrcv?svg=true)](https://ci.appveyor.com/project/sheredom/utest-h) - [![Build Status](https://travis-ci.org/sheredom/utest.h.svg)](https://travis-ci.org/sheredom/utest.h) A simple one header solution to unit testing for C/C++. diff --git a/README.md b/README.md index 2db2732..37c43bb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ -# utest.h # +# 🧪 utest.h [![Build status](https://ci.appveyor.com/api/projects/status/i2u3a0pw4pxprrcv?svg=true)](https://ci.appveyor.com/project/sheredom/utest-h) - [![Build Status](https://travis-ci.org/sheredom/utest.h.svg)](https://travis-ci.org/sheredom/utest.h) A simple one header solution to unit testing for C/C++. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d9db1b6..a634c04 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,7 +24,7 @@ # For more information, please refer to project(utest) -cmake_minimum_required(VERSION 2.8.7) +cmake_minimum_required(VERSION 3.1.3) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../) @@ -33,10 +33,18 @@ main.c test.c test.cpp + test11.cpp stdint_include.c + type_printers.c + type_printers.cpp ) if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") + list(FIND CMAKE_C_COMPILE_FEATURES c_std_11 IDX) + if (${IDX} GREATER -1) + set_source_files_properties(type_printers.c PROPERTIES + COMPILE_FLAGS "-std=gnu11") + endif() set_source_files_properties(main.c test.c PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu89" ) @@ -54,11 +62,11 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set_source_files_properties(test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Werror" + COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu++98" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=gnu++98" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(test.cpp PROPERTIES @@ -67,3 +75,19 @@ else() message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") endif() + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror -std=c++11" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "/Wall /WX /wd4514 /wd5045" + ) +else() + message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") +endif() diff --git a/README.md b/README.md index 2db2732..37c43bb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ -# utest.h # +# 🧪 utest.h [![Build status](https://ci.appveyor.com/api/projects/status/i2u3a0pw4pxprrcv?svg=true)](https://ci.appveyor.com/project/sheredom/utest-h) - [![Build Status](https://travis-ci.org/sheredom/utest.h.svg)](https://travis-ci.org/sheredom/utest.h) A simple one header solution to unit testing for C/C++. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d9db1b6..a634c04 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,7 +24,7 @@ # For more information, please refer to project(utest) -cmake_minimum_required(VERSION 2.8.7) +cmake_minimum_required(VERSION 3.1.3) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../) @@ -33,10 +33,18 @@ main.c test.c test.cpp + test11.cpp stdint_include.c + type_printers.c + type_printers.cpp ) if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") + list(FIND CMAKE_C_COMPILE_FEATURES c_std_11 IDX) + if (${IDX} GREATER -1) + set_source_files_properties(type_printers.c PROPERTIES + COMPILE_FLAGS "-std=gnu11") + endif() set_source_files_properties(main.c test.c PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu89" ) @@ -54,11 +62,11 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set_source_files_properties(test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Werror" + COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu++98" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=gnu++98" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(test.cpp PROPERTIES @@ -67,3 +75,19 @@ else() message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") endif() + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror -std=c++11" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "/Wall /WX /wd4514 /wd5045" + ) +else() + message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") +endif() diff --git a/test/test.cpp b/test/test.cpp index 61c1b68..1903ad2 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -52,9 +52,9 @@ ASSERT_GE(2, 1); } -UTEST(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } +UTEST(cpp, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } -UTEST(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } +UTEST(cpp, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } UTEST(cpp, EXPECT_TRUE) { EXPECT_TRUE(1); } @@ -78,9 +78,9 @@ EXPECT_GE(2, 1); } -UTEST(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } +UTEST(cpp, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } -UTEST(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } +UTEST(cpp, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } UTEST(cpp, no_double_eval) { int i = 0; @@ -101,12 +101,12 @@ ASSERT_EQ(13, utest_fixture->foo); } -UTEST_F(MyTestF, cpp) { +UTEST_F(MyTestF, cpp_1) { ASSERT_EQ(42, utest_fixture->foo); utest_fixture->foo = 13; } -UTEST_F(MyTestF, cpp2) { +UTEST_F(MyTestF, cpp_2) { ASSERT_EQ(42, utest_fixture->foo); utest_fixture->foo = 13; } @@ -128,12 +128,12 @@ ASSERT_EQ(utest_index, utest_fixture->bar); } -UTEST_I(MyTestI, cpp, 2) { +UTEST_I(MyTestI, cpp_1, 2) { ASSERT_GT(2, utest_fixture->bar); utest_fixture->foo = 13; } -UTEST_I(MyTestI, cpp2, 128) { +UTEST_I(MyTestI, cpp_2, 128) { ASSERT_GT(128, utest_fixture->bar); utest_fixture->foo = 13; } diff --git a/README.md b/README.md index 2db2732..37c43bb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ -# utest.h # +# 🧪 utest.h [![Build status](https://ci.appveyor.com/api/projects/status/i2u3a0pw4pxprrcv?svg=true)](https://ci.appveyor.com/project/sheredom/utest-h) - [![Build Status](https://travis-ci.org/sheredom/utest.h.svg)](https://travis-ci.org/sheredom/utest.h) A simple one header solution to unit testing for C/C++. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d9db1b6..a634c04 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,7 +24,7 @@ # For more information, please refer to project(utest) -cmake_minimum_required(VERSION 2.8.7) +cmake_minimum_required(VERSION 3.1.3) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../) @@ -33,10 +33,18 @@ main.c test.c test.cpp + test11.cpp stdint_include.c + type_printers.c + type_printers.cpp ) if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") + list(FIND CMAKE_C_COMPILE_FEATURES c_std_11 IDX) + if (${IDX} GREATER -1) + set_source_files_properties(type_printers.c PROPERTIES + COMPILE_FLAGS "-std=gnu11") + endif() set_source_files_properties(main.c test.c PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu89" ) @@ -54,11 +62,11 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set_source_files_properties(test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Werror" + COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu++98" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=gnu++98" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(test.cpp PROPERTIES @@ -67,3 +75,19 @@ else() message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") endif() + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror -std=c++11" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "/Wall /WX /wd4514 /wd5045" + ) +else() + message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") +endif() diff --git a/test/test.cpp b/test/test.cpp index 61c1b68..1903ad2 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -52,9 +52,9 @@ ASSERT_GE(2, 1); } -UTEST(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } +UTEST(cpp, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } -UTEST(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } +UTEST(cpp, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } UTEST(cpp, EXPECT_TRUE) { EXPECT_TRUE(1); } @@ -78,9 +78,9 @@ EXPECT_GE(2, 1); } -UTEST(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } +UTEST(cpp, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } -UTEST(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } +UTEST(cpp, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } UTEST(cpp, no_double_eval) { int i = 0; @@ -101,12 +101,12 @@ ASSERT_EQ(13, utest_fixture->foo); } -UTEST_F(MyTestF, cpp) { +UTEST_F(MyTestF, cpp_1) { ASSERT_EQ(42, utest_fixture->foo); utest_fixture->foo = 13; } -UTEST_F(MyTestF, cpp2) { +UTEST_F(MyTestF, cpp_2) { ASSERT_EQ(42, utest_fixture->foo); utest_fixture->foo = 13; } @@ -128,12 +128,12 @@ ASSERT_EQ(utest_index, utest_fixture->bar); } -UTEST_I(MyTestI, cpp, 2) { +UTEST_I(MyTestI, cpp_1, 2) { ASSERT_GT(2, utest_fixture->bar); utest_fixture->foo = 13; } -UTEST_I(MyTestI, cpp2, 128) { +UTEST_I(MyTestI, cpp_2, 128) { ASSERT_GT(128, utest_fixture->bar); utest_fixture->foo = 13; } diff --git a/test/test11.cpp b/test/test11.cpp new file mode 100644 index 0000000..d9228ae --- /dev/null +++ b/test/test11.cpp @@ -0,0 +1,139 @@ +// 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 +// disable 'conditional expression is constant' - our examples below use this! +#pragma warning(disable : 4127) +#endif + +UTEST(cpp11, ASSERT_TRUE) { ASSERT_TRUE(1); } + +UTEST(cpp11, ASSERT_FALSE) { ASSERT_FALSE(0); } + +UTEST(cpp11, ASSERT_EQ) { ASSERT_EQ(1, 1); } + +UTEST(cpp11, ASSERT_NE) { ASSERT_NE(1, 2); } + +UTEST(cpp11, ASSERT_LT) { ASSERT_LT(1, 2); } + +UTEST(cpp11, ASSERT_LE) { + ASSERT_LE(1, 1); + ASSERT_LE(1, 2); +} + +UTEST(cpp11, ASSERT_GT) { ASSERT_GT(2, 1); } + +UTEST(cpp11, ASSERT_GE) { + ASSERT_GE(1, 1); + ASSERT_GE(2, 1); +} + +UTEST(cpp11, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } + +UTEST(cpp11, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } + +UTEST(cpp11, EXPECT_TRUE) { EXPECT_TRUE(1); } + +UTEST(cpp11, EXPECT_FALSE) { EXPECT_FALSE(0); } + +UTEST(cpp11, EXPECT_EQ) { EXPECT_EQ(1, 1); } + +UTEST(cpp11, EXPECT_NE) { EXPECT_NE(1, 2); } + +UTEST(cpp11, EXPECT_LT) { EXPECT_LT(1, 2); } + +UTEST(cpp11, EXPECT_LE) { + EXPECT_LE(1, 1); + EXPECT_LE(1, 2); +} + +UTEST(cpp11, EXPECT_GT) { EXPECT_GT(2, 1); } + +UTEST(cpp11, EXPECT_GE) { + EXPECT_GE(1, 1); + EXPECT_GE(2, 1); +} + +UTEST(cpp11, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } + +UTEST(cpp11, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +UTEST(cpp11, no_double_eval) { + int i = 0; + ASSERT_EQ(i++, 0); + ASSERT_EQ(i, 1); +} + +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, cpp11_1) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, cpp11_2) { + 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, cpp11_1, 2) { + ASSERT_GT(2, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, cpp11_2, 128) { + ASSERT_GT(128, utest_fixture->bar); + utest_fixture->foo = 13; +} diff --git a/README.md b/README.md index 2db2732..37c43bb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ -# utest.h # +# 🧪 utest.h [![Build status](https://ci.appveyor.com/api/projects/status/i2u3a0pw4pxprrcv?svg=true)](https://ci.appveyor.com/project/sheredom/utest-h) - [![Build Status](https://travis-ci.org/sheredom/utest.h.svg)](https://travis-ci.org/sheredom/utest.h) A simple one header solution to unit testing for C/C++. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d9db1b6..a634c04 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,7 +24,7 @@ # For more information, please refer to project(utest) -cmake_minimum_required(VERSION 2.8.7) +cmake_minimum_required(VERSION 3.1.3) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../) @@ -33,10 +33,18 @@ main.c test.c test.cpp + test11.cpp stdint_include.c + type_printers.c + type_printers.cpp ) if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") + list(FIND CMAKE_C_COMPILE_FEATURES c_std_11 IDX) + if (${IDX} GREATER -1) + set_source_files_properties(type_printers.c PROPERTIES + COMPILE_FLAGS "-std=gnu11") + endif() set_source_files_properties(main.c test.c PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu89" ) @@ -54,11 +62,11 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set_source_files_properties(test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Werror" + COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu++98" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=gnu++98" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(test.cpp PROPERTIES @@ -67,3 +75,19 @@ else() message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") endif() + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror -std=c++11" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "/Wall /WX /wd4514 /wd5045" + ) +else() + message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") +endif() diff --git a/test/test.cpp b/test/test.cpp index 61c1b68..1903ad2 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -52,9 +52,9 @@ ASSERT_GE(2, 1); } -UTEST(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } +UTEST(cpp, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } -UTEST(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } +UTEST(cpp, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } UTEST(cpp, EXPECT_TRUE) { EXPECT_TRUE(1); } @@ -78,9 +78,9 @@ EXPECT_GE(2, 1); } -UTEST(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } +UTEST(cpp, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } -UTEST(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } +UTEST(cpp, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } UTEST(cpp, no_double_eval) { int i = 0; @@ -101,12 +101,12 @@ ASSERT_EQ(13, utest_fixture->foo); } -UTEST_F(MyTestF, cpp) { +UTEST_F(MyTestF, cpp_1) { ASSERT_EQ(42, utest_fixture->foo); utest_fixture->foo = 13; } -UTEST_F(MyTestF, cpp2) { +UTEST_F(MyTestF, cpp_2) { ASSERT_EQ(42, utest_fixture->foo); utest_fixture->foo = 13; } @@ -128,12 +128,12 @@ ASSERT_EQ(utest_index, utest_fixture->bar); } -UTEST_I(MyTestI, cpp, 2) { +UTEST_I(MyTestI, cpp_1, 2) { ASSERT_GT(2, utest_fixture->bar); utest_fixture->foo = 13; } -UTEST_I(MyTestI, cpp2, 128) { +UTEST_I(MyTestI, cpp_2, 128) { ASSERT_GT(128, utest_fixture->bar); utest_fixture->foo = 13; } diff --git a/test/test11.cpp b/test/test11.cpp new file mode 100644 index 0000000..d9228ae --- /dev/null +++ b/test/test11.cpp @@ -0,0 +1,139 @@ +// 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 +// disable 'conditional expression is constant' - our examples below use this! +#pragma warning(disable : 4127) +#endif + +UTEST(cpp11, ASSERT_TRUE) { ASSERT_TRUE(1); } + +UTEST(cpp11, ASSERT_FALSE) { ASSERT_FALSE(0); } + +UTEST(cpp11, ASSERT_EQ) { ASSERT_EQ(1, 1); } + +UTEST(cpp11, ASSERT_NE) { ASSERT_NE(1, 2); } + +UTEST(cpp11, ASSERT_LT) { ASSERT_LT(1, 2); } + +UTEST(cpp11, ASSERT_LE) { + ASSERT_LE(1, 1); + ASSERT_LE(1, 2); +} + +UTEST(cpp11, ASSERT_GT) { ASSERT_GT(2, 1); } + +UTEST(cpp11, ASSERT_GE) { + ASSERT_GE(1, 1); + ASSERT_GE(2, 1); +} + +UTEST(cpp11, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } + +UTEST(cpp11, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } + +UTEST(cpp11, EXPECT_TRUE) { EXPECT_TRUE(1); } + +UTEST(cpp11, EXPECT_FALSE) { EXPECT_FALSE(0); } + +UTEST(cpp11, EXPECT_EQ) { EXPECT_EQ(1, 1); } + +UTEST(cpp11, EXPECT_NE) { EXPECT_NE(1, 2); } + +UTEST(cpp11, EXPECT_LT) { EXPECT_LT(1, 2); } + +UTEST(cpp11, EXPECT_LE) { + EXPECT_LE(1, 1); + EXPECT_LE(1, 2); +} + +UTEST(cpp11, EXPECT_GT) { EXPECT_GT(2, 1); } + +UTEST(cpp11, EXPECT_GE) { + EXPECT_GE(1, 1); + EXPECT_GE(2, 1); +} + +UTEST(cpp11, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } + +UTEST(cpp11, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +UTEST(cpp11, no_double_eval) { + int i = 0; + ASSERT_EQ(i++, 0); + ASSERT_EQ(i, 1); +} + +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, cpp11_1) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, cpp11_2) { + 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, cpp11_1, 2) { + ASSERT_GT(2, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, cpp11_2, 128) { + ASSERT_GT(128, utest_fixture->bar); + utest_fixture->foo = 13; +} diff --git a/test/type_printers.c b/test/type_printers.c new file mode 100644 index 0000000..a2edcf7 --- /dev/null +++ b/test/type_printers.c @@ -0,0 +1,2 @@ +#include "utest.h" +#include "type_printers.inc" diff --git a/README.md b/README.md index 2db2732..37c43bb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ -# utest.h # +# 🧪 utest.h [![Build status](https://ci.appveyor.com/api/projects/status/i2u3a0pw4pxprrcv?svg=true)](https://ci.appveyor.com/project/sheredom/utest-h) - [![Build Status](https://travis-ci.org/sheredom/utest.h.svg)](https://travis-ci.org/sheredom/utest.h) A simple one header solution to unit testing for C/C++. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d9db1b6..a634c04 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,7 +24,7 @@ # For more information, please refer to project(utest) -cmake_minimum_required(VERSION 2.8.7) +cmake_minimum_required(VERSION 3.1.3) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../) @@ -33,10 +33,18 @@ main.c test.c test.cpp + test11.cpp stdint_include.c + type_printers.c + type_printers.cpp ) if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") + list(FIND CMAKE_C_COMPILE_FEATURES c_std_11 IDX) + if (${IDX} GREATER -1) + set_source_files_properties(type_printers.c PROPERTIES + COMPILE_FLAGS "-std=gnu11") + endif() set_source_files_properties(main.c test.c PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu89" ) @@ -54,11 +62,11 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set_source_files_properties(test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Werror" + COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu++98" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=gnu++98" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(test.cpp PROPERTIES @@ -67,3 +75,19 @@ else() message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") endif() + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror -std=c++11" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "/Wall /WX /wd4514 /wd5045" + ) +else() + message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") +endif() diff --git a/test/test.cpp b/test/test.cpp index 61c1b68..1903ad2 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -52,9 +52,9 @@ ASSERT_GE(2, 1); } -UTEST(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } +UTEST(cpp, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } -UTEST(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } +UTEST(cpp, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } UTEST(cpp, EXPECT_TRUE) { EXPECT_TRUE(1); } @@ -78,9 +78,9 @@ EXPECT_GE(2, 1); } -UTEST(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } +UTEST(cpp, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } -UTEST(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } +UTEST(cpp, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } UTEST(cpp, no_double_eval) { int i = 0; @@ -101,12 +101,12 @@ ASSERT_EQ(13, utest_fixture->foo); } -UTEST_F(MyTestF, cpp) { +UTEST_F(MyTestF, cpp_1) { ASSERT_EQ(42, utest_fixture->foo); utest_fixture->foo = 13; } -UTEST_F(MyTestF, cpp2) { +UTEST_F(MyTestF, cpp_2) { ASSERT_EQ(42, utest_fixture->foo); utest_fixture->foo = 13; } @@ -128,12 +128,12 @@ ASSERT_EQ(utest_index, utest_fixture->bar); } -UTEST_I(MyTestI, cpp, 2) { +UTEST_I(MyTestI, cpp_1, 2) { ASSERT_GT(2, utest_fixture->bar); utest_fixture->foo = 13; } -UTEST_I(MyTestI, cpp2, 128) { +UTEST_I(MyTestI, cpp_2, 128) { ASSERT_GT(128, utest_fixture->bar); utest_fixture->foo = 13; } diff --git a/test/test11.cpp b/test/test11.cpp new file mode 100644 index 0000000..d9228ae --- /dev/null +++ b/test/test11.cpp @@ -0,0 +1,139 @@ +// 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 +// disable 'conditional expression is constant' - our examples below use this! +#pragma warning(disable : 4127) +#endif + +UTEST(cpp11, ASSERT_TRUE) { ASSERT_TRUE(1); } + +UTEST(cpp11, ASSERT_FALSE) { ASSERT_FALSE(0); } + +UTEST(cpp11, ASSERT_EQ) { ASSERT_EQ(1, 1); } + +UTEST(cpp11, ASSERT_NE) { ASSERT_NE(1, 2); } + +UTEST(cpp11, ASSERT_LT) { ASSERT_LT(1, 2); } + +UTEST(cpp11, ASSERT_LE) { + ASSERT_LE(1, 1); + ASSERT_LE(1, 2); +} + +UTEST(cpp11, ASSERT_GT) { ASSERT_GT(2, 1); } + +UTEST(cpp11, ASSERT_GE) { + ASSERT_GE(1, 1); + ASSERT_GE(2, 1); +} + +UTEST(cpp11, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } + +UTEST(cpp11, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } + +UTEST(cpp11, EXPECT_TRUE) { EXPECT_TRUE(1); } + +UTEST(cpp11, EXPECT_FALSE) { EXPECT_FALSE(0); } + +UTEST(cpp11, EXPECT_EQ) { EXPECT_EQ(1, 1); } + +UTEST(cpp11, EXPECT_NE) { EXPECT_NE(1, 2); } + +UTEST(cpp11, EXPECT_LT) { EXPECT_LT(1, 2); } + +UTEST(cpp11, EXPECT_LE) { + EXPECT_LE(1, 1); + EXPECT_LE(1, 2); +} + +UTEST(cpp11, EXPECT_GT) { EXPECT_GT(2, 1); } + +UTEST(cpp11, EXPECT_GE) { + EXPECT_GE(1, 1); + EXPECT_GE(2, 1); +} + +UTEST(cpp11, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } + +UTEST(cpp11, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +UTEST(cpp11, no_double_eval) { + int i = 0; + ASSERT_EQ(i++, 0); + ASSERT_EQ(i, 1); +} + +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, cpp11_1) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, cpp11_2) { + 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, cpp11_1, 2) { + ASSERT_GT(2, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, cpp11_2, 128) { + ASSERT_GT(128, utest_fixture->bar); + utest_fixture->foo = 13; +} diff --git a/test/type_printers.c b/test/type_printers.c new file mode 100644 index 0000000..a2edcf7 --- /dev/null +++ b/test/type_printers.c @@ -0,0 +1,2 @@ +#include "utest.h" +#include "type_printers.inc" diff --git a/test/type_printers.cpp b/test/type_printers.cpp new file mode 100644 index 0000000..a2edcf7 --- /dev/null +++ b/test/type_printers.cpp @@ -0,0 +1,2 @@ +#include "utest.h" +#include "type_printers.inc" diff --git a/README.md b/README.md index 2db2732..37c43bb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ -# utest.h # +# 🧪 utest.h [![Build status](https://ci.appveyor.com/api/projects/status/i2u3a0pw4pxprrcv?svg=true)](https://ci.appveyor.com/project/sheredom/utest-h) - [![Build Status](https://travis-ci.org/sheredom/utest.h.svg)](https://travis-ci.org/sheredom/utest.h) A simple one header solution to unit testing for C/C++. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d9db1b6..a634c04 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,7 +24,7 @@ # For more information, please refer to project(utest) -cmake_minimum_required(VERSION 2.8.7) +cmake_minimum_required(VERSION 3.1.3) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../) @@ -33,10 +33,18 @@ main.c test.c test.cpp + test11.cpp stdint_include.c + type_printers.c + type_printers.cpp ) if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") + list(FIND CMAKE_C_COMPILE_FEATURES c_std_11 IDX) + if (${IDX} GREATER -1) + set_source_files_properties(type_printers.c PROPERTIES + COMPILE_FLAGS "-std=gnu11") + endif() set_source_files_properties(main.c test.c PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu89" ) @@ -54,11 +62,11 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set_source_files_properties(test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Werror" + COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu++98" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=gnu++98" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(test.cpp PROPERTIES @@ -67,3 +75,19 @@ else() message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") endif() + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror -std=c++11" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "/Wall /WX /wd4514 /wd5045" + ) +else() + message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") +endif() diff --git a/test/test.cpp b/test/test.cpp index 61c1b68..1903ad2 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -52,9 +52,9 @@ ASSERT_GE(2, 1); } -UTEST(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } +UTEST(cpp, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } -UTEST(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } +UTEST(cpp, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } UTEST(cpp, EXPECT_TRUE) { EXPECT_TRUE(1); } @@ -78,9 +78,9 @@ EXPECT_GE(2, 1); } -UTEST(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } +UTEST(cpp, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } -UTEST(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } +UTEST(cpp, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } UTEST(cpp, no_double_eval) { int i = 0; @@ -101,12 +101,12 @@ ASSERT_EQ(13, utest_fixture->foo); } -UTEST_F(MyTestF, cpp) { +UTEST_F(MyTestF, cpp_1) { ASSERT_EQ(42, utest_fixture->foo); utest_fixture->foo = 13; } -UTEST_F(MyTestF, cpp2) { +UTEST_F(MyTestF, cpp_2) { ASSERT_EQ(42, utest_fixture->foo); utest_fixture->foo = 13; } @@ -128,12 +128,12 @@ ASSERT_EQ(utest_index, utest_fixture->bar); } -UTEST_I(MyTestI, cpp, 2) { +UTEST_I(MyTestI, cpp_1, 2) { ASSERT_GT(2, utest_fixture->bar); utest_fixture->foo = 13; } -UTEST_I(MyTestI, cpp2, 128) { +UTEST_I(MyTestI, cpp_2, 128) { ASSERT_GT(128, utest_fixture->bar); utest_fixture->foo = 13; } diff --git a/test/test11.cpp b/test/test11.cpp new file mode 100644 index 0000000..d9228ae --- /dev/null +++ b/test/test11.cpp @@ -0,0 +1,139 @@ +// 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 +// disable 'conditional expression is constant' - our examples below use this! +#pragma warning(disable : 4127) +#endif + +UTEST(cpp11, ASSERT_TRUE) { ASSERT_TRUE(1); } + +UTEST(cpp11, ASSERT_FALSE) { ASSERT_FALSE(0); } + +UTEST(cpp11, ASSERT_EQ) { ASSERT_EQ(1, 1); } + +UTEST(cpp11, ASSERT_NE) { ASSERT_NE(1, 2); } + +UTEST(cpp11, ASSERT_LT) { ASSERT_LT(1, 2); } + +UTEST(cpp11, ASSERT_LE) { + ASSERT_LE(1, 1); + ASSERT_LE(1, 2); +} + +UTEST(cpp11, ASSERT_GT) { ASSERT_GT(2, 1); } + +UTEST(cpp11, ASSERT_GE) { + ASSERT_GE(1, 1); + ASSERT_GE(2, 1); +} + +UTEST(cpp11, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } + +UTEST(cpp11, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } + +UTEST(cpp11, EXPECT_TRUE) { EXPECT_TRUE(1); } + +UTEST(cpp11, EXPECT_FALSE) { EXPECT_FALSE(0); } + +UTEST(cpp11, EXPECT_EQ) { EXPECT_EQ(1, 1); } + +UTEST(cpp11, EXPECT_NE) { EXPECT_NE(1, 2); } + +UTEST(cpp11, EXPECT_LT) { EXPECT_LT(1, 2); } + +UTEST(cpp11, EXPECT_LE) { + EXPECT_LE(1, 1); + EXPECT_LE(1, 2); +} + +UTEST(cpp11, EXPECT_GT) { EXPECT_GT(2, 1); } + +UTEST(cpp11, EXPECT_GE) { + EXPECT_GE(1, 1); + EXPECT_GE(2, 1); +} + +UTEST(cpp11, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } + +UTEST(cpp11, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +UTEST(cpp11, no_double_eval) { + int i = 0; + ASSERT_EQ(i++, 0); + ASSERT_EQ(i, 1); +} + +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, cpp11_1) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, cpp11_2) { + 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, cpp11_1, 2) { + ASSERT_GT(2, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, cpp11_2, 128) { + ASSERT_GT(128, utest_fixture->bar); + utest_fixture->foo = 13; +} diff --git a/test/type_printers.c b/test/type_printers.c new file mode 100644 index 0000000..a2edcf7 --- /dev/null +++ b/test/type_printers.c @@ -0,0 +1,2 @@ +#include "utest.h" +#include "type_printers.inc" diff --git a/test/type_printers.cpp b/test/type_printers.cpp new file mode 100644 index 0000000..a2edcf7 --- /dev/null +++ b/test/type_printers.cpp @@ -0,0 +1,2 @@ +#include "utest.h" +#include "type_printers.inc" diff --git a/test/type_printers.inc b/test/type_printers.inc new file mode 100644 index 0000000..c521657 --- /dev/null +++ b/test/type_printers.inc @@ -0,0 +1,54 @@ +#include + +#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L + +#ifdef __cplusplus +UTEST(type_printers, cpp) { +#elif defined(UTEST_OVERLOADABLE) +UTEST(type_printers, overloadable) { +#elif __STDC_VERSION__ +UTEST(type_printers, stdc) { +#else +#error +#endif + +#define TO_S_X(val) #val +#define S(val) TO_S_X(val) + FILE *old = utest_state.output; + FILE *out = tmpfile(); + ASSERT_TRUE(!!out); + utest_state.output = out; + + int i = INT_MIN; + long l = LONG_MIN; + long long ll = LLONG_MIN; + unsigned u = UINT_MAX; + unsigned long ul = ULONG_MAX; + unsigned long long ull = ULLONG_MAX; + float f = 0.f; + double d = 0.; + long double ld = 0.l; + utest_type_printer(i); + utest_type_printer(l); + utest_type_printer(ll); + utest_type_printer(u); + utest_type_printer(ul); + utest_type_printer(ull); + utest_type_printer(f); + utest_type_printer(d); + utest_type_printer(ld); + + char expected[1024] = {0}; + size_t expected_len = + UTEST_SNPRINTF(expected, sizeof expected - 1, "%d%ld%lld%u%lu%llu%f%f%Lf", + i, l, ll, u, ul, ull, f, d, ld); + fflush(out); + rewind(out); + char buf[1024] = {'\0'}; + size_t n = fread(buf, 1, sizeof buf, out); + fclose(out); + utest_state.output = old; + ASSERT_EQ(n, expected_len); + ASSERT_STREQ(buf, expected); +} +#endif diff --git a/README.md b/README.md index 2db2732..37c43bb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ -# utest.h # +# 🧪 utest.h [![Build status](https://ci.appveyor.com/api/projects/status/i2u3a0pw4pxprrcv?svg=true)](https://ci.appveyor.com/project/sheredom/utest-h) - [![Build Status](https://travis-ci.org/sheredom/utest.h.svg)](https://travis-ci.org/sheredom/utest.h) A simple one header solution to unit testing for C/C++. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d9db1b6..a634c04 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,7 +24,7 @@ # For more information, please refer to project(utest) -cmake_minimum_required(VERSION 2.8.7) +cmake_minimum_required(VERSION 3.1.3) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../) @@ -33,10 +33,18 @@ main.c test.c test.cpp + test11.cpp stdint_include.c + type_printers.c + type_printers.cpp ) if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") + list(FIND CMAKE_C_COMPILE_FEATURES c_std_11 IDX) + if (${IDX} GREATER -1) + set_source_files_properties(type_printers.c PROPERTIES + COMPILE_FLAGS "-std=gnu11") + endif() set_source_files_properties(main.c test.c PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu89" ) @@ -54,11 +62,11 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set_source_files_properties(test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Werror" + COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu++98" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(test.cpp PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=gnu++98" ) elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(test.cpp PROPERTIES @@ -67,3 +75,19 @@ else() message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") endif() + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Werror -std=c++11" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=c++11" + ) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + set_source_files_properties(test11.cpp PROPERTIES + COMPILE_FLAGS "/Wall /WX /wd4514 /wd5045" + ) +else() + message(WARNING "Unknown compiler '${CMAKE_C_COMPILER_ID}'!") +endif() diff --git a/test/test.cpp b/test/test.cpp index 61c1b68..1903ad2 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -52,9 +52,9 @@ ASSERT_GE(2, 1); } -UTEST(c, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } +UTEST(cpp, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } -UTEST(c, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } +UTEST(cpp, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } UTEST(cpp, EXPECT_TRUE) { EXPECT_TRUE(1); } @@ -78,9 +78,9 @@ EXPECT_GE(2, 1); } -UTEST(c, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } +UTEST(cpp, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } -UTEST(c, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } +UTEST(cpp, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } UTEST(cpp, no_double_eval) { int i = 0; @@ -101,12 +101,12 @@ ASSERT_EQ(13, utest_fixture->foo); } -UTEST_F(MyTestF, cpp) { +UTEST_F(MyTestF, cpp_1) { ASSERT_EQ(42, utest_fixture->foo); utest_fixture->foo = 13; } -UTEST_F(MyTestF, cpp2) { +UTEST_F(MyTestF, cpp_2) { ASSERT_EQ(42, utest_fixture->foo); utest_fixture->foo = 13; } @@ -128,12 +128,12 @@ ASSERT_EQ(utest_index, utest_fixture->bar); } -UTEST_I(MyTestI, cpp, 2) { +UTEST_I(MyTestI, cpp_1, 2) { ASSERT_GT(2, utest_fixture->bar); utest_fixture->foo = 13; } -UTEST_I(MyTestI, cpp2, 128) { +UTEST_I(MyTestI, cpp_2, 128) { ASSERT_GT(128, utest_fixture->bar); utest_fixture->foo = 13; } diff --git a/test/test11.cpp b/test/test11.cpp new file mode 100644 index 0000000..d9228ae --- /dev/null +++ b/test/test11.cpp @@ -0,0 +1,139 @@ +// 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 +// disable 'conditional expression is constant' - our examples below use this! +#pragma warning(disable : 4127) +#endif + +UTEST(cpp11, ASSERT_TRUE) { ASSERT_TRUE(1); } + +UTEST(cpp11, ASSERT_FALSE) { ASSERT_FALSE(0); } + +UTEST(cpp11, ASSERT_EQ) { ASSERT_EQ(1, 1); } + +UTEST(cpp11, ASSERT_NE) { ASSERT_NE(1, 2); } + +UTEST(cpp11, ASSERT_LT) { ASSERT_LT(1, 2); } + +UTEST(cpp11, ASSERT_LE) { + ASSERT_LE(1, 1); + ASSERT_LE(1, 2); +} + +UTEST(cpp11, ASSERT_GT) { ASSERT_GT(2, 1); } + +UTEST(cpp11, ASSERT_GE) { + ASSERT_GE(1, 1); + ASSERT_GE(2, 1); +} + +UTEST(cpp11, ASSERT_STREQ) { ASSERT_STREQ("foo", "foo"); } + +UTEST(cpp11, ASSERT_STRNE) { ASSERT_STRNE("foo", "bar"); } + +UTEST(cpp11, EXPECT_TRUE) { EXPECT_TRUE(1); } + +UTEST(cpp11, EXPECT_FALSE) { EXPECT_FALSE(0); } + +UTEST(cpp11, EXPECT_EQ) { EXPECT_EQ(1, 1); } + +UTEST(cpp11, EXPECT_NE) { EXPECT_NE(1, 2); } + +UTEST(cpp11, EXPECT_LT) { EXPECT_LT(1, 2); } + +UTEST(cpp11, EXPECT_LE) { + EXPECT_LE(1, 1); + EXPECT_LE(1, 2); +} + +UTEST(cpp11, EXPECT_GT) { EXPECT_GT(2, 1); } + +UTEST(cpp11, EXPECT_GE) { + EXPECT_GE(1, 1); + EXPECT_GE(2, 1); +} + +UTEST(cpp11, EXPECT_STREQ) { EXPECT_STREQ("foo", "foo"); } + +UTEST(cpp11, EXPECT_STRNE) { EXPECT_STRNE("foo", "bar"); } + +UTEST(cpp11, no_double_eval) { + int i = 0; + ASSERT_EQ(i++, 0); + ASSERT_EQ(i, 1); +} + +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, cpp11_1) { + ASSERT_EQ(42, utest_fixture->foo); + utest_fixture->foo = 13; +} + +UTEST_F(MyTestF, cpp11_2) { + 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, cpp11_1, 2) { + ASSERT_GT(2, utest_fixture->bar); + utest_fixture->foo = 13; +} + +UTEST_I(MyTestI, cpp11_2, 128) { + ASSERT_GT(128, utest_fixture->bar); + utest_fixture->foo = 13; +} diff --git a/test/type_printers.c b/test/type_printers.c new file mode 100644 index 0000000..a2edcf7 --- /dev/null +++ b/test/type_printers.c @@ -0,0 +1,2 @@ +#include "utest.h" +#include "type_printers.inc" diff --git a/test/type_printers.cpp b/test/type_printers.cpp new file mode 100644 index 0000000..a2edcf7 --- /dev/null +++ b/test/type_printers.cpp @@ -0,0 +1,2 @@ +#include "utest.h" +#include "type_printers.inc" diff --git a/test/type_printers.inc b/test/type_printers.inc new file mode 100644 index 0000000..c521657 --- /dev/null +++ b/test/type_printers.inc @@ -0,0 +1,54 @@ +#include + +#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L + +#ifdef __cplusplus +UTEST(type_printers, cpp) { +#elif defined(UTEST_OVERLOADABLE) +UTEST(type_printers, overloadable) { +#elif __STDC_VERSION__ +UTEST(type_printers, stdc) { +#else +#error +#endif + +#define TO_S_X(val) #val +#define S(val) TO_S_X(val) + FILE *old = utest_state.output; + FILE *out = tmpfile(); + ASSERT_TRUE(!!out); + utest_state.output = out; + + int i = INT_MIN; + long l = LONG_MIN; + long long ll = LLONG_MIN; + unsigned u = UINT_MAX; + unsigned long ul = ULONG_MAX; + unsigned long long ull = ULLONG_MAX; + float f = 0.f; + double d = 0.; + long double ld = 0.l; + utest_type_printer(i); + utest_type_printer(l); + utest_type_printer(ll); + utest_type_printer(u); + utest_type_printer(ul); + utest_type_printer(ull); + utest_type_printer(f); + utest_type_printer(d); + utest_type_printer(ld); + + char expected[1024] = {0}; + size_t expected_len = + UTEST_SNPRINTF(expected, sizeof expected - 1, "%d%ld%lld%u%lu%llu%f%f%Lf", + i, l, ll, u, ul, ull, f, d, ld); + fflush(out); + rewind(out); + char buf[1024] = {'\0'}; + size_t n = fread(buf, 1, sizeof buf, out); + fclose(out); + utest_state.output = old; + ASSERT_EQ(n, expected_len); + ASSERT_STREQ(buf, expected); +} +#endif diff --git a/utest.h b/utest.h index db9f4a9..a62eb9e 100644 --- a/utest.h +++ b/utest.h @@ -297,9 +297,15 @@ /* long long is a c++11 extension - TODO: grok for c++11 version here */ -#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || \ + defined(__cplusplus) && (__cplusplus >= 201103L) + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif + UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long int i) { UTEST_PRINTF("%lld", i); @@ -310,7 +316,28 @@ utest_type_printer(long long unsigned int i) { UTEST_PRINTF("%llu", i); } + +#ifdef __clang__ +#pragma clang diagnostic pop #endif + +#endif +#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) +#define utest_type_printer(val) \ + UTEST_PRINTF(_Generic((val), int \ + : "%d", long \ + : "%ld", long long \ + : "%lld", unsigned \ + : "%u", unsigned long \ + : "%lu", unsigned long long \ + : "%llu", float \ + : "%f", double \ + : "%f", long double \ + : "%Lf", default \ + : _Generic((val - val), ptrdiff_t \ + : "%p", default \ + : "undef")), \ + (val)) #else /* we don't have the ability to print the values we got, so we create a macro @@ -319,13 +346,20 @@ #define utest_type_printer(...) UTEST_PRINTF("undef") #endif +#if defined(__cplusplus) && (__cplusplus >= 201103L) +#define UTEST_TYPEOF(x) decltype(x) +#else +#define UTEST_TYPEOF(x) typeof(x) +#endif + #if defined(__clang__) #define UTEST_EXPECT(x, y, cond) \ { \ - _Pragma("clang diagnostic push") _Pragma( \ - "clang diagnostic ignored \"-Wlanguage-extension-token\"") typeof(y) \ - xEval = (x); \ - typeof(y) yEval = (y); \ + _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__); \ UTEST_PRINTF(" Expected : "); \ @@ -340,8 +374,8 @@ #elif defined(__GNUC__) #define UTEST_EXPECT(x, y, cond) \ { \ - typeof(y) xEval = (x); \ - typeof(y) yEval = (y); \ + UTEST_TYPEOF(y) xEval = (x); \ + UTEST_TYPEOF(y) yEval = (y); \ if (!((xEval)cond(yEval))) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : "); \ @@ -405,10 +439,11 @@ #if defined(__clang__) #define UTEST_ASSERT(x, y, cond) \ { \ - _Pragma("clang diagnostic push") _Pragma( \ - "clang diagnostic ignored \"-Wlanguage-extension-token\"") typeof(y) \ - xEval = (x); \ - typeof(y) yEval = (y); \ + _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__); \ UTEST_PRINTF(" Expected : "); \ @@ -424,8 +459,8 @@ #elif defined(__GNUC__) #define UTEST_ASSERT(x, y, cond) \ { \ - typeof(y) xEval = (x); \ - typeof(y) yEval = (y); \ + UTEST_TYPEOF(y) xEval = (x); \ + UTEST_TYPEOF(y) yEval = (y); \ if (!((xEval)cond(yEval))) { \ UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : "); \