Skip to content

Commit

Permalink
Update CMake and cpp files to fix the compilation on newer MSVC compi…
Browse files Browse the repository at this point in the history
…lers

 * 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.
  • Loading branch information
Stefan Ivanov authored and philipp-classen committed Nov 6, 2018
1 parent a264b83 commit 2c50b36
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 35 deletions.
25 changes: 16 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down
33 changes: 18 additions & 15 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion examples/src/TestReverseArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

#include "cppqc.h"

#include <array>
#include <algorithm>
#include <boost/static_assert.hpp>

const int ArraySize = 5;

Expand Down
1 change: 0 additions & 1 deletion examples/src/TestSort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "cppqc.h"

#include <algorithm>
#include <boost/static_assert.hpp>
#include <iterator>
#include <sstream>

Expand Down
1 change: 0 additions & 1 deletion examples/src/TestSortCompact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "cppqc/CompactCheck.h"

#include <algorithm>
#include <boost/static_assert.hpp>
#include <iterator>
#include <sstream>

Expand Down
39 changes: 36 additions & 3 deletions include/cppqc/Arbitrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,41 @@

#include <limits>
#include <random>
#include <type_traits>

namespace cppqc {

/*
* Type trait for checking if a type is found in a parameter pack.
*/
template <class T, class... Us>
struct IsOneOf;

template <class T, class U, class... Us>
struct IsOneOf<T, U, Us...> {
static constexpr bool value = IsOneOf<T, Us...>::value;
};

template <class T, class... Us>
struct IsOneOf<T, T, Us...> {
static constexpr bool value = true;
};

template <class T>
struct IsOneOf<T> {
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 <class T>
struct IntDistributionSupported {
static constexpr bool value =
IsOneOf<T, short, int, long, long long, unsigned short, unsigned int, unsigned long, unsigned long long>::value;
};

// default generators

/*
Expand All @@ -43,7 +75,8 @@ namespace cppqc {
*/
template <class Integral>
Integral arbitraryBoundedIntegral(RngEngine& rng, std::size_t /*size*/) {
std::uniform_int_distribution<Integral> dist{
using DistributionType = typename std::conditional<IntDistributionSupported<Integral>::value, Integral, int>::type;
std::uniform_int_distribution<DistributionType> dist{
std::numeric_limits<Integral>::lowest(),
std::numeric_limits<Integral>::max()};
return dist(rng);
Expand Down Expand Up @@ -273,8 +306,8 @@ struct ArbitraryImpl<long double> {
};

inline char arbitraryChar(RngEngine& rng, std::size_t) {
std::uniform_int_distribution<char> dist{0x20, 0x7f};
return dist(rng);
std::uniform_int_distribution<int> dist{0x20, 0x7f};
return static_cast<char>(dist(rng));
}
inline std::vector<char> shrinkChar(char c) {
const char possShrinks[] = {'a', 'b', 'c', 'A', 'B', 'C',
Expand Down
9 changes: 5 additions & 4 deletions include/cppqc/Generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <tuple>
#include <utility>
#include <vector>
#include <ctime>

namespace cppqc {

Expand Down Expand Up @@ -233,7 +234,7 @@ std::vector<T> sample(const Generator<T>& g,
if (num == 0)
num = 20;
if (seed == 0)
seed = time(nullptr);
seed = std::time(nullptr);
RngEngine rng(seed);
std::vector<T> ret;
ret.reserve(num);
Expand All @@ -254,7 +255,7 @@ void sampleOutput(const Generator<T>& 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) {
Expand All @@ -276,7 +277,7 @@ std::vector<std::pair<T, std::vector<T>>> sampleShrink(const Generator<T>& g,
if (num == 0)
num = 20;
if (seed == 0)
seed = time(nullptr);
seed = std::time(nullptr);
RngEngine rng(seed);
std::vector<std::pair<T, std::vector<T>>> ret;
ret.reserve(num);
Expand All @@ -301,7 +302,7 @@ void sampleShrinkOutput(const Generator<T>& 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) {
Expand Down
2 changes: 1 addition & 1 deletion include/cppqc/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ inline SeedType resolveSeed(SeedType originalSeed = USE_DEFAULT_SEED) {
throw std::invalid_argument{err.str()};
}
} else {
return static_cast<SeedType>(time(0));
return static_cast<SeedType>(std::time(0));
}
} else {
return originalSeed;
Expand Down
1 change: 1 addition & 0 deletions test/arbitrary-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "catch.hpp"
#include "cppqc/Arbitrary.h"

#include <array>
#include <random>
#include <unordered_set>

Expand Down

0 comments on commit 2c50b36

Please sign in to comment.