CNORXZ
Container with Native Operation Routines and Expressions
Loading...
Searching...
No Matches
range_unit_test.cc
Go to the documentation of this file.
1// -*- C++ -*-
12#include <cstdlib>
13#include <iostream>
14
15#include "gtest/gtest.h"
16
17#include "ranges/ranges.h"
18#include "array/array.h"
19
20namespace
21{
22
23 using namespace CNORXZ;
24
25 class CR_Test : public ::testing::Test
26 {
27 protected:
28
29 CR_Test()
30 {
31 mSize = 7;
32 cr = CRangeFactory(mSize).create();
33 }
34
35 SizeT mSize;
36 RangePtr cr;
37 };
38
39 class UR_Test : public ::testing::Test
40 {
41 protected:
42
43 UR_Test()
44 {
45 mMeta = { "These", "are", "test", "strings", "foo", "bar", "baz" };
46 std::sort(mMeta.begin(), mMeta.end(), std::less<String>());
47 ur = URangeFactory<String>(mMeta).create();
48 }
49
50 Vector<String> mMeta;
51 RangePtr ur;
52 };
53
54 class MR_Test : public ::testing::Test
55 {
56 protected:
57
58 MR_Test()
59 {
60 mMeta = { "test", "strings", "foo" };
61 std::sort(mMeta.begin(), mMeta.end(), std::less<String>());
62 mSize = 7;
63 cr = CRangeFactory(mSize).create();
64 auto crx = std::dynamic_pointer_cast<CRange>(cr);
65 ur = URangeFactory<String>(mMeta).create();
66 auto urx = std::dynamic_pointer_cast<URange<String>>(ur);
67 mr = mrange(crx,urx);
68 mr2 = mrange(crx,urx,crx);
69 }
70
71 Vector<String> mMeta;
72 SizeT mSize;
73 RangePtr mr;
74 RangePtr mr2;
75 RangePtr cr;
76 RangePtr ur;
77 };
78
79 class YR_Test : public ::testing::Test
80 {
81 protected:
82
83 YR_Test()
84 {
85 mMeta = { "test", "strings", "foo" };
86 std::sort(mMeta.begin(), mMeta.end(), std::less<String>());
87 mSize = 7;
88 cr = CRangeFactory(mSize).create();
89 ur = URangeFactory<String>(mMeta).create();
90 yr = cr * ur;
91 yr2 = cr * ur * cr;
92 }
93
94 Vector<String> mMeta;
95 SizeT mSize;
96 RangePtr yr;
97 RangePtr yr2;
98 RangePtr cr;
99 RangePtr ur;
100 };
101
102 TEST_F(CR_Test, Basics)
103 {
104 auto crx = std::dynamic_pointer_cast<CRange>(cr);
105 EXPECT_FALSE(crx == nullptr);
106 EXPECT_EQ(cr->size(), mSize);
107 EXPECT_EQ(crx->size(), mSize);
108
109 EXPECT_TRUE(crx->begin() != crx->end());
110 EXPECT_FALSE(crx->begin() == crx->end());
111 EXPECT_EQ(crx->begin().pos(), 0u);
112 EXPECT_EQ(crx->end().pos(), mSize);
113
114 EXPECT_TRUE(cr->begin() != cr->end());
115 EXPECT_FALSE(cr->begin() == cr->end());
116 EXPECT_EQ(cr->begin().pos(), 0u);
117 EXPECT_EQ(cr->end().pos(), mSize);
118
119
120 SizeT cnt = 0;
121 auto endxi = cr->end();
122 for(auto xi = cr->begin(); xi != endxi; ++xi){
123 EXPECT_EQ(xi.pos(), cnt);
124 EXPECT_TRUE(*xi == DType(cnt));
125 EXPECT_EQ((*xi).str(), std::to_string(cnt));
126 ++cnt;
127 }
128
129 SizeT cnt2 = 0;
130 for(auto x: *crx){
131 EXPECT_EQ(x, cnt2++);
132 }
133 }
134
135 TEST_F(CR_Test, prange)
136 {
137 auto crx = std::dynamic_pointer_cast<CRange>(cr);
138 auto beg = crx->begin();
139 auto end = crx->end();
140
141 const SizeT begPos = 1;
142 const SizeT endPos = mSize-2;
143 const SizeT pSize = endPos - begPos;
144 beg += begPos;
145 end -= mSize-endPos+1;
146 auto pr = beg.prange(end);
147 auto prx = std::dynamic_pointer_cast<PRange<CRange>>(pr);
148
149 EXPECT_EQ(pr->size(), pSize);
150 EXPECT_EQ(prx->size(), pSize);
151
152 for(auto i = prx->begin(); i != prx->end(); ++i){
153 EXPECT_EQ(*i, i.lex()+begPos);
154 }
155
156 EXPECT_EQ(prx->orig(),cr);
157 }
158
159 TEST_F(UR_Test, Basics)
160 {
161 auto urx = std::dynamic_pointer_cast<URange<String>>(ur);
162 EXPECT_FALSE(urx == nullptr);
163 EXPECT_EQ(ur->size(), mMeta.size());
164 EXPECT_EQ(urx->size(), mMeta.size());
165
166 EXPECT_TRUE(urx->begin() != urx->end());
167 EXPECT_FALSE(urx->begin() == urx->end());
168 EXPECT_EQ(urx->begin().pos(), 0u);
169 EXPECT_EQ(urx->end().pos(), mMeta.size());
170
171 EXPECT_TRUE(ur->begin() != ur->end());
172 EXPECT_FALSE(ur->begin() == ur->end());
173 EXPECT_EQ(ur->begin().pos(), 0u);
174 EXPECT_EQ(ur->end().pos(), mMeta.size());
175
176 SizeT cnt = 0;
177 auto endxi = ur->end();
178 for(auto xi = ur->begin(); xi != endxi; ++xi){
179 EXPECT_EQ(xi.pos(), cnt);
180 EXPECT_TRUE(*xi == DType(mMeta[cnt]));
181 EXPECT_EQ((*xi).str(), mMeta[cnt]);
182 ++cnt;
183 }
184
185 SizeT cnt2 = 0;
186 for(const auto& x: *urx){
187 EXPECT_EQ(x, mMeta[cnt2++]);
188 }
189 }
190
191 TEST_F(UR_Test, prange)
192 {
193 auto urx = std::dynamic_pointer_cast<URange<String>>(ur);
194 auto beg = urx->begin();
195 auto end = urx->end();
196
197 const SizeT begPos = 1;
198 const SizeT endPos = mMeta.size() - 2;
199 const SizeT pSize = endPos - begPos;
200 beg += begPos;
201 end -= mMeta.size() - endPos + 1;
202 auto pr = beg.prange(end);
203 auto prx = std::dynamic_pointer_cast<PRange<URange<String>>>(pr);
204
205 EXPECT_EQ(pr->size(), pSize);
206 EXPECT_EQ(prx->size(), pSize);
207
208 for(auto i = prx->begin(); i != prx->end(); ++i){
209 EXPECT_EQ(*i, mMeta[i.lex()+begPos]);
210 }
211 EXPECT_EQ(prx->orig(),ur);
212 }
213
214 TEST_F(MR_Test, Basics2d)
215 {
216 auto mrx = std::dynamic_pointer_cast<MRange<CRange,URange<String>>>(mr);
217 EXPECT_FALSE(mrx == nullptr);
218
219 EXPECT_EQ(mr->size(), mMeta.size()*mSize);
220 EXPECT_EQ(mrx->size(), mMeta.size()*mSize);
221 EXPECT_EQ(mr->dim(), 2u);
222 EXPECT_EQ(mrx->dim(), 2u);
223
224 EXPECT_TRUE(mrx->begin() != mrx->end());
225 EXPECT_FALSE(mrx->begin() == mrx->end());
226 EXPECT_EQ(mrx->begin().pos(), 0u);
227 EXPECT_EQ(mrx->end().pos(), mrx->size());
228
229 EXPECT_TRUE(mr->begin() != mr->end());
230 EXPECT_FALSE(mr->begin() == mr->end());
231 EXPECT_EQ(mr->begin().pos(), 0u);
232 EXPECT_EQ(mr->end().pos(), mr->size());
233
234 const SizeT mmsize = mMeta.size();
235 auto mkm = [&](SizeT i) { return Tuple<SizeT,String>(i/mmsize,mMeta[i % mmsize]); };
236
237 SizeT cnt = 0;
238 auto endxi = mr->end();
239 for(auto xi = mr->begin(); xi != endxi; ++xi){
240 EXPECT_EQ(xi.pos(), cnt);
241 EXPECT_EQ(xi.lex(), cnt);
242 auto meta = mkm(cnt);
243 EXPECT_TRUE(*xi == DType(meta));
244 EXPECT_EQ((*xi).str(), toString(meta));
245 ++cnt;
246 }
247
248 cnt = 0;
249 auto endxxi = mrx->end();
250 for(auto xxi = mrx->begin(); xxi != endxxi; ++xxi){
251 EXPECT_EQ(xxi.pos(), cnt);
252 EXPECT_EQ(xxi.lex(), cnt);
253 auto ci = xxi.pack()[CSizeT<0>{}];
254 auto ui = xxi.pack()[CSizeT<1>{}];
255 Tuple<SizeT,String> meta(*(*ci),*(*ui));
256 auto meta2 = mkm(cnt);
257 EXPECT_EQ(meta, meta2);
258 EXPECT_TRUE(*xxi == meta);
259 ++cnt;
260 }
261
262 cnt = 0;
263 for(auto x: *mrx){
264 auto meta = mkm(cnt);
265 EXPECT_EQ(x, meta);
266 ++cnt;
267 }
268 }
269
270 TEST_F(MR_Test, Basics3d)
271 {
272 auto mrx = std::dynamic_pointer_cast<MRange<CRange,URange<String>,CRange>>(mr2);
273 EXPECT_FALSE(mrx == nullptr);
274
275 EXPECT_EQ(mr2->size(), mMeta.size()*mSize*mSize);
276 EXPECT_EQ(mrx->size(), mMeta.size()*mSize*mSize);
277 EXPECT_EQ(mr2->dim(), 3u);
278 EXPECT_EQ(mrx->dim(), 3u);
279
280 EXPECT_TRUE(mrx->begin() != mrx->end());
281 EXPECT_FALSE(mrx->begin() == mrx->end());
282 EXPECT_EQ(mrx->begin().pos(), 0u);
283 EXPECT_EQ(mrx->end().pos(), mrx->size());
284
285 EXPECT_TRUE(mr2->begin() != mr2->end());
286 EXPECT_FALSE(mr2->begin() == mr2->end());
287 EXPECT_EQ(mr2->begin().pos(), 0u);
288 EXPECT_EQ(mr2->end().pos(), mr2->size());
289
290 const SizeT s1 = mMeta.size();
291 const SizeT s2 = mSize;
292 const SizeT s1s2 = s1*s2;
293 auto mkm = [&](SizeT i) { return Tuple<SizeT,String,SizeT>(i/s1s2,mMeta[(i/s2) % s1],i % s2); };
294
295 SizeT cnt = 0;
296 auto endxi = mr2->end();
297 for(auto xi = mr2->begin(); xi != endxi; ++xi){
298 EXPECT_EQ(xi.pos(), cnt);
299 EXPECT_EQ(xi.lex(), cnt);
300 auto meta = mkm(cnt);
301 EXPECT_TRUE(*xi == DType(meta));
302 EXPECT_EQ((*xi).str(), toString(meta));
303 ++cnt;
304 }
305
306 cnt = 0;
307 auto endxxi = mrx->end();
308 for(auto xxi = mrx->begin(); xxi != endxxi; ++xxi){
309 EXPECT_EQ(xxi.pos(), cnt);
310 EXPECT_EQ(xxi.lex(), cnt);
311 auto ci1 = xxi.pack()[CSizeT<0>{}];
312 auto ui = xxi.pack()[CSizeT<1>{}];
313 auto ci2 = xxi.pack()[CSizeT<2>{}];
314 Tuple<SizeT,String,SizeT> meta(*(*ci1),*(*ui),*(*ci2));
315 auto meta2 = mkm(cnt);
316 EXPECT_EQ(meta, meta2);
317 EXPECT_TRUE(*xxi == meta);
318 ++cnt;
319 }
320
321 cnt = 0;
322 for(auto x: *mrx){
323 auto meta = mkm(cnt);
324 EXPECT_EQ(x, meta);
325 ++cnt;
326 }
327 }
328
329 TEST_F(YR_Test, Basics2d)
330 {
331
332 EXPECT_EQ(yr->size(), mMeta.size()*mSize);
333 EXPECT_EQ(yr->dim(), 2u);
334
335 EXPECT_TRUE(yr->begin() != yr->end());
336 EXPECT_FALSE(yr->begin() == yr->end());
337 EXPECT_EQ(yr->begin().pos(), 0u);
338 EXPECT_EQ(yr->end().pos(), yr->size());
339
340 const SizeT mmsize = mMeta.size();
341 auto mkm = [&](SizeT i) { return Vector<DType>({DType(i/mmsize),DType(mMeta[i % mmsize])}); };
342
343 SizeT cnt = 0;
344 auto endxi = yr->end();
345 for(auto xi = yr->begin(); xi != endxi; ++xi){
346 EXPECT_EQ(xi.pos(), cnt);
347 auto meta = mkm(cnt);
348 EXPECT_TRUE(*xi == DType(meta));
349 EXPECT_EQ(yr->stringMeta(cnt), toString(meta));
350 EXPECT_EQ((*xi).str(), toString(meta));
351 ++cnt;
352 }
353 }
354
355 TEST_F(YR_Test, Basics3d)
356 {
357
358 EXPECT_EQ(yr2->size(), mMeta.size()*mSize*mSize);
359 EXPECT_EQ(yr2->dim(), 3u);
360
361 EXPECT_TRUE(yr2->begin() != yr2->end());
362 EXPECT_FALSE(yr2->begin() == yr2->end());
363 EXPECT_EQ(yr2->begin().pos(), 0u);
364 EXPECT_EQ(yr2->end().pos(), yr2->size());
365
366 const SizeT s1 = mMeta.size();
367 const SizeT s2 = mSize;
368 const SizeT s1s2 = s1*s2;
369 auto mkm = [&](SizeT i) { return Vector<DType>({DType(i/s1s2),DType(mMeta[(i/s2) % s1]),DType(i % s2)}); };
370
371 SizeT cnt = 0;
372 auto endxi = yr2->end();
373 for(auto xi = yr2->begin(); xi != endxi; ++xi){
374 EXPECT_EQ(xi.pos(), cnt);
375 auto meta = mkm(cnt);
376 EXPECT_EQ(yr2->stringMeta(cnt), toString(meta));
377 EXPECT_TRUE(*xi == DType(meta));
378 EXPECT_EQ((*xi).str(), toString(meta));
379 ++cnt;
380 }
381 }
382
383 TEST_F(YR_Test, Index2d)
384 {
385 auto yrx = std::dynamic_pointer_cast<YRange>(yr);
386 EXPECT_FALSE(yrx == nullptr);
387 auto yi = yrx->begin();
388
389 EXPECT_EQ(yi.pmax().val(), yr->size());
390 EXPECT_EQ(yi.lmax().val(), yr->size());
391 EXPECT_EQ(yi.range(), yr);
392 EXPECT_EQ(yi.range(), yrx);
393 EXPECT_EQ(yi.dim(), 2u);
394
395 const SizeT mmsize = mMeta.size();
396 auto mkm = [&](SizeT i) { return Vector<DType>({DType(i/mmsize),DType(mMeta[i % mmsize])}); };
397 for(SizeT i = 0; i != yr->size(); ++i){
398 auto a = yi + i;
399 EXPECT_EQ(a.lex(), i);
400 EXPECT_EQ(a.pos(), i);
401 auto mmi = mkm(i);
402 EXPECT_TRUE(a.meta() == mmi);
403 EXPECT_TRUE(*a == mmi);
404 EXPECT_EQ(a.stringMeta(), toString(mmi));
405
406 for(SizeT j = 0; j != yr->size(); ++j){
407 const Int jj = static_cast<Int>(j) - static_cast<Int>(i);
408 auto b = a + jj;
409 auto mmj = mkm(j);
410 EXPECT_EQ(b.lex(), j);
411 EXPECT_EQ(b.pos(), j);
412 EXPECT_TRUE(*b == mmj);
413 EXPECT_EQ(b.stringMeta(), toString(mmj));
414 }
415 }
416 yi += yi.lmax().val() + 10;
417 EXPECT_EQ(yi.lex(), yi.lmax().val());
418 EXPECT_EQ(yi.pos(), yi.pmax().val());
419
420 yi -= yi.lmax().val() + 20;
421 EXPECT_EQ(yi.lex(), 0u);
422 EXPECT_EQ(yi.pos(), 0u);
423 }
424
425 TEST_F(YR_Test, Index3d)
426 {
427 auto yrx = std::dynamic_pointer_cast<YRange>(yr2);
428 EXPECT_FALSE(yrx == nullptr);
429 auto yi = yrx->begin();
430
431 EXPECT_EQ(yi.pmax().val(), yr2->size());
432 EXPECT_EQ(yi.lmax().val(), yr2->size());
433 EXPECT_EQ(yi.range(), yr2);
434 EXPECT_EQ(yi.range(), yrx);
435 EXPECT_EQ(yi.dim(), 3u);
436
437 const SizeT s1 = mMeta.size();
438 const SizeT s2 = mSize;
439 const SizeT s1s2 = s1*s2;
440 auto mkm = [&](SizeT i) { return Vector<DType>({DType(i/s1s2),DType(mMeta[(i/s2) % s1]),DType(i % s2)}); };
441 for(SizeT i = 0; i != yr2->size(); ++i){
442 auto a = yi + i;
443 EXPECT_EQ(a.lex(), i);
444 EXPECT_EQ(a.pos(), i);
445 auto mmi = mkm(i);
446 EXPECT_TRUE(a.meta() == mmi);
447 EXPECT_TRUE(*a == mmi);
448 EXPECT_EQ(a.stringMeta(), toString(mmi));
449
450 for(SizeT j = 0; j != yr2->size(); ++j){
451 const Int jj = static_cast<Int>(j) - static_cast<Int>(i);
452 auto b = a + jj;
453 auto mmj = mkm(j);
454 EXPECT_EQ(b.lex(), j);
455 EXPECT_EQ(b.pos(), j);
456 EXPECT_TRUE(*b == mmj);
457 EXPECT_EQ(b.stringMeta(), toString(mmj));
458 }
459 }
460 yi += yi.lmax().val() + 10;
461 EXPECT_EQ(yi.lex(), yi.lmax().val());
462 EXPECT_EQ(yi.pos(), yi.pmax().val());
463
464 yi -= yi.lmax().val() + 20;
465 EXPECT_EQ(yi.lex(), 0u);
466 EXPECT_EQ(yi.pos(), 0u);
467 }
468
470 {
471 const SizeT s1 = mMeta.size();
472 const SizeT s2 = mSize;
473 auto crx = std::dynamic_pointer_cast<CRange>(cr);
474 auto urx = std::dynamic_pointer_cast<URange<String>>(ur);
475 for(auto cix = crx->begin(); cix != crx->end(); ++cix){
476 for(auto uix = urx->begin(); uix != urx->end(); ++uix){
477 const SizeT p1 = cix.lex()*s1 + uix.lex();
478 EXPECT_EQ(mindex(cix*uix).lex(), p1);
479 for(auto ci2x = crx->begin(); ci2x != crx->end(); ++ci2x){
480 const SizeT p2 = cix.lex()*s1*s2 + uix.lex()*s2 + ci2x.lex();
481 EXPECT_EQ(mindex(cix*uix*ci2x).lex(), p2);
482 }
483 }
484 }
485 for(auto ci = cr->begin(); ci != cr->end(); ++ci){
486 for(auto ui = ur->begin(); ui != ur->end(); ++ui){
487 const SizeT p = ci.lex()*s1 + ui.lex();
488 EXPECT_EQ(yindex(ci*ui).lex(), p);
489 for(auto ci2 = cr->begin(); ci2 != cr->end(); ++ci2){
490 const SizeT p2 = ci.lex()*s1*s2 + ui.lex()*s2 + ci2.lex();
491 EXPECT_EQ(yindex(ci*ui*ci2).lex(), p2);
492 }
493 }
494 }
495 }
496
497 TEST_F(YR_Test, prange)
498 {
499 auto yrx = std::dynamic_pointer_cast<YRange>(yr);
500 auto beg = yrx->begin();
501 auto last = yrx->end();
502
503 const SizeT begI = 1;
504 const SizeT lastI = mSize - 2 - 1;
505 const SizeT begJ = 1;
506 const SizeT lastJ = mMeta.size() - 1;
507
508 const SizeT begPos = begI * mMeta.size() + begJ;
509 const SizeT lastPos = lastI * mMeta.size() + lastJ;
510 const SizeT pSize = (lastI-begI+1)*(lastJ-begJ+1);
511 beg += begPos;
512 last = lastPos;
513 auto pr = beg.prange(last);
514 auto prx = std::dynamic_pointer_cast<YRange>(pr);
515
516 EXPECT_EQ(pr->size(), pSize);
517 EXPECT_EQ(prx->size(), pSize);
518
519 const SizeT mmsize = lastJ-begJ+1;
520 auto mkm = [&](SizeT i) {
521 return Vector<DType>({DType(i/mmsize+begI),DType(mMeta[i % mmsize+begJ])}); };
522
523 for(auto i = prx->begin(); i != prx->end(); ++i){
524 EXPECT_TRUE(*i == mkm(i.lex()));
525 }
526
527 const SizeT begI2 = 1;
528 const SizeT lastI2 = 3;
529 const SizeT begJ2 = 0;
530 const SizeT lastJ2 = 0;
531
532 const SizeT begPos2 = begI2 * (lastJ-begJ+1) + begJ2;
533 const SizeT lastPos2 = lastI2 * (lastJ-begJ+1) + lastJ2;
534 const SizeT pSize2 = (lastI2-begI2+1)*(lastJ2-begJ2+1);
535
536 auto beg2 = prx->begin();
537 auto last2 = prx->begin();
538
539 beg2 = begPos2;
540 last2 = lastPos2;
541 auto pr2 = beg2.prange(last2);
542 auto prx2 = std::dynamic_pointer_cast<YRange>(pr2);
543
544 EXPECT_EQ(pr2->size(), pSize2);
545 EXPECT_EQ(prx2->size(), pSize2);
546
547 const SizeT mmsize2 = lastJ2-begJ2+1;
548 auto mkm2 = [&](SizeT i) {
549 return Vector<DType>({DType(i/mmsize2+begI2+begI),DType(mMeta[i % mmsize2+begJ2+begJ])}); };
550
551 for(auto i = prx2->begin(); i != prx2->end(); ++i){
552 EXPECT_TRUE(*i == mkm2(i.lex()));
553 }
554
555 }
556
557// RCast_Test
558}
559
560int main(int argc, char** argv)
561{
562 ::testing::InitGoogleTest(&argc, argv);
563 return RUN_ALL_TESTS();
564}
Array main header.
uint64_t SizeT
Definition types.h:38
constexpr decltype(auto) mindex(const Sptr< Indices > &... is)
Definition mrange.cc.h:617
Sptr< RangeBase > RangePtr
Definition types.h:157
std::vector< T, Allocator< T > > Vector
Definition types.h:310
int32_t Int
Definition types.h:36
RangePtr mrange(const Sptr< Ranges > &... rs)
Definition mrange.cc.h:800
std::integral_constant< SizeT, N > CSizeT
Definition types.h:81
Sptr< Range > rangeCast(const RangePtr r)
std::tuple< T... > Tuple
Definition types.h:60
YIndex yindex(const DPack &pack)
Definition yrange.cc:524
String toString(const T &a)
RangePtr prange(const Sptr< RangeT > &range, const Vector< SizeT > &parts)
Definition prange.cc.h:401
int main(int argc, char **argv)
Ranges main header.