1 // Copyright (c) 2018-2019 Cem Bassoy
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 #include <boost/test/unit_test.hpp>
8 #include <boost/numeric/ublas/tensor/extents.hpp>
9 #include <vector>
10
11 BOOST_AUTO_TEST_SUITE ( test_extents )
12
13
14 //*boost::unit_test::label("extents")
15 //*boost::unit_test::label("constructor")
16
BOOST_AUTO_TEST_CASE(test_extents_ctor)17 BOOST_AUTO_TEST_CASE(test_extents_ctor)
18 {
19 using namespace boost::numeric;
20 using extents = ublas::basic_extents<unsigned>;
21
22
23 auto e0 = extents{};
24 BOOST_CHECK( e0.empty());
25 BOOST_CHECK_EQUAL ( e0.size(),0);
26
27 auto e1 = extents{1,1};
28 BOOST_CHECK(!e1.empty());
29 BOOST_CHECK_EQUAL ( e1.size(),2);
30
31 auto e2 = extents{1,2};
32 BOOST_CHECK(!e2.empty());
33 BOOST_CHECK_EQUAL ( e2.size(),2);
34
35 auto e3 = extents{2,1};
36 BOOST_CHECK (!e3.empty());
37 BOOST_CHECK_EQUAL ( e3.size(),2);
38
39 auto e4 = extents{2,3};
40 BOOST_CHECK(!e4.empty());
41 BOOST_CHECK_EQUAL ( e4.size(),2);
42
43 auto e5 = extents{2,3,1};
44 BOOST_CHECK (!e5.empty());
45 BOOST_CHECK_EQUAL ( e5.size(),3);
46
47 auto e6 = extents{1,2,3}; // 6
48 BOOST_CHECK(!e6.empty());
49 BOOST_CHECK_EQUAL ( e6.size(),3);
50
51 auto e7 = extents{4,2,3}; // 7
52 BOOST_CHECK(!e7.empty());
53 BOOST_CHECK_EQUAL ( e7.size(),3);
54
55 BOOST_CHECK_THROW( extents({1,0}), std::length_error );
56 BOOST_CHECK_THROW( extents({0} ), std::length_error );
57 BOOST_CHECK_THROW( extents({3} ), std::length_error );
58 BOOST_CHECK_THROW( extents({0,1}), std::length_error );
59 }
60
61
62
63
64 struct fixture {
65 using extents_type = boost::numeric::ublas::basic_extents<unsigned>;
fixturefixture66 fixture() : extents{
67 extents_type{}, // 0
68
69 extents_type{1,1}, // 1
70 extents_type{1,2}, // 2
71 extents_type{2,1}, // 3
72
73 extents_type{2,3}, // 4
74 extents_type{2,3,1}, // 5
75 extents_type{1,2,3}, // 6
76 extents_type{1,1,2,3}, // 7
77 extents_type{1,2,3,1,1}, // 8
78
79 extents_type{4,2,3}, // 9
80 extents_type{4,2,1,3}, // 10
81 extents_type{4,2,1,3,1}, // 11
82 extents_type{1,4,2,1,3,1}, // 12
83 } // 13
84 {}
85 std::vector<extents_type> extents;
86 };
87
88 BOOST_FIXTURE_TEST_CASE(test_extents_access, fixture, *boost::unit_test::label("extents") *boost::unit_test::label("access"))
89 {
90 using namespace boost::numeric;
91
92 BOOST_REQUIRE_EQUAL(extents.size(),13);
93
94 BOOST_CHECK_EQUAL (extents[ 0].size(), 0);
95 BOOST_CHECK (extents[ 0].empty() );
96
97 BOOST_REQUIRE_EQUAL(extents[ 1].size(), 2);
98 BOOST_REQUIRE_EQUAL(extents[ 2].size(), 2);
99 BOOST_REQUIRE_EQUAL(extents[ 3].size(), 2);
100 BOOST_REQUIRE_EQUAL(extents[ 4].size(), 2);
101 BOOST_REQUIRE_EQUAL(extents[ 5].size(), 3);
102 BOOST_REQUIRE_EQUAL(extents[ 6].size(), 3);
103 BOOST_REQUIRE_EQUAL(extents[ 7].size(), 4);
104 BOOST_REQUIRE_EQUAL(extents[ 8].size(), 5);
105 BOOST_REQUIRE_EQUAL(extents[ 9].size(), 3);
106 BOOST_REQUIRE_EQUAL(extents[10].size(), 4);
107 BOOST_REQUIRE_EQUAL(extents[11].size(), 5);
108 BOOST_REQUIRE_EQUAL(extents[12].size(), 6);
109
110
111 BOOST_CHECK_EQUAL(extents[1][0],1);
112 BOOST_CHECK_EQUAL(extents[1][1],1);
113
114 BOOST_CHECK_EQUAL(extents[2][0],1);
115 BOOST_CHECK_EQUAL(extents[2][1],2);
116
117 BOOST_CHECK_EQUAL(extents[3][0],2);
118 BOOST_CHECK_EQUAL(extents[3][1],1);
119
120 BOOST_CHECK_EQUAL(extents[4][0],2);
121 BOOST_CHECK_EQUAL(extents[4][1],3);
122
123 BOOST_CHECK_EQUAL(extents[5][0],2);
124 BOOST_CHECK_EQUAL(extents[5][1],3);
125 BOOST_CHECK_EQUAL(extents[5][2],1);
126
127 BOOST_CHECK_EQUAL(extents[6][0],1);
128 BOOST_CHECK_EQUAL(extents[6][1],2);
129 BOOST_CHECK_EQUAL(extents[6][2],3);
130
131 BOOST_CHECK_EQUAL(extents[7][0],1);
132 BOOST_CHECK_EQUAL(extents[7][1],1);
133 BOOST_CHECK_EQUAL(extents[7][2],2);
134 BOOST_CHECK_EQUAL(extents[7][3],3);
135
136 BOOST_CHECK_EQUAL(extents[8][0],1);
137 BOOST_CHECK_EQUAL(extents[8][1],2);
138 BOOST_CHECK_EQUAL(extents[8][2],3);
139 BOOST_CHECK_EQUAL(extents[8][3],1);
140 BOOST_CHECK_EQUAL(extents[8][4],1);
141
142 BOOST_CHECK_EQUAL(extents[9][0],4);
143 BOOST_CHECK_EQUAL(extents[9][1],2);
144 BOOST_CHECK_EQUAL(extents[9][2],3);
145
146 BOOST_CHECK_EQUAL(extents[10][0],4);
147 BOOST_CHECK_EQUAL(extents[10][1],2);
148 BOOST_CHECK_EQUAL(extents[10][2],1);
149 BOOST_CHECK_EQUAL(extents[10][3],3);
150
151 BOOST_CHECK_EQUAL(extents[11][0],4);
152 BOOST_CHECK_EQUAL(extents[11][1],2);
153 BOOST_CHECK_EQUAL(extents[11][2],1);
154 BOOST_CHECK_EQUAL(extents[11][3],3);
155 BOOST_CHECK_EQUAL(extents[11][4],1);
156
157 BOOST_CHECK_EQUAL(extents[12][0],1);
158 BOOST_CHECK_EQUAL(extents[12][1],4);
159 BOOST_CHECK_EQUAL(extents[12][2],2);
160 BOOST_CHECK_EQUAL(extents[12][3],1);
161 BOOST_CHECK_EQUAL(extents[12][4],3);
162 BOOST_CHECK_EQUAL(extents[12][5],1);
163 }
164
165 BOOST_FIXTURE_TEST_CASE(test_extents_copy_ctor, fixture, *boost::unit_test::label("extents") *boost::unit_test::label("copy_ctor"))
166 {
167 BOOST_REQUIRE_EQUAL(extents.size(),13);
168
169 auto e0 = extents[ 0]; // {}
170 auto e1 = extents[ 1]; // {1,1}
171 auto e2 = extents[ 2]; // {1,2}
172 auto e3 = extents[ 3]; // {2,1}
173 auto e4 = extents[ 4]; // {2,3}
174 auto e5 = extents[ 5]; // {2,3,1}
175 auto e6 = extents[ 6]; // {1,2,3}
176 auto e7 = extents[ 7]; // {1,1,2,3}
177 auto e8 = extents[ 8]; // {1,2,3,1,1}
178 auto e9 = extents[ 9]; // {4,2,3}
179 auto e10 = extents[10]; // {4,2,1,3}
180 auto e11 = extents[11]; // {4,2,1,3,1}
181 auto e12 = extents[12]; // {1,4,2,1,3,1}
182
183 BOOST_CHECK_EQUAL (e0.size(), 0);
184 BOOST_CHECK (e0.empty() );
185
186 BOOST_REQUIRE_EQUAL(e1 .size(), 2);
187 BOOST_REQUIRE_EQUAL(e2 .size(), 2);
188 BOOST_REQUIRE_EQUAL(e3 .size(), 2);
189 BOOST_REQUIRE_EQUAL(e4 .size(), 2);
190 BOOST_REQUIRE_EQUAL(e5 .size(), 3);
191 BOOST_REQUIRE_EQUAL(e6 .size(), 3);
192 BOOST_REQUIRE_EQUAL(e7 .size(), 4);
193 BOOST_REQUIRE_EQUAL(e8 .size(), 5);
194 BOOST_REQUIRE_EQUAL(e9 .size(), 3);
195 BOOST_REQUIRE_EQUAL(e10.size(), 4);
196 BOOST_REQUIRE_EQUAL(e11.size(), 5);
197 BOOST_REQUIRE_EQUAL(e12.size(), 6);
198
199
200 BOOST_CHECK_EQUAL(e1[0],1);
201 BOOST_CHECK_EQUAL(e1[1],1);
202
203 BOOST_CHECK_EQUAL(e2[0],1);
204 BOOST_CHECK_EQUAL(e2[1],2);
205
206 BOOST_CHECK_EQUAL(e3[0],2);
207 BOOST_CHECK_EQUAL(e3[1],1);
208
209 BOOST_CHECK_EQUAL(e4[0],2);
210 BOOST_CHECK_EQUAL(e4[1],3);
211
212 BOOST_CHECK_EQUAL(e5[0],2);
213 BOOST_CHECK_EQUAL(e5[1],3);
214 BOOST_CHECK_EQUAL(e5[2],1);
215
216 BOOST_CHECK_EQUAL(e6[0],1);
217 BOOST_CHECK_EQUAL(e6[1],2);
218 BOOST_CHECK_EQUAL(e6[2],3);
219
220 BOOST_CHECK_EQUAL(e7[0],1);
221 BOOST_CHECK_EQUAL(e7[1],1);
222 BOOST_CHECK_EQUAL(e7[2],2);
223 BOOST_CHECK_EQUAL(e7[3],3);
224
225 BOOST_CHECK_EQUAL(e8[0],1);
226 BOOST_CHECK_EQUAL(e8[1],2);
227 BOOST_CHECK_EQUAL(e8[2],3);
228 BOOST_CHECK_EQUAL(e8[3],1);
229 BOOST_CHECK_EQUAL(e8[4],1);
230
231 BOOST_CHECK_EQUAL(e9[0],4);
232 BOOST_CHECK_EQUAL(e9[1],2);
233 BOOST_CHECK_EQUAL(e9[2],3);
234
235 BOOST_CHECK_EQUAL(e10[0],4);
236 BOOST_CHECK_EQUAL(e10[1],2);
237 BOOST_CHECK_EQUAL(e10[2],1);
238 BOOST_CHECK_EQUAL(e10[3],3);
239
240 BOOST_CHECK_EQUAL(e11[0],4);
241 BOOST_CHECK_EQUAL(e11[1],2);
242 BOOST_CHECK_EQUAL(e11[2],1);
243 BOOST_CHECK_EQUAL(e11[3],3);
244 BOOST_CHECK_EQUAL(e11[4],1);
245
246 BOOST_CHECK_EQUAL(e12[0],1);
247 BOOST_CHECK_EQUAL(e12[1],4);
248 BOOST_CHECK_EQUAL(e12[2],2);
249 BOOST_CHECK_EQUAL(e12[3],1);
250 BOOST_CHECK_EQUAL(e12[4],3);
251 BOOST_CHECK_EQUAL(e12[5],1);
252
253 }
254
255 BOOST_FIXTURE_TEST_CASE(test_extents_is, fixture, *boost::unit_test::label("extents") *boost::unit_test::label("query"))
256 {
257 BOOST_REQUIRE_EQUAL(extents.size(),13);
258
259 auto e0 = extents[ 0]; // {}
260 auto e1 = extents[ 1]; // {1,1}
261 auto e2 = extents[ 2]; // {1,2}
262 auto e3 = extents[ 3]; // {2,1}
263 auto e4 = extents[ 4]; // {2,3}
264 auto e5 = extents[ 5]; // {2,3,1}
265 auto e6 = extents[ 6]; // {1,2,3}
266 auto e7 = extents[ 7]; // {1,1,2,3}
267 auto e8 = extents[ 8]; // {1,2,3,1,1}
268 auto e9 = extents[ 9]; // {4,2,3}
269 auto e10 = extents[10]; // {4,2,1,3}
270 auto e11 = extents[11]; // {4,2,1,3,1}
271 auto e12 = extents[12]; // {1,4,2,1,3,1}
272
273 BOOST_CHECK( e0.empty ());
274 BOOST_CHECK( ! e0.is_scalar());
275 BOOST_CHECK( ! e0.is_vector());
276 BOOST_CHECK( ! e0.is_matrix());
277 BOOST_CHECK( ! e0.is_tensor());
278
279 BOOST_CHECK( ! e1.empty () );
280 BOOST_CHECK( e1.is_scalar() );
281 BOOST_CHECK( ! e1.is_vector() );
282 BOOST_CHECK( ! e1.is_matrix() );
283 BOOST_CHECK( ! e1.is_tensor() );
284
285 BOOST_CHECK( ! e2.empty () );
286 BOOST_CHECK( ! e2.is_scalar() );
287 BOOST_CHECK( e2.is_vector() );
288 BOOST_CHECK( ! e2.is_matrix() );
289 BOOST_CHECK( ! e2.is_tensor() );
290
291 BOOST_CHECK( ! e3.empty () );
292 BOOST_CHECK( ! e3.is_scalar() );
293 BOOST_CHECK( e3.is_vector() );
294 BOOST_CHECK( ! e3.is_matrix() );
295 BOOST_CHECK( ! e3.is_tensor() );
296
297 BOOST_CHECK( ! e4.empty () );
298 BOOST_CHECK( ! e4.is_scalar() );
299 BOOST_CHECK( ! e4.is_vector() );
300 BOOST_CHECK( e4.is_matrix() );
301 BOOST_CHECK( ! e4.is_tensor() );
302
303 BOOST_CHECK( ! e5.empty () );
304 BOOST_CHECK( ! e5.is_scalar() );
305 BOOST_CHECK( ! e5.is_vector() );
306 BOOST_CHECK( e5.is_matrix() );
307 BOOST_CHECK( ! e5.is_tensor() );
308
309 BOOST_CHECK( ! e6.empty () );
310 BOOST_CHECK( ! e6.is_scalar() );
311 BOOST_CHECK( ! e6.is_vector() );
312 BOOST_CHECK( ! e6.is_matrix() );
313 BOOST_CHECK( e6.is_tensor() );
314
315 BOOST_CHECK( ! e7.empty () );
316 BOOST_CHECK( ! e7.is_scalar() );
317 BOOST_CHECK( ! e7.is_vector() );
318 BOOST_CHECK( ! e7.is_matrix() );
319 BOOST_CHECK( e7.is_tensor() );
320
321 BOOST_CHECK( ! e8.empty () );
322 BOOST_CHECK( ! e8.is_scalar() );
323 BOOST_CHECK( ! e8.is_vector() );
324 BOOST_CHECK( ! e8.is_matrix() );
325 BOOST_CHECK( e8.is_tensor() );
326
327 BOOST_CHECK( ! e9.empty () );
328 BOOST_CHECK( ! e9.is_scalar() );
329 BOOST_CHECK( ! e9.is_vector() );
330 BOOST_CHECK( ! e9.is_matrix() );
331 BOOST_CHECK( e9.is_tensor() );
332
333 BOOST_CHECK( ! e10.empty () );
334 BOOST_CHECK( ! e10.is_scalar() );
335 BOOST_CHECK( ! e10.is_vector() );
336 BOOST_CHECK( ! e10.is_matrix() );
337 BOOST_CHECK( e10.is_tensor() );
338
339 BOOST_CHECK( ! e11.empty () );
340 BOOST_CHECK( ! e11.is_scalar() );
341 BOOST_CHECK( ! e11.is_vector() );
342 BOOST_CHECK( ! e11.is_matrix() );
343 BOOST_CHECK( e11.is_tensor() );
344
345 BOOST_CHECK( ! e12.empty () );
346 BOOST_CHECK( ! e12.is_scalar() );
347 BOOST_CHECK( ! e12.is_vector() );
348 BOOST_CHECK( ! e12.is_matrix() );
349 BOOST_CHECK( e12.is_tensor() );
350 }
351
352
353 BOOST_FIXTURE_TEST_CASE(test_extents_squeeze, fixture, *boost::unit_test::label("extents") *boost::unit_test::label("squeeze"))
354 {
355 BOOST_REQUIRE_EQUAL(extents.size(),13);
356
357 auto e0 = extents[ 0].squeeze(); // {}
358 auto e1 = extents[ 1].squeeze(); // {1,1}
359 auto e2 = extents[ 2].squeeze(); // {1,2}
360 auto e3 = extents[ 3].squeeze(); // {2,1}
361
362 auto e4 = extents[ 4].squeeze(); // {2,3}
363 auto e5 = extents[ 5].squeeze(); // {2,3}
364 auto e6 = extents[ 6].squeeze(); // {2,3}
365 auto e7 = extents[ 7].squeeze(); // {2,3}
366 auto e8 = extents[ 8].squeeze(); // {2,3}
367
368 auto e9 = extents[ 9].squeeze(); // {4,2,3}
369 auto e10 = extents[10].squeeze(); // {4,2,3}
370 auto e11 = extents[11].squeeze(); // {4,2,3}
371 auto e12 = extents[12].squeeze(); // {4,2,3}
372
373 BOOST_CHECK( (e0 == extents_type{} ) );
374 BOOST_CHECK( (e1 == extents_type{1,1}) );
375 BOOST_CHECK( (e2 == extents_type{1,2}) );
376 BOOST_CHECK( (e3 == extents_type{2,1}) );
377
378 BOOST_CHECK( (e4 == extents_type{2,3}) );
379 BOOST_CHECK( (e5 == extents_type{2,3}) );
380 BOOST_CHECK( (e6 == extents_type{2,3}) );
381 BOOST_CHECK( (e7 == extents_type{2,3}) );
382 BOOST_CHECK( (e8 == extents_type{2,3}) );
383
384 BOOST_CHECK( (e9 == extents_type{4,2,3}) );
385 BOOST_CHECK( (e10 == extents_type{4,2,3}) );
386 BOOST_CHECK( (e11 == extents_type{4,2,3}) );
387 BOOST_CHECK( (e12 == extents_type{4,2,3}) );
388
389 }
390
391
392 BOOST_FIXTURE_TEST_CASE(test_extents_valid, fixture, *boost::unit_test::label("extents") *boost::unit_test::label("valid"))
393 {
394
395 using namespace boost::numeric;
396
397 BOOST_REQUIRE_EQUAL(extents.size(),13);
398
399 for(auto const& e : extents){
400 if(e.empty())
401 BOOST_CHECK_EQUAL(e.valid(),false);
402 else
403 BOOST_CHECK_EQUAL(e.valid(), true );
404 }
405
406 BOOST_CHECK_EQUAL( extents_type{}.valid() , false );
407
408 BOOST_CHECK_THROW( ublas::basic_extents<unsigned>({0,1}), std::length_error );
409 BOOST_CHECK_THROW( ublas::basic_extents<unsigned>({1,0,1}), std::length_error );
410
411 }
412
413
414 BOOST_FIXTURE_TEST_CASE(test_extents_product, fixture, *boost::unit_test::label("extents") *boost::unit_test::label("product"))
415 {
416
417 auto e0 = extents[ 0].product(); // {}
418 auto e1 = extents[ 1].product(); // {1,1}
419 auto e2 = extents[ 2].product(); // {1,2}
420 auto e3 = extents[ 3].product(); // {2,1}
421 auto e4 = extents[ 4].product(); // {2,3}
422 auto e5 = extents[ 5].product(); // {2,3,1}
423 auto e6 = extents[ 6].product(); // {1,2,3}
424 auto e7 = extents[ 7].product(); // {1,1,2,3}
425 auto e8 = extents[ 8].product(); // {1,2,3,1,1}
426 auto e9 = extents[ 9].product(); // {4,2,3}
427 auto e10 = extents[10].product(); // {4,2,1,3}
428 auto e11 = extents[11].product(); // {4,2,1,3,1}
429 auto e12 = extents[12].product(); // {1,4,2,1,3,1}
430
431 BOOST_CHECK_EQUAL( e0 , 0 );
432 BOOST_CHECK_EQUAL( e1 , 1 );
433 BOOST_CHECK_EQUAL( e2 , 2 );
434 BOOST_CHECK_EQUAL( e3 , 2 );
435 BOOST_CHECK_EQUAL( e4 , 6 );
436 BOOST_CHECK_EQUAL( e5 , 6 );
437 BOOST_CHECK_EQUAL( e6 , 6 );
438 BOOST_CHECK_EQUAL( e7 , 6 );
439 BOOST_CHECK_EQUAL( e8 , 6 );
440 BOOST_CHECK_EQUAL( e9 , 24 );
441 BOOST_CHECK_EQUAL( e10, 24 );
442 BOOST_CHECK_EQUAL( e11, 24 );
443 BOOST_CHECK_EQUAL( e12, 24 );
444
445
446 }
447
448 BOOST_AUTO_TEST_SUITE_END()
449
450