| 1 |
#include <iostream> |
| 2 |
#include <sstream> |
| 3 |
|
| 4 |
#include "UnitTest++.h" |
| 5 |
#include "test_utils.hh" |
| 6 |
|
| 7 |
#include "geners/listIO.hh" |
| 8 |
#include "geners/arrayIO.hh" |
| 9 |
#include "geners/dequeIO.hh" |
| 10 |
#include "geners/setIO.hh" |
| 11 |
#include "geners/mapIO.hh" |
| 12 |
#include "geners/complexIO.hh" |
| 13 |
|
| 14 |
namespace haha { |
| 15 |
typedef struct {char a[2];} MyDummy; |
| 16 |
} |
| 17 |
|
| 18 |
gs_declare_type_as_pod(haha::MyDummy) |
| 19 |
gs_specialize_class_id(haha::MyDummy, 0) |
| 20 |
|
| 21 |
namespace { |
| 22 |
class Simple |
| 23 |
{ |
| 24 |
public: |
| 25 |
inline Simple() : a_(0.0), b_(0) {} |
| 26 |
inline Simple(double a, int b) : a_(a), b_(b) {} |
| 27 |
|
| 28 |
inline double a() const {return a_;} |
| 29 |
inline int b() const {return b_;} |
| 30 |
|
| 31 |
inline bool operator==(const Simple& r) const |
| 32 |
{return a_ == r.a_ && b_ == r.b_;} |
| 33 |
inline bool operator!=(const Simple& r) const |
| 34 |
{return !(*this == r);} |
| 35 |
|
| 36 |
// Methods related to I/O follow |
| 37 |
inline gs::ClassId classId() const {return gs::ClassId(*this);} |
| 38 |
bool write(std::ostream& of) const; |
| 39 |
|
| 40 |
static inline const char* classname() {return "Simple";} |
| 41 |
static inline unsigned version() {return 1;} |
| 42 |
static void restore(const gs::ClassId& id, std::istream& in, |
| 43 |
Simple* objectPtr); |
| 44 |
private: |
| 45 |
double a_; |
| 46 |
int b_; |
| 47 |
}; |
| 48 |
|
| 49 |
bool Simple::write(std::ostream& of) const |
| 50 |
{ |
| 51 |
gs::write_pod(of, a_); |
| 52 |
gs::write_pod(of, b_); |
| 53 |
return !of.fail(); |
| 54 |
} |
| 55 |
|
| 56 |
void Simple::restore(const gs::ClassId& id, std::istream& in, |
| 57 |
Simple* objectPtr) |
| 58 |
{ |
| 59 |
static const gs::ClassId myId(gs::ClassId::makeId<Simple>()); |
| 60 |
myId.ensureSameId(id); |
| 61 |
|
| 62 |
assert(objectPtr); |
| 63 |
gs::read_pod(in, &objectPtr->a_); |
| 64 |
gs::read_pod(in, &objectPtr->b_); |
| 65 |
if (in.fail()) throw gs::IOReadFailure(); |
| 66 |
} |
| 67 |
|
| 68 |
struct Order |
| 69 |
{ |
| 70 |
inline bool operator()(const std::vector<int>& v1, |
| 71 |
const std::vector<int>& v2) |
| 72 |
{ |
| 73 |
const unsigned sz1 = v1.size(); |
| 74 |
const unsigned sz2 = v2.size(); |
| 75 |
for (unsigned i=0; i<sz1 && i<sz2; ++i) |
| 76 |
{ |
| 77 |
if (v1[i] < v2[i]) |
| 78 |
return true; |
| 79 |
if (v2[i] < v1[i]) |
| 80 |
return false; |
| 81 |
} |
| 82 |
if (sz1 < sz2) |
| 83 |
return true; |
| 84 |
else |
| 85 |
return false; |
| 86 |
} |
| 87 |
}; |
| 88 |
} |
| 89 |
|
| 90 |
gs_specialize_class_id(Order, 1) |
| 91 |
|
| 92 |
using namespace gs; |
| 93 |
using namespace std; |
| 94 |
using namespace haha; |
| 95 |
|
| 96 |
namespace { |
| 97 |
TEST(write_item) |
| 98 |
{ |
| 99 |
std::stringstream stream; |
| 100 |
std::vector<ClassId> proto; |
| 101 |
|
| 102 |
const int d = 234; |
| 103 |
int d2 = 0; |
| 104 |
CHECK(write_item(stream, d, true)); |
| 105 |
restore_item(stream, &d2, true); |
| 106 |
CHECK(d == d2); |
| 107 |
|
| 108 |
std::complex<double> cmplx(3.0, 17.0), rcmplx(0.0, 0.0); |
| 109 |
write_pod(stream, cmplx); |
| 110 |
read_pod(stream, &rcmplx); |
| 111 |
CHECK(cmplx == rcmplx); |
| 112 |
rcmplx = std::complex<double>(0.0, 0.0); |
| 113 |
CHECK(write_item(stream, cmplx, true)); |
| 114 |
restore_item(stream, &rcmplx, true); |
| 115 |
CHECK(cmplx == rcmplx); |
| 116 |
|
| 117 |
int intarr[3] = {3, 2, 1}, readarr[3] = {0,}; |
| 118 |
CHECK(write_array(stream, intarr, 3)); |
| 119 |
read_array(stream, readarr, 3); |
| 120 |
for (unsigned i=0; i<3; ++i) |
| 121 |
CHECK_EQUAL(intarr[i], readarr[i]); |
| 122 |
|
| 123 |
std::pair<double,Simple> p1, p3; |
| 124 |
p1 = std::make_pair(23.0, Simple(45.0, -123)); |
| 125 |
CHECK(write_item(stream, p1, true)); |
| 126 |
restore_item(stream, &p3, true); |
| 127 |
CHECK(p1 == p3); |
| 128 |
|
| 129 |
std::basic_string<char> s, sin = "hahahahswqdeq"; |
| 130 |
s += 13; |
| 131 |
s += 27; |
| 132 |
CHECK(write_item(stream, s, false)); |
| 133 |
restore_item(stream, &sin, false); |
| 134 |
CHECK(sin == s); |
| 135 |
|
| 136 |
CPP11_array<std::complex<float>,3> carr, r_carr; |
| 137 |
carr[0] = std::complex<float>(1.f, 2.f); |
| 138 |
carr[1] = std::complex<float>(17.f, 23.f); |
| 139 |
carr[2] = std::complex<float>(234.f, -15.f); |
| 140 |
CHECK(write_item(stream, carr, true)); |
| 141 |
restore_item(stream, &r_carr, true); |
| 142 |
CHECK(carr == r_carr); |
| 143 |
|
| 144 |
CPP11_array<float,15> arr, r_arr; |
| 145 |
for (unsigned i=0; i<15; ++i) |
| 146 |
arr[i] = i + 1.f; |
| 147 |
CHECK(write_item(stream, arr, true)); |
| 148 |
restore_item(stream, &r_arr, true); |
| 149 |
CHECK(r_arr == arr); |
| 150 |
|
| 151 |
Simple obj1(15.0, 777), obj2; |
| 152 |
CHECK(obj1.classId().write(stream) && obj1.write(stream)); |
| 153 |
ClassId simpleId(stream, 1); |
| 154 |
Simple::restore(simpleId, stream, &obj2); |
| 155 |
CHECK(obj1 == obj2); |
| 156 |
|
| 157 |
std::vector<Simple> sarr(2); |
| 158 |
std::vector<Simple> r_sarr; |
| 159 |
sarr[0] = Simple(23.0, 17); |
| 160 |
sarr[1] = Simple(-10.0, 3); |
| 161 |
CHECK(write_item(stream, sarr, true)); |
| 162 |
restore_item(stream, &r_sarr, true); |
| 163 |
CHECK(r_sarr == sarr); |
| 164 |
|
| 165 |
CPP11_array<MyDummy,2> darr, r_darr; |
| 166 |
darr[0].a[0] = 'a'; |
| 167 |
darr[0].a[1] = 's'; |
| 168 |
darr[1].a[0] = 'd'; |
| 169 |
darr[1].a[1] = 'f'; |
| 170 |
r_darr[0].a[0] = 'h'; |
| 171 |
CHECK(write_item(stream, darr, true)); |
| 172 |
restore_item(stream, &r_darr, true); |
| 173 |
CHECK_EQUAL('a', r_darr[0].a[0]); |
| 174 |
CHECK_EQUAL('s', r_darr[0].a[1]); |
| 175 |
CHECK_EQUAL('d', r_darr[1].a[0]); |
| 176 |
CHECK_EQUAL('f', r_darr[1].a[1]); |
| 177 |
|
| 178 |
std::string haha = "crap", dudle = "dudle"; |
| 179 |
CHECK(write_item(stream, haha, true)); |
| 180 |
CHECK(write_item(stream, dudle, false)); |
| 181 |
|
| 182 |
std::string r_haha; |
| 183 |
restore_item(stream, &r_haha, true); |
| 184 |
CHECK(r_haha == haha); |
| 185 |
restore_item(stream, &r_haha, false); |
| 186 |
CHECK(r_haha == dudle); |
| 187 |
|
| 188 |
CPP11_array<std::string,2> arrs; |
| 189 |
arrs[0] = haha; |
| 190 |
arrs[1] = dudle; |
| 191 |
process_const_item<GenericWriter>(arrs, stream, &proto, true); |
| 192 |
|
| 193 |
CPP11_array<std::string,2> r_arrs; |
| 194 |
process_item<GenericReader>(r_arrs, stream, &proto, true); |
| 195 |
CHECK(r_arrs == arrs); |
| 196 |
|
| 197 |
std::basic_string<int> bs; |
| 198 |
bs += 10; |
| 199 |
bs += 20; |
| 200 |
process_const_item<GenericWriter>(bs, stream, &proto, true); |
| 201 |
|
| 202 |
std::basic_string<int> rbs; |
| 203 |
process_item<GenericReader>(rbs, stream, &proto, true); |
| 204 |
CHECK(rbs == bs); |
| 205 |
|
| 206 |
std::vector<int> v1, v2; |
| 207 |
for (unsigned i=0; i<109; ++i) |
| 208 |
v1.push_back(static_cast<int>(test_rng()*1000000)); |
| 209 |
process_const_item<GenericWriter>(v1, stream, &proto, true); |
| 210 |
process_item<GenericReader>(v2, stream, &proto, true); |
| 211 |
CHECK(v1 == v2); |
| 212 |
|
| 213 |
std::list<int> lst, lst2; |
| 214 |
lst.push_back(1); |
| 215 |
lst.push_back(2); |
| 216 |
lst.push_back(3); |
| 217 |
process_const_item<GenericWriter>(lst, stream, &proto, false); |
| 218 |
process_item<GenericReader>(lst2, stream, &proto, false); |
| 219 |
CHECK(lst == lst2); |
| 220 |
|
| 221 |
std::deque<double> dq, dq2; |
| 222 |
dq.push_back(4.0); |
| 223 |
dq.push_back(5.0); |
| 224 |
process_const_item<GenericWriter>(dq, stream, &proto, true); |
| 225 |
process_item<GenericReader>(dq2, stream, &proto, true); |
| 226 |
CHECK(dq == dq2); |
| 227 |
|
| 228 |
std::multiset<float> sf, sf2; |
| 229 |
sf.insert(2.f); |
| 230 |
sf.insert(4.f); |
| 231 |
sf.insert(4.f); |
| 232 |
sf.insert(3.f); |
| 233 |
process_const_item<GenericWriter>(sf, stream, &proto, true); |
| 234 |
process_item<GenericReader>(sf2, stream, &proto, true); |
| 235 |
CHECK(sf == sf2); |
| 236 |
|
| 237 |
std::map<int,double> mp, mp2; |
| 238 |
mp.insert(std::make_pair(1, 10.0)); |
| 239 |
mp.insert(std::make_pair(2, 15.0)); |
| 240 |
process_const_item<GenericWriter>(mp, stream, &proto, true); |
| 241 |
process_item<GenericReader>(mp2, stream, &proto, true); |
| 242 |
CHECK(mp == mp2); |
| 243 |
|
| 244 |
std::vector<bool> vb, vb2; |
| 245 |
vb.push_back(1); |
| 246 |
vb.push_back(0); |
| 247 |
vb.push_back(1); |
| 248 |
process_const_item<GenericWriter>(vb, stream, &proto, true); |
| 249 |
process_item<GenericReader>(vb2, stream, &proto, true); |
| 250 |
CHECK(vb == vb2); |
| 251 |
|
| 252 |
int ival = 34456, kval = 978645, jval = 0, lval = 32390, mval = 2579; |
| 253 |
process_const_item<GenericWriter>(ival, stream, &proto, true); |
| 254 |
process_const_item<GenericWriter>(kval, stream, &proto, true); |
| 255 |
process_const_item<GenericWriter>(lval, stream, &proto, true); |
| 256 |
process_const_item<GenericWriter>(mval, stream, &proto, true); |
| 257 |
|
| 258 |
int* iread = 0, *jread = &jval; |
| 259 |
process_item<GenericReader>(iread, stream, &proto, true); |
| 260 |
CHECK(*iread == ival); |
| 261 |
process_item<GenericReader>(jread, stream, &proto, true); |
| 262 |
CHECK(jval == kval); |
| 263 |
delete iread; |
| 264 |
CPP11_shared_ptr<int> sptr(new int()); |
| 265 |
process_item<GenericReader>(sptr, stream, &proto, true); |
| 266 |
CHECK(*sptr == lval); |
| 267 |
CPP11_shared_ptr<int> sptr2; |
| 268 |
process_item<GenericReader>(sptr2, stream, &proto, true); |
| 269 |
CHECK(*sptr2 == mval); |
| 270 |
|
| 271 |
std::vector<int*> vec1; |
| 272 |
for (unsigned i=0; i<10; ++i) |
| 273 |
vec1.push_back(new int(static_cast<int>(test_rng()*1000000))); |
| 274 |
process_const_item<GenericWriter>(vec1, stream, &proto, true); |
| 275 |
|
| 276 |
std::vector<int*> vec3; |
| 277 |
CHECK(process_item<GenericReader>(vec3, stream, &proto, true)); |
| 278 |
CHECK_EQUAL(10U, vec3.size()); |
| 279 |
for (unsigned i=0; i<10; ++i) |
| 280 |
{ |
| 281 |
CHECK(*vec1[i] == *vec3[i]); |
| 282 |
delete vec3[i]; |
| 283 |
} |
| 284 |
|
| 285 |
std::vector<CPP11_shared_ptr<int> > vec2; |
| 286 |
process_const_item<GenericWriter>(vec1, stream, &proto, true); |
| 287 |
CHECK(process_item<GenericReader>(vec2, stream, &proto, true)); |
| 288 |
for (unsigned i=0; i<10; ++i) |
| 289 |
{ |
| 290 |
CHECK(*vec1[i] == *vec2[i]); |
| 291 |
delete vec1[i]; |
| 292 |
} |
| 293 |
|
| 294 |
// Check that writing out an item by pointer is equivalent |
| 295 |
// to writing it out by reference -- up to a point. Writing |
| 296 |
// out the class id will be automatically turned on if the |
| 297 |
// item is written out by pointer. |
| 298 |
ival = 456; |
| 299 |
CHECK(write_item(stream, &ival, false)); |
| 300 |
CHECK(ival == 456); |
| 301 |
|
| 302 |
int ival_2 = 0; |
| 303 |
restore_item(stream, &ival_2, true); |
| 304 |
CHECK(ival == ival_2); |
| 305 |
|
| 306 |
// Check write_array/read_array for objects (non-PODs) |
| 307 |
const unsigned nsimple = 5; |
| 308 |
Simple simparr[nsimple]; |
| 309 |
for (unsigned i=0; i<nsimple; ++i) |
| 310 |
simparr[i] = Simple(test_rng(), static_cast<int>(test_rng()*100)); |
| 311 |
CHECK(write_array(stream, simparr, nsimple)); |
| 312 |
|
| 313 |
Simple simparr2[nsimple]; |
| 314 |
read_array(stream, simparr2, nsimple); |
| 315 |
for (unsigned i=0; i<nsimple; ++i) |
| 316 |
CHECK(simparr[i] == simparr2[i]); |
| 317 |
|
| 318 |
{ |
| 319 |
std::map<std::vector<int>,double,Order> mp1; |
| 320 |
std::vector<int> v1(2), v2(2); |
| 321 |
v1[0] = 1; |
| 322 |
v1[1] = 2; |
| 323 |
v2[0] = 1; |
| 324 |
v2[1] = 3; |
| 325 |
mp1[v1] = 2.0; |
| 326 |
mp1[v2] = 3.0; |
| 327 |
std::stringstream s; |
| 328 |
CHECK(write_item(s, mp1, true)); |
| 329 |
std::map<std::vector<int>,double,Order> mp2; |
| 330 |
restore_item(s, &mp2, true); |
| 331 |
CHECK(mp1 == mp2); |
| 332 |
} |
| 333 |
} |
| 334 |
} |