Skip to content

Commit

Permalink
Merge branch 'develop_doctest' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
AzothAmmo committed Jan 25, 2017
2 parents 75e50ee + 1d67d44 commit a8e9963
Show file tree
Hide file tree
Showing 76 changed files with 8,804 additions and 4,222 deletions.
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required (VERSION 2.6.2)
project (cereal)

option(SKIP_PORTABILITY_TEST "Skip portability tests" OFF)
option(SKIP_PORTABILITY_TEST "Skip portability (32 bit) tests" OFF)
if(NOT CMAKE_VERSION VERSION_LESS 3.0) # installing cereal requires INTERFACE lib
option(JUST_INSTALL_CEREAL "Don't do anything besides installing the library" OFF)
endif()
Expand Down Expand Up @@ -49,13 +49,15 @@ endif()

include_directories(./include)

find_package(Boost COMPONENTS serialization unit_test_framework)
# Boost serialization for performance sandbox
find_package(Boost COMPONENTS serialization)

if(Boost_FOUND)
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
enable_testing()
add_subdirectory(unittests)
endif(Boost_FOUND)

enable_testing()
add_subdirectory(unittests)

add_subdirectory(sandbox)

Expand Down
25 changes: 9 additions & 16 deletions unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
file(GLOB TESTS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)

# A semi-colon separated list of test sources that should not be automatically built with boost unit test
# A semi-colon separated list of test sources that should not be automatically built with doctest
set(SPECIAL_TESTS "portability_test.cpp")

# Build the portability test only if we are on a 64-bit machine (void* is 8 bytes)
Expand All @@ -15,7 +15,7 @@ if((${CMAKE_SIZEOF_VOID_P} EQUAL 8) AND (NOT SKIP_PORTABILITY_TEST))

endif()

# Build all of the non-special tests and link against the boost unit test framework
# Build all of the non-special tests
foreach(TEST_SOURCE ${TESTS})

string(REPLACE ".cpp" "" TEST_TARGET "${TEST_SOURCE}")
Expand All @@ -27,21 +27,16 @@ foreach(TEST_SOURCE ${TESTS})
if(IS_SPECIAL_TEST EQUAL -1)

add_executable(${TEST_TARGET} ${TEST_SOURCE})
set_target_properties(${TEST_TARGET} PROPERTIES COMPILE_DEFINITIONS "BOOST_TEST_DYN_LINK;BOOST_TEST_MODULE=${TEST_TARGET}")
target_link_libraries(${TEST_TARGET} ${Boost_LIBRARIES})
target_link_libraries(${TEST_TARGET} ${CEREAL_THREAD_LIBS})
add_test("${TEST_TARGET}" "${TEST_TARGET}")

