Skip to content

Commit 732395d

Browse files
AzothAmmofiesh
authored andcommitted
Working towards cpp17 tests
Progress towards USCiLab#448 Lots of warnings from g++7.2, these are addressed in USCiLab#423 which hasn't been merged yet. CMAKE can be cleaned up a bit - may put back in the ability to use older cmake and just throw errors as necessary, otherwise just force people to use at least cmake 3.1
1 parent 6d1d395 commit 732395d

File tree

10 files changed

+261
-67
lines changed

10 files changed

+261
-67
lines changed

CMakeLists.txt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required (VERSION 2.6.2)
1+
cmake_minimum_required (VERSION 3.1)
22
project (cereal)
33

44
option(SKIP_PORTABILITY_TEST "Skip portability (32 bit) tests" OFF)
@@ -14,6 +14,16 @@ else()
1414
set(CEREAL_THREAD_LIBS "")
1515
endif()
1616

17+
# Default to C++11
18+
if(NOT DEFINED CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD STREQUAL "98")
19+
set(CMAKE_CXX_STANDARD 11)
20+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
21+
endif()
22+
23+
if(CMAKE_CXX_STANDARD GREATER 14)
24+
cmake_minimum_required(VERSION 3.8)
25+
endif()
26+
1727
if(MSVC)
1828
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj /W3 /WX")
1929
else()
@@ -22,14 +32,6 @@ else()
2232
if(WITH_WERROR)
2333
set(CMAKE_CXX_FLAGS "-Werror ${CMAKE_CXX_FLAGS}")
2434
endif(WITH_WERROR)
25-
if(CMAKE_VERSION VERSION_LESS 3.1)
26-
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
27-
else()
28-
if(NOT DEFINED CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD STREQUAL "98")
29-
set(CMAKE_CXX_STANDARD 11)
30-
endif()
31-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
32-
endif()
3335
endif()
3436

3537
if(NOT CMAKE_VERSION VERSION_LESS 3.0)

include/cereal/macros.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,15 @@
132132
#endif // end !defined(CEREAL_HAS_NOEXCEPT)
133133
#endif // ifndef CEREAL_NOEXCEPT
134134

135+
// ######################################################################
136+
//! Checks if C++17 is available
137+
#if __cplusplus >= 201703L
138+
#define CEREAL_HAS_CPP17
139+
#endif
140+
141+
//! Checks if C++14 is available
142+
#if __cplusplus >= 201402L
143+
#define CEREAL_HAS_CPP14
144+
#endif
145+
135146
#endif // CEREAL_MACROS_HPP_

