Arcane  4.2.1.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
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) << "Différence des valeurs v1=" << val1 << " v2=" << val2;
49 ASSERT_TRUE(val1 == global_index) << "Mauvaise valeur v1=" << val1 << " attendue=" << 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) << "Différence des valeurs v1=" << val1 << " v2=" << val2;
82 ASSERT_TRUE(val1 == global_index) << "Mauvaise valeur v1=" << val1 << " attendue=" << 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) << "Différence des valeurs v1=" << val1 << " v2=" << val2;
118 ASSERT_TRUE(val1 == global_index) << "Mauvaise valeur v1=" << val1 << " attendue=" << 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 // S'assurer que les itérateurs Array sont de type random_access_iterator
179 // De plus, la fonction std::sort nécessite un itérateur aléatoire afin que nous puissions
180 // la tester avec notre vue.
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) << "Taille incorrecte de a_view";
196 ASSERT_EQ(a_view[0], vector_values[0]) << "a_view[0] incorrect";
197 ASSERT_EQ(a_view[1], vector_values[1]) << "a_view[1] incorrect";
198 ASSERT_EQ(a_view[2], vector_values[2]) << "a_view[2] incorrect";
199
200 ConstArrayView<Int64> a_const_view(vector_size, vector_values.data());
201 ASSERT_EQ(a_const_view.size(), vector_size) << "Taille incorrecte de a_const_view";
202 ASSERT_EQ(a_const_view[0], vector_values[0]) << "a_const_view[0] incorrect";
203 ASSERT_EQ(a_const_view[1], vector_values[1]) << "a_const_view[1] incorrect";
204 ASSERT_EQ(a_const_view[2], vector_values[2]) << "a_const_view[2] incorrect";
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) << "Taille incorrecte de a_const_bytes (1)";
213 ASSERT_EQ(a_const_bytes2.size(), 24) << "Taille incorrecte de a_const_bytes2 (1)";
214 ASSERT_EQ(a_bytes.size(), 24) << "Taille incorrecte de a_bytes (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()) << "Taille incorrecte de span2";
219 ASSERT_EQ(span3.size(), a_view.size()) << "Taille incorrecte de span3";
220 ASSERT_EQ(span4.size(), a_const_view.size()) << "Taille incorrecte de span4";
221 span3 = a_const_view;
222 span3 = a_view;
223 span2 = a_view;
224 ASSERT_EQ(span2.size(), a_view.size()) << "Taille incorrecte de span2 (2)";
225 ASSERT_EQ(span3.size(), a_view.size()) << "Taille incorrecte de span3 (2)";
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// Vérifie que \a a1 et \a a2 sont identiques
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) << "Taille incorrecte " << message;
243 for (size_type i = 0, n = a1.size(); i < n; ++i)
244 ASSERT_EQ(a1[i], a2[i]) << "Valeur incorrecte[" << i << "]" << message;
245}
246
247/*---------------------------------------------------------------------------*/
248/*---------------------------------------------------------------------------*/
249
250// Vérifie que \a a1 et \a a2 sont identiques
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) << "Taille incorrecte " << message;
259 const Int64 s1_dim2 = a1.dim2Size();
260 const Int64 s2_dim2 = a2.dim2Size();
261 ASSERT_EQ(s1_dim2, s2_dim2) << "Taille incorrecte " << 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]) << "Valeur incorrecte[" << 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 std::array<Real, 3> v2r{ 2.0, 4.1, -2.3 };
444 SmallSpan<const Real> small2(v2r);
445 LargeSpan<const std::byte> small_fb2(asBytes(small2));
446
447 //Span<Int64,DynExtent,-1> a4(v2.data()+1,v2.size()-1);
448 //ASSERT_EQ(a4[-1],2);
449 }
450}
451
452/*---------------------------------------------------------------------------*/
453/*---------------------------------------------------------------------------*/
454
455TEST(SmallSpan, StdArray)
456{
457 using namespace Arccore;
458 _testSpanStdArray<SmallSpan<Int64>, SmallSpan<const Int64>>();
459}
460
461/*---------------------------------------------------------------------------*/
462/*---------------------------------------------------------------------------*/
463
464template <typename SpanType, typename ConstSpanType> void
465_testSpan2StdArray()
466{
467 using namespace Arccore;
468
469 std::array<Int64, 6> v1{ 5, 7, 9, 32, -5, -6 };
470 std::array<Int64, 5> v2{ 1, 9, 32, 41, -5 };
471 std::array<const Int64, 12> v3{ 12, 33, 47, 55, 36, 13, 9, 7, 5, 1, 45, 38 };
472
473 SpanType s0{};
474 SpanType s1{ v1.data(), 3, 2 };
475 SpanType s2{ v2.data(), 1, 5 };
476 ConstSpanType s3{ v3.data(), 4, 3 };
477
478 ASSERT_EQ(s1[2][1], s1(2, 1));
479
480#ifdef ARCCORE_HAS_MULTI_SUBSCRIPT
481 {
482 bool is_ok = s1[2, 1] == s1(2, 1);
483 ASSERT_TRUE(is_ok);
484 }
485#endif
486
487 {
488 SpanType span0{ s0 };
489 _checkSame2(span0, s0, "span0==s0");
490
491 SpanType span1{ s1 };
492 _checkSame2(span1, s1, "span1==s1");
493
494 SpanType span2{ s1 };
495 ASSERT_TRUE(span1 == span2);
496 ASSERT_FALSE(span1 != span2);
497
498 SpanType const_span2{ s1 };
499 ASSERT_TRUE(span1 == const_span2);
500 ASSERT_FALSE(span1 != const_span2);
501 }
502
503 {
504 ConstSpanType span0{ s0 };
505 _checkSame2(span0, s0, "const span0==s0");
506
507 ConstSpanType span1{ s1 };
508 _checkSame2(span1, s1, "const span1==s1");
509
510 span0 = s2;
511 _checkSame2(span0, s2, "const span0==s2");
512
513 ConstSpanType span2{ s3 };
514 _checkSame2(span2, s3, "const span2==s3");
515
516 ConstSpanType span3{ s3 };
517 ASSERT_TRUE(span2 == span3);
518 ASSERT_FALSE(span2 != span3);
519
520 span1 = s3;
521 _checkSame2(span1, s3, "const span1==s3");
522 }
523}
524
525/*---------------------------------------------------------------------------*/
526/*---------------------------------------------------------------------------*/
527
528TEST(Span2, StdArray)
529{
530 using namespace Arccore;
531 _testSpan2StdArray<Span2<Int64>, Span2<const Int64>>();
532}
533
534/*---------------------------------------------------------------------------*/
535/*---------------------------------------------------------------------------*/
536
537TEST(SmallSpan2, StdArray)
538{
539 using namespace Arccore;
540 _testSpan2StdArray<SmallSpan2<Int64>, SmallSpan2<const Int64>>();
541}
542
543/*---------------------------------------------------------------------------*/
544/*---------------------------------------------------------------------------*/
545
546template <typename ViewType> void
547_testSubViewInterval()
548{
549 using namespace Arccore;
550
551 std::array<Int64, 12> vlist{ 9, 13, 32, 27, 43, -5, 2, -7, 8, 11, 25, 48 };
552 ViewType view{ vlist };
553
554 {
555 ViewType null_view;
556 ViewType sub_view0{ view.subViewInterval(1, 0) };
557 ViewType sub_view1{ view.subViewInterval(-1, 2) };
558 ViewType sub_view2{ view.subViewInterval(2, 2) };
559 ViewType sub_view3{ view.subViewInterval(0, 0) };
560 std::cout << "SUBVIEW0=" << sub_view1 << '\n';
561 _checkSame(sub_view0, null_view, "sub_view0_null");
562 _checkSame(sub_view1, null_view, "sub_view1_null");
563 _checkSame(sub_view2, null_view, "sub_view2_null");
564 _checkSame(sub_view3, null_view, "sub_view3_null");
565 }
566
567 {
568 std::array<Int64, 4> vlist0{ 9, 13, 32, 27 };
569 std::array<Int64, 4> vlist1{ 43, -5, 2, -7 };
570 std::array<Int64, 4> vlist2{ 8, 11, 25, 48 };
571
572 ViewType sub_view0{ view.subViewInterval(0, 3) };
573 ViewType sub_view1{ view.subViewInterval(1, 3) };
574 ViewType sub_view2{ view.subViewInterval(2, 3) };
575 std::cout << "SUBVIEW1=" << sub_view1 << '\n';
576 _checkSame(sub_view0, vlist0, "sub_view0");
577 _checkSame(sub_view1, vlist1, "sub_view1");
578 _checkSame(sub_view2, vlist2, "sub_view2");
579 }
580
581 {
582 std::array<Int64, 2> vlist0{ 9, 13 };
583 std::array<Int64, 2> vlist1{ 32, 27 };
584 std::array<Int64, 2> vlist2{ 43, -5 };
585 std::array<Int64, 2> vlist3{ 2, -7 };
586 std::array<Int64, 4> vlist4{ 8, 11, 25, 48 };
587
588 ViewType sub_view0{ view.subViewInterval(0, 5) };
589 ViewType sub_view1{ view.subViewInterval(1, 5) };
590 ViewType sub_view2{ view.subViewInterval(2, 5) };
591 ViewType sub_view3{ view.subViewInterval(3, 5) };
592 ViewType sub_view4{ view.subViewInterval(4, 5) };
593 std::cout << "SUBVIEW2=" << sub_view1 << '\n';
594 _checkSame(sub_view0, vlist0, "sub_view0");
595 _checkSame(sub_view1, vlist1, "sub_view1");
596 _checkSame(sub_view2, vlist2, "sub_view2");
597 _checkSame(sub_view3, vlist3, "sub_view3");
598 _checkSame(sub_view4, vlist4, "sub_view4");
599 }
600}
601
602/*---------------------------------------------------------------------------*/
603/*---------------------------------------------------------------------------*/
604
605template <typename ViewType> void
606_testSubSpanInterval()
607{
608 using namespace Arccore;
609
610 std::array<Int64, 12> vlist{ 9, 13, 32, 27, 43, -5, 2, -7, 8, 11, 25, 48 };
611 ViewType view{ vlist };
612
613 {
614 ViewType null_view;
615 ViewType sub_view0{ view.subSpanInterval(1, 0) };
616 ViewType sub_view1{ view.subSpanInterval(-1, 2) };
617 ViewType sub_view2{ view.subSpanInterval(2, 2) };
618 ViewType sub_view3{ view.subSpanInterval(0, 0) };
619 std::cout << "SUBVIEW0=" << sub_view1 << '\n';
620 _checkSame(sub_view0, null_view, "sub_view0_null");
621 _checkSame(sub_view1, null_view, "sub_view1_null");
622 _checkSame(sub_view2, null_view, "sub_view2_null");
623 _checkSame(sub_view3, null_view, "sub_view3_null");
624 }
625
626 {
627 std::array<Int64, 4> vlist0{ 9, 13, 32, 27 };
628 std::array<Int64, 4> vlist1{ 43, -5, 2, -7 };
629 std::array<Int64, 4> vlist2{ 8, 11, 25, 48 };
630
631 ViewType sub_view0{ view.subSpanInterval(0, 3) };
632 ViewType sub_view1{ view.subSpanInterval(1, 3) };
633 ViewType sub_view2{ view.subSpanInterval(2, 3) };
634 std::cout << "SUBVIEW1=" << sub_view1 << '\n';
635 _checkSame(sub_view0, vlist0, "sub_view0");
636 _checkSame(sub_view1, vlist1, "sub_view1");
637 _checkSame(sub_view2, vlist2, "sub_view2");
638 }
639
640 {
641 std::array<Int64, 2> vlist0{ 9, 13 };
642 std::array<Int64, 2> vlist1{ 32, 27 };
643 std::array<Int64, 2> vlist2{ 43, -5 };
644 std::array<Int64, 2> vlist3{ 2, -7 };
645 std::array<Int64, 4> vlist4{ 8, 11, 25, 48 };
646
647 ViewType sub_view0{ view.subSpanInterval(0, 5) };
648 ViewType sub_view1{ view.subSpanInterval(1, 5) };
649 ViewType sub_view2{ view.subSpanInterval(2, 5) };
650 ViewType sub_view3{ view.subSpanInterval(3, 5) };
651 ViewType sub_view4{ view.subSpanInterval(4, 5) };
652 std::cout << "SUBVIEW2=" << sub_view1 << '\n';
653 _checkSame(sub_view0, vlist0, "sub_view0");
654 _checkSame(sub_view1, vlist1, "sub_view1");
655 _checkSame(sub_view2, vlist2, "sub_view2");
656 _checkSame(sub_view3, vlist3, "sub_view3");
657 _checkSame(sub_view4, vlist4, "sub_view4");
658 }
659}
660
661/*---------------------------------------------------------------------------*/
662/*---------------------------------------------------------------------------*/
663
664template <typename ViewType> void
665_testSubPartInterval()
666{
667 using namespace Arccore;
668
669 std::array<Int64, 12> vlist{ 9, 13, 32, 27, 43, -5, 2, -7, 8, 11, 25, 48 };
670 ViewType view{ vlist };
671
672 {
673 ViewType null_view;
674 ViewType sub_view0{ view.subPartInterval(1, 0) };
675 ViewType sub_view1{ view.subPartInterval(-1, 2) };
676 ViewType sub_view2{ view.subPartInterval(2, 2) };
677 ViewType sub_view3{ view.subPartInterval(0, 0) };
678 std::cout << "SUBVIEW0=" << sub_view1 << '\n';
679 _checkSame(sub_view0, null_view, "sub_view0_null");
680 _checkSame(sub_view1, null_view, "sub_view1_null");
681 _checkSame(sub_view2, null_view, "sub_view2_null");
682 _checkSame(sub_view3, null_view, "sub_view3_null");
683 }
684
685 {
686 std::array<Int64, 4> vlist0{ 9, 13, 32, 27 };
687 std::array<Int64, 4> vlist1{ 43, -5, 2, -7 };
688 std::array<Int64, 4> vlist2{ 8, 11, 25, 48 };
689
690 ViewType sub_view0{ view.subPartInterval(0, 3) };
691 ViewType sub_view1{ view.subPartInterval(1, 3) };
692 ViewType sub_view2{ view.subPartInterval(2, 3) };
693 std::cout << "SUBVIEW1=" << sub_view1 << '\n';
694 _checkSame(sub_view0, vlist0, "sub_view0");
695 _checkSame(sub_view1, vlist1, "sub_view1");
696 _checkSame(sub_view2, vlist2, "sub_view2");
697 }
698
699 {
700 std::array<Int64, 2> vlist0{ 9, 13 };
701 std::array<Int64, 2> vlist1{ 32, 27 };
702 std::array<Int64, 2> vlist2{ 43, -5 };
703 std::array<Int64, 2> vlist3{ 2, -7 };
704 std::array<Int64, 4> vlist4{ 8, 11, 25, 48 };
705
706 ViewType sub_view0{ view.subPartInterval(0, 5) };
707 ViewType sub_view1{ view.subPartInterval(1, 5) };
708 ViewType sub_view2{ view.subPartInterval(2, 5) };
709 ViewType sub_view3{ view.subPartInterval(3, 5) };
710 ViewType sub_view4{ view.subPartInterval(4, 5) };
711 std::cout << "SUBVIEW2=" << sub_view1 << '\n';
712 _checkSame(sub_view0, vlist0, "sub_view0");
713 _checkSame(sub_view1, vlist1, "sub_view1");
714 _checkSame(sub_view2, vlist2, "sub_view2");
715 _checkSame(sub_view3, vlist3, "sub_view3");
716 _checkSame(sub_view4, vlist4, "sub_view4");
717 }
718}
719
720/*---------------------------------------------------------------------------*/
721/*---------------------------------------------------------------------------*/
722
723TEST(ArrayView, SubViewInterval)
724{
725 using namespace Arccore;
726 _testSubViewInterval<ArrayView<Int64>>();
727 _testSubViewInterval<ConstArrayView<Int64>>();
728 _testSubSpanInterval<Span<Int64>>();
729 _testSubSpanInterval<SmallSpan<Int64>>();
730
731 _testSubPartInterval<ArrayView<Int64>>();
732 _testSubPartInterval<ConstArrayView<Int64>>();
733 _testSubPartInterval<Span<Int64>>();
734 _testSubPartInterval<SmallSpan<Int64>>();
735}
736
737TEST(ArrayView, Copyable)
738{
739 using namespace Arccore;
740 ASSERT_TRUE(std::is_trivially_copyable_v<ArrayView<int>>);
741 ASSERT_TRUE(std::is_trivially_copyable_v<ConstArrayView<int>>);
742
743 ASSERT_TRUE(std::is_trivially_copyable_v<Array2View<int>>);
744 ASSERT_TRUE(std::is_trivially_copyable_v<ConstArray2View<int>>);
745
746 ASSERT_TRUE(std::is_trivially_copyable_v<Array3View<int>>);
747 ASSERT_TRUE(std::is_trivially_copyable_v<ConstArray3View<int>>);
748
749 ASSERT_TRUE(std::is_trivially_copyable_v<Array4View<int>>);
750 ASSERT_TRUE(std::is_trivially_copyable_v<ConstArray4View<int>>);
751
752 ASSERT_TRUE(std::is_trivially_copyable_v<Span<int>>);
753 ASSERT_TRUE(std::is_trivially_copyable_v<Span<const int>>);
754
755 ASSERT_TRUE(std::is_trivially_copyable_v<Span2<int>>);
756 ASSERT_TRUE(std::is_trivially_copyable_v<Span2<const int>>);
757}
758
759TEST(Span, FixedValue)
760{
761 using namespace Arccore;
762 std::cout << "sizeof(Span<Int32,1>) = " << sizeof(Span<Int32, 1>) << "\n";
763 std::cout << "sizeof(Span<Int32,DynExtent>) = " << sizeof(Span<Int32, DynExtent>) << "\n";
764 std::cout << "sizeof(Span<Int64,1>) = " << sizeof(Span<Int64, 1>) << "\n";
765 std::cout << "sizeof(Span<Int64,DynExtent>) = " << sizeof(Span<Int64, DynExtent>) << "\n";
766
767 // Vérifie que [[no_unique_address]] est bien pris en compte
768 // A priori cela n'est pas le cas avec VS2022.
769 ASSERT_EQ(sizeof(Span<Int32, 1>), sizeof(void*));
770 ASSERT_EQ(sizeof(Span<Int64, 1>), sizeof(void*));
771
772 std::array<Int64, 12> vlist{ 9, 13, 32, 27, 43, -5, 2, -7, 8, 11, 25, 48 };
773 Span<Int64, 12> fixed_list(vlist);
774 Int32 sub_view_size = 5;
775 Span<Int64> sub_view1 = fixed_list.subspan(0, sub_view_size);
776 ASSERT_EQ(sub_view1.size(), sub_view_size);
777
778 Span<Int64, 12> fixed_span_only_ptr(vlist.data());
779 ASSERT_EQ(fixed_span_only_ptr.data(), vlist.data());
780 Span<const Int64, 12> fixed_span_only_const_ptr(vlist.data());
781 ASSERT_EQ(fixed_span_only_const_ptr.data(), vlist.data());
782}
783
784TEST(MDSpan, Misc)
785{
786 using namespace Arcane;
787 Int32 v1[4] = { 1, 3, -5, 2 };
788
789 MDSpan<Int32, MDDim1> mdspan1;
790 ASSERT_EQ(mdspan1.extent0(),0);
791
792 mdspan1 = MDSpan<Int32,MDDim1>(v1,4);
793 ASSERT_EQ(mdspan1.extent0(),4);
794 ASSERT_EQ(mdspan1.data(),v1);
795 ASSERT_EQ(mdspan1[0],1);
796 ASSERT_EQ(mdspan1[1],3);
797 ASSERT_EQ(mdspan1[2],-5);
798 ASSERT_EQ(mdspan1[3],2);
799
800 MDSpan<const Int32, MDDim1> const_mdspan1(mdspan1);
801 ASSERT_EQ(const_mdspan1.data(), mdspan1.data());
802 ASSERT_EQ(const_mdspan1.extent0(), mdspan1.extent0());
803
804 // Conversion à partir de ArrayView
805 {
806 ArrayView arrayview1(4,v1);
807
808 MDSpan<Int32, MDDim1> mdspan2(arrayview1);
809 ASSERT_EQ(mdspan2.data(), mdspan1.data());
810 ASSERT_EQ(mdspan2.extent0(), mdspan1.extent0());
811
812 MDSpan<const Int32, MDDim1> const_mdspan2(arrayview1);
813 ASSERT_EQ(const_mdspan2.data(), mdspan1.data());
814 ASSERT_EQ(const_mdspan2.extent0(), mdspan1.extent0());
815
816 mdspan2 = arrayview1;
817 ASSERT_EQ(mdspan2.data(), mdspan1.data());
818 ASSERT_EQ(mdspan2.extent0(), mdspan1.extent0());
819
820 const_mdspan2 = arrayview1;
821 ASSERT_EQ(const_mdspan2.data(), mdspan1.data());
822 ASSERT_EQ(const_mdspan2.extent0(), mdspan1.extent0());
823 }
824
825 // Conversion à partir de ConstArrayView
826 {
827 ConstArrayView const_arrayview1(4,v1);
828
829 MDSpan<const Int32, MDDim1> const_mdspan2(const_arrayview1);
830 ASSERT_EQ(const_mdspan2.data(), mdspan1.data());
831 ASSERT_EQ(const_mdspan2.extent0(), mdspan1.extent0());
832
833 const_mdspan2 = const_arrayview1;
834 ASSERT_EQ(const_mdspan2.data(), mdspan1.data());
835 ASSERT_EQ(const_mdspan2.extent0(), mdspan1.extent0());
836 }
837}
838
839/*---------------------------------------------------------------------------*/
840/*---------------------------------------------------------------------------*/
841
842namespace Arcane
843{
844template class ArrayView<Int32>;
845template class ConstArrayView<Int32>;
846template class ArrayView<double>;
847template class ConstArrayView<double>;
848
849template class Span<Int32>;
850template class Span<const Int32>;
851template class Span<double>;
852template class Span<const double>;
853
854template class SmallSpan<Int32>;
855template class SmallSpan<const Int32>;
856template class SmallSpan<double>;
857template class SmallSpan<const double>;
858
859template class Span<Int32, 4>;
860template class Span<const Int32, 5>;
861template class Span<double, 6>;
862template class Span<const double, 7>;
863
864template class SmallSpan<Int32, 4>;
865template class SmallSpan<const Int32, 5>;
866template class SmallSpan<double, 6>;
867template class SmallSpan<const double, 7>;
868
869template class Span2<Int32>;
870template class Span2<const Int32>;
871template class Span2<double>;
872template class Span2<const double>;
873
874template class SmallSpan2<Int32>;
875template class SmallSpan2<const Int32>;
876template class SmallSpan2<double>;
877template class SmallSpan2<const double>;
878
879template class MDSpan<Int32,MDDim1>;
880template class MDSpan<Int32,MDDim2>;
881template class MDSpan<Int32,MDDim3>;
882template class MDSpan<Int32,MDDim4>;
883} // namespace Arcane
884
885/*---------------------------------------------------------------------------*/
886/*---------------------------------------------------------------------------*/
#define ASSERT_FALSE(condition)
Vérifie que condition est faux.
Definition Assertion.h:137
#define ASSERT_TRUE(condition)
Vérifie que condition est vrai.
Definition Assertion.h:125
Types et fonctions associés aux classes Span2Impl, Small2Span et Span2.
Types et fonctions associés aux classes SpanImpl, SmallSpan et Span.
Types et fonctions associés aux classes Array3View et ConstArray3View.
Types et fonctions associés aux classes Array4View et ConstArray4View.
Types et fonctions associés aux classes ArrayView et ConstArrayView.
Types et fonctions associés à la classe MDSpan.
Vue modifiable pour un tableau 2D.
Vue modifiable d'un tableau d'un type T.
Vue constante d'un tableau de type T.
Classe de base des vues multi-dimensionnelles.
constexpr DataType * data() noexcept
Pointeur de base pour la mémoire allouée.
constexpr ExtentIndexType extent0() const
Valeur de la première dimension.
Vue pour un tableau 2D dont la taille est un 'Int32'.
Definition Span2.h:246
Vue d'un tableau d'éléments de type T.
Definition Span.h:802
Vue pour un tableau 2D dont la taille est un 'Int64'.
Definition Span2.h:321
constexpr __host__ __device__ SizeType size() const noexcept
Retourne la taille du tableau.
Definition Span.h:325
Vue d'un tableau d'éléments de type T.
Definition Span.h:633
constexpr __host__ __device__ Span< T, DynExtent > subspan(Int64 abegin, Int64 asize) const
Sous-vue à partir de l'élément abegin et contenant asize éléments.
Definition Span.h:721
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type représentant un entier.
Espace de nom de Arccore.
constexpr __host__ __device__ Integer arccoreCheckArraySize(unsigned long long size)
Vérifie que size peut être converti dans un 'Integer' pour servir de taille à un tableau....
SmallSpan< DataType > asSmallSpan(SmallSpan< std::byte, Extent > bytes)
Convertit un SmallSpan<std::byte> en un SmallSpan<DataType>.
Definition Span.h:1140
Span< DataType > asSpan(Span< std::byte, Extent > bytes)
Convertit un Span<std::byte> en un Span<DataType>.
Definition Span.h:1120
Impl::SpanTypeFromSize< conststd::byte, SizeType >::SpanType asBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Convertit la vue en un tableau d'octets non modifiables.
Definition Span.h:1028
Impl::SpanTypeFromSize< std::byte, SizeType >::SpanType asWritableBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Convertit la vue en un tableau d'octets modifiables.
Definition Span.h:1064