CNORXZ
Container with Native Operation Routines and Expressions
Loading...
Searching...
No Matches
marray_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 "cnorxz.h"
18#include "test_numbers.h"
19
20namespace
21{
22 using namespace CNORXZ;
23 using Test::Numbers;
24
25 class MA_1D_Test : public ::testing::Test
26 {
27 protected:
28
29 MA_1D_Test()
30 {
31 mSize = 7;
32 mCR1 = CRangeFactory(mSize).create();
33 mA = MArray<Double>(mCR1, Numbers::get(0,mSize));
34 }
35
36 SizeT mSize;
37 RangePtr mCR1;
39 };
40
41 class MA_2D_Test : public ::testing::Test
42 {
43 protected:
44
45 MA_2D_Test()
46 {
47 mSize = 5;
48 mStrMeta = { "another", "test", "string", "vector", "for", "this", "Test" };
49 mCR1 = CRangeFactory(mSize).create();
50 mUR1 = URangeFactory<String>(mStrMeta).create();
51 const SizeT ssize = mStrMeta.size();
52 const SizeT size = mSize * ssize;
53 mA = MArray<Double>(mCR1*mUR1, Numbers::get(0,size));
54 mA2 = MArray<Double>(mUR1*mCR1, Numbers::get(0,size));
55 }
56
57 SizeT mSize;
58 Vector<String> mStrMeta;
59 RangePtr mCR1;
60 RangePtr mUR1;
63 };
64
65 TEST_F(MA_1D_Test, Basics)
66 {
67 auto crx = std::dynamic_pointer_cast<CRange>(mCR1);
68 EXPECT_EQ(mA.size(), mSize);
69 EXPECT_FALSE(mA.isView());
70
71 auto ei = crx->end();
72 EXPECT_EQ(ei.lex(), mSize);
73 for(auto i = crx->begin(); i != ei; ++i){
74 EXPECT_EQ(i.lex(), i.pos());
75 EXPECT_EQ(mA[i], mA.data()[i.lex()]);
76 EXPECT_EQ(mA.at(i), mA.data()[i.lex()]);
77 }
78 EXPECT_THROW({mA.at(ei);}, std::runtime_error);
79 }
80
81 TEST_F(MA_1D_Test, Slice)
82 {
83 auto crx = std::dynamic_pointer_cast<CRange>(mCR1);
84 auto beg = crx->begin() + 2;
85 auto last = crx->begin() + 5;
86
87 //const auto& a = mA;
88 auto sl = mA.sl(beg,last);
89 EXPECT_EQ(sl->size(), last.lex()-beg.lex()+1);
90
91 SizeT c = 0;
92 auto ii = crx->begin();
93 for(auto i = sl->begin() ; i != sl->end(); ++i, ++c, ++ii){
94 EXPECT_EQ(i.lex(), c);
95 EXPECT_EQ(ii.lex(), c);
96 EXPECT_EQ((*sl)[i], mA.data()[i.lex()+beg.lex()]);
97 EXPECT_EQ((*sl)[ii], mA.data()[i.lex()+beg.lex()]);
98 EXPECT_EQ(sl->at(i), mA.data()[i.lex()+beg.lex()]);
99 EXPECT_EQ(sl->at(ii), mA.data()[i.lex()+beg.lex()]);
100 }
101 }
102
103 TEST_F(MA_2D_Test, Basics)
104 {
105 const SizeT ssize = mStrMeta.size();
106 const SizeT size = mSize * ssize;
107 EXPECT_EQ(mA.range()->dim(), 2u);
108 EXPECT_EQ(mA.size(), size);
109 EXPECT_EQ(mA.range()->sub(0), mCR1);
110 EXPECT_EQ(mA.range()->sub(1), mUR1);
111 auto cr1x = std::dynamic_pointer_cast<CRange>(mCR1);
112 auto ur1x = std::dynamic_pointer_cast<URange<String>>(mUR1);
113 for(auto ci = cr1x->begin(); ci != cr1x->end(); ++ci){
114 for(auto ui = ur1x->begin(); ui != ur1x->end(); ++ui){
115 const SizeT p = ci.lex()*ssize + ui.lex();
116 EXPECT_EQ( mA[ci*ui], mA.data()[p] );
117 }
118 }
119 }
120
121 TEST_F(MA_2D_Test, Operation)
122 {
123 auto uip1 = std::make_shared<UIndex<String>>(mUR1);
124 auto cip1 = std::make_shared<CIndex>(mCR1);
125 mA2(uip1*cip1) = mA(cip1*uip1);
126 for(auto ui = mUR1->begin(); ui != mUR1->end(); ++ui){
127 for(auto ci = mCR1->begin(); ci != mCR1->end(); ++ci){
128 EXPECT_EQ(mA2[ui*ci], mA[ci*ui]);
129 }
130 }
131 }
132
133 TEST_F(MA_2D_Test, Slice)
134 {
135 auto crx = std::dynamic_pointer_cast<CRange>(mCR1);
136 auto beg = crx->begin() + 2;
137 auto last = crx->begin() + 5;
138
139 auto uip1 = std::make_shared<UIndex<String>>(mUR1);
140 auto cip1 = std::make_shared<CIndex>(mCR1);
141 auto beg1 = (*cip1)+1;
142 auto last1 = (*cip1)+3;
143 auto beg2 = (*uip1)+2;
144 auto last2 = (*uip1)+5;
145
146 auto sl = mA.sl(mindex(beg1*beg2),mindex(last1*last2));
147 EXPECT_EQ(sl->size(), (last1.lex()-beg1.lex()+1)*(last2.lex()-beg2.lex()+1));
148
150 EXPECT_EQ(mi.lmax().val(), sl->size());
151 auto i1 = *mi.pack()[CSizeT<0>{}];
152 auto i2 = *mi.pack()[CSizeT<1>{}];
153 EXPECT_EQ(i1.lmax().val(), last1.lex()-beg1.lex()+1);
154 EXPECT_EQ(i2.lmax().val(), last2.lex()-beg2.lex()+1);
155 for(i1 = 0; i1.lex() != i1.lmax().val(); ++ i1){
156 for(i2 = 0; i2.lex() != i2.lmax().val(); ++i2){
157 const SizeT p = (i1.lex()+beg1.lex())*uip1->lmax().val() + i2.lex()+beg2.lex();
158 EXPECT_EQ((*sl)[i1*i2], mA.data()[p]);
159 }
160 }
161 }
162
163} // end namespace
164
165int main(int argc, char** argv)
166{
167 ::testing::InitGoogleTest(&argc, argv);
168 return RUN_ALL_TESTS();
169}
cnorxz main header
int main(int argc, char **argv)
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
std::integral_constant< SizeT, N > CSizeT
Definition types.h:81
Sptr< Range > rangeCast(const RangePtr r)
Test numbers utilities.