CNORXZ
Container with Native Operation Routines and Expressions
Loading...
Searching...
No Matches
cereal_unit_test.cc
Go to the documentation of this file.
1// -*- C++ -*-
12#include <cstdlib>
13#include <iostream>
14#include <sstream>
15
16#include "gtest/gtest.h"
17
18#include "cnorxz.h"
19#include "cnorxz_cereal.h"
20#include <cereal/archives/json.hpp>
21#include <cereal/archives/binary.hpp>
22
23namespace
24{
25 using namespace CNORXZ;
26
27 class CerCRange_Test : public ::testing::Test
28 {
29 protected:
30
31 CerCRange_Test()
32 {
33 mSize = 7;
34 mR = CRangeFactory(mSize).create();
35 }
36
37 SizeT mSize;
38 RangePtr mR;
39 std::stringstream mS;
40 };
41
42 class CerURange_Test : public ::testing::Test
43 {
44 protected:
45
46 CerURange_Test()
47 {
48 mMeta = { "some", "random", "words", "with", "no", "meaning" };
49 mR = URangeFactory(mMeta).create();
50 }
51
52 Vector<String> mMeta;
53 RangePtr mR;
54 std::stringstream mS;
55 };
56
57 class CerYRange_Test : public ::testing::Test
58 {
59 protected:
60
61 CerYRange_Test()
62 {
63 mSize = 7;
64 mMeta = { "some", "random", "words", "with", "no", "meaning" };
65 mR1 = CRangeFactory(mSize).create();
66 mR2 = URangeFactory(mMeta).create();
67 mR = YRangeFactory({mR1,mR2}).create();
68 }
69
70 SizeT mSize;
71 Vector<String> mMeta;
72 RangePtr mR1;
73 RangePtr mR2;
74 RangePtr mR;
75 std::stringstream mS;
76 std::stringstream mS2;
77 };
78
79 class CerMArray_Test : public ::testing::Test
80 {
81 protected:
82
83 CerMArray_Test()
84 {
85 mSize = 7;
86 mMeta = { "some", "random", "words", "with", "no", "meaning" };
87 mR1 = CRangeFactory(mSize).create();
88 mR2 = URangeFactory(mMeta).create();
89 mR = YRangeFactory({mR1,mR2}).create();
90 mData.resize(mR->size());
91 for(SizeT i = 0; i != mData.size(); ++i){
92 mData[i] = static_cast<Double>(i)/static_cast<Double>(mData.size());
93 }
94 mArr = MArray(mR, mData);
95 }
96
97 SizeT mSize;
98 Vector<String> mMeta;
99 RangePtr mR1;
100 RangePtr mR2;
101 RangePtr mR;
102 Vector<Double> mData;
103 MArray<Double> mArr;
104 std::stringstream mS;
105 };
106
107 TEST_F(CerCRange_Test, Serialize)
108 {
109 {
110 cereal::JSONOutputArchive ar(mS);
111 ar(mR);
112 }
113 RangePtr r = nullptr;
114 {
115 cereal::JSONInputArchive ar(mS);
116 ar(r);
117 }
118 EXPECT_EQ(r->size(), mR->size());
119 EXPECT_EQ(r->dim(), mR->dim());
120 auto i1 = r->begin();
121 auto i2 = mR->begin();
122 auto i1e = r->end();
123 auto i2e = mR->end();
124 for(; i1 != i1e; ++i1, ++i2){
125 EXPECT_TRUE(*i1 == *i2);
126 }
127 }
128
129 TEST_F(CerURange_Test, Serialize)
130 {
131 {
132 cereal::JSONOutputArchive ar(mS);
133 ar(mR);
134 }
135 RangePtr r = nullptr;
136 {
137 cereal::JSONInputArchive ar(mS);
138 ar(r);
139 }
140 EXPECT_EQ(r->size(), mR->size());
141 EXPECT_EQ(r->dim(), mR->dim());
142 auto i1 = r->begin();
143 auto i2 = mR->begin();
144 auto i1e = r->end();
145 auto i2e = mR->end();
146 for(; i1 != i1e; ++i1, ++i2){
147 EXPECT_TRUE(*i1 == *i2);
148 }
149 }
150
151 TEST_F(CerYRange_Test, Serialize)
152 {
153 {
154 cereal::JSONOutputArchive ar(mS);
155 ar(cereal::make_nvp("mR",mR));
156 }
157 RangePtr r = nullptr;
158 {
159 cereal::JSONInputArchive ar(mS);
160 cer::save_load(ar, "mR", r);
161 }
162 EXPECT_EQ(r->size(), mR->size());
163 EXPECT_EQ(r->dim(), mR->dim());
164 auto i1 = r->begin();
165 auto i2 = mR->begin();
166 auto i1e = r->end();
167 auto i2e = mR->end();
168 for(; i1 != i1e; ++i1, ++i2){
169 EXPECT_TRUE(*i1 == *i2);
170 }
171 }
172
173 TEST_F(CerYRange_Test, Binary)
174 {
175 {
176 cereal::BinaryOutputArchive ar(mS);
177 ar(cereal::make_nvp("mR",mR));
178 }
179 RangePtr r = nullptr;
180 {
181 cereal::BinaryInputArchive ar(mS);
182 cer::save_load(ar, "mR", r);
183 }
184 EXPECT_EQ(r->size(), mR->size());
185 EXPECT_EQ(r->dim(), mR->dim());
186 auto i1 = r->begin();
187 auto i2 = mR->begin();
188 auto i1e = r->end();
189 auto i2e = mR->end();
190 for(; i1 != i1e; ++i1, ++i2){
191 EXPECT_TRUE(*i1 == *i2);
192 }
193 }
194
195 TEST_F(CerYRange_Test, NoDuplicates)
196 {
197 {
198 cereal::JSONOutputArchive ar(mS);
199 ar(cereal::make_nvp("mR",mR));
200 }
201 {
202 cereal::JSONOutputArchive ar(mS2);
203 ar(cereal::make_nvp("mR",mR));
204 }
205 RangePtr r = nullptr;
206 {
207 cereal::JSONInputArchive ar(mS);
208 cer::save_load(ar, "mR", r);
209 }
210 RangePtr r2 = nullptr;
211 {
212 cereal::JSONInputArchive ar(mS2);
213 cer::save_load(ar, "mR", r2);
214 }
215
216 EXPECT_TRUE(r == r2);
217 EXPECT_EQ(r->size(), r2->size());
218 EXPECT_EQ(r->dim(), r2->dim());
219 for(SizeT i = 0; i != r->dim(); ++i){
220 EXPECT_TRUE(r->sub(i) == r2->sub(i));
221 }
222 auto i1 = r->begin();
223 auto i2 = r2->begin();
224 auto i1e = r->end();
225 auto i2e = r2->end();
226 for(; i1 != i1e; ++i1, ++i2){
227 EXPECT_TRUE(*i1 == *i2);
228 }
229 }
230
231 TEST_F(CerMArray_Test, ToStream)
232 {
233 cer::write<cer::Format::JSON>(mS, mArr);
234 //std::cout << mS.str() << std::endl;
236 cer::read<cer::Format::JSON>(mS, arr);
237
238 auto i1 = mArr.begin();
239 auto i2 = arr.begin();
240 auto i1e = mArr.end();
241 auto i2e = arr.end();
242
243 for(; i1 != i1e; ++i1, ++i2){
244 EXPECT_TRUE(i1.meta() == i2.meta());
245 EXPECT_EQ(*i1, *i2);
246 }
247 }
248
249 TEST_F(CerMArray_Test, ToTxtFile)
250 {
251 cer::writeFile<cer::Format::JSON>("cxzcer.testfile.json", mArr);
253 cer::readFile<cer::Format::JSON>("cxzcer.testfile.json", arr);
254
255 auto i1 = mArr.begin();
256 auto i2 = arr.begin();
257 auto i1e = mArr.end();
258 auto i2e = arr.end();
259
260 for(; i1 != i1e; ++i1, ++i2){
261 EXPECT_TRUE(i1.meta() == i2.meta());
262 EXPECT_EQ(*i1, *i2);
263 }
264 }
265
266 TEST_F(CerMArray_Test, ToBinaryFile)
267 {
268 cer::writeFile<cer::Format::BINARY>("cxzcer.testfile.bin", mArr);
270 cer::readFile<cer::Format::BINARY>("cxzcer.testfile.bin", arr);
271
272 auto i1 = mArr.begin();
273 auto i2 = arr.begin();
274 auto i1e = mArr.end();
275 auto i2e = arr.end();
276
277 for(; i1 != i1e; ++i1, ++i2){
278 EXPECT_TRUE(i1.meta() == i2.meta());
279 EXPECT_EQ(*i1, *i2);
280 }
281 }
282}
283
284int main(int argc, char** argv)
285{
286 ::testing::InitGoogleTest(&argc, argv);
287 return RUN_ALL_TESTS();
288}
int main(int argc, char **argv)
cnorxz main header
CNORXZ Cereal main header.
RangePtr save_load(Archive &ar, const String &name, RangePtr &r)
double Double
Definition types.h:39
uint64_t SizeT
Definition types.h:38
Sptr< RangeBase > RangePtr
Definition types.h:157
std::vector< T, Allocator< T > > Vector
Definition types.h:310
Sptr< Range > rangeCast(const RangePtr r)