# TODO: This won't work right now, because we would need a 32-bit boost
## If we are on a 64-bit machine, create an extra 32-bit version of the test
#if(${CMAKE_SIZEOF_VOID_P} EQUAL 8)
# add_executable(${TEST_TARGET}_32 ${TEST_SOURCE})
# set_target_properties(${TEST_TARGET}_32 PROPERTIES
# COMPILE_DEFINITIONS "BOOST_TEST_DYN_LINK;BOOST_TEST_MODULE=${TEST_TARGET}"
# COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
# target_link_libraries(${TEST_TARGET}_32 ${Boost_LIBRARIES})
# add_test("${TEST_TARGET}_32" "${TEST_TARGET}_32")
#endif()
# If we are on a 64-bit machine, create an extra 32-bit version of the test if portability testing is enabled
if((${CMAKE_SIZEOF_VOID_P} EQUAL 8) AND (NOT SKIP_PORTABILITY_TEST))
add_executable(${TEST_TARGET}_32 ${TEST_SOURCE})
set_target_properties(${TEST_TARGET}_32 PROPERTIES
COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
add_test("${TEST_TARGET}_32" "${TEST_TARGET}_32")
endif()

endif()

Expand Down Expand Up @@ -69,11 +64,9 @@ foreach(TEST_SOURCE ${TESTS})
add_dependencies(coverage ${COVERAGE_TARGET})

add_executable(${COVERAGE_TARGET} EXCLUDE_FROM_ALL ${TEST_SOURCE})
set_target_properties(${COVERAGE_TARGET} PROPERTIES COMPILE_DEFINITIONS "BOOST_TEST_DYN_LINK;BOOST_TEST_MODULE=${COVERAGE_TARGET}")
set_target_properties(${COVERAGE_TARGET} PROPERTIES COMPILE_FLAGS "-coverage")
set_target_properties(${COVERAGE_TARGET} PROPERTIES LINK_FLAGS "-coverage")
set_target_properties(${COVERAGE_TARGET} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/coverage")
target_link_libraries(${COVERAGE_TARGET} ${Boost_LIBRARIES})
target_link_libraries(${COVERAGE_TARGET} ${CEREAL_THREAD_LIBS})
endif()
endforeach()
Expand Down
77 changes: 8 additions & 69 deletions unittests/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,90 +24,29 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "common.hpp"
#include <boost/test/unit_test.hpp>
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "array.hpp"

template <class IArchive, class OArchive>
void test_array()
{
std::random_device rd;
std::mt19937 gen(rd());

for(int ii=0; ii<100; ++ii)
{
std::array<int, 100> o_podarray;
for(auto & elem : o_podarray)
elem = random_value<int>(gen);

std::array<StructInternalSerialize, 100> o_iserarray;
for(auto & elem : o_iserarray)
elem = StructInternalSerialize( random_value<int>(gen), random_value<int>(gen) );

std::array<StructInternalSplit, 100> o_isplarray;
for(auto & elem : o_isplarray)
elem = StructInternalSplit( random_value<int>(gen), random_value<int>(gen) );

std::array<StructExternalSerialize, 100> o_eserarray;
for(auto & elem : o_eserarray)
elem = StructExternalSerialize( random_value<int>(gen), random_value<int>(gen) );

std::array<StructExternalSplit, 100> o_esplarray;
for(auto & elem : o_esplarray)
elem = StructExternalSplit( random_value<int>(gen), random_value<int>(gen) );

std::ostringstream os;
{
OArchive oar(os);

oar(o_podarray);
oar(o_iserarray);
oar(o_isplarray);
oar(o_eserarray);
oar(o_esplarray);
}

std::array<int, 100> i_podarray;
std::array<StructInternalSerialize, 100> i_iserarray;
std::array<StructInternalSplit, 100> i_isplarray;
std::array<StructExternalSerialize, 100> i_eserarray;
std::array<StructExternalSplit, 100> i_esplarray;

std::istringstream is(os.str());
{
IArchive iar(is);

iar(i_podarray);
iar(i_iserarray);
iar(i_isplarray);
iar(i_eserarray);
iar(i_esplarray);
}

BOOST_CHECK_EQUAL_COLLECTIONS(i_podarray.begin(), i_podarray.end(), o_podarray.begin(), o_podarray.end());
BOOST_CHECK_EQUAL_COLLECTIONS(i_iserarray.begin(), i_iserarray.end(), o_iserarray.begin(), o_iserarray.end());
BOOST_CHECK_EQUAL_COLLECTIONS(i_isplarray.begin(), i_isplarray.end(), o_isplarray.begin(), o_isplarray.end());
BOOST_CHECK_EQUAL_COLLECTIONS(i_eserarray.begin(), i_eserarray.end(), o_eserarray.begin(), o_eserarray.end());
BOOST_CHECK_EQUAL_COLLECTIONS(i_esplarray.begin(), i_esplarray.end(), o_esplarray.begin(), o_esplarray.end());
}
}
TEST_SUITE("array");

BOOST_AUTO_TEST_CASE( binary_array )
TEST_CASE("binary_array")
{
test_array<cereal::BinaryInputArchive, cereal::BinaryOutputArchive>();
}

BOOST_AUTO_TEST_CASE( portable_binary_array )
TEST_CASE("portable_binary_array")
{
test_array<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>();
}

BOOST_AUTO_TEST_CASE( xml_array )
TEST_CASE("xml_array")
{
test_array<cereal::XMLInputArchive, cereal::XMLOutputArchive>();
}

BOOST_AUTO_TEST_CASE( json_array )
TEST_CASE("json_array")
{
test_array<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
}

TEST_SUITE_END();
95 changes: 95 additions & 0 deletions unittests/array.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright (c) 2014, Randolph Voorhies, Shane Grant
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of cereal nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES AND SHANE GRANT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CEREAL_TEST_ARRAY_H_
#define CEREAL_TEST_ARRAY_H_
#include "common.hpp"

template <class IArchive, class OArchive> inline
void test_array()
{
std::random_device rd;
std::mt19937 gen(rd());

for(int ii=0; ii<100; ++ii)
{
std::array<int, 100> o_podarray;
for(auto & elem : o_podarray)
elem = random_value<int>(gen);

std::array<StructInternalSerialize, 100> o_iserarray;
for(auto & elem : o_iserarray)
elem = StructInternalSerialize( random_value<int>(gen), random_value<int>(gen) );

std::array<StructInternalSplit, 100> o_isplarray;
for(auto & elem : o_isplarray)
elem = StructInternalSplit( random_value<int>(gen), random_value<int>(gen) );

std::array<StructExternalSerialize, 100> o_eserarray;
for(auto & elem : o_eserarray)
elem = StructExternalSerialize( random_value<int>(gen), random_value<int>(gen) );

std::array<StructExternalSplit, 100> o_esplarray;
for(auto & elem : o_esplarray)
elem = StructExternalSplit( random_value<int>(gen), random_value<int>(gen) );

std::ostringstream os;
{
OArchive oar(os);

oar(o_podarray);
oar(o_iserarray);
oar(o_isplarray);
oar(o_eserarray);
oar(o_esplarray);
}

std::array<int, 100> i_podarray;
std::array<StructInternalSerialize, 100> i_iserarray;
std::array<StructInternalSplit, 100> i_isplarray;
std::array<StructExternalSerialize, 100> i_eserarray;
std::array<StructExternalSplit, 100> i_esplarray;

std::istringstream is(os.str());
{
IArchive iar(is);

iar(i_podarray);
iar(i_iserarray);
iar(i_isplarray);
iar(i_eserarray);
iar(i_esplarray);
}

check_collection( i_podarray, o_podarray );
check_collection( i_iserarray, o_iserarray );
check_collection( i_isplarray, o_isplarray );
check_collection( i_eserarray, o_eserarray );
check_collection( i_esplarray, o_esplarray );
}
}

#endif // CEREAL_TEST_ARRAY_H_
Loading

0 comments on commit a8e9963

Please sign in to comment.