From 2c50b36add60c2fb662130faca44f91bfe861bfd Mon Sep 17 00:00:00 2001 From: Stefan Ivanov Date: Mon, 22 Oct 2018 14:47:44 +0300 Subject: [PATCH] Update CMake and cpp files to fix the compilation on newer MSVC compilers * Add proper flags on when building on Windows. * Remove all dependnecies on Boost for the core CPP QuickCheck project. Also add an option to use Boost. * Modernize some of the CMake infrastructure to use targets and target properties. * Add type trait machinery to determine the correct type to use for std::uniform_int_distribution based on the standard. * Fix non-standard call to std::time. --- CMakeLists.txt | 25 +++++++++++++------- examples/CMakeLists.txt | 33 ++++++++++++++------------ examples/src/TestReverseArray.cpp | 2 +- examples/src/TestSort.cpp | 1 - examples/src/TestSortCompact.cpp | 1 - include/cppqc/Arbitrary.h | 39 ++++++++++++++++++++++++++++--- include/cppqc/Generator.h | 9 +++---- include/cppqc/Test.h | 2 +- test/arbitrary-tests.cpp | 1 + 9 files changed, 78 insertions(+), 35 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 559395e..84a3dfa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,15 +1,21 @@ cmake_minimum_required(VERSION 2.6) project(CppQuickCheck) -set(CMAKE_CXX_FLAGS "-O3 -g -Wall -std=c++11") +if(WIN32) + set(CMAKE_CXX_FLAGS "/W4 /permissive- /EHsc") +else() + set(CMAKE_CXX_FLAGS "-O3 -g -Wall -std=c++11") +endif() -find_package(Boost REQUIRED) -include_directories(${Boost_INCLUDE_DIR}) -include_directories("${PROJECT_SOURCE_DIR}/include") +add_library(cppqc SHARED src/Arbitrary.cpp) +target_include_directories(cppqc PUBLIC include) -add_subdirectory(examples) +option(CPPQC_USE_BOOST "Use Boost, allowing to build some of the examples." OFF) +if(CPPQC_USE_BOOST) + find_package(Boost REQUIRED) +endif() -add_library(cppqc SHARED src/Arbitrary.cpp) +add_subdirectory(examples) install(DIRECTORY "include/" DESTINATION "include" PATTERN ".*" EXCLUDE) @@ -23,11 +29,12 @@ add_executable( test/arbitrary-tests.cpp test/shrink-explosion-protection.cpp test/compact-check-tests.cpp - test/functional-tests.cpp) -target_link_libraries(all-catch-tests cppqc) + test/functional-tests.cpp +) +target_link_libraries(all-catch-tests PRIVATE cppqc) add_test(all-catch-tests all-catch-tests) -# workaround to force cmake to build test executable before running the test +# workaround to force CMake to build test executable before running the test # (source: http://stackoverflow.com/a/736838/783510) add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS all-catch-tests) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 794e0c4..d228d1f 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,33 +1,36 @@ -add_executable(sampleOutput src/sampleOutput.cpp) -target_link_libraries(sampleOutput cppqc) - -add_executable(sampleShrinkOutput src/sampleShrinkOutput.cpp) -target_link_libraries(sampleShrinkOutput cppqc) - +if(CPPQC_USE_BOOST) + add_executable(sampleOutput src/sampleOutput.cpp) + target_link_libraries(sampleOutput PRIVATE cppqc) + target_include_directories(sampleOutput PUBLIC ${Boost_INCLUDE_DIR}) + + add_executable(sampleShrinkOutput src/sampleShrinkOutput.cpp) + target_link_libraries(sampleShrinkOutput PRIVATE cppqc) + target_include_directories(sampleShrinkOutput PUBLIC ${Boost_INCLUDE_DIR}) +endif() add_executable(testReverse src/TestReverse.cpp) -target_link_libraries(testReverse cppqc) +target_link_libraries(testReverse PRIVATE cppqc) add_executable(testReverseArray src/TestReverseArray.cpp) -target_link_libraries(testReverseArray cppqc) +target_link_libraries(testReverseArray PRIVATE cppqc) add_executable(testSort src/TestSort.cpp) -target_link_libraries(testSort cppqc) +target_link_libraries(testSort PRIVATE cppqc) add_executable(testSortCompact src/TestSortCompact.cpp) -target_link_libraries(testSortCompact cppqc) +target_link_libraries(testSortCompact PRIVATE cppqc) add_executable(testChooseGenerator src/TestChooseGenerator.cpp) -target_link_libraries(testChooseGenerator cppqc) +target_link_libraries(testChooseGenerator PRIVATE cppqc) add_executable(exampleElementsGen src/exampleElementsGen.cpp) -target_link_libraries(exampleElementsGen cppqc) +target_link_libraries(exampleElementsGen PRIVATE cppqc) add_executable(testSlowShrinking src/TestSlowShrinking.cpp) -target_link_libraries(testSlowShrinking cppqc) +target_link_libraries(testSlowShrinking PRIVATE cppqc) add_executable(testWithCustomGenerator src/TestWithCustomGenerator.cpp) -target_link_libraries(testSlowShrinking cppqc) +target_link_libraries(testWithCustomGenerator PRIVATE cppqc) # requires c++1y compile flag #add_executable(testBoostTupleSupport src/BoostTupleSupport.cpp) -#target_link_libraries(testBoostTupleSupport cppqc) +#target_link_libraries(testBoostTupleSupport PRIVATE cppqc) diff --git a/examples/src/TestReverseArray.cpp b/examples/src/TestReverseArray.cpp index 1631f68..3ee8080 100644 --- a/examples/src/TestReverseArray.cpp +++ b/examples/src/TestReverseArray.cpp @@ -25,8 +25,8 @@ #include "cppqc.h" +#include #include -#include const int ArraySize = 5; diff --git a/examples/src/TestSort.cpp b/examples/src/TestSort.cpp index 8991cd8..b498f56 100644 --- a/examples/src/TestSort.cpp +++ b/examples/src/TestSort.cpp @@ -26,7 +26,6 @@ #include "cppqc.h" #include -#include #include #include diff --git a/examples/src/TestSortCompact.cpp b/examples/src/TestSortCompact.cpp index f0200f5..bc6341c 100644 --- a/examples/src/TestSortCompact.cpp +++ b/examples/src/TestSortCompact.cpp @@ -27,7 +27,6 @@ #include "cppqc/CompactCheck.h" #include -#include #include #include diff --git a/include/cppqc/Arbitrary.h b/include/cppqc/Arbitrary.h index 5802c04..807e65e 100644 --- a/include/cppqc/Arbitrary.h +++ b/include/cppqc/Arbitrary.h @@ -30,9 +30,41 @@ #include #include +#include namespace cppqc { +/* + * Type trait for checking if a type is found in a parameter pack. + */ +template +struct IsOneOf; + +template +struct IsOneOf { + static constexpr bool value = IsOneOf::value; +}; + +template +struct IsOneOf { + static constexpr bool value = true; +}; + +template +struct IsOneOf { + static constexpr bool value = false; +}; + +/* + * Type trait for checking if a type can be used as the template parameter of + * std::uniform_int_distribution. + */ +template +struct IntDistributionSupported { + static constexpr bool value = + IsOneOf::value; +}; + // default generators /* @@ -43,7 +75,8 @@ namespace cppqc { */ template Integral arbitraryBoundedIntegral(RngEngine& rng, std::size_t /*size*/) { - std::uniform_int_distribution dist{ + using DistributionType = typename std::conditional::value, Integral, int>::type; + std::uniform_int_distribution dist{ std::numeric_limits::lowest(), std::numeric_limits::max()}; return dist(rng); @@ -273,8 +306,8 @@ struct ArbitraryImpl { }; inline char arbitraryChar(RngEngine& rng, std::size_t) { - std::uniform_int_distribution dist{0x20, 0x7f}; - return dist(rng); + std::uniform_int_distribution dist{0x20, 0x7f}; + return static_cast(dist(rng)); } inline std::vector shrinkChar(char c) { const char possShrinks[] = {'a', 'b', 'c', 'A', 'B', 'C', diff --git a/include/cppqc/Generator.h b/include/cppqc/Generator.h index 0eab623..4ed3e5e 100644 --- a/include/cppqc/Generator.h +++ b/include/cppqc/Generator.h @@ -38,6 +38,7 @@ #include #include #include +#include namespace cppqc { @@ -233,7 +234,7 @@ std::vector sample(const Generator& g, if (num == 0) num = 20; if (seed == 0) - seed = time(nullptr); + seed = std::time(nullptr); RngEngine rng(seed); std::vector ret; ret.reserve(num); @@ -254,7 +255,7 @@ void sampleOutput(const Generator& g, if (num == 0) num = 20; if (seed == 0) - seed = time(nullptr); + seed = std::time(nullptr); RngEngine rng(seed); try { for (std::size_t i = 0; i < num; ++i) { @@ -276,7 +277,7 @@ std::vector>> sampleShrink(const Generator& g, if (num == 0) num = 20; if (seed == 0) - seed = time(nullptr); + seed = std::time(nullptr); RngEngine rng(seed); std::vector>> ret; ret.reserve(num); @@ -301,7 +302,7 @@ void sampleShrinkOutput(const Generator& g, if (num == 0) num = 20; if (seed == 0) - seed = time(nullptr); + seed = std::time(nullptr); RngEngine rng(seed); try { for (std::size_t i = 0; i < num; ++i) { diff --git a/include/cppqc/Test.h b/include/cppqc/Test.h index 31fcb95..900c11a 100644 --- a/include/cppqc/Test.h +++ b/include/cppqc/Test.h @@ -173,7 +173,7 @@ inline SeedType resolveSeed(SeedType originalSeed = USE_DEFAULT_SEED) { throw std::invalid_argument{err.str()}; } } else { - return static_cast(time(0)); + return static_cast(std::time(0)); } } else { return originalSeed; diff --git a/test/arbitrary-tests.cpp b/test/arbitrary-tests.cpp index 6b2f9ac..7759ed0 100644 --- a/test/arbitrary-tests.cpp +++ b/test/arbitrary-tests.cpp @@ -26,6 +26,7 @@ #include "catch.hpp" #include "cppqc/Arbitrary.h" +#include #include #include