Skip to content

Commit 2c50b36

Browse files
Stefan Ivanovphilipp-classen
authored andcommitted
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.
1 parent a264b83 commit 2c50b36

File tree

9 files changed

+78
-35
lines changed

9 files changed

+78
-35
lines changed

CMakeLists.txt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
cmake_minimum_required(VERSION 2.6)
22
project(CppQuickCheck)
3-
set(CMAKE_CXX_FLAGS "-O3 -g -Wall -std=c++11")
3+
if(WIN32)
4+
set(CMAKE_CXX_FLAGS "/W4 /permissive- /EHsc")
5+
else()
6+
set(CMAKE_CXX_FLAGS "-O3 -g -Wall -std=c++11")
7+
endif()
48

5-
find_package(Boost REQUIRED)
6-
include_directories(${Boost_INCLUDE_DIR})
79

8-
include_directories("${PROJECT_SOURCE_DIR}/include")
10+
add_library(cppqc SHARED src/Arbitrary.cpp)
11+
target_include_directories(cppqc PUBLIC include)
912

10-
add_subdirectory(examples)
13+
option(CPPQC_USE_BOOST "Use Boost, allowing to build some of the examples." OFF)
14+
if(CPPQC_USE_BOOST)
15+
find_package(Boost REQUIRED)
16+
endif()
1117

12-
add_library(cppqc SHARED src/Arbitrary.cpp)
18+
add_subdirectory(examples)
1319

1420
install(DIRECTORY "include/" DESTINATION "include"
1521
PATTERN ".*" EXCLUDE)
@@ -23,11 +29,12 @@ add_executable(
2329
test/arbitrary-tests.cpp
2430
test/shrink-explosion-protection.cpp
2531
test/compact-check-tests.cpp
26-
test/functional-tests.cpp)
27-
target_link_libraries(all-catch-tests cppqc)
32+
test/functional-tests.cpp
33+
)
34+
target_link_libraries(all-catch-tests PRIVATE cppqc)
2835
add_test(all-catch-tests all-catch-tests)
2936

30-
# workaround to force cmake to build test executable before running the test
37+
# workaround to force CMake to build test executable before running the test
3138
# (source: http://stackoverflow.com/a/736838/783510)
3239
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}
3340
DEPENDS all-catch-tests)

examples/CMakeLists.txt

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1-
add_executable(sampleOutput src/sampleOutput.cpp)
2-
target_link_libraries(sampleOutput cppqc)
3-
4-
add_executable(sampleShrinkOutput src/sampleShrinkOutput.cpp)
5-
target_link_libraries(sampleShrinkOutput cppqc)
6-
1+
if(CPPQC_USE_BOOST)
2+
add_executable(sampleOutput src/sampleOutput.cpp)
3+
target_link_libraries(sampleOutput PRIVATE cppqc)
4+
target_include_directories(sampleOutput PUBLIC ${Boost_INCLUDE_DIR})
5+
6+
add_executable(sampleShrinkOutput src/sampleShrinkOutput.cpp)
7+
target_link_libraries(sampleShrinkOutput PRIVATE cppqc)
8+
target_include_directories(sampleShrinkOutput PUBLIC ${Boost_INCLUDE_DIR})
9+
endif()
710
add_executable(testReverse src/TestReverse.cpp)
8-
target_link_libraries(testReverse cppqc)
11+
target_link_libraries(testReverse PRIVATE cppqc)
912

1013
add_executable(testReverseArray src/TestReverseArray.cpp)
11-
target_link_libraries(testReverseArray cppqc)
14+
target_link_libraries(testReverseArray PRIVATE cppqc)
1215

1316
add_executable(testSort src/TestSort.cpp)
14-
target_link_libraries(testSort cppqc)
17+
target_link_libraries(testSort PRIVATE cppqc)
1518

1619
add_executable(testSortCompact src/TestSortCompact.cpp)
17-
target_link_libraries(testSortCompact cppqc)
20+
target_link_libraries(testSortCompact PRIVATE cppqc)
1821

1922
add_executable(testChooseGenerator src/TestChooseGenerator.cpp)
20-
target_link_libraries(testChooseGenerator cppqc)
23+
target_link_libraries(testChooseGenerator PRIVATE cppqc)
2124

2225
add_executable(exampleElementsGen src/exampleElementsGen.cpp)
23-
target_link_libraries(exampleElementsGen cppqc)
26+
target_link_libraries(exampleElementsGen PRIVATE cppqc)
2427

2528
add_executable(testSlowShrinking src/TestSlowShrinking.cpp)
26-
target_link_libraries(testSlowShrinking cppqc)
29+
target_link_libraries(testSlowShrinking PRIVATE cppqc)
2730

2831
add_executable(testWithCustomGenerator src/TestWithCustomGenerator.cpp)
29-
target_link_libraries(testSlowShrinking cppqc)
32+
target_link_libraries(testWithCustomGenerator PRIVATE cppqc)
3033

3134
# requires c++1y compile flag
3235
#add_executable(testBoostTupleSupport src/BoostTupleSupport.cpp)
33-
#target_link_libraries(testBoostTupleSupport cppqc)
36+
#target_link_libraries(testBoostTupleSupport PRIVATE cppqc)

examples/src/TestReverseArray.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
#include "cppqc.h"
2727

