| 1 |
#include "UnitTest++.h" |
| 2 |
|
| 3 |
#include "test_utils.hh" |
| 4 |
|
| 5 |
#include "geners/VarPack.hh" |
| 6 |
#include "geners/IOPtr.hh" |
| 7 |
#include "geners/StringArchive.hh" |
| 8 |
#include "geners/Record.hh" |
| 9 |
#include "geners/Reference.hh" |
| 10 |
|
| 11 |
using namespace gs; |
| 12 |
using namespace std; |
| 13 |
|
| 14 |
#ifdef CPP11_STD_AVAILABLE |
| 15 |
|
| 16 |
#define io_ptr(obj) gs::make_IOPtr(obj) |
| 17 |
|
| 18 |
namespace { |
| 19 |
TEST(VarPack) |
| 20 |
{ |
| 21 |
typedef CPP11_shared_ptr<double> Dptr; |
| 22 |
|
| 23 |
StringArchive ar0; |
| 24 |
|
| 25 |
// Write a tuple which we will later read as a variable pack |
| 26 |
int i1 = 15; |
| 27 |
Dptr dp(new double()); |
| 28 |
auto agw = make_tuple( |
| 29 |
&i1, |
| 30 |
dp, |
| 31 |
35 |
| 32 |
); |
| 33 |
i1 = 30; |
| 34 |
*dp = 345.0; |
| 35 |
ar0 << Record(agw, "varpack", "top"); |
| 36 |
const unsigned long len0 = ar0.dataSize(); |
| 37 |
|
| 38 |
i1 = 13; |
| 39 |
*dp = 678.0; |
| 40 |
std::get<2>(agw) = 64; |
| 41 |
ar0 << Record(agw, "varpack", "top"); |
| 42 |
const unsigned long len1 = ar0.dataSize(); |
| 43 |
|
| 44 |
// Figure out what kind of record size we are expecting for the |
| 45 |
// second write operation. The first thing is the class id of |
| 46 |
// the pack itself. |
| 47 |
const ClassId& agwid = ClassId::itemId(agw); |
| 48 |
unsigned idlen = agwid.id().size() + sizeof(unsigned long); |
| 49 |
|
| 50 |
// We have a bare int pointer in the pack. The I/O code will switch |
| 51 |
// to writing out the class id for that pointer. |
| 52 |
idlen += ClassId::makeId<int>().id().size() + sizeof(unsigned long); |
| 53 |
|
| 54 |
// We have a shared double pointer in the pack. The I/O code will |
| 55 |
// switch to writing out the class id for that pointer. |
| 56 |
idlen += ClassId::makeId<double>().id().size() + sizeof(unsigned long); |
| 57 |
|
| 58 |
// Of course, we also need to take into account the size of |
| 59 |
// the data itself |
| 60 |
unsigned long datasize = sizeof(int) + sizeof(double) + sizeof(int); |
| 61 |
|
| 62 |
// Make sure the above arguments are correct |
| 63 |
CHECK_EQUAL(datasize + idlen, len1-len0); |
| 64 |
|
| 65 |
// Make a variable pack with IOPtr objects. For them, the I/O |
| 66 |
// code will not switch to writing out the class ids. |
| 67 |
long lv = 10; |
| 68 |
float fv = 5.f; |
| 69 |
double dv = 1.0; |
| 70 |
auto pack = make_VarPack( |
| 71 |
io_ptr(lv), |
| 72 |
io_ptr(fv), |
| 73 |
io_ptr(dv) |
| 74 |
); |
| 75 |
ar0 << Record(pack, "pack", "top"); |
| 76 |
const unsigned long lena = ar0.dataSize(); |
| 77 |
|
| 78 |
lv = 11; |
| 79 |
fv = 6.f; |
| 80 |
dv = 2.0; |
| 81 |
ar0 << Record(pack, "pack", "top"); |
| 82 |
const unsigned long lenb = ar0.dataSize(); |
| 83 |
const ClassId& packid = ClassId::itemId(pack); |
| 84 |
idlen = packid.id().size() + sizeof(unsigned long); |
| 85 |
datasize = sizeof(long) + sizeof(float) + sizeof(double); |
| 86 |
CHECK_EQUAL(datasize + idlen, lenb - lena); |
| 87 |
|
| 88 |
// Make sure we can read varpacks back |
| 89 |
int ival; |
| 90 |
Dptr pd(new double()); |
| 91 |
Dptr pk = pd; |
| 92 |
auto rag = make_VarPack( |
| 93 |
&ival, |
| 94 |
pk, |
| 95 |
12 |
| 96 |
); |
| 97 |
Reference<decltype(rag)> ref(ar0, "varpack", "top"); |
| 98 |
CHECK(ref.size() == 2); |
| 99 |
|
| 100 |
ref.restore(0, &rag); |
| 101 |
CHECK(ival == 30); |
| 102 |
CHECK(*std::get<1>(rag) == 345.0); |
| 103 |
CHECK(std::get<2>(rag) == 35); |
| 104 |
|
| 105 |
ref.restore(1, &rag); |
| 106 |
CHECK(ival == 13); |
| 107 |
CHECK(*std::get<1>(rag) == 678.0); |
| 108 |
CHECK(std::get<2>(rag) == 64); |
| 109 |
|
| 110 |
Reference<decltype(pack)> pref(ar0, "pack", "top"); |
| 111 |
CHECK(pref.size() == 2); |
| 112 |
pref.restore(0, &pack); |
| 113 |
CHECK(lv == 10); |
| 114 |
CHECK(fv == 5.f); |
| 115 |
CHECK(dv == 1.0); |
| 116 |
|
| 117 |
pref.restore(1, &pack); |
| 118 |
CHECK(lv == 11); |
| 119 |
CHECK(fv == 6.f); |
| 120 |
CHECK(dv == 2.0); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
#else // CPP11_STD_AVAILABLE |
| 125 |
|
| 126 |
namespace { |
| 127 |
TEST(VarPack) |
| 128 |
{ |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
#endif // CPP11_STD_AVAILABLE |