| 1 |
#include <sstream> |
| 2 |
#include "UnitTest++.h" |
| 3 |
|
| 4 |
#include "test_utils.hh" |
| 5 |
|
| 6 |
#include "geners/CompressedIO.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 |
namespace { |
| 15 |
TEST(compressedIO_read) |
| 16 |
{ |
| 17 |
StringArchive ar; |
| 18 |
|
| 19 |
const unsigned len = 100000; |
| 20 |
std::string s(len, '\0'); |
| 21 |
for (unsigned i=0; i<len; ++i) |
| 22 |
s[i] = i % 128; |
| 23 |
ar << Record(s, "dummy", "haha"); |
| 24 |
|
| 25 |
std::ostringstream os; |
| 26 |
CHECK(write_compressed_item(os, ar)); |
| 27 |
|
| 28 |
std::istringstream is(os.str()); |
| 29 |
CPP11_auto_ptr<StringArchive> par = |
| 30 |
read_compressed_item<StringArchive>(is); |
| 31 |
CHECK(ar == *par); |
| 32 |
|
| 33 |
Reference<std::string> ref(*par, "dummy", "haha"); |
| 34 |
CHECK(ref.unique()); |
| 35 |
std::string rb; |
| 36 |
ref.restore(0, &rb); |
| 37 |
CHECK(rb == s); |
| 38 |
} |
| 39 |
|
| 40 |
TEST(compressedIO_restore) |
| 41 |
{ |
| 42 |
std::ostringstream os1; |
| 43 |
std::ostringstream os2; |
| 44 |
|
| 45 |
const unsigned len = 100000; |
| 46 |
std::string s(len, '\0'); |
| 47 |
for (unsigned i=0; i<len; ++i) |
| 48 |
s[i] = i % 128; |
| 49 |
|
| 50 |
CHECK(write_item(os1, s)); |
| 51 |
CHECK(write_compressed_item(os2, s)); |
| 52 |
|
| 53 |
// Size of compressed string should be smaller |
| 54 |
const std::string& st1 = os1.str(); |
| 55 |
const std::string& st2 = os2.str(); |
| 56 |
CHECK(st2.size() < st1.size()); |
| 57 |
|
| 58 |
std::istringstream is1(st1); |
| 59 |
std::string rb1; |
| 60 |
restore_item(is1, &rb1); |
| 61 |
CHECK(rb1 == s); |
| 62 |
|
| 63 |
std::istringstream is2(st2); |
| 64 |
std::string rb2; |
| 65 |
restore_compressed_item(is2, &rb2); |
| 66 |
CHECK(rb2 == s); |
| 67 |
} |
| 68 |
} |