28+
#include <array>
2829
#include <algorithm>
29-
#include <boost/static_assert.hpp>
3030

3131
const int ArraySize = 5;
3232

examples/src/TestSort.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "cppqc.h"
2727

2828
#include <algorithm>
29-
#include <boost/static_assert.hpp>
3029
#include <iterator>
3130
#include <sstream>
3231

examples/src/TestSortCompact.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "cppqc/CompactCheck.h"
2828

2929
#include <algorithm>
30-
#include <boost/static_assert.hpp>
3130
#include <iterator>
3231
#include <sstream>
3332

include/cppqc/Arbitrary.h

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,41 @@
3030

3131
#include <limits>
3232
#include <random>
33+
#include <type_traits>
3334

3435
namespace cppqc {
3536

37+
/*
38+
* Type trait for checking if a type is found in a parameter pack.
39+
*/
40+
template <class T, class... Us>
41+
struct IsOneOf;
42+
43+
template <class T, class U, class... Us>
44+
struct IsOneOf<T, U, Us...> {
45+
static constexpr bool value = IsOneOf<T, Us...>::value;
46+
};
47+
48+
template <class T, class... Us>
49+
struct IsOneOf<T, T, Us...> {
50+
static constexpr bool value = true;
51+
};
52+
53+
template <class T>
54+
struct IsOneOf<T> {
55+
static constexpr bool value = false;
56+
};
57+
58+
/*
59+
* Type trait for checking if a type can be used as the template parameter of
60+
* std::uniform_int_distribution.
61+
*/
62+
template <class T>
63+
struct IntDistributionSupported {
64+
static constexpr bool value =
65+
IsOneOf<T, short, int, long, long long, unsigned short, unsigned int, unsigned long, unsigned long long>::value;
66+
};
67+
3668
// default generators
3769

3870
/*
@@ -43,7 +75,8 @@ namespace cppqc {
4375
*/
4476
template <class Integral>
4577
Integral arbitraryBoundedIntegral(RngEngine& rng, std::size_t /*size*/) {
46-
std::uniform_int_distribution<Integral> dist{
78+
using DistributionType = typename std::conditional<IntDistributionSupported<Integral>::value, Integral, int>::type;
79+
std::uniform_int_distribution<DistributionType> dist{
4780
std::numeric_limits<Integral>::lowest(),
4881
std::numeric_limits<Integral>::max()};
4982
return dist(rng);
@@ -273,8 +306,8 @@ struct ArbitraryImpl<long double> {
273306
};
274307

275308
inline char arbitraryChar(RngEngine& rng, std::size_t) {
276-
std::uniform_int_distribution<char> dist{0x20, 0x7f};
277-
return dist(rng);
309+
std::uniform_int_distribution<int> dist{0x20, 0x7f};
310+
return static_cast<char>(dist(rng));
278311
}
279312
inline std::vector<char> shrinkChar(char c) {
280313
const char possShrinks[] = {'a', 'b', 'c', 'A', 'B', 'C',

include/cppqc/Generator.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <tuple>
3939
#include <utility>
4040
#include <vector>
41+
#include <ctime>
4142

4243
namespace cppqc {
4344

@@ -233,7 +234,7 @@ std::vector<T> sample(const Generator<T>& g,
233234
if (num == 0)
234235
num = 20;
235236
if (seed == 0)
236-
seed = time(nullptr);
237+
seed = std::time(nullptr);
237238
RngEngine rng(seed);
238239
std::vector<T> ret;
239240
ret.reserve(num);
@@ -254,7 +255,7 @@ void sampleOutput(const Generator<T>& g,
254255
if (num == 0)
255256
num = 20;
256257
if (seed == 0)
257-
seed = time(nullptr);
258+
seed = std::time(nullptr);
258259
RngEngine rng(seed);
259260
try {
260261
for (std::size_t i = 0; i < num; ++i) {
@@ -276,7 +277,7 @@ std::vector<std::pair<T, std::vector<T>>> sampleShrink(const Generator<T>& g,
276277
if (num == 0)
277278
num = 20;
278279
if (seed == 0)
279-
seed = time(nullptr);
280+
seed = std::time(nullptr);
280281
RngEngine rng(seed);
281282
std::vector<std::pair<T, std::vector<T>>> ret;
282283
ret.reserve(num);
@@ -301,7 +302,7 @@ void sampleShrinkOutput(const Generator<T>& g,
301302
if (num == 0)
302303
num = 20;
303304
if (seed == 0)
304-
seed = time(nullptr);
305+
seed = std::time(nullptr);
305306
RngEngine rng(seed);
306307
try {
307308
for (std::size_t i = 0; i < num; ++i) {

include/cppqc/Test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ inline SeedType resolveSeed(SeedType originalSeed = USE_DEFAULT_SEED) {
173173
throw std::invalid_argument{err.str()};
174174
}
175175
} else {
176-
return static_cast<SeedType>(time(0));
176+
return static_cast<SeedType>(std::time(0));
177177
}
178178
} else {
179179
return originalSeed;

test/arbitrary-tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "catch.hpp"
2727
#include "cppqc/Arbitrary.h"
2828

29+
#include <array>
2930
#include <random>
3031
#include <unordered_set>
3132

0 commit comments

Comments
 (0)