Debian Codegraph @

tests/test_valarrayIO.cc

raw
1 #include "UnitTest++.h"
2
3 #include "test_utils.hh"
4
5 #include "geners/valarrayIO.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 namespace {
14 class Persistent3
15 {
16 public:
17 inline Persistent3(int n=0) : n_(n) {}
18
19 inline bool operator==(const Persistent3& r) const
20 {return n_ == r.n_;}
21 inline bool operator!=(const Persistent3& r) const
22 {return !(*this == r);}
23
24 inline Persistent3& operator+=(const int& r)
25 {
26 n_ += r;
27 return *this;
28 }
29
30 inline ClassId classId() const {return ClassId(*this);}
31
32 inline bool write(std::ostream& os) const
33 {
34 write_pod(os, n_);
35 return !os.bad() && !os.fail();
36 }
37
38 static inline const char* classname() {return "gs::test::Persistent3";}
39 static inline unsigned version() {return 1;}
40 static inline Persistent3* read(const ClassId& id, std::istream& in)
41 {
42 assert(id == ClassId::makeId<Persistent3>());
43 int n;
44 read_pod(in, &n);
45 if (in.fail())
46 return 0;
47 else
48 return new Persistent3(n);
49 }
50
51 private:
52 int n_;
53 };
54 }
55
56 namespace {
57 TEST(valarrayIO_1)
58 {
59 const unsigned sz = 10;
60 StringArchive ar0;
61
62 valarray<int> v0(sz);
63 for (unsigned i=0; i<sz; ++i)
64 v0[i] = static_cast<int>(i*2 + 23);
65 CHECK_EQUAL(sz, v0.size());
66
67 ar0 << Record(v0, "v0", "top");
68
69 valarray<int> v0_read;
70 Reference<valarray<int> >(ar0, "v0", "top").restore(0,&v0_read);
71
72 CHECK_EQUAL(sz, v0_read.size());
73 for (unsigned i=0; i<sz; ++i)
74 CHECK_EQUAL(v0[i], v0_read[i]);
75 }
76
77 TEST(valarrayIO_2)
78 {
79 const unsigned sz = 17;
80 StringArchive ar0;
81
82 valarray<Persistent3> v0(sz);
83 for (unsigned i=0; i<sz; ++i)
84 v0[i] = Persistent3(i*2 + 23);
85 CHECK_EQUAL(sz, v0.size());
86
87 ar0 << Record(v0, "v0", "top");
88
89 Reference<valarray<Persistent3> > ref(ar0, "v0", "top");
90 CHECK(ref.unique());
91
92 valarray<Persistent3> v0_read;
93 ref.restore(0,&v0_read);
94
95 CHECK_EQUAL(sz, v0_read.size());
96 for (unsigned i=0; i<sz; ++i)
97 CHECK(v0[i] == v0_read[i]);
98 }
99 }