CNORXZ
Container with Native Operation Routines and Expressions
Loading...
Searching...
No Matches
xpr_unit_test.cc
Go to the documentation of this file.
1// -*- C++ -*-
12#include "gtest/gtest.h"
13
14#include "xpr/xpr.h"
15
16namespace
17{
18 using namespace CNORXZ;
19
20 template <class PosT1, class PosT2>
21 constexpr auto mkMPos(const PosT1& a, const PosT2& b)
22 {
23 return MPos<PosT1,PosT2>(a,b);
24 }
25
26 class Pos_Test : public ::testing::Test
27 {
28 protected:
29
30 static constexpr SizeT s1 = 7;
31 static constexpr SizeT s2 = 3;
32 static constexpr SizeT ss1 = 4;
33 static constexpr SizeT ss2 = 2;
34
35 Pos_Test()
36 {
37 mUp1 = UPos(s1);
38 mUp2 = UPos(s2);
39 }
40
41 UPos mUp1;
42 UPos mUp2;
43 SPos<ss1> mS4p;
44 SPos<ss2> mS2p;
45 };
46
47 class For_Test : public ::testing::Test
48 {
49 protected:
50
51 class TestXpr1
52 {
53 public:
54 constexpr TestXpr1(const IndexId<0>& id) : mId(id) {}
55
56 template <class PosT>
57 inline SizeT operator()(const PosT& last) const
58 {
59 const SizeT o = 1u;
60 return o << last.val();
61 }
62
63 template <SizeT I>
64 inline UPos rootSteps(const IndexId<I>& id) const
65 {
66 return UPos( mId == id ? 1u : 0u );
67 }
68
69 private:
70 IndexId<0> mId;
71 };
72
73 class TestXpr2
74 {
75 public:
76 constexpr TestXpr2(const IndexId<0>& id, SizeT size) :
77 mId(id), mSize(size), mCnt(size) {}
78
79 template <class PosT>
80 inline void operator()(const PosT& last) const
81 {
82 --mCnt;
83 EXPECT_EQ(mCnt, mSize-last.val()-1);
84 }
85
86 template <SizeT I>
87 inline UPos rootSteps(const IndexId<I>& id) const
88 {
89 return UPos( mId == id ? 1u : 0u );
90 }
91
92 private:
93 IndexId<0> mId;
94 SizeT mSize;
95 mutable SizeT mCnt;
96 };
97
98 static constexpr SizeT sSize = 7u;
99
100 For_Test()
101 {
102 mSize = sSize;
103 mId1 = 10u;
104 mId2 = 11u;
105 mId3 = 12u;
106 }
107
108 SizeT mSize;
109 PtrId mId1;
110 PtrId mId2;
111 PtrId mId3;
112 };
113
114 TEST_F(Pos_Test, Basics)
115 {
116 EXPECT_EQ( mUp1.size(), 1u );
117 EXPECT_EQ( mUp2.size(), 1u );
118 EXPECT_EQ( mS4p.size(), 1u );
119 EXPECT_EQ( mS2p.size(), 1u );
120
121 EXPECT_EQ( mUp1.val(), s1 );
122 EXPECT_EQ( mUp2.val(), s2 );
123 EXPECT_EQ( mS4p.val(), ss1 );
124 EXPECT_EQ( mS2p.val(), ss2 );
125
126 auto s6p = mS4p + mS2p;
127 auto s8p = mS4p * mS2p;
128 auto up3 = mS4p * mUp1;
129 auto up4 = mUp1 * mS2p;
130 auto up5 = mS4p + mUp1;
131 auto up6 = mUp1 + mS2p;
132
133 EXPECT_EQ( s6p.size(), 1u );
134 EXPECT_EQ( s8p.size(), 1u );
135 EXPECT_EQ( up3.size(), 1u );
136 EXPECT_EQ( up4.size(), 1u );
137 EXPECT_EQ( up5.size(), 1u );
138 EXPECT_EQ( up6.size(), 1u );
139
140 EXPECT_EQ( s6p.val(), ss1+ss2 );
141 EXPECT_EQ( s8p.val(), ss1*ss2 );
142 EXPECT_EQ( up3.val(), ss1*s1 );
143 EXPECT_EQ( up4.val(), s1*ss2 );
144 EXPECT_EQ( up5.val(), ss1+s1 );
145 EXPECT_EQ( up6.val(), s1+ss2 );
146 }
147
148 TEST_F(Pos_Test, Multi)
149 {
150 auto mp1 = mkMPos(mS2p, mUp1);
151 auto mp2 = mkMPos(mUp2, mS4p);
152 auto mp3a = mp1 + mp2;
153 auto mp3b = mp2 + mp1;
154 auto mp4 = mp1 * mS2p;
155 auto mp5 = mp2 * mUp1;
156
157 EXPECT_EQ( mp1.size(), 2u );
158 EXPECT_EQ( mp2.size(), 2u );
159 EXPECT_EQ( mp3a.size(), 2u );
160 EXPECT_EQ( mp3b.size(), 2u );
161 EXPECT_EQ( mp4.size(), 2u );
162 EXPECT_EQ( mp5.size(), 2u );
163
164 EXPECT_EQ(mp1.val(), mS2p.val());
165 EXPECT_EQ(mp1.next().val(), mUp1.val());
166 EXPECT_EQ(mp2.val(), mUp2.val());
167 EXPECT_EQ(mp2.next().val(), mS4p.val());
168 EXPECT_EQ(mp3a.val(), mS2p.val() + mUp2.val());
169 EXPECT_EQ(mp3a.next().val(), mUp1.val() + mS4p.val());
170 EXPECT_EQ(mp3b.val(), mS2p.val() + mUp2.val());
171 EXPECT_EQ(mp3b.next().val(), mUp1.val() + mS4p.val());
172 EXPECT_EQ(mp4.val(), mS2p.val() * mS2p.val());
173 EXPECT_EQ(mp4.next().val(), mUp1.val() * mS2p.val());
174 EXPECT_EQ(mp5.val(), mUp2.val() * mUp1.val());
175 EXPECT_EQ(mp5.next().val(), mS4p.val() * mUp1.val());
176 }
177
178 TEST_F(Pos_Test, Extend)
179 {
180 auto mp1 = mkMPos(mS2p, mUp1);
181 auto mp2 = mkMPos(mUp2, mS4p);
182 auto mp3 = mp1.extend(mUp2);
183 auto mp4 = mp2.extend(mS2p);
184
185 EXPECT_EQ( mp3.size(), 3u );
186 EXPECT_EQ( mp4.size(), 3u );
187
188 EXPECT_EQ( mp3.val(), mS2p.val() );
189 EXPECT_EQ( mp3.next().val(), mUp1.val() );
190 EXPECT_EQ( mp3.next().next().val(), mUp2.val() );
191
192 EXPECT_EQ( mp4.val(), mUp2.val() );
193 EXPECT_EQ( mp4.next().val(), mS4p.val() );
194 EXPECT_EQ( mp4.next().next().val(), mS2p.val() );
195 }
196
197 TEST_F(Pos_Test, Dyn)
198 {
199 DPos dp01(static_cast<UPos>(mS2p));
200 DPos dp02(mUp1);
201
202 DPos dp1(mkMPos(static_cast<UPos>(mS2p), mUp1));
203 DPos dp2(mkMPos(mUp2, static_cast<UPos>(mS4p)));
204 auto dp3a = dp1 + dp2;
205 auto dp3b = dp2 + dp1;
206 auto dp4 = dp1 * dp01;
207 auto dp5 = dp2 * dp02;
208
209 EXPECT_EQ(dp01.size(), 1u);
210 EXPECT_EQ(dp02.size(), 1u);
211
212 EXPECT_EQ(dp01.val(), mS2p.val());
213 EXPECT_EQ(dp02.val(), mUp1.val());
214
215 EXPECT_EQ(dp1.size(), 2u);
216 EXPECT_EQ(dp2.size(), 2u);
217
218 EXPECT_EQ(dp3a.size(), 2u);
219 EXPECT_EQ(dp3b.size(), 2u);
220 EXPECT_EQ(dp4.size(), 2u);
221 EXPECT_EQ(dp5.size(), 2u);
222
223 EXPECT_EQ(dp1.val(), mS2p.val());
224 EXPECT_EQ(dp1.sub().val(), mUp1.val());
225 EXPECT_EQ(dp2.val(), mUp2.val());
226 EXPECT_EQ(dp2.sub().val(), mS4p.val());
227 EXPECT_EQ(dp3a.val(), mS2p.val() + mUp2.val());
228 EXPECT_EQ(dp3a.sub().val(), mUp1.val() + mS4p.val());
229 EXPECT_EQ(dp3b.val(), mS2p.val() + mUp2.val());
230 EXPECT_EQ(dp3b.sub().val(), mUp1.val() + mS4p.val());
231 EXPECT_EQ(dp4.val(), mS2p.val() * mS2p.val());
232 EXPECT_EQ(dp4.sub().val(), mUp1.val() * mS2p.val());
233 EXPECT_EQ(dp5.val(), mUp2.val() * mUp1.val());
234 EXPECT_EQ(dp5.sub().val(), mS4p.val() * mUp1.val());
235 }
236
237 TEST_F(For_Test, For)
238 {
239 auto loop = mkFor(mSize, IndexId<0>(mId1), TestXpr1( IndexId<0>(mId1) ),
240 [](auto& o, const auto& e) { o += e; });
241
242 const UPos rs = loop.rootSteps(IndexId<0>(mId1));
243 EXPECT_EQ(rs.val(), 1u);
244 const UPos rs2 = loop.rootSteps(IndexId<0>(mId2));
245 EXPECT_EQ(rs2.val(), 0u);
246 const SizeT res = loop();
247 EXPECT_EQ(res, (1u << mSize) - 1u);
248
249 auto loop2 = mkFor(mSize, IndexId<0>(mId1), TestXpr2( IndexId<0>(mId1), mSize ));
250 loop2();
251 }
252
253 TEST_F(For_Test, SFor)
254 {
255 auto loop = mkSFor<sSize>(IndexId<0>(mId1), TestXpr1( IndexId<0>(mId1) ),
256 [](const auto& a, const auto& b) { return a + b; });
257
258 const UPos rs = loop.rootSteps(IndexId<0>(mId1));
259 EXPECT_EQ(rs.val(), 1u);
260 const UPos rs2 = loop.rootSteps(IndexId<0>(mId2));
261 EXPECT_EQ(rs2.val(), 0u);
262 const SizeT res = loop();
263 EXPECT_EQ(res, (1u << mSize) - 1u);
264
265 auto loop2 = mkSFor<sSize>(IndexId<0>(mId1), TestXpr2( IndexId<0>(mId1), mSize ));
266 loop2();
267 }
268}
269
270int main(int argc, char** argv)
271{
272 ::testing::InitGoogleTest(&argc, argv);
273 return RUN_ALL_TESTS();
274}
std::intptr_t PtrId
Definition types.h:35
uint64_t SizeT
Definition types.h:38
Sptr< Range > rangeCast(const RangePtr r)
constexpr decltype(auto) mkFor(SizeT size, const IndexId< L > &id, const Xpr &xpr, F &&f)
Definition for.cc.h:91
Xpression main header.
int main(int argc, char **argv)