Skip to content

Commit

Permalink
Merge pull request #2614 from STEllAR-GROUP/serialize_deque
Browse files Browse the repository at this point in the history
Adding serialization for std::deque
  • Loading branch information
hkaiser committed May 9, 2017
2 parents f7b118e + 78bebd8 commit 53dbad1
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 1 deletion.
46 changes: 46 additions & 0 deletions hpx/runtime/serialization/deque.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2017 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef HPX_SERIALIZATION_DEQUE_HPP
#define HPX_SERIALIZATION_DEQUE_HPP

#include <hpx/runtime/serialization/serialize.hpp>

#include <deque>

namespace hpx { namespace serialization
{
template <typename T, typename Allocator>
void serialize(input_archive & ar, std::deque<T, Allocator> & d, unsigned)
{
// normal load ...
typedef typename std::deque<T, Allocator>::size_type size_type;
size_type size;
ar >> size; //-V128

d.resize(size);
if(size == 0) return;

for(auto & e: d)
{
ar >> e;
}
}

template <typename T, typename Allocator>
void serialize(output_archive & ar, const std::deque<T, Allocator> & d, unsigned)
{
// normal save ...
ar << d.size(); //-V128
if(d.empty()) return;

for(auto const & e: d)
{
ar << e;
}
}
}}

