| 1 |
#include "UnitTest++.h" |
| 2 |
|
| 3 |
#include "test_utils.hh" |
| 4 |
|
| 5 |
#include "geners/VarPack.hh" |
| 6 |
#include "geners/StringArchive.hh" |
| 7 |
#include "geners/Record.hh" |
| 8 |
#include "geners/Reference.hh" |
| 9 |
|
| 10 |
using namespace gs; |
| 11 |
using namespace std; |
| 12 |
|
| 13 |
#ifdef CPP11_STD_AVAILABLE |
| 14 |
|
| 15 |
namespace { |
| 16 |
TEST(tupleIO) |
| 17 |
{ |
| 18 |
// |
| 19 |
// Tuple-related ops |
| 20 |
// |
| 21 |
// tuple_cat -- concatenate two tuples |
| 22 |
// tie -- make tuple of references |
| 23 |
// swap |
| 24 |
// tuple_size<tuple>::value |
| 25 |
// tuple_element<N, tuple>::type |
| 26 |
// std::get<N>(tuple) |
| 27 |
// make_tuple |
| 28 |
// |
| 29 |
StringArchive ar0; |
| 30 |
|
| 31 |
// Test that archive works with bare tuples |
| 32 |
auto tuple1 = make_tuple(2, 3.f, 4.0, 5LL); |
| 33 |
auto tuplecopy0(tuple1); |
| 34 |
ar0 << Record(tuple1, "tuple1_0", "top"); |
| 35 |
|
| 36 |
tuple1 = make_tuple(6, 7.f, 8.0, 9LL); |
| 37 |
ar0 << Record(tuple1, "tuple1_1", "top"); |
| 38 |
|
| 39 |
// Get them back |
| 40 |
std::tuple<int, float, double, long long> tuple2; |
| 41 |
Reference<decltype(tuple2)>(ar0, "tuple1_0", "top").restore(0,&tuple2); |
| 42 |
CHECK(tuplecopy0 == tuple2); |
| 43 |
Reference<decltype(tuple2)>(ar0, "tuple1_1", "top").restore(0,&tuple2); |
| 44 |
CHECK(tuple1 == tuple2); |
| 45 |
|
| 46 |
// Create a vector of tuples of PODs. For these, the |
| 47 |
// amount of space taken in the archive should be just |
| 48 |
// the size of the PODs (plus some header). |
| 49 |
std::vector<std::tuple<int,float,double> > v1, v2; |
| 50 |
for (unsigned i=0; i<3; ++i) |
| 51 |
{ |
| 52 |
auto tmp2 = std::make_tuple( |
| 53 |
static_cast<int>(test_rng()*10000), |
| 54 |
static_cast<float>(test_rng()), |
| 55 |
static_cast<double>(test_rng()) |
| 56 |
); |
| 57 |
v1.push_back(tmp2); |
| 58 |
if (i < 2) |
| 59 |
{ |
| 60 |
v2.push_back(tmp2); |
| 61 |
CHECK(v1 == v2); |
| 62 |
} |
| 63 |
} |
| 64 |
const unsigned long lenc = ar0.dataSize(); |
| 65 |
ar0 << Record(v1, "v1", "top"); |
| 66 |
const unsigned long lenv1 = ar0.dataSize(); |
| 67 |
const unsigned long sv1 = lenv1 - lenc; |
| 68 |
ar0 << Record(v2, "v2", "top"); |
| 69 |
const unsigned long lenv2 = ar0.dataSize(); |
| 70 |
const unsigned long sv2 = lenv2 - lenv1; |
| 71 |
CHECK_EQUAL(sizeof(int) + sizeof(float) + sizeof(double), sv1 - sv2); |
| 72 |
|
| 73 |
// Make sure we can read these back |
| 74 |
std::vector<std::tuple<int,float,double> > r1, r2; |
| 75 |
Reference<decltype(r1)>(ar0, "v1", "top").restore(0, &r1); |
| 76 |
Reference<decltype(r2)>(ar0, "v2", "top").restore(0, &r2); |
| 77 |
|
| 78 |
CHECK(r1 == v1); |
| 79 |
CHECK(r2 == v2); |
| 80 |
|
| 81 |
// Make sure we can read these back as VarPack objects as well |
| 82 |
std::vector<VarPack<int,float,double> > r1v; |
| 83 |
Reference<decltype(r1v)>(ar0, "v1", "top").restore(0, &r1v); |
| 84 |
|
| 85 |
const unsigned nPacks = r1v.size(); |
| 86 |
CHECK(nPacks == v1.size()); |
| 87 |
for (unsigned i=0; i<nPacks; ++i) |
| 88 |
{ |
| 89 |
CHECK(std::get<0>(r1v[i]) == std::get<0>(v1[i])); |
| 90 |
CHECK(std::get<1>(r1v[i]) == std::get<1>(v1[i])); |
| 91 |
CHECK(std::get<2>(r1v[i]) == std::get<2>(v1[i])); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
#else // CPP11_STD_AVAILABLE |
| 97 |
|
| 98 |
namespace { |
| 99 |
TEST(tupleIO) |
| 100 |
{ |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
#endif // CPP11_STD_AVAILABLE |