include/cereal/types/optional.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*! \file optional.hpp
22
\brief Support for std::optional
3-
\ingroup OtherTypes */
3+
\ingroup STLSupport */
44
/*
55
Copyright (c) 2017, Juan Pedro Bolivar Puente
66
All rights reserved.
@@ -30,10 +30,11 @@
3030
#ifndef CEREAL_TYPES_STD_OPTIONAL_
3131
#define CEREAL_TYPES_STD_OPTIONAL_
3232

33-
#include <cereal/cereal.hpp>
33+
#include "cereal/cereal.hpp"
3434
#include <optional>
3535

3636
namespace cereal {
37+
//! Saving for std::optional
3738
template <class Archive, typename T> inline
3839
void CEREAL_SAVE_FUNCTION_NAME(Archive& ar, const std::optional<T>& optional)
3940
{
@@ -45,6 +46,7 @@ namespace cereal {
4546
}
4647
}
4748

49+
//! Loading for std::optional
4850
template <class Archive, typename T> inline
4951
void CEREAL_LOAD_FUNCTION_NAME(Archive& ar, std::optional<T>& optional)
5052
{

include/cereal/types/variant.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*! \file variant.hpp
22
\brief Support for std::variant
3-
\ingroup OtherTypes */
3+
\ingroup STLSupport */
44
/*
55
Copyright (c) 2014, 2017, Randolph Voorhies, Shane Grant, Juan Pedro
66
Bolivar Puente. All rights reserved.

unittests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ if(NOT MSVC)
8787
endforeach()
8888
endif(NOT MSVC)
8989

90+
if(CMAKE_CXX_STANDARD GREATER 14)
91+
add_subdirectory(cpp17)
92+
endif()
93+
9094
if(NOT CMAKE_VERSION VERSION_LESS 3.0)
9195
add_test(test_cmake_config_module ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake-config-module.cmake)
9296
endif()

unittests/cpp17/CMakeLists.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
file(GLOB TESTS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
2+
3+
# Build all of the non-special tests
4+
foreach(TEST_SOURCE ${TESTS})
5+
message(STATUS ${TEST_SOURCE})
6+
7+
string(REPLACE ".cpp" "" TEST_TARGET "${TEST_SOURCE}")
8+
set(TEST_TARGET "test_cpp17_${TEST_TARGET}")
9+
10+
add_executable(${TEST_TARGET} ${TEST_SOURCE})
11+
target_link_libraries(${TEST_TARGET} ${CEREAL_THREAD_LIBS})
12+
add_test("${TEST_TARGET}" "${TEST_TARGET}")
13+
14+
# If we are on a 64-bit machine, create an extra 32-bit version of the test if portability testing is enabled
15+
if((NOT MSVC) AND (${CMAKE_SIZEOF_VOID_P} EQUAL 8) AND (NOT SKIP_PORTABILITY_TEST))
16+
add_executable(${TEST_TARGET}_32 ${TEST_SOURCE})
17+
set_target_properties(${TEST_TARGET}_32 PROPERTIES
18+
COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
19+
add_test("${TEST_TARGET}_32" "${TEST_TARGET}_32")
20+
endif()
21+
22+
endforeach()
23+
24+
if(NOT MSVC)
25+
# add tests to coverage
26+
foreach(TEST_SOURCE ${TESTS})
27+
string(REPLACE ".cpp" "" COVERAGE_TARGET "${TEST_SOURCE}")
28+
set(COVERAGE_TARGET "coverage_${COVERAGE_TARGET}")
29+
30+
add_dependencies(coverage ${COVERAGE_TARGET})
31+
32+
add_executable(${COVERAGE_TARGET} EXCLUDE_FROM_ALL ${TEST_SOURCE})
33+
set_target_properties(${COVERAGE_TARGET} PROPERTIES COMPILE_FLAGS "-coverage")
34+
set_target_properties(${COVERAGE_TARGET} PROPERTIES LINK_FLAGS "-coverage")
35+
set_target_properties(${COVERAGE_TARGET} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/coverage")
36+
target_link_libraries(${COVERAGE_TARGET} ${CEREAL_THREAD_LIBS})
37+
endforeach()
38+
endif(NOT MSVC)

include/cereal/types/optional.cpp renamed to unittests/cpp17/optional.cpp

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,61 +26,9 @@
2626
*/
2727

2828
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
29+
#include "optional.hpp"
2930

30-
#include "common.hpp"
31-
32-
#if __cplusplus >= 201703L
33-
34-
#include <cereal/types/optional.hpp>
35-
36-
template <class IArchive, class OArchive> inline
37-
void test_std_optional()
38-
{
39-
std::random_device rd;
40-
std::mt19937 gen(rd());
41-
42-
std::optional<int> o_bv1 = random_value<int>(gen);
43-
std::optional<double> o_bv2 = random_value<double>(gen);
44-
std::optional<std::string> o_bv3 = random_basic_string<char>(gen);
45-
std::optional<int> o_bv4 = std::nullopt;
46-
std::optional<double> o_bv5 = std::nullopt;
47-
std::optional<std::string> o_bv6 = std::nullopt;
48-
49-
std::ostringstream os;
50-
{
51-
OArchive oar(os);
52-
53-
oar(o_bv1);
54-
oar(o_bv2);
55-
oar(o_bv3);
56-
oar(o_bv4);
57-
oar(o_bv5);
58-
oar(o_bv6);
59-
}
60-
61-
decltype(o_bv1) i_bv1;
62-
decltype(o_bv2) i_bv2;
63-
decltype(o_bv3) i_bv3;
64-
decltype(o_bv4) i_bv4;
65-
decltype(o_bv5) i_bv5;
66-
decltype(o_bv6) i_bv6;
67-
68-
std::istringstream is(os.str());
69-
{
70-
IArchive iar(is);
71-
72-
iar(i_bv1);
73-
iar(i_bv2);
74-
iar(i_bv3);
75-
}
76-
77-
CHECK_EQ( *i_bv1, std::get<int>(o_bv1) );
78-
CHECK_EQ( *i_bv2, doctest::Approx(std::get<double>(o_bv2)).epsilon(1e-5) );
79-
CHECK_EQ( *i_bv3, std::get<std::string>(o_bv3) );
80-
CHECK( !i_bv4 );
81-
CHECK( !i_bv5 );
82-
CHECK( !i_bv6 );
83-
}
31+
#ifdef CEREAL_HAS_CPP17
8432