#endif
3 changes: 2 additions & 1 deletion tests/unit/serialization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ set(tests
serialization_builtins
serialization_complex
serialization_custom_constructor
serialization_list
serialization_deque
serialization_map
serialization_list
serialization_set
serialization_simple
serialization_smart_ptr
Expand Down
211 changes: 211 additions & 0 deletions tests/unit/serialization/serialization_deque.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
// Copyright (c) 2014 Thomas Heller
// Copyright (c) 2017 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <hpx/runtime/serialization/serialize.hpp>
#include <hpx/runtime/serialization/deque.hpp>

#include <hpx/runtime/serialization/input_archive.hpp>
#include <hpx/runtime/serialization/output_archive.hpp>

#include <hpx/util/lightweight_test.hpp>

#include <cstddef>
#include <deque>
#include <vector>

template <typename T>
struct A
{
A() {}

A(T t) : t_(t) {}
T t_;

A & operator=(T t) { t_ = t; return *this; }

template <typename Archive>
void serialize(Archive & ar, unsigned)
{
ar & t_;
}
};

void test_bool()
{
{
std::vector<char> buffer;
hpx::serialization::output_archive oarchive(buffer);

std::deque<bool> os;
os.push_back(true);
os.push_back(false);
os.push_back(false);
os.push_back(true);
oarchive << os;

hpx::serialization::input_archive iarchive(buffer);
std::deque<bool> is;
iarchive >> is;
HPX_TEST_EQ(os.size(), is.size());
auto ot = os.begin();
auto it = is.begin();
for(std::size_t i = 0; i < os.size(); ++i)
{
HPX_TEST_EQ(*ot, *it);
}
}
{
std::vector<char> buffer;
hpx::serialization::output_archive oarchive(buffer);

std::deque<A<bool> > os;
os.push_back(true);
os.push_back(false);
os.push_back(false);
os.push_back(true);
oarchive << os;

hpx::serialization::input_archive iarchive(buffer);
std::deque<A<bool> > is;
iarchive >> is;
HPX_TEST_EQ(os.size(), is.size());
auto ot = os.begin();
auto it = is.begin();
for(std::size_t i = 0; i < os.size(); ++i)
{
HPX_TEST_EQ(ot->t_, it->t_);
}
}
}

template <typename T>
void test(T min, T max)
{
{
std::vector<char> buffer;
hpx::serialization::output_archive oarchive(buffer);
std::deque<T> os;
for(T c = min; c < max; ++c)
{
os.push_back(c);
}
oarchive << os;
hpx::serialization::input_archive iarchive(buffer);
std::deque<T> is;
iarchive >> is;
HPX_TEST_EQ(os.size(), is.size());
auto ot = os.begin();
auto it = is.begin();
for(std::size_t i = 0; i < os.size(); ++i)
{
HPX_TEST_EQ(*ot, *it);
}
}
{
std::vector<char> buffer;
hpx::serialization::output_archive oarchive(buffer);
std::deque<A<T> > os;
for(T c = min; c < max; ++c)
{
os.push_back(c);
}
oarchive << os;
hpx::serialization::input_archive iarchive(buffer);
std::deque<A<T> > is;
iarchive >> is;
HPX_TEST_EQ(os.size(), is.size());
auto ot = os.begin();
auto it = is.begin();
for(std::size_t i = 0; i < os.size(); ++i)
{
HPX_TEST_EQ(ot->t_, it->t_);
}
}
}

template <typename T>
void test_fp(T min, T max)
{
{
std::vector<char> buffer;
hpx::serialization::output_archive oarchive(buffer);
std::deque<T> os;
for(T c = min; c < max; c += static_cast<T>(0.5))
{
os.push_back(c);
}
oarchive << os;
hpx::serialization::input_archive iarchive(buffer);
std::deque<T> is;
iarchive >> is;
HPX_TEST_EQ(os.size(), is.size());
auto ot = os.begin();
auto it = is.begin();
for(std::size_t i = 0; i < os.size(); ++i)
{
HPX_TEST_EQ(*ot, *it);
}
}
{
std::vector<char> buffer;
hpx::serialization::output_archive oarchive(buffer);
std::deque<A<T> > os;
for(T c = min; c < max; c += static_cast<T>(0.5))
{
os.push_back(c);
}
oarchive << os;
hpx::serialization::input_archive iarchive(buffer);
std::deque<A<T> > is;
iarchive >> is;
HPX_TEST_EQ(os.size(), is.size());
auto ot = os.begin();
auto it = is.begin();
for(std::size_t i = 0; i < os.size(); ++i)
{
HPX_TEST_EQ(ot->t_, it->t_);
}
}
}

int main()
{
test_bool();
test<char>((std::numeric_limits<char>::min)(),
(std::numeric_limits<char>::max)());
test<int>((std::numeric_limits<int>::min)(),
(std::numeric_limits<int>::min)() + 100);
test<int>((std::numeric_limits<int>::max)() - 100,
(std::numeric_limits<int>::max)());
test<int>(-100, 100);
test<unsigned>((std::numeric_limits<unsigned>::min)(),
(std::numeric_limits<unsigned>::min)() + 100);
test<unsigned>((std::numeric_limits<unsigned>::max)() - 100,
(std::numeric_limits<unsigned>::max)());
test<long>((std::numeric_limits<long>::min)(),
(std::numeric_limits<long>::min)() + 100);
test<long>((std::numeric_limits<long>::max)() - 100,
(std::numeric_limits<long>::max)());
test<long>(-100, 100);
test<unsigned long>((std::numeric_limits<unsigned long>::min)(),
(std::numeric_limits<unsigned long>::min)() + 100);
test<unsigned long>((std::numeric_limits<unsigned long>::max)() - 100,
(std::numeric_limits<unsigned long>::max)());
test_fp<float>((std::numeric_limits<float>::min)(),
(std::numeric_limits<float>::min)() + 100);
test_fp<float>((std::numeric_limits<float>::max)() - 100,
(std::numeric_limits<float>::max)()); //it's incorrect
// because floatmax() - 100 causes cancellations error,
// digits are not affected
test_fp<float>(-100, 100);
test<double>((std::numeric_limits<double>::min)(),
(std::numeric_limits<double>::min)() + 100);
test<double>((std::numeric_limits<double>::max)() - 100,
(std::numeric_limits<double>::max)()); //it's the same
test<double>(-100, 100);

return hpx::util::report_errors();
}

0 comments on commit 53dbad1

Please sign in to comment.