From 66528b68bee4921ca73f5cc6c7aef5097e5f033e Mon Sep 17 00:00:00 2001 From: Shane Grant Date: Fri, 4 Nov 2016 15:51:57 -0700 Subject: [PATCH] last? conversions for #139 --- unittests/polymorphic.hpp | 7 +- unittests/unordered_map.cpp | 106 ++------------- unittests/unordered_map.hpp | 124 ++++++++++++++++++ unittests/unordered_multimap.cpp | 138 ++------------------ unittests/unordered_multimap.hpp | 155 ++++++++++++++++++++++ unittests/unordered_multiset.cpp | 116 ++--------------- unittests/unordered_multiset.hpp | 134 +++++++++++++++++++ unittests/unordered_set.cpp | 97 ++------------ unittests/unordered_set.hpp | 114 ++++++++++++++++ unittests/user_data_adapters.cpp | 101 ++------------ unittests/user_data_adapters.hpp | 120 +++++++++++++++++ unittests/valarray.cpp | 85 ++---------- unittests/valarray.hpp | 101 ++++++++++++++ unittests/vector.cpp | 93 ++----------- unittests/vector.hpp | 110 ++++++++++++++++ unittests/versioning.cpp | 202 ++-------------------------- unittests/versioning.hpp | 217 +++++++++++++++++++++++++++++++ 17 files changed, 1151 insertions(+), 869 deletions(-) create mode 100644 unittests/unordered_map.hpp create mode 100644 unittests/unordered_multimap.hpp create mode 100644 unittests/unordered_multiset.hpp create mode 100644 unittests/unordered_set.hpp create mode 100644 unittests/user_data_adapters.hpp create mode 100644 unittests/valarray.hpp create mode 100644 unittests/vector.hpp create mode 100644 unittests/versioning.hpp diff --git a/unittests/polymorphic.hpp b/unittests/polymorphic.hpp index 18f7ff6ec..788770b70 100644 --- a/unittests/polymorphic.hpp +++ b/unittests/polymorphic.hpp @@ -30,7 +30,6 @@ #if CEREAL_THREAD_SAFE #include -static std::mutex boostTestMutex; #endif struct PolyBaseA @@ -258,6 +257,10 @@ void test_polymorphic() std::random_device rd; std::mt19937 gen(rd()); + #if CEREAL_THREAD_SAFE + static std::mutex testMutex; + #endif + auto rngB = [&](){ return random_value( gen ) % 2 == 0; }; auto rngI = [&](){ return random_value( gen ); }; auto rngL = [&](){ return random_value( gen ); }; @@ -319,7 +322,7 @@ void test_polymorphic() auto o_lockedA = o_weakA.lock(); #if CEREAL_THREAD_SAFE - std::lock_guard lock( boostTestMutex ); + std::lock_guard lock( testMutex ); #endif CHECK_EQ(i_shared.get(), i_locked.get()); diff --git a/unittests/unordered_map.cpp b/unittests/unordered_map.cpp index df81833c9..a0c0cbbae 100644 --- a/unittests/unordered_map.cpp +++ b/unittests/unordered_map.cpp @@ -24,119 +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 +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "unordered_map.hpp" -template -void test_unordered_map() -{ - std::random_device rd; - std::mt19937 gen(rd()); - - for(int ii=0; ii<100; ++ii) - { - std::unordered_map o_podunordered_map; - for(int j=0; j<100; ++j) - o_podunordered_map.insert({random_value(gen), random_value(gen)}); - - std::unordered_map o_iserunordered_map; - for(int j=0; j<100; ++j) - o_iserunordered_map.insert({random_value(gen), { random_value(gen), random_value(gen) }}); - - std::unordered_map o_isplunordered_map; - for(int j=0; j<100; ++j) - o_isplunordered_map.insert({random_value(gen), { random_value(gen), random_value(gen) }}); - - std::unordered_map o_eserunordered_map; - for(int j=0; j<100; ++j) - o_eserunordered_map.insert({random_value(gen), { random_value(gen), random_value(gen) }}); - - std::unordered_map o_esplunordered_map; - for(int j=0; j<100; ++j) - o_esplunordered_map.insert({random_value(gen), { random_value(gen), random_value(gen) }}); - - std::ostringstream os; - { - OArchive oar(os); - - oar(o_podunordered_map); - oar(o_iserunordered_map); - oar(o_isplunordered_map); - oar(o_eserunordered_map); - oar(o_esplunordered_map); - } - - std::unordered_map i_podunordered_map; - std::unordered_map i_iserunordered_map; - std::unordered_map i_isplunordered_map; - std::unordered_map i_eserunordered_map; - std::unordered_map i_esplunordered_map; - - std::istringstream is(os.str()); - { - IArchive iar(is); - - iar(i_podunordered_map); - iar(i_iserunordered_map); - iar(i_isplunordered_map); - iar(i_eserunordered_map); - iar(i_esplunordered_map); - } - - for(auto const & p : i_podunordered_map) - { - auto v = o_podunordered_map.find(p.first); - BOOST_CHECK(v != o_podunordered_map.end()); - BOOST_CHECK_EQUAL(p.second, v->second); - } - - for(auto const & p : i_iserunordered_map) - { - auto v = o_iserunordered_map.find(p.first); - BOOST_CHECK(v != o_iserunordered_map.end()); - BOOST_CHECK_EQUAL(p.second, v->second); - } - - for(auto const & p : i_isplunordered_map) - { - auto v = o_isplunordered_map.find(p.first); - BOOST_CHECK(v != o_isplunordered_map.end()); - BOOST_CHECK_EQUAL(p.second, v->second); - } - - for(auto const & p : i_eserunordered_map) - { - auto v = o_eserunordered_map.find(p.first); - BOOST_CHECK(v != o_eserunordered_map.end()); - BOOST_CHECK_EQUAL(p.second, v->second); - } - - for(auto const & p : i_esplunordered_map) - { - auto v = o_esplunordered_map.find(p.first); - BOOST_CHECK(v != o_esplunordered_map.end()); - BOOST_CHECK_EQUAL(p.second, v->second); - } - } -} +TEST_SUITE("unordered_map"); -BOOST_AUTO_TEST_CASE( binary_unordered_map ) +TEST_CASE("binary_unordered_map") { test_unordered_map(); } -BOOST_AUTO_TEST_CASE( portable_binary_unordered_map ) +TEST_CASE("portable_binary_unordered_map") { test_unordered_map(); } -BOOST_AUTO_TEST_CASE( xml_unordered_map ) +TEST_CASE("xml_unordered_map") { test_unordered_map(); } -BOOST_AUTO_TEST_CASE( json_unordered_map ) +TEST_CASE("json_unordered_map") { test_unordered_map(); } +TEST_SUITE_END(); diff --git a/unittests/unordered_map.hpp b/unittests/unordered_map.hpp new file mode 100644 index 000000000..78961978d --- /dev/null +++ b/unittests/unordered_map.hpp @@ -0,0 +1,124 @@ +/* + 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_UNORDERED_MAP_H_ +#define CEREAL_TEST_UNORDERED_MAP_H_ +#include "common.hpp" + +template inline +void test_unordered_map() +{ + std::random_device rd; + std::mt19937 gen(rd()); + + for(int ii=0; ii<100; ++ii) + { + std::unordered_map o_podunordered_map; + for(int j=0; j<100; ++j) + o_podunordered_map.insert({random_value(gen), random_value(gen)}); + + std::unordered_map o_iserunordered_map; + for(int j=0; j<100; ++j) + o_iserunordered_map.insert({random_value(gen), { random_value(gen), random_value(gen) }}); + + std::unordered_map o_isplunordered_map; + for(int j=0; j<100; ++j) + o_isplunordered_map.insert({random_value(gen), { random_value(gen), random_value(gen) }}); + + std::unordered_map o_eserunordered_map; + for(int j=0; j<100; ++j) + o_eserunordered_map.insert({random_value(gen), { random_value(gen), random_value(gen) }}); + + std::unordered_map o_esplunordered_map; + for(int j=0; j<100; ++j) + o_esplunordered_map.insert({random_value(gen), { random_value(gen), random_value(gen) }}); + + std::ostringstream os; + { + OArchive oar(os); + + oar(o_podunordered_map); + oar(o_iserunordered_map); + oar(o_isplunordered_map); + oar(o_eserunordered_map); + oar(o_esplunordered_map); + } + + std::unordered_map i_podunordered_map; + std::unordered_map i_iserunordered_map; + std::unordered_map i_isplunordered_map; + std::unordered_map i_eserunordered_map; + std::unordered_map i_esplunordered_map; + + std::istringstream is(os.str()); + { + IArchive iar(is); + + iar(i_podunordered_map); + iar(i_iserunordered_map); + iar(i_isplunordered_map); + iar(i_eserunordered_map); + iar(i_esplunordered_map); + } + + for(auto const & p : i_podunordered_map) + { + auto v = o_podunordered_map.find(p.first); + CHECK_NE(v, o_podunordered_map.end()); + CHECK_EQ(p.second, v->second); + } + + for(auto const & p : i_iserunordered_map) + { + auto v = o_iserunordered_map.find(p.first); + CHECK_NE(v, o_iserunordered_map.end()); + CHECK_EQ(p.second, v->second); + } + + for(auto const & p : i_isplunordered_map) + { + auto v = o_isplunordered_map.find(p.first); + CHECK_NE(v, o_isplunordered_map.end()); + CHECK_EQ(p.second, v->second); + } + + for(auto const & p : i_eserunordered_map) + { + auto v = o_eserunordered_map.find(p.first); + CHECK_NE(v, o_eserunordered_map.end()); + CHECK_EQ(p.second, v->second); + } + + for(auto const & p : i_esplunordered_map) + { + auto v = o_esplunordered_map.find(p.first); + CHECK_NE(v, o_esplunordered_map.end()); + CHECK_EQ(p.second, v->second); + } + } +} + +#endif // CEREAL_TEST_UNORDERED_MAP_H_ diff --git a/unittests/unordered_multimap.cpp b/unittests/unordered_multimap.cpp index ad0913d5f..4d9a7c33f 100644 --- a/unittests/unordered_multimap.cpp +++ b/unittests/unordered_multimap.cpp @@ -24,149 +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 +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "unordered_multimap.hpp" -template -void test_unordered_multimap() -{ - std::random_device rd; - std::mt19937 gen(rd()); - - for(int ii=0; ii<100; ++ii) - { - std::unordered_multimap o_podunordered_multimap; - for(int j=0; j<100; ++j) - { - auto key = random_value(gen); - o_podunordered_multimap.insert({key, random_value(gen)}); - o_podunordered_multimap.insert({key, random_value(gen)}); - } - - std::unordered_multimap o_iserunordered_multimap; - for(int j=0; j<100; ++j) - { - auto key = random_value(gen); - o_iserunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); - o_iserunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); - } - - std::unordered_multimap o_isplunordered_multimap; - for(int j=0; j<100; ++j) - { - auto key = random_value(gen); - o_isplunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); - o_isplunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); - } - - std::unordered_multimap o_eserunordered_multimap; - for(int j=0; j<100; ++j) - { - auto key = random_value(gen); - o_eserunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); - o_eserunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); - } - - std::unordered_multimap o_esplunordered_multimap; - for(int j=0; j<100; ++j) - { - auto key = random_value(gen); - o_esplunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); - o_esplunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); - } - - std::ostringstream os; - { - OArchive oar(os); - - oar(o_podunordered_multimap); - oar(o_iserunordered_multimap); - oar(o_isplunordered_multimap); - oar(o_eserunordered_multimap); - oar(o_esplunordered_multimap); - } - - std::unordered_multimap i_podunordered_multimap; - std::unordered_multimap i_iserunordered_multimap; - std::unordered_multimap i_isplunordered_multimap; - std::unordered_multimap i_eserunordered_multimap; - std::unordered_multimap i_esplunordered_multimap; - - std::istringstream is(os.str()); - { - IArchive iar(is); +TEST_SUITE("unordered_multimap"); - iar(i_podunordered_multimap); - iar(i_iserunordered_multimap); - iar(i_isplunordered_multimap); - iar(i_eserunordered_multimap); - iar(i_esplunordered_multimap); - } - - BOOST_CHECK_EQUAL(i_podunordered_multimap.size(), o_podunordered_multimap.size()); - BOOST_CHECK_EQUAL(i_iserunordered_multimap.size(), o_iserunordered_multimap.size()); - BOOST_CHECK_EQUAL(i_isplunordered_multimap.size(), o_isplunordered_multimap.size()); - BOOST_CHECK_EQUAL(i_eserunordered_multimap.size(), o_eserunordered_multimap.size()); - BOOST_CHECK_EQUAL(i_esplunordered_multimap.size(), o_esplunordered_multimap.size()); - - for(auto const & p : i_podunordered_multimap) - { - size_t const bucket = o_podunordered_multimap.bucket(p.first); - auto bucket_begin = o_podunordered_multimap.begin(bucket); - auto bucket_end = o_podunordered_multimap.end(bucket); - BOOST_CHECK(std::find(bucket_begin, bucket_end, p) != bucket_end); - } - - for(auto const & p : i_iserunordered_multimap) - { - size_t const bucket = o_iserunordered_multimap.bucket(p.first); - auto bucket_begin = o_iserunordered_multimap.begin(bucket); - auto bucket_end = o_iserunordered_multimap.end(bucket); - BOOST_CHECK(std::find(bucket_begin, bucket_end, p) != bucket_end); - } - - for(auto const & p : i_isplunordered_multimap) - { - size_t const bucket = o_isplunordered_multimap.bucket(p.first); - auto bucket_begin = o_isplunordered_multimap.begin(bucket); - auto bucket_end = o_isplunordered_multimap.end(bucket); - BOOST_CHECK(std::find(bucket_begin, bucket_end, p) != bucket_end); - } - - for(auto const & p : i_eserunordered_multimap) - { - size_t const bucket = o_eserunordered_multimap.bucket(p.first); - auto bucket_begin = o_eserunordered_multimap.begin(bucket); - auto bucket_end = o_eserunordered_multimap.end(bucket); - BOOST_CHECK(std::find(bucket_begin, bucket_end, p) != bucket_end); - } - - for(auto const & p : i_esplunordered_multimap) - { - size_t const bucket = o_esplunordered_multimap.bucket(p.first); - auto bucket_begin = o_esplunordered_multimap.begin(bucket); - auto bucket_end = o_esplunordered_multimap.end(bucket); - BOOST_CHECK(std::find(bucket_begin, bucket_end, p) != bucket_end); - } - } -} - -BOOST_AUTO_TEST_CASE( binary_unordered_multimap ) +TEST_CASE("binary_unordered_multimap") { test_unordered_multimap(); } -BOOST_AUTO_TEST_CASE( portable_binary_unordered_multimap ) +TEST_CASE("portable_binary_unordered_multimap") { test_unordered_multimap(); } -BOOST_AUTO_TEST_CASE( xml_unordered_multimap ) +TEST_CASE("xml_unordered_multimap") { test_unordered_multimap(); } -BOOST_AUTO_TEST_CASE( json_unordered_multimap ) +TEST_CASE("json_unordered_multimap") { test_unordered_multimap(); } + +TEST_SUITE_END(); diff --git a/unittests/unordered_multimap.hpp b/unittests/unordered_multimap.hpp new file mode 100644 index 000000000..6f000e6db --- /dev/null +++ b/unittests/unordered_multimap.hpp @@ -0,0 +1,155 @@ +/* + 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_UNORDERED_MULTIMAP_H_ +#define CEREAL_TEST_UNORDERED_MULTIMAP_H_ +#include "common.hpp" + +template inline +void test_unordered_multimap() +{ + std::random_device rd; + std::mt19937 gen(rd()); + + for(int ii=0; ii<100; ++ii) + { + std::unordered_multimap o_podunordered_multimap; + for(int j=0; j<100; ++j) + { + auto key = random_value(gen); + o_podunordered_multimap.insert({key, random_value(gen)}); + o_podunordered_multimap.insert({key, random_value(gen)}); + } + + std::unordered_multimap o_iserunordered_multimap; + for(int j=0; j<100; ++j) + { + auto key = random_value(gen); + o_iserunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); + o_iserunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); + } + + std::unordered_multimap o_isplunordered_multimap; + for(int j=0; j<100; ++j) + { + auto key = random_value(gen); + o_isplunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); + o_isplunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); + } + + std::unordered_multimap o_eserunordered_multimap; + for(int j=0; j<100; ++j) + { + auto key = random_value(gen); + o_eserunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); + o_eserunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); + } + + std::unordered_multimap o_esplunordered_multimap; + for(int j=0; j<100; ++j) + { + auto key = random_value(gen); + o_esplunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); + o_esplunordered_multimap.insert({key, { random_value(gen), random_value(gen) }}); + } + + std::ostringstream os; + { + OArchive oar(os); + + oar(o_podunordered_multimap); + oar(o_iserunordered_multimap); + oar(o_isplunordered_multimap); + oar(o_eserunordered_multimap); + oar(o_esplunordered_multimap); + } + + std::unordered_multimap i_podunordered_multimap; + std::unordered_multimap i_iserunordered_multimap; + std::unordered_multimap i_isplunordered_multimap; + std::unordered_multimap i_eserunordered_multimap; + std::unordered_multimap i_esplunordered_multimap; + + std::istringstream is(os.str()); + { + IArchive iar(is); + + iar(i_podunordered_multimap); + iar(i_iserunordered_multimap); + iar(i_isplunordered_multimap); + iar(i_eserunordered_multimap); + iar(i_esplunordered_multimap); + } + + CHECK_EQ(i_podunordered_multimap.size(), o_podunordered_multimap.size()); + CHECK_EQ(i_iserunordered_multimap.size(), o_iserunordered_multimap.size()); + CHECK_EQ(i_isplunordered_multimap.size(), o_isplunordered_multimap.size()); + CHECK_EQ(i_eserunordered_multimap.size(), o_eserunordered_multimap.size()); + CHECK_EQ(i_esplunordered_multimap.size(), o_esplunordered_multimap.size()); + + for(auto const & p : i_podunordered_multimap) + { + size_t const bucket = o_podunordered_multimap.bucket(p.first); + auto bucket_begin = o_podunordered_multimap.begin(bucket); + auto bucket_end = o_podunordered_multimap.end(bucket); + CHECK_NE(std::find(bucket_begin, bucket_end, p), bucket_end); + } + + for(auto const & p : i_iserunordered_multimap) + { + size_t const bucket = o_iserunordered_multimap.bucket(p.first); + auto bucket_begin = o_iserunordered_multimap.begin(bucket); + auto bucket_end = o_iserunordered_multimap.end(bucket); + CHECK_NE(std::find(bucket_begin, bucket_end, p), bucket_end); + } + + for(auto const & p : i_isplunordered_multimap) + { + size_t const bucket = o_isplunordered_multimap.bucket(p.first); + auto bucket_begin = o_isplunordered_multimap.begin(bucket); + auto bucket_end = o_isplunordered_multimap.end(bucket); + CHECK_NE(std::find(bucket_begin, bucket_end, p), bucket_end); + } + + for(auto const & p : i_eserunordered_multimap) + { + size_t const bucket = o_eserunordered_multimap.bucket(p.first); + auto bucket_begin = o_eserunordered_multimap.begin(bucket); + auto bucket_end = o_eserunordered_multimap.end(bucket); + CHECK_NE(std::find(bucket_begin, bucket_end, p), bucket_end); + } + + for(auto const & p : i_esplunordered_multimap) + { + size_t const bucket = o_esplunordered_multimap.bucket(p.first); + auto bucket_begin = o_esplunordered_multimap.begin(bucket); + auto bucket_end = o_esplunordered_multimap.end(bucket); + CHECK_NE(std::find(bucket_begin, bucket_end, p), bucket_end); + } + } +} + +#endif // CEREAL_TEST_UNORDERED_MULTIMAP_H_ diff --git a/unittests/unordered_multiset.cpp b/unittests/unordered_multiset.cpp index 9e7a56d40..a98ffa17c 100644 --- a/unittests/unordered_multiset.cpp +++ b/unittests/unordered_multiset.cpp @@ -24,129 +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 +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "unordered_multiset.hpp" -template -void test_unordered_multiset() -{ - std::random_device rd; - std::mt19937 gen(rd()); - - for(int ii=0; ii<100; ++ii) - { - std::unordered_multiset o_podunordered_multiset; - for(int j=0; j<100; ++j) - { - int value = random_value(gen); - o_podunordered_multiset.insert(value); - o_podunordered_multiset.insert(value); - } - - std::unordered_multiset> o_iserunordered_multiset; - for(int j=0; j<100; ++j) - { - StructInternalSerialize value = { random_value(gen), random_value(gen) }; - o_iserunordered_multiset.insert(value); - o_iserunordered_multiset.insert(value); - } - - std::unordered_multiset> o_isplunordered_multiset; - for(int j=0; j<100; ++j) - { - StructInternalSplit value = { random_value(gen), random_value(gen) }; - o_isplunordered_multiset.insert(value); - o_isplunordered_multiset.insert(value); - } - - std::unordered_multiset> o_eserunordered_multiset; - for(int j=0; j<100; ++j) - { - StructExternalSerialize value = { random_value(gen), random_value(gen) }; - o_eserunordered_multiset.insert(value); - o_eserunordered_multiset.insert(value); - } - - std::unordered_multiset> o_esplunordered_multiset; - for(int j=0; j<100; ++j) - { - StructExternalSplit value = { random_value(gen), random_value(gen) }; - o_esplunordered_multiset.insert(value); - o_esplunordered_multiset.insert(value); - } - - std::ostringstream os; - { - OArchive oar(os); - - oar(o_podunordered_multiset); - oar(o_iserunordered_multiset); - oar(o_isplunordered_multiset); - oar(o_eserunordered_multiset); - oar(o_esplunordered_multiset); - } - - std::unordered_multiset i_podunordered_multiset; - std::unordered_multiset> i_iserunordered_multiset; - std::unordered_multiset> i_isplunordered_multiset; - std::unordered_multiset> i_eserunordered_multiset; - std::unordered_multiset> i_esplunordered_multiset; - - std::istringstream is(os.str()); - { - IArchive iar(is); - - iar(i_podunordered_multiset); - iar(i_iserunordered_multiset); - iar(i_isplunordered_multiset); - iar(i_eserunordered_multiset); - iar(i_esplunordered_multiset); - } - - for(auto const & p : i_podunordered_multiset) - { - BOOST_CHECK_EQUAL(o_podunordered_multiset.count(p), i_podunordered_multiset.count(p)); - } - - for(auto const & p : i_iserunordered_multiset) - { - BOOST_CHECK_EQUAL(o_iserunordered_multiset.count(p), i_iserunordered_multiset.count(p)); - } - - for(auto const & p : i_isplunordered_multiset) - { - BOOST_CHECK_EQUAL(o_isplunordered_multiset.count(p), i_isplunordered_multiset.count(p)); - } - - for(auto const & p : i_eserunordered_multiset) - { - BOOST_CHECK_EQUAL(o_eserunordered_multiset.count(p), i_eserunordered_multiset.count(p)); - } - - for(auto const & p : i_esplunordered_multiset) - { - BOOST_CHECK_EQUAL(o_esplunordered_multiset.count(p), i_esplunordered_multiset.count(p)); - } - } -} +TEST_SUITE("unordered_multiset"); -BOOST_AUTO_TEST_CASE( binary_unordered_multiset ) +TEST_CASE("binary_unordered_multiset") { test_unordered_multiset(); } -BOOST_AUTO_TEST_CASE( portable_binary_unordered_multiset ) +TEST_CASE("portable_binary_unordered_multiset") { test_unordered_multiset(); } -BOOST_AUTO_TEST_CASE( xml_unordered_multiset ) +TEST_CASE("xml_unordered_multiset") { test_unordered_multiset(); } -BOOST_AUTO_TEST_CASE( json_unordered_multiset ) +TEST_CASE("json_unordered_multiset") { test_unordered_multiset(); } +TEST_SUITE_END(); diff --git a/unittests/unordered_multiset.hpp b/unittests/unordered_multiset.hpp new file mode 100644 index 000000000..ae9d07236 --- /dev/null +++ b/unittests/unordered_multiset.hpp @@ -0,0 +1,134 @@ +/* + 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_UNORDERED_MULTISET_H_ +#define CEREAL_TEST_UNORDERED_MULTISET_H_ +#include "common.hpp" + +template inline +void test_unordered_multiset() +{ + std::random_device rd; + std::mt19937 gen(rd()); + + for(int ii=0; ii<100; ++ii) + { + std::unordered_multiset o_podunordered_multiset; + for(int j=0; j<100; ++j) + { + int value = random_value(gen); + o_podunordered_multiset.insert(value); + o_podunordered_multiset.insert(value); + } + + std::unordered_multiset> o_iserunordered_multiset; + for(int j=0; j<100; ++j) + { + StructInternalSerialize value = { random_value(gen), random_value(gen) }; + o_iserunordered_multiset.insert(value); + o_iserunordered_multiset.insert(value); + } + + std::unordered_multiset> o_isplunordered_multiset; + for(int j=0; j<100; ++j) + { + StructInternalSplit value = { random_value(gen), random_value(gen) }; + o_isplunordered_multiset.insert(value); + o_isplunordered_multiset.insert(value); + } + + std::unordered_multiset> o_eserunordered_multiset; + for(int j=0; j<100; ++j) + { + StructExternalSerialize value = { random_value(gen), random_value(gen) }; + o_eserunordered_multiset.insert(value); + o_eserunordered_multiset.insert(value); + } + + std::unordered_multiset> o_esplunordered_multiset; + for(int j=0; j<100; ++j) + { + StructExternalSplit value = { random_value(gen), random_value(gen) }; + o_esplunordered_multiset.insert(value); + o_esplunordered_multiset.insert(value); + } + + std::ostringstream os; + { + OArchive oar(os); + + oar(o_podunordered_multiset); + oar(o_iserunordered_multiset); + oar(o_isplunordered_multiset); + oar(o_eserunordered_multiset); + oar(o_esplunordered_multiset); + } + + std::unordered_multiset i_podunordered_multiset; + std::unordered_multiset> i_iserunordered_multiset; + std::unordered_multiset> i_isplunordered_multiset; + std::unordered_multiset> i_eserunordered_multiset; + std::unordered_multiset> i_esplunordered_multiset; + + std::istringstream is(os.str()); + { + IArchive iar(is); + + iar(i_podunordered_multiset); + iar(i_iserunordered_multiset); + iar(i_isplunordered_multiset); + iar(i_eserunordered_multiset); + iar(i_esplunordered_multiset); + } + + for(auto const & p : i_podunordered_multiset) + { + CHECK_EQ(o_podunordered_multiset.count(p), i_podunordered_multiset.count(p)); + } + + for(auto const & p : i_iserunordered_multiset) + { + CHECK_EQ(o_iserunordered_multiset.count(p), i_iserunordered_multiset.count(p)); + } + + for(auto const & p : i_isplunordered_multiset) + { + CHECK_EQ(o_isplunordered_multiset.count(p), i_isplunordered_multiset.count(p)); + } + + for(auto const & p : i_eserunordered_multiset) + { + CHECK_EQ(o_eserunordered_multiset.count(p), i_eserunordered_multiset.count(p)); + } + + for(auto const & p : i_esplunordered_multiset) + { + CHECK_EQ(o_esplunordered_multiset.count(p), i_esplunordered_multiset.count(p)); + } + } +} + +#endif // CEREAL_TEST_UNORDERED_MULTISET_H_ diff --git a/unittests/unordered_set.cpp b/unittests/unordered_set.cpp index a4b16b971..0c8be1436 100644 --- a/unittests/unordered_set.cpp +++ b/unittests/unordered_set.cpp @@ -24,110 +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 +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "unordered_set.hpp" -template -void test_unordered_set() -{ - std::random_device rd; - std::mt19937 gen(rd()); - - for(int ii=0; ii<100; ++ii) - { - std::unordered_set o_podunordered_set; - for(int j=0; j<100; ++j) - o_podunordered_set.insert(random_value(gen)); - - std::unordered_set> o_iserunordered_set; - for(int j=0; j<100; ++j) - o_iserunordered_set.insert({ random_value(gen), random_value(gen) }); - - std::unordered_set> o_isplunordered_set; - for(int j=0; j<100; ++j) - o_isplunordered_set.insert({ random_value(gen), random_value(gen) }); - - std::unordered_set> o_eserunordered_set; - for(int j=0; j<100; ++j) - o_eserunordered_set.insert({ random_value(gen), random_value(gen) }); - - std::unordered_set> o_esplunordered_set; - for(int j=0; j<100; ++j) - o_esplunordered_set.insert({ random_value(gen), random_value(gen) }); - - std::ostringstream os; - { - OArchive oar(os); - - oar(o_podunordered_set); - oar(o_iserunordered_set); - oar(o_isplunordered_set); - oar(o_eserunordered_set); - oar(o_esplunordered_set); - } - - std::unordered_set i_podunordered_set; - std::unordered_set> i_iserunordered_set; - std::unordered_set> i_isplunordered_set; - std::unordered_set> i_eserunordered_set; - std::unordered_set> i_esplunordered_set; +TEST_SUITE("unordered_set"); - std::istringstream is(os.str()); - { - IArchive iar(is); - - iar(i_podunordered_set); - iar(i_iserunordered_set); - iar(i_isplunordered_set); - iar(i_eserunordered_set); - iar(i_esplunordered_set); - } - - for(auto const & p : i_podunordered_set) - { - BOOST_CHECK_EQUAL(o_podunordered_set.count(p), 1lu); - } - - for(auto const & p : i_iserunordered_set) - { - BOOST_CHECK_EQUAL(o_iserunordered_set.count(p), 1lu); - } - - for(auto const & p : i_isplunordered_set) - { - BOOST_CHECK_EQUAL(o_isplunordered_set.count(p), 1lu); - } - - for(auto const & p : i_eserunordered_set) - { - BOOST_CHECK_EQUAL(o_eserunordered_set.count(p), 1lu); - } - - for(auto const & p : i_esplunordered_set) - { - BOOST_CHECK_EQUAL(o_esplunordered_set.count(p), 1lu); - } - } -} - -BOOST_AUTO_TEST_CASE( binary_unordered_set ) +TEST_CASE("binary_unordered_set") { test_unordered_set(); } -BOOST_AUTO_TEST_CASE( portable_binary_unordered_set ) +TEST_CASE("portable_binary_unordered_set") { test_unordered_set(); } -BOOST_AUTO_TEST_CASE( xml_unordered_set ) +TEST_CASE("xml_unordered_set") { test_unordered_set(); } -BOOST_AUTO_TEST_CASE( json_unordered_set ) +TEST_CASE("json_unordered_set") { test_unordered_set(); } - +TEST_SUITE_END(); diff --git a/unittests/unordered_set.hpp b/unittests/unordered_set.hpp new file mode 100644 index 000000000..334731363 --- /dev/null +++ b/unittests/unordered_set.hpp @@ -0,0 +1,114 @@ +/* + 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_UNORDERED_SET_H_ +#define CEREAL_TEST_UNORDERED_SET_H_ +#include "common.hpp" + +template inline +void test_unordered_set() +{ + std::random_device rd; + std::mt19937 gen(rd()); + + for(int ii=0; ii<100; ++ii) + { + std::unordered_set o_podunordered_set; + for(int j=0; j<100; ++j) + o_podunordered_set.insert(random_value(gen)); + + std::unordered_set> o_iserunordered_set; + for(int j=0; j<100; ++j) + o_iserunordered_set.insert({ random_value(gen), random_value(gen) }); + + std::unordered_set> o_isplunordered_set; + for(int j=0; j<100; ++j) + o_isplunordered_set.insert({ random_value(gen), random_value(gen) }); + + std::unordered_set> o_eserunordered_set; + for(int j=0; j<100; ++j) + o_eserunordered_set.insert({ random_value(gen), random_value(gen) }); + + std::unordered_set> o_esplunordered_set; + for(int j=0; j<100; ++j) + o_esplunordered_set.insert({ random_value(gen), random_value(gen) }); + + std::ostringstream os; + { + OArchive oar(os); + + oar(o_podunordered_set); + oar(o_iserunordered_set); + oar(o_isplunordered_set); + oar(o_eserunordered_set); + oar(o_esplunordered_set); + } + + std::unordered_set i_podunordered_set; + std::unordered_set> i_iserunordered_set; + std::unordered_set> i_isplunordered_set; + std::unordered_set> i_eserunordered_set; + std::unordered_set> i_esplunordered_set; + + std::istringstream is(os.str()); + { + IArchive iar(is); + + iar(i_podunordered_set); + iar(i_iserunordered_set); + iar(i_isplunordered_set); + iar(i_eserunordered_set); + iar(i_esplunordered_set); + } + + for(auto const & p : i_podunordered_set) + { + CHECK_EQ(o_podunordered_set.count(p), 1lu); + } + + for(auto const & p : i_iserunordered_set) + { + CHECK_EQ(o_iserunordered_set.count(p), 1lu); + } + + for(auto const & p : i_isplunordered_set) + { + CHECK_EQ(o_isplunordered_set.count(p), 1lu); + } + + for(auto const & p : i_eserunordered_set) + { + CHECK_EQ(o_eserunordered_set.count(p), 1lu); + } + + for(auto const & p : i_esplunordered_set) + { + CHECK_EQ(o_esplunordered_set.count(p), 1lu); + } + } +} + +#endif // CEREAL_TEST_UNORDERED_SET_H_ diff --git a/unittests/user_data_adapters.cpp b/unittests/user_data_adapters.cpp index 429d60500..9a8884806 100644 --- a/unittests/user_data_adapters.cpp +++ b/unittests/user_data_adapters.cpp @@ -24,114 +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" -#define CEREAL_FUTURE_EXPERIMENTAL -#include -#include +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "user_data_adapters.hpp" -struct SomeStruct {}; +TEST_SUITE("user_data_adapters"); -struct UserData -{ - UserData( SomeStruct * pp, SomeStruct & r ) : - p(pp), ref(r) {} - - SomeStruct * p; - std::reference_wrapper ref; -}; - -struct UserStruct -{ - UserStruct( std::int32_t i, - SomeStruct * pointer, - SomeStruct & reference ) : - i32( i ), - p( pointer ), - ref( reference ) - { } - - UserStruct & operator=( UserStruct const & ) = delete; - - std::int32_t i32; - SomeStruct const * p; - SomeStruct & ref; - - template - void serialize( Archive & ar ) - { - ar( i32 ); - } - - template - static void load_and_construct( Archive & ar, cereal::construct & construct ) - { - std::int32_t ii; - ar( ii ); - auto & data = cereal::get_user_data( ar ); - construct( ii, data.p, data.ref.get() ); - } -}; - -template -void test_user_data_adapters() -{ - std::random_device rd; - std::mt19937 gen(rd()); - - auto rng = [&](){ return random_value(gen); }; - - for(int ii=0; ii<100; ++ii) - { - SomeStruct ss; - std::unique_ptr o_ptr( new UserStruct( rng(), &ss, ss ) ); - - std::ostringstream os; - { - OArchive oar(os); - - oar(o_ptr); - } - - decltype( o_ptr ) i_ptr; - - std::istringstream is(os.str()); - { - UserData ud(&ss, ss); - cereal::UserDataAdapter iar(ud, is); - - iar(i_ptr); - } - - BOOST_CHECK_EQUAL( i_ptr->p == o_ptr->p, true ); - BOOST_CHECK_EQUAL( std::addressof(i_ptr->ref) == std::addressof(o_ptr->ref), true ); - BOOST_CHECK_EQUAL( i_ptr->i32, o_ptr->i32 ); - - std::istringstream bad_is(os.str()); - { - IArchive iar(bad_is); - - BOOST_CHECK_THROW( iar(i_ptr), ::cereal::Exception ); - } - } -} - -BOOST_AUTO_TEST_CASE( binary_user_data_adapters ) +TEST_CASE("binary_user_data_adapters") { test_user_data_adapters(); } -BOOST_AUTO_TEST_CASE( portable_binary_user_data_adapters ) +TEST_CASE("portable_binary_user_data_adapters") { test_user_data_adapters(); } -BOOST_AUTO_TEST_CASE( xml_user_data_adapters ) +TEST_CASE("xml_user_data_adapters") { test_user_data_adapters(); } -BOOST_AUTO_TEST_CASE( json_user_data_adapters ) +TEST_CASE("json_user_data_adapters") { test_user_data_adapters(); } +TEST_SUITE_END(); diff --git a/unittests/user_data_adapters.hpp b/unittests/user_data_adapters.hpp new file mode 100644 index 000000000..23a2510c0 --- /dev/null +++ b/unittests/user_data_adapters.hpp @@ -0,0 +1,120 @@ +/* + 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_USER_DATA_ADAPTERS_H_ +#define CEREAL_TEST_USER_DATA_ADAPTERS_H_ + +#include "common.hpp" +#define CEREAL_FUTURE_EXPERIMENTAL +#include + +struct SomeStruct {}; + +struct UserData +{ + UserData( SomeStruct * pp, SomeStruct & r ) : + p(pp), ref(r) {} + + SomeStruct * p; + std::reference_wrapper ref; +}; + +struct UserStruct +{ + UserStruct( std::int32_t i, + SomeStruct * pointer, + SomeStruct & reference ) : + i32( i ), + p( pointer ), + ref( reference ) + { } + + UserStruct & operator=( UserStruct const & ) = delete; + + std::int32_t i32; + SomeStruct const * p; + SomeStruct & ref; + + template + void serialize( Archive & ar ) + { + ar( i32 ); + } + + template + static void load_and_construct( Archive & ar, cereal::construct & construct ) + { + std::int32_t ii; + ar( ii ); + auto & data = cereal::get_user_data( ar ); + construct( ii, data.p, data.ref.get() ); + } +}; + +template inline +void test_user_data_adapters() +{ + std::random_device rd; + std::mt19937 gen(rd()); + + auto rng = [&](){ return random_value(gen); }; + + for(int ii=0; ii<100; ++ii) + { + SomeStruct ss; + std::unique_ptr o_ptr( new UserStruct( rng(), &ss, ss ) ); + + std::ostringstream os; + { + OArchive oar(os); + + oar(o_ptr); + } + + decltype( o_ptr ) i_ptr; + + std::istringstream is(os.str()); + { + UserData ud(&ss, ss); + cereal::UserDataAdapter iar(ud, is); + + iar(i_ptr); + } + + CHECK_EQ( i_ptr->p, o_ptr->p ); + CHECK_EQ( std::addressof(i_ptr->ref), std::addressof(o_ptr->ref) ); + CHECK_EQ( i_ptr->i32, o_ptr->i32 ); + + std::istringstream bad_is(os.str()); + { + IArchive iar(bad_is); + + CHECK_THROWS_AS( iar(i_ptr), ::cereal::Exception ); + } + } +} + +#endif // CEREAL_TEST_USER_DATA_ADAPTERS_H_ diff --git a/unittests/valarray.cpp b/unittests/valarray.cpp index 7a1fbd1f5..4866c0297 100644 --- a/unittests/valarray.cpp +++ b/unittests/valarray.cpp @@ -24,96 +24,29 @@ 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. */ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "valarray.hpp" -#include "common.hpp" -#include +TEST_SUITE("valarray"); -template -void test_valarray() -{ - std::random_device rd; - std::mt19937 gen(rd()); - - for (int ii = 0; ii<100; ++ii) - { - std::valarray o_podvalarray(100); - for (auto & elem : o_podvalarray) - elem = random_value(gen); - - std::valarray o_iservalarray(100); - for (auto & elem : o_iservalarray) - elem = StructInternalSerialize(random_value(gen), random_value(gen)); - - std::valarray o_isplvalarray(100); - for (auto & elem : o_isplvalarray) - elem = StructInternalSplit(random_value(gen), random_value(gen)); - - std::valarray o_eservalarray(100); - for (auto & elem : o_eservalarray) - elem = StructExternalSerialize(random_value(gen), random_value(gen)); - - std::valarray o_esplvalarray(100); - for (auto & elem : o_esplvalarray) - elem = StructExternalSplit(random_value(gen), random_value(gen)); - - std::ostringstream os; - { - OArchive oar(os); - - oar(o_podvalarray); - oar(o_iservalarray); - oar(o_isplvalarray); - oar(o_eservalarray); - oar(o_esplvalarray); - } - - std::valarray i_podvalarray; - std::valarray i_iservalarray; - std::valarray i_isplvalarray; - std::valarray i_eservalarray; - std::valarray i_esplvalarray; - - std::istringstream is(os.str()); - { - IArchive iar(is); - - iar(i_podvalarray); - iar(i_iservalarray); - iar(i_isplvalarray); - iar(i_eservalarray); - iar(i_esplvalarray); - } - - BOOST_CHECK_EQUAL(i_podvalarray.size(), o_podvalarray.size()); - BOOST_CHECK_EQUAL(i_iservalarray.size(), o_iservalarray.size()); - BOOST_CHECK_EQUAL(i_isplvalarray.size(), o_isplvalarray.size()); - BOOST_CHECK_EQUAL(i_eservalarray.size(), o_eservalarray.size()); - BOOST_CHECK_EQUAL(i_esplvalarray.size(), o_esplvalarray.size()); - - BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_podvalarray), std::end(i_podvalarray), std::begin(o_podvalarray), std::end(o_podvalarray)); - BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_iservalarray), std::end(i_iservalarray), std::begin(o_iservalarray), std::end(o_iservalarray)); - BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_isplvalarray), std::end(i_isplvalarray), std::begin(o_isplvalarray), std::end(o_isplvalarray)); - BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_eservalarray), std::end(i_eservalarray), std::begin(o_eservalarray), std::end(o_eservalarray)); - BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_esplvalarray), std::end(i_esplvalarray), std::begin(o_esplvalarray), std::end(o_esplvalarray)); - } -} - -BOOST_AUTO_TEST_CASE(binary_valarray) +TEST_CASE("binary_valarray") { test_valarray(); } -BOOST_AUTO_TEST_CASE(portable_binary_valarray) +TEST_CASE("portable_binary_valarray") { test_valarray(); } -BOOST_AUTO_TEST_CASE(xml_valarray) +TEST_CASE("xml_valarray") { test_valarray(); } -BOOST_AUTO_TEST_CASE(json_valarray) +TEST_CASE("json_valarray") { test_valarray(); } + +TEST_SUITE_END(); diff --git a/unittests/valarray.hpp b/unittests/valarray.hpp new file mode 100644 index 000000000..19df153be --- /dev/null +++ b/unittests/valarray.hpp @@ -0,0 +1,101 @@ +/* +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_VALARRAY_H_ +#define CEREAL_TEST_VALARRAY_H_ +#include "common.hpp" + +template inline +void test_valarray() +{ + std::random_device rd; + std::mt19937 gen(rd()); + + for (int ii = 0; ii<100; ++ii) + { + std::valarray o_podvalarray(100); + for (auto & elem : o_podvalarray) + elem = random_value(gen); + + std::valarray o_iservalarray(100); + for (auto & elem : o_iservalarray) + elem = StructInternalSerialize(random_value(gen), random_value(gen)); + + std::valarray o_isplvalarray(100); + for (auto & elem : o_isplvalarray) + elem = StructInternalSplit(random_value(gen), random_value(gen)); + + std::valarray o_eservalarray(100); + for (auto & elem : o_eservalarray) + elem = StructExternalSerialize(random_value(gen), random_value(gen)); + + std::valarray o_esplvalarray(100); + for (auto & elem : o_esplvalarray) + elem = StructExternalSplit(random_value(gen), random_value(gen)); + + std::ostringstream os; + { + OArchive oar(os); + + oar(o_podvalarray); + oar(o_iservalarray); + oar(o_isplvalarray); + oar(o_eservalarray); + oar(o_esplvalarray); + } + + std::valarray i_podvalarray; + std::valarray i_iservalarray; + std::valarray i_isplvalarray; + std::valarray i_eservalarray; + std::valarray i_esplvalarray; + + std::istringstream is(os.str()); + { + IArchive iar(is); + + iar(i_podvalarray); + iar(i_iservalarray); + iar(i_isplvalarray); + iar(i_eservalarray); + iar(i_esplvalarray); + } + + CHECK_EQ(i_podvalarray.size(), o_podvalarray.size()); + CHECK_EQ(i_iservalarray.size(), o_iservalarray.size()); + CHECK_EQ(i_isplvalarray.size(), o_isplvalarray.size()); + CHECK_EQ(i_eservalarray.size(), o_eservalarray.size()); + CHECK_EQ(i_esplvalarray.size(), o_esplvalarray.size()); + + check_collection(i_podvalarray , o_podvalarray ); + check_collection(i_iservalarray, o_iservalarray); + check_collection(i_isplvalarray, o_isplvalarray); + check_collection(i_eservalarray, o_eservalarray); + check_collection(i_esplvalarray, o_esplvalarray); + } +} + +#endif // CEREAL_TEST_VALARRAY_H_ diff --git a/unittests/vector.cpp b/unittests/vector.cpp index fdb686951..be172ba58 100644 --- a/unittests/vector.cpp +++ b/unittests/vector.cpp @@ -24,106 +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 +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "vector.hpp" -template -void test_vector() -{ - std::random_device rd; - std::mt19937 gen(rd()); - - for(int ii=0; ii<100; ++ii) - { - std::vector o_podvector(100); - for(auto & elem : o_podvector) - elem = random_value(gen); - - std::vector o_boolvector; o_boolvector.resize(100); - for( size_t i = 0; i < 100; ++i ) - o_boolvector[i] = (random_value(gen) % 2) == 0; - - std::vector o_iservector(100); - for(auto & elem : o_iservector) - elem = StructInternalSerialize( random_value(gen), random_value(gen) ); - - std::vector o_isplvector(100); - for(auto & elem : o_isplvector) - elem = StructInternalSplit( random_value(gen), random_value(gen) ); - - std::vector o_eservector(100); - for(auto & elem : o_eservector) - elem = StructExternalSerialize( random_value(gen), random_value(gen) ); - - std::vector o_esplvector(100); - for(auto & elem : o_esplvector) - elem = StructExternalSplit( random_value(gen), random_value(gen) ); - - std::ostringstream os; - { - OArchive oar(os); +TEST_SUITE("vector"); - oar(o_podvector); - oar(o_boolvector); - oar(o_iservector); - oar(o_isplvector); - oar(o_eservector); - oar(o_esplvector); - } - - std::vector i_podvector; - std::vector i_boolvector; - std::vector i_iservector; - std::vector i_isplvector; - std::vector i_eservector; - std::vector i_esplvector; - - std::istringstream is(os.str()); - { - IArchive iar(is); - - iar(i_podvector); - iar(i_boolvector); - iar(i_iservector); - iar(i_isplvector); - iar(i_eservector); - iar(i_esplvector); - } - - BOOST_CHECK_EQUAL(i_podvector.size(), o_podvector.size()); - BOOST_CHECK_EQUAL(i_boolvector.size(), o_boolvector.size()); - BOOST_CHECK_EQUAL(i_iservector.size(), o_iservector.size()); - BOOST_CHECK_EQUAL(i_isplvector.size(), o_isplvector.size()); - BOOST_CHECK_EQUAL(i_eservector.size(), o_eservector.size()); - BOOST_CHECK_EQUAL(i_esplvector.size(), o_esplvector.size()); - - BOOST_CHECK_EQUAL_COLLECTIONS(i_podvector.begin(), i_podvector.end(), o_podvector.begin(), o_podvector.end()); - BOOST_CHECK_EQUAL_COLLECTIONS(i_boolvector.begin(), i_boolvector.end(), o_boolvector.begin(), o_boolvector.end()); - BOOST_CHECK_EQUAL_COLLECTIONS(i_iservector.begin(), i_iservector.end(), o_iservector.begin(), o_iservector.end()); - BOOST_CHECK_EQUAL_COLLECTIONS(i_isplvector.begin(), i_isplvector.end(), o_isplvector.begin(), o_isplvector.end()); - BOOST_CHECK_EQUAL_COLLECTIONS(i_eservector.begin(), i_eservector.end(), o_eservector.begin(), o_eservector.end()); - BOOST_CHECK_EQUAL_COLLECTIONS(i_esplvector.begin(), i_esplvector.end(), o_esplvector.begin(), o_esplvector.end()); - } -} - -BOOST_AUTO_TEST_CASE( binary_vector ) +TEST_CASE("binary_vector") { test_vector(); } -BOOST_AUTO_TEST_CASE( portable_binary_vector ) +TEST_CASE("portable_binary_vector") { test_vector(); } -BOOST_AUTO_TEST_CASE( xml_vector ) +TEST_CASE("xml_vector") { test_vector(); } -BOOST_AUTO_TEST_CASE( json_vector ) +TEST_CASE("json_vector") { test_vector(); } - +TEST_SUITE_END(); diff --git a/unittests/vector.hpp b/unittests/vector.hpp new file mode 100644 index 000000000..a0297905b --- /dev/null +++ b/unittests/vector.hpp @@ -0,0 +1,110 @@ +/* + 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_VECTOR_H_ +#define CEREAL_TEST_VECTOR_H_ +#include "common.hpp" + +template inline +void test_vector() +{ + std::random_device rd; + std::mt19937 gen(rd()); + + for(int ii=0; ii<100; ++ii) + { + std::vector o_podvector(100); + for(auto & elem : o_podvector) + elem = random_value(gen); + + std::vector o_boolvector; o_boolvector.resize(100); + for( size_t i = 0; i < 100; ++i ) + o_boolvector[i] = (random_value(gen) % 2) == 0; + + std::vector o_iservector(100); + for(auto & elem : o_iservector) + elem = StructInternalSerialize( random_value(gen), random_value(gen) ); + + std::vector o_isplvector(100); + for(auto & elem : o_isplvector) + elem = StructInternalSplit( random_value(gen), random_value(gen) ); + + std::vector o_eservector(100); + for(auto & elem : o_eservector) + elem = StructExternalSerialize( random_value(gen), random_value(gen) ); + + std::vector o_esplvector(100); + for(auto & elem : o_esplvector) + elem = StructExternalSplit( random_value(gen), random_value(gen) ); + + std::ostringstream os; + { + OArchive oar(os); + + oar(o_podvector); + oar(o_boolvector); + oar(o_iservector); + oar(o_isplvector); + oar(o_eservector); + oar(o_esplvector); + } + + std::vector i_podvector; + std::vector i_boolvector; + std::vector i_iservector; + std::vector i_isplvector; + std::vector i_eservector; + std::vector i_esplvector; + + std::istringstream is(os.str()); + { + IArchive iar(is); + + iar(i_podvector); + iar(i_boolvector); + iar(i_iservector); + iar(i_isplvector); + iar(i_eservector); + iar(i_esplvector); + } + + CHECK_EQ(i_podvector.size(), o_podvector.size()); + CHECK_EQ(i_boolvector.size(), o_boolvector.size()); + CHECK_EQ(i_iservector.size(), o_iservector.size()); + CHECK_EQ(i_isplvector.size(), o_isplvector.size()); + CHECK_EQ(i_eservector.size(), o_eservector.size()); + CHECK_EQ(i_esplvector.size(), o_esplvector.size()); + + check_collection(i_podvector, o_podvector ); + check_collection(i_boolvector, o_boolvector); + check_collection(i_iservector, o_iservector); + check_collection(i_isplvector, o_isplvector); + check_collection(i_eservector, o_eservector); + check_collection(i_esplvector, o_esplvector); + } +} + +#endif // CEREAL_TEST_VECTOR_H_ diff --git a/unittests/versioning.cpp b/unittests/versioning.cpp index 218a0ea86..872742644 100644 --- a/unittests/versioning.cpp +++ b/unittests/versioning.cpp @@ -24,227 +24,51 @@ (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 +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "versioning.hpp" -#if CEREAL_THREAD_SAFE -#include -static std::mutex boostTestMutex; -#endif - -namespace Nested -{ - struct NestedClass - { - int x; - - template - void serialize( Archive & ar ) - { ar( x ); } - }; -} - -CEREAL_CLASS_VERSION( Nested::NestedClass, 1 ) - -class VersionStructMS -{ - public: - bool x; - std::uint32_t v; - - private: - friend class cereal::access; - template - void serialize( Archive & ar, std::uint32_t const version ) - { - ar( x ); - v = version; - } -}; - -struct VersionStructMSP -{ - uint8_t x; - std::uint32_t v; - template - void save( Archive & ar, std::uint32_t const /*version*/ ) const - { - ar( x ); - } - - template - void load( Archive & ar, std::uint32_t const version ) - { - ar( x ); - v = version; - } -}; - -struct VersionStructNMS -{ - std::int32_t x; - std::uint32_t v; -}; - -template -void serialize( Archive & ar, VersionStructNMS & vnms, const std::uint32_t version ) -{ - ar( vnms.x ); - vnms.v = version; -} - -struct VersionStructNMSP -{ - double x; - std::uint32_t v; -}; - -template -void save( Archive & ar, VersionStructNMSP const & vnms, const std::uint32_t /*version*/ ) -{ - ar( vnms.x ); -} - -template -void load( Archive & ar, VersionStructNMSP & vnms, const std::uint32_t version ) -{ - ar( vnms.x ); - vnms.v = version; -} - -CEREAL_CLASS_VERSION( VersionStructMSP, 33 ) -CEREAL_CLASS_VERSION( VersionStructNMS, 66 ) -CEREAL_CLASS_VERSION( VersionStructNMSP, 99 ) +TEST_SUITE("versioning"); -template -void test_versioning() -{ - std::random_device rd; - std::mt19937 gen(rd()); - - for(size_t i=0; i<100; ++i) - { - VersionStructMS o_MS = {random_value(gen) % 2 ? true : false, 1}; - VersionStructMSP o_MSP = {random_value(gen), 1}; - VersionStructNMS o_NMS = {random_value(gen), 1}; - VersionStructNMSP o_NMSP = {random_value(gen), 1}; - VersionStructMS o_MS2 = {random_value(gen) % 2 ? true : false, 1}; - VersionStructMSP o_MSP2 = {random_value(gen), 1}; - VersionStructNMS o_NMS2 = {random_value(gen), 1}; - VersionStructNMSP o_NMSP2 = {random_value(gen), 1}; - - std::ostringstream os; - { - OArchive oar(os); - oar( o_MS ); - oar( o_MSP ); - oar( o_NMS ); - oar( o_NMSP ); - oar( o_MS2 ); - oar( o_MSP2 ); - oar( o_NMS2 ); - oar( o_NMSP2 ); - } - - decltype(o_MS) i_MS; - decltype(o_MSP) i_MSP; - decltype(o_NMS) i_NMS; - decltype(o_NMSP) i_NMSP; - decltype(o_MS2) i_MS2; - decltype(o_MSP2) i_MSP2; - decltype(o_NMS2) i_NMS2; - decltype(o_NMSP2) i_NMSP2; - - std::istringstream is(os.str()); - { - IArchive iar(is); - iar( i_MS ); - iar( i_MSP ); - iar( i_NMS ); - iar( i_NMSP ); - iar( i_MS2 ); - iar( i_MSP2 ); - iar( i_NMS2 ); - iar( i_NMSP2 ); - } - - #if CEREAL_THREAD_SAFE - std::lock_guard lock( boostTestMutex ); - #endif - - BOOST_CHECK_EQUAL(o_MS.x, i_MS.x); - BOOST_CHECK_EQUAL(i_MS.v, 0u); - BOOST_CHECK_EQUAL(o_MSP.x, i_MSP.x); - BOOST_CHECK_EQUAL(i_MSP.v, 33u); - BOOST_CHECK_EQUAL(o_NMS.x, i_NMS.x); - BOOST_CHECK_EQUAL(i_NMS.v, 66u); - BOOST_CHECK_CLOSE(o_NMSP.x, i_NMSP.x, 1e-5); - BOOST_CHECK_EQUAL(i_NMSP.v, 99u); - - BOOST_CHECK_EQUAL(o_MS2.x, i_MS2.x); - BOOST_CHECK_EQUAL(i_MS2.v, 0u); - BOOST_CHECK_EQUAL(o_MSP2.x, i_MSP2.x); - BOOST_CHECK_EQUAL(i_MSP2.v, 33u); - BOOST_CHECK_EQUAL(o_NMS2.x, i_NMS2.x); - BOOST_CHECK_EQUAL(i_NMS2.v, 66u); - BOOST_CHECK_CLOSE(o_NMSP2.x, i_NMSP2.x, 1e-5); - BOOST_CHECK_EQUAL(i_NMSP2.v, 99u); - } -} - -BOOST_AUTO_TEST_CASE( binary_versioning ) +TEST_CASE("binary_versioning") { test_versioning(); } -BOOST_AUTO_TEST_CASE( portable_binary_versioning ) +TEST_CASE("portable_binary_versioning") { test_versioning(); } -BOOST_AUTO_TEST_CASE( xml_versioning ) +TEST_CASE("xml_versioning") { test_versioning(); } -BOOST_AUTO_TEST_CASE( json_versioning ) +TEST_CASE("json_versioning") { test_versioning(); } #if CEREAL_THREAD_SAFE -template -void test_versioning_threading() -{ - std::vector> pool; - for( size_t i = 0; i < 100; ++i ) - pool.emplace_back( std::async( std::launch::async, - [](){ test_versioning(); return true; } ) ); - - for( auto & future : pool ) - future.wait(); - - for( auto & future : pool ) - BOOST_CHECK( future.get() == true ); -} - -BOOST_AUTO_TEST_CASE( binary_versioning_threading ) +TEST_CASE("binary_versioning_threading") { test_versioning_threading(); } -BOOST_AUTO_TEST_CASE( portable_binary_versioning_threading ) +TEST_CASE("portable_binary_versioning_threading") { test_versioning_threading(); } -BOOST_AUTO_TEST_CASE( xml_versioning_threading ) +TEST_CASE("xml_versioning_threading") { test_versioning_threading(); } -BOOST_AUTO_TEST_CASE( json_versioning_threading ) +TEST_CASE("json_versioning_threading") { test_versioning_threading(); } #endif // CEREAL_THREAD_SAFE + +TEST_SUITE_END(); diff --git a/unittests/versioning.hpp b/unittests/versioning.hpp new file mode 100644 index 000000000..33e21d261 --- /dev/null +++ b/unittests/versioning.hpp @@ -0,0 +1,217 @@ +/* + 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_VERSIONING_H_ +#define CEREAL_TEST_VERSIONING_H_ +#include "common.hpp" + +#if CEREAL_THREAD_SAFE +#include +#endif + +namespace Nested +{ + struct NestedClass + { + int x; + + template + void serialize( Archive & ar ) + { ar( x ); } + }; +} + +CEREAL_CLASS_VERSION( Nested::NestedClass, 1 ) + +class VersionStructMS +{ + public: + bool x; + std::uint32_t v; + + private: + friend class cereal::access; + template + void serialize( Archive & ar, std::uint32_t const version ) + { + ar( x ); + v = version; + } +}; + +struct VersionStructMSP +{ + uint8_t x; + std::uint32_t v; + template + void save( Archive & ar, std::uint32_t const /*version*/ ) const + { + ar( x ); + } + + template + void load( Archive & ar, std::uint32_t const version ) + { + ar( x ); + v = version; + } +}; + +struct VersionStructNMS +{ + std::int32_t x; + std::uint32_t v; +}; + +template +void serialize( Archive & ar, VersionStructNMS & vnms, const std::uint32_t version ) +{ + ar( vnms.x ); + vnms.v = version; +} + +struct VersionStructNMSP +{ + double x; + std::uint32_t v; +}; + +template +void save( Archive & ar, VersionStructNMSP const & vnms, const std::uint32_t /*version*/ ) +{ + ar( vnms.x ); +} + +template +void load( Archive & ar, VersionStructNMSP & vnms, const std::uint32_t version ) +{ + ar( vnms.x ); + vnms.v = version; +} + +CEREAL_CLASS_VERSION( VersionStructMSP, 33 ) +CEREAL_CLASS_VERSION( VersionStructNMS, 66 ) +CEREAL_CLASS_VERSION( VersionStructNMSP, 99 ) + +template inline +void test_versioning() +{ + std::random_device rd; + std::mt19937 gen(rd()); + + #if CEREAL_THREAD_SAFE + #include + static std::mutex testMutex; + #endif + + for(size_t i=0; i<100; ++i) + { + VersionStructMS o_MS = {random_value(gen) % 2 ? true : false, 1}; + VersionStructMSP o_MSP = {random_value(gen), 1}; + VersionStructNMS o_NMS = {random_value(gen), 1}; + VersionStructNMSP o_NMSP = {random_value(gen), 1}; + VersionStructMS o_MS2 = {random_value(gen) % 2 ? true : false, 1}; + VersionStructMSP o_MSP2 = {random_value(gen), 1}; + VersionStructNMS o_NMS2 = {random_value(gen), 1}; + VersionStructNMSP o_NMSP2 = {random_value(gen), 1}; + + std::ostringstream os; + { + OArchive oar(os); + oar( o_MS ); + oar( o_MSP ); + oar( o_NMS ); + oar( o_NMSP ); + oar( o_MS2 ); + oar( o_MSP2 ); + oar( o_NMS2 ); + oar( o_NMSP2 ); + } + + decltype(o_MS) i_MS; + decltype(o_MSP) i_MSP; + decltype(o_NMS) i_NMS; + decltype(o_NMSP) i_NMSP; + decltype(o_MS2) i_MS2; + decltype(o_MSP2) i_MSP2; + decltype(o_NMS2) i_NMS2; + decltype(o_NMSP2) i_NMSP2; + + std::istringstream is(os.str()); + { + IArchive iar(is); + iar( i_MS ); + iar( i_MSP ); + iar( i_NMS ); + iar( i_NMSP ); + iar( i_MS2 ); + iar( i_MSP2 ); + iar( i_NMS2 ); + iar( i_NMSP2 ); + } + + #if CEREAL_THREAD_SAFE + std::lock_guard lock( testMutex ); + #endif + + CHECK_EQ(o_MS.x, i_MS.x); + CHECK_EQ(i_MS.v, 0u); + CHECK_EQ(o_MSP.x, i_MSP.x); + CHECK_EQ(i_MSP.v, 33u); + CHECK_EQ(o_NMS.x, i_NMS.x); + CHECK_EQ(i_NMS.v, 66u); + CHECK_EQ(o_NMSP.x, doctest::Approx(i_NMSP.x).epsilon(1e-5)); + CHECK_EQ(i_NMSP.v, 99u); + + CHECK_EQ(o_MS2.x, i_MS2.x); + CHECK_EQ(i_MS2.v, 0u); + CHECK_EQ(o_MSP2.x, i_MSP2.x); + CHECK_EQ(i_MSP2.v, 33u); + CHECK_EQ(o_NMS2.x, i_NMS2.x); + CHECK_EQ(i_NMS2.v, 66u); + CHECK_EQ(o_NMSP2.x, doctest::Approx(i_NMSP2.x).epsilon(1e-5)); + CHECK_EQ(i_NMSP2.v, 99u); + } +} + +#if CEREAL_THREAD_SAFE +template inline +void test_versioning_threading() +{ + std::vector> pool; + for( size_t i = 0; i < 100; ++i ) + pool.emplace_back( std::async( std::launch::async, + [](){ test_versioning(); return true; } ) ); + + for( auto & future : pool ) + future.wait(); + + for( auto & future : pool ) + CHECK_UNARY( future.get() ); +} +#endif // CEREAL_THREAD_SAFE + +#endif // CEREAL_TEST_VERSIONING_H_