8533
TEST_SUITE("std_optional");
8634

@@ -106,4 +54,4 @@ TEST_CASE("json_std_optional")
10654

10755
TEST_SUITE_END();
10856

109-
#endif // is c++17
57+
#endif // CEREAL_HAS_CPP17

unittests/cpp17/optional.hpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Copyright (c) 2017, Juan Pedro Bolivar Puente
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
* Neither the name of cereal nor the
13+
names of its contributors may be used to endorse or promote products
14+
derived from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES AND SHANE GRANT BE LIABLE FOR ANY
20+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
#ifndef CEREAL_TEST_OPTIONAL_H_
28+
#define CEREAL_TEST_OPTIONAL_H_
29+
#include "../common.hpp"
30+
31+
#ifdef CEREAL_HAS_CPP17
32+
#include <cereal/types/optional.hpp>
33+
34+
template <class IArchive, class OArchive> inline
35+
void test_std_optional()
36+
{
37+
std::random_device rd;
38+
std::mt19937 gen(rd());
39+
40+
std::optional<int> o_o1 = random_value<int>(gen);
41+
std::optional<double> o_o2 = random_value<double>(gen);
42+
std::optional<std::string> o_o3 = random_basic_string<char>(gen);
43+
std::optional<int> o_o4 = std::nullopt;
44+
std::optional<double> o_o5 = std::nullopt;
45+
std::optional<std::string> o_o6 = std::nullopt;
46+
std::optional<std::optional<long>> o_o7 = std::make_optional<std::optional<long>>( std::make_optional<long>( random_value<long>(gen) ) );
47+
48+
std::ostringstream os;
49+
{
50+
OArchive oar(os);
51+
52+
oar(o_o1);
53+
oar(o_o2);
54+
oar(o_o3);
55+
oar(o_o4);
56+
oar(o_o5);
57+
oar(o_o6);
58+
oar(o_o7);
59+
}
60+
61+
decltype(o_o1) i_o1;
62+
decltype(o_o2) i_o2;
63+
decltype(o_o3) i_o3;
64+
decltype(o_o4) i_o4;
65+
decltype(o_o5) i_o5;
66+
decltype(o_o6) i_o6;
67+
decltype(o_o7) i_o7;
68+
69+
std::istringstream is(os.str());
70+
{
71+
IArchive iar(is);
72+
73+
iar(i_o1);
74+
iar(i_o2);
75+
iar(i_o3);
76+
iar(i_o4);
77+
iar(i_o5);
78+
iar(i_o6);
79+
iar(i_o7);
80+
}
81+
82+
CHECK_EQ( *i_o1, *o_o1 );
83+
CHECK_EQ( *i_o2, doctest::Approx(*o_o2).epsilon(1e-5) );
84+
CHECK_EQ( *i_o3, *o_o3 );
85+
CHECK_EQ( *i_o4, *o_o4 );
86+
CHECK_EQ( *i_o5, *o_o5 );
87+
CHECK_EQ( *i_o6, *o_o6 );
88+
CHECK_EQ( **i_o7, **o_o7 );
89+
}
90+
91+
#endif // CEREAL_HAS_CPP17
92+
#endif // CEREAL_TEST_OPTIONAL_H_

0 commit comments

Comments
 (0)