| 1 |
#include <numeric> |
| 2 |
|
| 3 |
#include "UnitTest++.h" |
| 4 |
|
| 5 |
#include "test_utils.hh" |
| 6 |
|
| 7 |
#include "geners/StringArchive.hh" |
| 8 |
#include "geners/ColumnPacker.hh" |
| 9 |
#include "geners/IOPtr.hh" |
| 10 |
#include "geners/CP_column_iterator.hh" |
| 11 |
|
| 12 |
using namespace gs; |
| 13 |
using namespace std; |
| 14 |
|
| 15 |
#ifdef CPP11_STD_AVAILABLE |
| 16 |
|
| 17 |
#define io_ptr(obj) gs::make_IOPtr(obj) |
| 18 |
#define io_proxy(obj) gs::make_IOProxy( obj , #obj ) |
| 19 |
|
| 20 |
namespace { |
| 21 |
TEST(ColumnPacker) |
| 22 |
{ |
| 23 |
StringArchive ar0("archive"); |
| 24 |
|
| 25 |
// Test that archive works with bare tuples |
| 26 |
auto tuple1 = tuple<int, float, double, long long>(); |
| 27 |
|
| 28 |
typedef ColumnPacker<decltype(tuple1)> Packer1; |
| 29 |
const unsigned bufferSize = 1024; |
| 30 |
Packer1 packer({"i1", "f1", "d1", "ll1"}, |
| 31 |
"Title", ar0, "Name", "Category", bufferSize); |
| 32 |
std::vector<decltype(tuple1)> t1copy; |
| 33 |
|
| 34 |
// We need to have enough data to fill at least a few buffers |
| 35 |
// in order to understand if things work correctly. |
| 36 |
unsigned datasize = sizeof(int) + sizeof(float) + sizeof(double) + |
| 37 |
sizeof(long long); |
| 38 |
const unsigned long rows_per_buffer = bufferSize/datasize + 1; |
| 39 |
const unsigned long nrows = 50*rows_per_buffer + 1; |
| 40 |
|
| 41 |
for (unsigned long row=0; row<nrows; ++row) |
| 42 |
{ |
| 43 |
tuple1 = make_tuple( |
| 44 |
(int)(test_rng()*10000), |
| 45 |
(float)(test_rng()), |
| 46 |
(double)(test_rng()), |
| 47 |
(long long)(test_rng()*100000) |
| 48 |
); |
| 49 |
t1copy.push_back(tuple1); |
| 50 |
CHECK(packer.fill(tuple1)); |
| 51 |
} |
| 52 |
|
| 53 |
// Now, let see if we can compare the contents to the |
| 54 |
// original tuple |
| 55 |
CHECK_EQUAL(nrows, packer.nRows()); |
| 56 |
CHECK_EQUAL(std::tuple_size<decltype(tuple1)>::value, |
| 57 |
packer.nColumns()); |
| 58 |
for (unsigned long row=0; row<nrows; ++row) |
| 59 |
{ |
| 60 |
packer.rowContents(row, &tuple1); |
| 61 |
CHECK(tuple1 == t1copy[row]); |
| 62 |
} |
| 63 |
|
| 64 |
// Write the packer to the archive and redo the comparison |
| 65 |
packer.write(); |
| 66 |
CHECK_EQUAL(nrows, packer.nRows()); |
| 67 |
CHECK_EQUAL(std::tuple_size<decltype(tuple1)>::value, |
| 68 |
packer.nColumns()); |
| 69 |
for (unsigned long row=0; row<nrows; ++row) |
| 70 |
{ |
| 71 |
packer.rowContents(row, &tuple1); |
| 72 |
CHECK(tuple1 == t1copy[row]); |
| 73 |
} |
| 74 |
|
| 75 |
// Check if we can read the packer back |
| 76 |
std::unique_ptr<Packer1> rp = |
| 77 |
CPReference<Packer1>(ar0, "Name", "Category").get(0); |
| 78 |
assert(rp.get()); |
| 79 |
|
| 80 |
CHECK_EQUAL(nrows, rp->nRows()); |
| 81 |
CHECK_EQUAL(std::tuple_size<decltype(tuple1)>::value, rp->nColumns()); |
| 82 |
for (unsigned long row=0; row<nrows; ++row) |
| 83 |
{ |
| 84 |
rp->rowContents(row, &tuple1); |
| 85 |
CHECK(tuple1 == t1copy[row]); |
| 86 |
} |
| 87 |
|
| 88 |
CHECK(*rp == packer); |
| 89 |
|
| 90 |
// Check single column lookup |
| 91 |
double sum = 0.0; |
| 92 |
double dummy = 0; |
| 93 |
for (unsigned long row=0; row<nrows; ++row) |
| 94 |
{ |
| 95 |
packer.rowContents(row, &tuple1); |
| 96 |
|
| 97 |
// Storage given by IOPtr |
| 98 |
packer.fetchItem<2>(row, io_ptr(dummy)); |
| 99 |
|
| 100 |
// Make sure all results are the same |
| 101 |
CHECK_EQUAL(std::get<2>(tuple1), dummy); |
| 102 |
sum += dummy; |
| 103 |
} |
| 104 |
|
| 105 |
// Try to use the iterator |
| 106 |
double sum2 = std::accumulate(CP_column_begin<2>(packer,io_ptr(dummy)), |
| 107 |
CP_column_end<2>(packer,io_ptr(dummy)), 0.0); |
| 108 |
CHECK_EQUAL(sum, sum2); |
| 109 |
|
| 110 |
// Check that we can extract a fraction of the columns |
| 111 |
float f1; |
| 112 |
double d1; |
| 113 |
unsigned haha; |
| 114 |
auto proto = std::make_tuple( |
| 115 |
io_proxy(d1), |
| 116 |
io_proxy(f1), |
| 117 |
io_proxy(haha) |
| 118 |
); |
| 119 |
typedef ColumnPacker<decltype(proto)> Packer2; |
| 120 |
std::unique_ptr<Packer2> rp2 = |
| 121 |
CPReference<Packer2>(proto, ar0, "Name", "Category").get(0); |
| 122 |
assert(rp2.get()); |
| 123 |
|
| 124 |
double sum3 = std::accumulate(CP_column_begin<0>(*rp2,io_ptr(dummy)), |
| 125 |
CP_column_end<0>(*rp2,io_ptr(dummy)), 0.0); |
| 126 |
CHECK_EQUAL(sum, sum3); |
| 127 |
|
| 128 |
// Cycle both readback tuples |
| 129 |
for (unsigned long row=0; row<nrows; ++row) |
| 130 |
{ |
| 131 |
packer.rowContents(row, &tuple1); |
| 132 |
rp2->rowContents(row, &proto); |
| 133 |
CHECK_EQUAL(d1, std::get<2>(tuple1)); |
| 134 |
CHECK_EQUAL(f1, std::get<1>(tuple1)); |
| 135 |
} |
| 136 |
|
| 137 |
// Check the "original" settings for rp2 |
| 138 |
CHECK_EQUAL(4U, rp2->nOriginalColumns()); |
| 139 |
CHECK_EQUAL(std::string("i1"), rp2->originalColumnName(0)); |
| 140 |
CHECK_EQUAL(std::string("f1"), rp2->originalColumnName(1)); |
| 141 |
CHECK_EQUAL(std::string("d1"), rp2->originalColumnName(2)); |
| 142 |
CHECK_EQUAL(std::string("ll1"), rp2->originalColumnName(3)); |
| 143 |
CHECK(packer.columnNames() == rp2->originalColumnNames()); |
| 144 |
CHECK_EQUAL(2U, rp2->nColumnsReadBack()); |
| 145 |
const std::vector<unsigned char> mask = {1, 1, 0}; |
| 146 |
CHECK(mask == rp2->readbackMask()); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
#else // CPP11_STD_AVAILABLE |
| 151 |
|
| 152 |
namespace { |
| 153 |
TEST(ColumnPacker) |
| 154 |
{ |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
#endif // CPP11_STD_AVAILABLE |