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