Arcane  4.2.1.0
Developer documentation
Loading...
Searching...
No Matches
TestArrayView.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7#include <gtest/gtest.h>
8
9#include "arccore/base/Span.h"
10#include "arccore/base/Span2.h"
14#include "arccore/base/MDSpan.h"
15
16#include <vector>
17#include <type_traits>
18#include <algorithm>
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
23TEST(Array2View, Misc)
24{
25 using namespace Arccore;
26 int nb_x = 3;
27 int nb_y = 4;
28 std::vector<Int32> buf(nb_x * nb_y);
29 for (size_t i = 0, n = buf.size(); i < n; ++i)
30 buf[i] = (Int32)(i + 1);
31
32 ConstArray2View<Int32> v(buf.data(), nb_x, nb_y);
33 Integer global_index = 0;
34 for (Integer x = 0, xn = v.dim1Size(); x < xn; ++x) {
35 for (Integer y = 0, yn = v.dim2Size(); y < yn; ++y) {
36 ++global_index;
37 Int32 val1 = v[x][y];
38 Int32 val2 = v.item(x, y);
39 std::cout << " V=" << val1 << " x=" << x << " y=" << y;
40 ASSERT_TRUE(val1 == val2) << "Difference values v1=" << val1 << " v2=" << val2;
41 ASSERT_TRUE(val1 == global_index) << "Bad value v1=" << val1 << " expected=" << global_index;
42 ASSERT_EQ(v(x, y), val1);
43#ifdef ARCCORE_HAS_MULTI_SUBSCRIPT
44 bool is_ok = v[x, y] == val1;
45 ASSERT_TRUE(is_ok);
46#endif
47 }
48 }
49}
50
51/*---------------------------------------------------------------------------*/
52/*---------------------------------------------------------------------------*/
53
54TEST(Array3View, Misc)
55{
56 using namespace Arccore;
57 int nb_x = 3;
58 int nb_y = 4;
59 int nb_z = 5;
60 std::vector<Int32> buf(nb_x * nb_y * nb_z);
61 for (size_t i = 0, n = buf.size(); i < n; ++i)
62 buf[i] = (Int32)(i + 1);
63
64 ConstArray3View<Int32> v(buf.data(), nb_x, nb_y, nb_z);
65 Integer global_index = 0;
66 for (Integer x = 0, xn = v.dim1Size(); x < xn; ++x) {
67 for (Integer y = 0, yn = v.dim2Size(); y < yn; ++y) {
68 for (Integer z = 0, zn = v.dim3Size(); z < zn; ++z) {
69 ++global_index;
70 Int32 val1 = v[x][y][z];
71 Int32 val2 = v.item(x, y, z);
72 std::cout << " V=" << val1 << " x=" << x << " y=" << y << " z=" << z << '\n';
73 ASSERT_TRUE(val1 == val2) << "Difference values v1=" << val1 << " v2=" << val2;
74 ASSERT_TRUE(val1 == global_index) << "Bad value v1=" << val1 << " expected=" << global_index;
75 ASSERT_EQ(v(x, y, z), val1);
76#ifdef ARCCORE_HAS_MULTI_SUBSCRIPT
77 bool is_ok = v[x, y, z] == val1;
78 ASSERT_TRUE(is_ok);
79#endif
80 }
81 }
82 }
83}
84
85/*---------------------------------------------------------------------------*/
86/*---------------------------------------------------------------------------*/
87
88TEST(Array4View, Misc)
89{
90 using namespace Arccore;
91 int nb_x = 2;
92 int nb_y = 3;
93 int nb_z = 4;
94 int nb_a = 5;
95 std::vector<Int32> buf(nb_x * nb_y * nb_z * nb_a);
96 for (size_t i = 0, n = buf.size(); i < n; ++i)
97 buf[i] = (Int32)(i + 1);
98
99 ConstArray4View<Int32> v(buf.data(), nb_x, nb_y, nb_z, nb_a);
100 Integer global_index = 0;
101 for (Integer x = 0, xn = v.dim1Size(); x < xn; ++x) {
102 for (Integer y = 0, yn = v.dim2Size(); y < yn; ++y) {
103 for (Integer z = 0, zn = v.dim3Size(); z < zn; ++z) {
104 for (Integer a = 0, an = v.dim4Size(); a < an; ++a) {
105 ++global_index;
106 Int32 val1 = v[x][y][z][a];
107 Int32 val2 = v.item(x, y, z, a);
108 std::cout << " V=" << val1 << " x=" << x << " y=" << y << " z=" << z << " a=" << a << '\n';
109 ASSERT_TRUE(val1 == val2) << "Difference values v1=" << val1 << " v2=" << val2;
110 ASSERT_TRUE(val1 == global_index) << "Bad value v1=" << val1 << " expected=" << global_index;
111 ASSERT_EQ(v(x, y, z, a), val1);
112#ifdef ARCCORE_HAS_MULTI_SUBSCRIPT
113 bool is_ok = v[x, y, z, a] == val1;
114 ASSERT_TRUE(is_ok);
115#endif
116 }
117 }
118 }
119 }
120}
121
122/*---------------------------------------------------------------------------*/
123/*---------------------------------------------------------------------------*/
124
125namespace
126{
127template <typename T> void
128_testIterator(T values)
129{
130 {
131 auto r1 = std::make_reverse_iterator(values.end());
132 auto r2 = std::make_reverse_iterator(values.begin());
133 for (; r1 != r2; ++r1) {
134 std::cout << "RVALUE = " << *r1 << '\n';
135 }
136 }
137 {
138 auto r1 = values.rbegin();
139 ASSERT_EQ((*r1), 7);
140 ++r1;
141 ASSERT_EQ((*r1), 9);
142 ++r1;
143 ASSERT_EQ((*r1), 4);
144 ++r1;
145 ASSERT_TRUE((r1 == values.rend()));
146 }
147}
148
149} // namespace
150
151TEST(ArrayView, Iterator)
152{
153 using namespace Arccore;
154
155 std::vector<Arccore::Int32> vector_values = { 4, 9, 7 };
156 Integer vec_size = arccoreCheckArraySize(vector_values.size());
157
158 ArrayView<Int32> values1(vec_size, vector_values.data());
159 _testIterator(values1);
160
161 ConstArrayView<Int32> values2(vec_size, vector_values.data());
162 _testIterator(values2);
163
164 Span<Int32> values3(vector_values.data(), vector_values.size());
165 _testIterator(values3);
166
167 Span<const Int32> values4(vector_values.data(), vector_values.size());
168 _testIterator(values4);
169
170 // Make sure Array iterators are for type random_access_iterator
171 // Also, the function std::sort requires a random iterator so we can test
172 // it with our view.
173 static_assert(std::random_access_iterator<ArrayView<Int32>::iterator>);
174 static_assert(std::random_access_iterator<ArrayView<Int32>::const_iterator>);
175 std::sort(values1.begin(),values1.end());
176}
177
178/*---------------------------------------------------------------------------*/
179/*---------------------------------------------------------------------------*/
180
181TEST(Span, Convert)
182{
183 using namespace Arccore;
184 std::vector<Int64> vector_values = { 5, 7, 11 };
185 Int32 vector_size = static_cast<Int32>(vector_values.size());
186 ArrayView<Int64> a_view(vector_size, vector_values.data());
187 ASSERT_EQ(a_view.size(), vector_size) << "Bad a_view size";
188 ASSERT_EQ(a_view[0], vector_values[0]) << "Bad a_view[0]";
189 ASSERT_EQ(a_view[1], vector_values[1]) << "Bad a_view[1]";
190 ASSERT_EQ(a_view[2], vector_values[2]) << "Bad a_view[2]";
191
192 ConstArrayView<Int64> a_const_view(vector_size, vector_values.data());
193 ASSERT_EQ(a_const_view.size(), vector_size) << "Bad a_const_view size";
194 ASSERT_EQ(a_const_view[0], vector_values[0]) << "Bad a_const_view[0]";
195 ASSERT_EQ(a_const_view[1], vector_values[1]) << "Bad a_const_view[1]";
196 ASSERT_EQ(a_const_view[2], vector_values[2]) << "Bad a_const_view[2]";
197
198 Span<const Int64> a_const_span(vector_values.data(), vector_values.size());
199 Span<Int64> a_span(vector_values.data(), vector_values.size());
200 ByteConstSpan a_const_bytes = asBytes(a_const_span);
201 ByteSpan a_bytes = asWritableBytes(a_span);
202 ByteConstSpan a_const_bytes2 = asBytes(a_span);
203 //ByteSpan a_bytes2 = asWritableBytes(a_const_span);
204 ASSERT_EQ(a_const_bytes.size(), 24) << "Bad a_const_bytes_size (1)";
205 ASSERT_EQ(a_const_bytes2.size(), 24) << "Bad a_const_bytes2_size (1)";
206 ASSERT_EQ(a_bytes.size(), 24) << "Bad a_bytes_size (2)";
207 Span<Int64> span2(a_view);
208 Span<const Int64> span3(a_view);
209 Span<const Int64> span4(a_const_view);
210 ASSERT_EQ(span2.size(), a_view.size()) << "Bad span2 size";
211 ASSERT_EQ(span3.size(), a_view.size()) << "Bad span3 size";
212 ASSERT_EQ(span4.size(), a_const_view.size()) << "Bad span4 size";
213 span3 = a_const_view;
214 span3 = a_view;
215 span2 = a_view;
216 ASSERT_EQ(span2.size(), a_view.size()) << "Bad span2 (2) size";
217 ASSERT_EQ(span3.size(), a_view.size()) << "Bad span3 (2) size";
218 std::cout << "View=" << a_view << '\n';
219 std::cout << "ConstView=" << a_const_view << '\n';
220 std::cout << "Span3=" << span3 << '\n';
221}
222
223/*---------------------------------------------------------------------------*/
224/*---------------------------------------------------------------------------*/
225
226// Checks that a1 and a2 are identical
227template <typename A1, typename A2>
228void _checkSame(A1& a1, A2& a2, const char* message)
229{
230 using namespace Arccore;
231 using size_type = typename A1::size_type;
232 Int64 s1 = a1.size();
233 Int64 s2 = a2.size();
234 ASSERT_EQ(s1, s2) << "Bad size " << message;
235 for (size_type i = 0, n = a1.size(); i < n; ++i)
236 ASSERT_EQ(a1[i], a2[i]) << "Bad value[" << i << "]" << message;
237}
238
239/*---------------------------------------------------------------------------*/
240/*---------------------------------------------------------------------------*/
241
242// Checks that a1 and a2 are identical
243template <typename A1, typename A2>
244void _checkSame2(A1& a1, A2& a2, const char* message)
245{
246 using namespace Arccore;
247 using size_type = typename A1::size_type;
248 const Int64 s1_dim1 = a1.dim1Size();
249 const Int64 s2_dim1 = a2.dim1Size();
250 ASSERT_EQ(s1_dim1, s2_dim1) << "Bad size " << message;
251 const Int64 s1_dim2 = a1.dim2Size();
252 const Int64 s2_dim2 = a2.dim2Size();
253 ASSERT_EQ(s1_dim2, s2_dim2) << "Bad size " << message;
254 for (size_type i = 0; i < s1_dim1; ++i)
255 for (size_type j = 0; j < s1_dim2; ++j)
256 ASSERT_EQ(a1[i][j], a2[i][j]) << "Bad value[" << i << ',' << j << "]" << message;
257}
258
259/*---------------------------------------------------------------------------*/
260/*---------------------------------------------------------------------------*/
261
262TEST(ArrayView, StdArray)
263{
264 using namespace Arccore;
265 std::array<Int64, 0> v0;
266 std::array<Int64, 2> v1{ 5, 7 };
267 std::array<Int64, 3> v2{ 2, 4, -2 };
268 std::array<const Int64, 4> v3{ 9, 13, 32, 27 };
269
270 {
271 ArrayView<Int64> view0{ v0 };
272 _checkSame(view0, v0, "view0==v0");
273
274 ArrayView<Int64> view1{ v1 };
275 _checkSame(view1, v1, "view1==v1");
276
277 view0 = v2;
278 _checkSame(view0, v2, "view0==v2");
279 }
280
281 {
282 ConstArrayView<Int64> view0{ v0 };
283 _checkSame(view0, v0, "const view0==v0");
284
285 ConstArrayView<Int64> view1{ v1 };
286 _checkSame(view1, v1, "const view1==v1");
287
288 view0 = v2;
289 _checkSame(view0, v2, "const view0==v2");
290
291 ConstArrayView<Int64> view2{ v3 };
292 _checkSame(view2, v3, "const view2==v3");
293
294 view1 = v3;
295 _checkSame(view1, v3, "const view1==v3");
296 }
297}
298
299/*---------------------------------------------------------------------------*/
300/*---------------------------------------------------------------------------*/
301
302template <typename SpanType, typename ConstSpanType> void
303_testSpanStdArray()
304{
305 using namespace Arccore;
306 std::array<Int64, 0> v0;
307 std::array<Int64, 2> v1{ 5, 7 };
308 std::array<Int64, 3> v2{ 2, 4, -2 };
309 std::array<const Int64, 4> v3{ 9, 13, 32, 27 };
310
311 {
312 SpanType span0{ v0 };
313 _checkSame(span0, v0, "span0==v0");
314
315 SpanType span1{ v1 };
316 _checkSame(span1, v1, "span1==v1");
317
318 SpanType span2{ v1 };
319 ASSERT_TRUE(span1 == span2);
320 ASSERT_FALSE(span1 != span2);
321
322 SpanType const_span2{ v1 };
323 ASSERT_TRUE(span1 == const_span2);
324 ASSERT_FALSE(span1 != const_span2);
325
326 span0 = v2;
327 _checkSame(span0, v2, "span0==v2");
328 }
329
330 {
331 ConstSpanType span0{ v0 };
332 _checkSame(span0, v0, "const span0==v0");
333
334 ConstSpanType span1{ v1 };
335 _checkSame(span1, v1, "const span1==v1");
336
337 span0 = v2;
338 _checkSame(span0, v2, "const span0==v2");
339
340 ConstSpanType span2{ v3 };
341 _checkSame(span2, v3, "const span2==v3");
342
343 ConstSpanType span3{ v3 };
344 ASSERT_TRUE(span2 == span3);
345 ASSERT_FALSE(span2 != span3);
346
347 span1 = v3;
348 _checkSame(span1, v3, "const span1==v3");
349 }
350 {
351 SpanType span1{ v1 };
352 ConstSpanType const_span1{ v1 };
353 ASSERT_TRUE(span1 == const_span1);
354
355 SpanType span2{ v2 };
356 ConstSpanType const_span3{ v3 };
357 ASSERT_TRUE(span2 != const_span3);
358 }
359}
360
361/*---------------------------------------------------------------------------*/
362/*---------------------------------------------------------------------------*/
363
364TEST(Span, StdArray)
365{
366 using namespace Arccore;
367 _testSpanStdArray<Span<Int64>, Span<const Int64>>();
368
369 std::array<Int64, 0> v0;
370 std::array<Int64, 2> v1{ 5, 7 };
371 std::array<Int64, 3> v2{ 2, 4, -2 };
372 auto s0 = asSpan(v0);
373 _checkSame(s0, v0, "s0==v0");
374 auto s1 = asSpan(v1);
375 _checkSame(s1, v1, "s1==v1");
376 auto s2 = asSpan(v2);
377 _checkSame(s2, v2, "s2==v2");
378
379 auto sp0 = asSmallSpan(v0);
380 _checkSame(sp0, v0, "s0==v0");
381 auto sp1 = asSmallSpan(v1);
382 _checkSame(sp1, v1, "s1==v1");
383 auto sp2 = asSmallSpan(v2);
384 _checkSame(sp2, v2, "s2==v2");
385
386 {
387 Span<Int64> ss0(s0);
388 auto bytes = asBytes(ss0);
389 auto x = asSpan<Int64>(bytes);
390 ASSERT_EQ(x.data(), nullptr);
391 ASSERT_EQ(x.size(), 0);
392 }
393 {
394 SmallSpan<Int64> ssp0(sp0);
395 auto bytes = asBytes(ssp0);
396 auto x = asSmallSpan<Int64>(bytes);
397 ASSERT_EQ(x.data(), nullptr);
398 ASSERT_EQ(x.size(), 0);
399 }
400
401 {
402 auto bytes = asWritableBytes(s1);
403 auto x = asSpan<Int64>(bytes);
404 _checkSame(x, v1, "x==v1");
405 }
406
407 {
408 auto bytes = asWritableBytes(s2);
409 auto x = asSpan<Int64>(bytes);
410 _checkSame(x, v2, "x==v2");
411 x[2] = 5;
412 ASSERT_EQ(v2[2], 5);
413 }
414
415 {
416 auto bytes = asWritableBytes(sp2);
417 auto x = asSmallSpan<Int64>(bytes);
418 _checkSame(x, v2, "x==v2");
419 x[2] = 5;
420 ASSERT_EQ(v2[2], 5);
421 }
422
423 {
424 std::array<Int64, 2> v1{ 5, 7 };
425 std::array<Int64, 3> v2{ 2, 4, -2 };
426 Span<Int64, 2> fixed_s1(v1);
427 Span<Int64, 3> fixed_s2(v2);
428 ASSERT_FALSE(fixed_s1 == fixed_s2);
429 ASSERT_TRUE(fixed_s1 != fixed_s2);
430
431 LargeSpan<const Int64> s1_a(s1);
434 ASSERT_FALSE(fb1 == fb2);
435
436 std::array<Real, 3> v2r{ 2.0, 4.1, -2.3 };
437 SmallSpan<const Real> small2(v2r);
438 LargeSpan<const std::byte> small_fb2(asBytes(small2));
439
440 //Span<Int64,DynExtent,-1> a4(v2.data()+1,v2.size()-1);
441 //ASSERT_EQ(a4[-1],2);
442 }
443}
444
445/*---------------------------------------------------------------------------*/
446/*---------------------------------------------------------------------------*/
447
448TEST(SmallSpan, StdArray)
449{
450 using namespace Arccore;
451 _testSpanStdArray<SmallSpan<Int64>, SmallSpan<const Int64>>();
452}
453
454/*---------------------------------------------------------------------------*/
455/*---------------------------------------------------------------------------*/
456
457template <typename SpanType, typename ConstSpanType> void
458_testSpan2StdArray()
459{
460 using namespace Arccore;
461
462 std::array<Int64, 6> v1{ 5, 7, 9, 32, -5, -6 };
463 std::array<Int64, 5> v2{ 1, 9, 32, 41, -5 };
464 std::array<const Int64, 12> v3{ 12, 33, 47, 55, 36, 13, 9, 7, 5, 1, 45, 38 };
465
466 SpanType s0{};
467 SpanType s1{ v1.data(), 3, 2 };
468 SpanType s2{ v2.data(), 1, 5 };
469 ConstSpanType s3{ v3.data(), 4, 3 };
470
471 ASSERT_EQ(s1[2][1], s1(2, 1));
472
473#ifdef ARCCORE_HAS_MULTI_SUBSCRIPT
474 {
475 bool is_ok = s1[2, 1] == s1(2, 1);
476 ASSERT_TRUE(is_ok);
477 }
478#endif
479
480 {
481 SpanType span0{ s0 };
482 _checkSame2(span0, s0, "span0==s0");
483
484 SpanType span1{ s1 };
485 _checkSame2(span1, s1, "span1==s1");
486
487 SpanType span2{ s1 };
488 ASSERT_TRUE(span1 == span2);
489 ASSERT_FALSE(span1 != span2);
490
491 SpanType const_span2{ s1 };
492 ASSERT_TRUE(span1 == const_span2);
493 ASSERT_FALSE(span1 != const_span2);
494 }
495
496 {
497 ConstSpanType span0{ s0 };
498 _checkSame2(span0, s0, "const span0==s0");
499
500 ConstSpanType span1{ s1 };
501 _checkSame2(span1, s1, "const span1==s1");
502
503 span0 = s2;
504 _checkSame2(span0, s2, "const span0==s2");
505
506 ConstSpanType span2{ s3 };
507 _checkSame2(span2, s3, "const span2==s3");
508
509 ConstSpanType span3{ s3 };
510 ASSERT_TRUE(span2 == span3);
511 ASSERT_FALSE(span2 != span3);
512
513 span1 = s3;
514 _checkSame2(span1, s3, "const span1==s3");
515 }
516}
517
518/*---------------------------------------------------------------------------*/
519/*---------------------------------------------------------------------------*/
520
521TEST(Span2, StdArray)
522{
523 using namespace Arccore;
524 _testSpan2StdArray<Span2<Int64>, Span2<const Int64>>();
525}
526
527/*---------------------------------------------------------------------------*/
528/*---------------------------------------------------------------------------*/
529
530TEST(SmallSpan2, StdArray)
531{
532 using namespace Arccore;
533 _testSpan2StdArray<SmallSpan2<Int64>, SmallSpan2<const Int64>>();
534}
535
536/*---------------------------------------------------------------------------*/
537/*---------------------------------------------------------------------------*/
538
539template <typename ViewType> void
540_testSubViewInterval()
541{
542 using namespace Arccore;
543
544 std::array<Int64, 12> vlist{ 9, 13, 32, 27, 43, -5, 2, -7, 8, 11, 25, 48 };
545 ViewType view{ vlist };
546
547 {
548 ViewType null_view;
549 ViewType sub_view0{ view.subViewInterval(1, 0) };
550 ViewType sub_view1{ view.subViewInterval(-1, 2) };
551 ViewType sub_view2{ view.subViewInterval(2, 2) };
552 ViewType sub_view3{ view.subViewInterval(0, 0) };
553 std::cout << "SUBVIEW0=" << sub_view1 << '\n';
554 _checkSame(sub_view0, null_view, "sub_view0_null");
555 _checkSame(sub_view1, null_view, "sub_view1_null");
556 _checkSame(sub_view2, null_view, "sub_view2_null");
557 _checkSame(sub_view3, null_view, "sub_view3_null");
558 }
559
560 {
561 std::array<Int64, 4> vlist0{ 9, 13, 32, 27 };
562 std::array<Int64, 4> vlist1{ 43, -5, 2, -7 };
563 std::array<Int64, 4> vlist2{ 8, 11, 25, 48 };
564
565 ViewType sub_view0{ view.subViewInterval(0, 3) };
566 ViewType sub_view1{ view.subViewInterval(1, 3) };
567 ViewType sub_view2{ view.subViewInterval(2, 3) };
568 std::cout << "SUBVIEW1=" << sub_view1 << '\n';
569 _checkSame(sub_view0, vlist0, "sub_view0");
570 _checkSame(sub_view1, vlist1, "sub_view1");
571 _checkSame(sub_view2, vlist2, "sub_view2");
572 }
573
574 {
575 std::array<Int64, 2> vlist0{ 9, 13 };
576 std::array<Int64, 2> vlist1{ 32, 27 };
577 std::array<Int64, 2> vlist2{ 43, -5 };
578 std::array<Int64, 2> vlist3{ 2, -7 };
579 std::array<Int64, 4> vlist4{ 8, 11, 25, 48 };
580
581 ViewType sub_view0{ view.subViewInterval(0, 5) };
582 ViewType sub_view1{ view.subViewInterval(1, 5) };
583 ViewType sub_view2{ view.subViewInterval(2, 5) };
584 ViewType sub_view3{ view.subViewInterval(3, 5) };
585 ViewType sub_view4{ view.subViewInterval(4, 5) };
586 std::cout << "SUBVIEW2=" << sub_view1 << '\n';
587 _checkSame(sub_view0, vlist0, "sub_view0");
588 _checkSame(sub_view1, vlist1, "sub_view1");
589 _checkSame(sub_view2, vlist2, "sub_view2");
590 _checkSame(sub_view3, vlist3, "sub_view3");
591 _checkSame(sub_view4, vlist4, "sub_view4");
592 }
593}
594
595/*---------------------------------------------------------------------------*/
596/*---------------------------------------------------------------------------*/
597
598template <typename ViewType> void
599_testSubSpanInterval()
600{
601 using namespace Arccore;
602
603 std::array<Int64, 12> vlist{ 9, 13, 32, 27, 43, -5, 2, -7, 8, 11, 25, 48 };
604 ViewType view{ vlist };
605
606 {
607 ViewType null_view;
608 ViewType sub_view0{ view.subSpanInterval(1, 0) };
609 ViewType sub_view1{ view.subSpanInterval(-1, 2) };
610 ViewType sub_view2{ view.subSpanInterval(2, 2) };
611 ViewType sub_view3{ view.subSpanInterval(0, 0) };
612 std::cout << "SUBVIEW0=" << sub_view1 << '\n';
613 _checkSame(sub_view0, null_view, "sub_view0_null");
614 _checkSame(sub_view1, null_view, "sub_view1_null");
615 _checkSame(sub_view2, null_view, "sub_view2_null");
616 _checkSame(sub_view3, null_view, "sub_view3_null");
617 }
618
619 {
620 std::array<Int64, 4> vlist0{ 9, 13, 32, 27 };
621 std::array<Int64, 4> vlist1{ 43, -5, 2, -7 };
622 std::array<Int64, 4> vlist2{ 8, 11, 25, 48 };
623
624 ViewType sub_view0{ view.subSpanInterval(0, 3) };
625 ViewType sub_view1{ view.subSpanInterval(1, 3) };
626 ViewType sub_view2{ view.subSpanInterval(2, 3) };
627 std::cout << "SUBVIEW1=" << sub_view1 << '\n';
628 _checkSame(sub_view0, vlist0, "sub_view0");
629 _checkSame(sub_view1, vlist1, "sub_view1");
630 _checkSame(sub_view2, vlist2, "sub_view2");
631 }
632
633 {
634 std::array<Int64, 2> vlist0{ 9, 13 };
635 std::array<Int64, 2> vlist1{ 32, 27 };
636 std::array<Int64, 2> vlist2{ 43, -5 };
637 std::array<Int64, 2> vlist3{ 2, -7 };
638 std::array<Int64, 4> vlist4{ 8, 11, 25, 48 };
639
640 ViewType sub_view0{ view.subSpanInterval(0, 5) };
641 ViewType sub_view1{ view.subSpanInterval(1, 5) };
642 ViewType sub_view2{ view.subSpanInterval(2, 5) };
643 ViewType sub_view3{ view.subSpanInterval(3, 5) };
644 ViewType sub_view4{ view.subSpanInterval(4, 5) };
645 std::cout << "SUBVIEW2=" << sub_view1 << '\n';
646 _checkSame(sub_view0, vlist0, "sub_view0");
647 _checkSame(sub_view1, vlist1, "sub_view1");
648 _checkSame(sub_view2, vlist2, "sub_view2");
649 _checkSame(sub_view3, vlist3, "sub_view3");
650 _checkSame(sub_view4, vlist4, "sub_view4");
651 }
652}
653
654/*---------------------------------------------------------------------------*/
655/*---------------------------------------------------------------------------*/
656
657template <typename ViewType> void
658_testSubPartInterval()
659{
660 using namespace Arccore;
661
662 std::array<Int64, 12> vlist{ 9, 13, 32, 27, 43, -5, 2, -7, 8, 11, 25, 48 };
663 ViewType view{ vlist };
664
665 {
666 ViewType null_view;
667 ViewType sub_view0{ view.subPartInterval(1, 0) };
668 ViewType sub_view1{ view.subPartInterval(-1, 2) };
669 ViewType sub_view2{ view.subPartInterval(2, 2) };
670 ViewType sub_view3{ view.subPartInterval(0, 0) };
671 std::cout << "SUBVIEW0=" << sub_view1 << '\n';
672 _checkSame(sub_view0, null_view, "sub_view0_null");
673 _checkSame(sub_view1, null_view, "sub_view1_null");
674 _checkSame(sub_view2, null_view, "sub_view2_null");
675 _checkSame(sub_view3, null_view, "sub_view3_null");
676 }
677
678 {
679 std::array<Int64, 4> vlist0{ 9, 13, 32, 27 };
680 std::array<Int64, 4> vlist1{ 43, -5, 2, -7 };
681 std::array<Int64, 4> vlist2{ 8, 11, 25, 48 };
682
683 ViewType sub_view0{ view.subPartInterval(0, 3) };
684 ViewType sub_view1{ view.subPartInterval(1, 3) };
685 ViewType sub_view2{ view.subPartInterval(2, 3) };
686 std::cout << "SUBVIEW1=" << sub_view1 << '\n';
687 _checkSame(sub_view0, vlist0, "sub_view0");
688 _checkSame(sub_view1, vlist1, "sub_view1");
689 _checkSame(sub_view2, vlist2, "sub_view2");
690 }
691
692 {
693 std::array<Int64, 2> vlist0{ 9, 13 };
694 std::array<Int64, 2> vlist1{ 32, 27 };
695 std::array<Int64, 2> vlist2{ 43, -5 };
696 std::array<Int64, 2> vlist3{ 2, -7 };
697 std::array<Int64, 4> vlist4{ 8, 11, 25, 48 };
698
699 ViewType sub_view0{ view.subPartInterval(0, 5) };
700 ViewType sub_view1{ view.subPartInterval(1, 5) };
701 ViewType sub_view2{ view.subPartInterval(2, 5) };
702 ViewType sub_view3{ view.subPartInterval(3, 5) };
703 ViewType sub_view4{ view.subPartInterval(4, 5) };
704 std::cout << "SUBVIEW2=" << sub_view1 << '\n';
705 _checkSame(sub_view0, vlist0, "sub_view0");
706 _checkSame(sub_view1, vlist1, "sub_view1");
707 _checkSame(sub_view2, vlist2, "sub_view2");
708 _checkSame(sub_view3, vlist3, "sub_view3");
709 _checkSame(sub_view4, vlist4, "sub_view4");
710 }
711}
712
713/*---------------------------------------------------------------------------*/
714/*---------------------------------------------------------------------------*/
715
716TEST(ArrayView, SubViewInterval)
717{
718 using namespace Arccore;
719 _testSubViewInterval<ArrayView<Int64>>();
720 _testSubViewInterval<ConstArrayView<Int64>>();
721 _testSubSpanInterval<Span<Int64>>();
722 _testSubSpanInterval<SmallSpan<Int64>>();
723
724 _testSubPartInterval<ArrayView<Int64>>();
725 _testSubPartInterval<ConstArrayView<Int64>>();
726 _testSubPartInterval<Span<Int64>>();
727 _testSubPartInterval<SmallSpan<Int64>>();
728}
729
730TEST(ArrayView, Copyable)
731{
732 using namespace Arccore;
733 ASSERT_TRUE(std::is_trivially_copyable_v<ArrayView<int>>);
734 ASSERT_TRUE(std::is_trivially_copyable_v<ConstArrayView<int>>);
735
736 ASSERT_TRUE(std::is_trivially_copyable_v<Array2View<int>>);
737 ASSERT_TRUE(std::is_trivially_copyable_v<ConstArray2View<int>>);
738
739 ASSERT_TRUE(std::is_trivially_copyable_v<Array3View<int>>);
740 ASSERT_TRUE(std::is_trivially_copyable_v<ConstArray3View<int>>);
741
742 ASSERT_TRUE(std::is_trivially_copyable_v<Array4View<int>>);
743 ASSERT_TRUE(std::is_trivially_copyable_v<ConstArray4View<int>>);
744
745 ASSERT_TRUE(std::is_trivially_copyable_v<Span<int>>);
746 ASSERT_TRUE(std::is_trivially_copyable_v<Span<const int>>);
747
748 ASSERT_TRUE(std::is_trivially_copyable_v<Span2<int>>);
749 ASSERT_TRUE(std::is_trivially_copyable_v<Span2<const int>>);
750}
751
752TEST(Span, FixedValue)
753{
754 using namespace Arccore;
755 std::cout << "sizeof(Span<Int32,1>) = " << sizeof(Span<Int32, 1>) << "\n";
756 std::cout << "sizeof(Span<Int32,DynExtent>) = " << sizeof(Span<Int32, DynExtent>) << "\n";
757 std::cout << "sizeof(Span<Int64,1>) = " << sizeof(Span<Int64, 1>) << "\n";
758 std::cout << "sizeof(Span<Int64,DynExtent>) = " << sizeof(Span<Int64, DynExtent>) << "\n";
759
760 // Verifies that [[no_unique_address]] is properly taken into account
761 // As far as I know, this is not the case with VS2022.
762 ASSERT_EQ(sizeof(Span<Int32, 1>), sizeof(void*));
763 ASSERT_EQ(sizeof(Span<Int64, 1>), sizeof(void*));
764
765 std::array<Int64, 12> vlist{ 9, 13, 32, 27, 43, -5, 2, -7, 8, 11, 25, 48 };
766 Span<Int64, 12> fixed_list(vlist);
767 Int32 sub_view_size = 5;
768 Span<Int64> sub_view1 = fixed_list.subspan(0, sub_view_size);
769 ASSERT_EQ(sub_view1.size(), sub_view_size);
770
771 Span<Int64, 12> fixed_span_only_ptr(vlist.data());
772 ASSERT_EQ(fixed_span_only_ptr.data(), vlist.data());
773 Span<const Int64, 12> fixed_span_only_const_ptr(vlist.data());
774 ASSERT_EQ(fixed_span_only_const_ptr.data(), vlist.data());
775}
776
777TEST(MDSpan, Misc)
778{
779 using namespace Arcane;
780 Int32 v1[4] = { 1, 3, -5, 2 };
781
782 MDSpan<Int32, MDDim1> mdspan1;
783 ASSERT_EQ(mdspan1.extent0(),0);
784
785 mdspan1 = MDSpan<Int32,MDDim1>(v1,4);
786 ASSERT_EQ(mdspan1.extent0(),4);
787 ASSERT_EQ(mdspan1.data(),v1);
788 ASSERT_EQ(mdspan1[0],1);
789 ASSERT_EQ(mdspan1[1],3);
790 ASSERT_EQ(mdspan1[2],-5);
791 ASSERT_EQ(mdspan1[3],2);
792
793 MDSpan<const Int32, MDDim1> const_mdspan1(mdspan1);
794 ASSERT_EQ(const_mdspan1.data(), mdspan1.data());
795 ASSERT_EQ(const_mdspan1.extent0(), mdspan1.extent0());
796
797 // Convert from ArrayView
798 {
799 ArrayView arrayview1(4,v1);
800
801 MDSpan<Int32, MDDim1> mdspan2(arrayview1);
802 ASSERT_EQ(mdspan2.data(), mdspan1.data());
803 ASSERT_EQ(mdspan2.extent0(), mdspan1.extent0());
804
805 MDSpan<const Int32, MDDim1> const_mdspan2(arrayview1);
806 ASSERT_EQ(const_mdspan2.data(), mdspan1.data());
807 ASSERT_EQ(const_mdspan2.extent0(), mdspan1.extent0());
808
809 mdspan2 = arrayview1;
810 ASSERT_EQ(mdspan2.data(), mdspan1.data());
811 ASSERT_EQ(mdspan2.extent0(), mdspan1.extent0());
812
813 const_mdspan2 = arrayview1;
814 ASSERT_EQ(const_mdspan2.data(), mdspan1.data());
815 ASSERT_EQ(const_mdspan2.extent0(), mdspan1.extent0());
816 }
817
818 // Convert from ConstArrayView
819 {
820 ConstArrayView const_arrayview1(4,v1);
821
822 MDSpan<const Int32, MDDim1> const_mdspan2(const_arrayview1);
823 ASSERT_EQ(const_mdspan2.data(), mdspan1.data());
824 ASSERT_EQ(const_mdspan2.extent0(), mdspan1.extent0());
825
826 const_mdspan2 = const_arrayview1;
827 ASSERT_EQ(const_mdspan2.data(), mdspan1.data());
828 ASSERT_EQ(const_mdspan2.extent0(), mdspan1.extent0());
829 }
830}
831
832/*---------------------------------------------------------------------------*/
833/*---------------------------------------------------------------------------*/
834
835namespace Arcane
836{
837template class ArrayView<Int32>;
838template class ConstArrayView<Int32>;
839template class ArrayView<double>;
840template class ConstArrayView<double>;
841
842template class Span<Int32>;
843template class Span<const Int32>;
844template class Span<double>;
845template class Span<const double>;
846
847template class SmallSpan<Int32>;
848template class SmallSpan<const Int32>;
849template class SmallSpan<double>;
850template class SmallSpan<const double>;
851
852template class Span<Int32, 4>;
853template class Span<const Int32, 5>;
854template class Span<double, 6>;
855template class Span<const double, 7>;
856
857template class SmallSpan<Int32, 4>;
858template class SmallSpan<const Int32, 5>;
859template class SmallSpan<double, 6>;
860template class SmallSpan<const double, 7>;
861
862template class Span2<Int32>;
863template class Span2<const Int32>;
864template class Span2<double>;
865template class Span2<const double>;
866
867template class SmallSpan2<Int32>;
868template class SmallSpan2<const Int32>;
869template class SmallSpan2<double>;
870template class SmallSpan2<const double>;
871
872template class MDSpan<Int32,MDDim1>;
873template class MDSpan<Int32,MDDim2>;
874template class MDSpan<Int32,MDDim3>;
875template class MDSpan<Int32,MDDim4>;
876} // namespace Arcane
877
878/*---------------------------------------------------------------------------*/
879/*---------------------------------------------------------------------------*/
#define ASSERT_FALSE(condition)
Checks that condition is false.
Definition Assertion.h:140
#define ASSERT_TRUE(condition)
Checks that condition is true.
Definition Assertion.h:128
Types and functions associated with the classes Span2Impl, Small2Span and Span2.
Types and functions associated with the classes SpanImpl, SmallSpan and Span.
Types and functions associated with the classes Array3View and ConstArray3View.
Types and functions associated with the classes Array4View and ConstArray4View.
Types and functions associated with the classes ArrayView and ConstArrayView.
Types and functions associated with the MDSpan class.
Modifiable view of an array of type T.
Constant view of an array of type T.
Base class for multidimensional views.
constexpr DataType * data() noexcept
Base pointer for allocated memory.
constexpr ExtentIndexType extent0() const
Value of the first dimension.
View for a 2D array whose size is an 'Int32'.
Definition Span2.h:245
View of an array of elements of type T.
Definition Span.h:803
View for a 2D array whose size is an 'Int64'.
Definition Span2.h:320
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:325
View of an array of elements of type T.
Definition Span.h:633
constexpr __host__ __device__ Span< T, DynExtent > subspan(Int64 abegin, Int64 asize) const
Sub-view starting from element abegin and containing asize elements.
Definition Span.h:722
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
Namespace of Arccore.
constexpr __host__ __device__ Integer arccoreCheckArraySize(unsigned long long size)
Checks that size can be converted into an 'Integer' to serve as an array size. If possible,...
SmallSpan< DataType > asSmallSpan(SmallSpan< std::byte, Extent > bytes)
Converts a SmallSpan<std::byte> into a SmallSpan<DataType>.
Definition Span.h:1142
Span< DataType > asSpan(Span< std::byte, Extent > bytes)
Converts a Span<std::byte> into a Span<DataType>.
Definition Span.h:1122
Impl::SpanTypeFromSize< conststd::byte, SizeType >::SpanType asBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converts the view into an array of non-modifiable bytes.
Definition Span.h:1030
Impl::SpanTypeFromSize< std::byte, SizeType >::SpanType asWritableBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converts the view into an array of modifiable bytes.
Definition Span.h:1066