8#include <gtest/gtest.h>
10#include "arcane/utils/NumArray.h"
12#include "arcane/utils/Real2.h"
13#include "arcane/utils/Real3.h"
14#include "arcane/utils/Real2x2.h"
15#include "arcane/utils/Real3x3.h"
17#include "arcane/utils/NumArrayUtils.h"
33 std::cout <<
"TEST_NUMARRAY Vide\n";
44 std::cout <<
"TEST_NUMARRAY Basique\n";
48 ASSERT_EQ(array1(1), 5.0);
49 std::cout <<
" V=" << array1(1) <<
"\n";
51 ASSERT_EQ(array1[2], 3.0);
52 std::cout <<
" V=" << array1(1) <<
"\n";
54 ASSERT_EQ(array1.totalNbElement(), 7);
58 std::cout <<
" V=" << array2(1, 2) <<
"\n";
60 ASSERT_EQ(array2.totalNbElement(), (7 * 5));
63 array3(1, 2, 3) = 5.0;
64 std::cout <<
" V=" << array3(1, 2, 3) <<
"\n";
65 ASSERT_EQ(array3(1, 2, 3), 5.0);
66 array3.resize(12, 4, 6);
67 ASSERT_EQ(array3.totalNbElement(), (12 * 4 * 6));
69 array3(1, 2, 3) = 4.0;
70 array3(2, 3, 5) = 1.0;
74 ASSERT_EQ(array3.extent0(), span_array3.extent0());
77 ASSERT_EQ(const_span_array3.to1DSpan(), span_array3.to1DSpan());
79 ASSERT_EQ(array3.extent0(), span_array3.extent0());
80 std::cout <<
"Array3: extents=" << array3.extent0()
81 <<
"," << array3.extent1() <<
"," << array3.extent2() <<
"\n";
82 for (Int32 i = 0; i < array3.extent0(); ++i) {
84 ASSERT_EQ(span_array2.
extent0(), span_array3.extent1());
85 ASSERT_EQ(span_array2.
extent1(), span_array3.extent2());
86 std::cout <<
" MDDim2 slice i=" << i <<
" X=" << span_array2.
extent0() <<
" Y=" << span_array2.
extent1() <<
"\n";
87 for (Int32 x = 0, xn = span_array2.
extent0(); x < xn; ++x) {
88 for (Int32 y = 0, yn = span_array2.
extent1(); y < yn; ++y) {
89 ASSERT_EQ(span_array2.
ptrAt(x, y), span_array3.ptrAt(i, x, y));
96 std::cout <<
"Array2: extents=" << array2.extent0() <<
"," << array2.extent1() <<
"\n";
97 for (Int32 i = 0; i < array2.extent0(); ++i) {
100 std::cout <<
" MDDim1 slice i=" << i <<
" X=" << span_array2.
extent0() <<
"\n";
101 for (Int32 x = 0, xn = span_array1.
extent0(); x < xn; ++x) {
102 ASSERT_EQ(span_array1.
ptrAt(x), span_array2.
ptrAt(i, x));
107 array4(1, 2, 3, 4) = 5.0;
108 std::cout <<
" V=" << array4(1, 2, 3, 4) <<
"\n";
109 array4.resize(8, 3, 7, 5);
110 ASSERT_EQ(array4.totalNbElement(), (8 * 3 * 7 * 5));
113 ASSERT_EQ(num_data1[0], 2.4);
114 ASSERT_EQ(num_data1[1], 5.6);
115 ASSERT_EQ(num_data1[2], 3.3);
116 ASSERT_EQ(num_data1[3], 5.4);
119 ASSERT_EQ(num_data2(0, 0), 1.4);
120 ASSERT_EQ(num_data2(0, 1), 15.6);
121 ASSERT_EQ(num_data2(1, 0), 33.3);
122 ASSERT_EQ(num_data2(1, 1), 7.4);
123 ASSERT_EQ(num_data2(2, 0), 4.2);
124 ASSERT_EQ(num_data2(2, 1), 6.5);
132 std::cout <<
"TEST_NUMARRAY Basique2\n";
137 std::cout <<
" V=" << array1(1) <<
"\n";
142 std::cout <<
" V=" << array2(1, 2) <<
"\n";
145 array3.resize(2, 3, 4);
146 array3(1, 2, 3) = 5.0;
147 std::cout <<
" V=" << array3(1, 2, 3) <<
"\n";
150 array4.resize(2, 3, 4, 5);
151 array4(1, 2, 3, 4) = 5.0;
152 std::cout <<
" V=" << array4(1, 2, 3, 4) <<
"\n";
160 std::cout <<
"TEST_NUMARRAY Extents\n";
167 ASSERT_EQ(1, MDDim1::nb_dynamic);
168 ASSERT_EQ(2, MDDim2::nb_dynamic);
169 ASSERT_EQ(3, MDDim3::nb_dynamic);
171 ASSERT_EQ(4, MDDim4::nb_dynamic);
195 constexpr int nb_x = 3;
196 constexpr int nb_y = 4;
197 constexpr int nb_z = 5;
202 auto v_extents = v.extentsWithOffset();
204 for (Int32 x = 0, xn = v.dim1Size(); x < xn; ++x) {
205 for (Int32 y = 0, yn = v.dim2Size(); y < yn; ++y) {
206 for (Int32 z = 0, zn = v.dim3Size(); z < zn; ++z) {
207 ArrayIndex<3> idx{ x, y, z };
208 Int64 offset = v_extents.offset(idx);
210 v({ x, y, z }) = offset;
216 std::cout <<
"CAPACITY V1=" << v.capacity() <<
"\n";
218 std::cout <<
"CAPACITY V2=" << v.capacity() <<
"\n";
220 std::cout <<
"CAPACITY V3=" << v.capacity() <<
"\n";
222 std::cout <<
"CAPACITY V4=" << v.capacity() <<
"\n";
224 ASSERT_EQ(v.data(), v.to1DSpan().data());
231 for( Int32& x : array1 ){
235 const auto& const_array1 = array1;
236 for( Int32 x : const_array1 )
243#if NUMARRAY_HAS_VALID_RESIZE
247 std::vector<Int64> valid_values = {
248 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
249 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
250 24, 25, 26, 27, 28, 29, 20, 21, 22, 23, 24, 25
252 ASSERT_EQ(valid_values.size(), (
size_t)36);
254 v_extents = v.extentsWithOffset();
256 for (Int64 x = 0, xn = v.dim1Size(); x < xn; ++x) {
257 for (Int64 y = 0, yn = v.dim2Size(); y < yn; ++y) {
258 for (Int64 z = 0, zn = v.dim3Size(); z < zn; ++z) {
259 ArrayBoundsIndex<3> idx{ x, y, z };
260 Int64 offset = v_extents.offset(idx);
261 Int64 val1 = v(x, y, z);
262 Int64 val2 = v({ x, y, z });
263 std::cout <<
"XYZ=" << x <<
" " << y <<
" " << z
264 <<
" V=" << val1 <<
" offset=" << offset <<
"\n";
265 ASSERT_EQ(index, offset);
266 ASSERT_EQ(val1, val2);
267 ASSERT_EQ(valid_values.at(offset), val1);
282 std::cout <<
"IN_CREATE_1\n";
284 std::cout <<
"IN_CREATE_2\n";
285 for (Int32 i = 0; i < size; ++i)
306 ASSERT_EQ(vi1.to1DSpan(), vi0.to1DSpan());
307 ASSERT_EQ(vi2.
to1DSpan(), vi1.to1DSpan());
309 ASSERT_EQ(vi3.to1DSpan(), vi0.to1DSpan());
313 ASSERT_EQ(vi0.to1DSpan(), vi0_span);
314 ASSERT_EQ(vi1_span, vi1.to1DSpan());
315 ASSERT_EQ(vi1.to1DSmallSpan(), vi0.to1DSmallSpan());
316 ASSERT_EQ(vi1.to1DConstSmallSpan(), vi0.to1DConstSmallSpan());
319 ASSERT_EQ(vi1_ref_span, vi1.to1DSpan());
327 ASSERT_EQ(vi1.to1DSpan(), vi0.to1DSpan());
328 ASSERT_EQ(vi2.
to1DSpan(), vi1.to1DSpan());
336 std::cout <<
"PART_1\n";
339 Int32 wanted_size1 = 23;
340 test_move = _createNumArray(wanted_size1);
341 std::cout <<
"PART_2\n";
342 ASSERT_EQ(test_move.totalNbElement(), wanted_size1) <<
"Taille incorrecte (test move 1)";
343 ASSERT_EQ(test_move[6], wanted_size1 + 8) <<
"Taille incorrecte (test move 2)";
344 Int32 wanted_size2 = 17;
345 test_move = _createNumArray(wanted_size2);
346 std::cout <<
"PART_3\n";
347 ASSERT_EQ(test_move.totalNbElement(), wanted_size2) <<
"Taille incorrecte (test move 3)";
348 ASSERT_EQ(test_move[3], wanted_size2 + 5) <<
"Taille incorrecte (test move 4)";
352 Int32 wanted_size1 = 31;
353 std::cout <<
"PART_4\n";
355 std::cout <<
"PART_5\n";
356 ASSERT_EQ(test_move.totalNbElement(), wanted_size1) <<
"Taille incorrecte (test move 1)";
357 ASSERT_EQ(test_move[7], wanted_size1 + 9) <<
"Taille incorrecte (test move 2)";
364TEST(NumArray3, Index)
366 ArrayIndex<3> index(1, 4, 2);
367 auto [i, j, k] = index();
377void _setNumArray2Values(T& a)
379 for (Int32 i = 0; i < a.dim1Size(); ++i) {
380 for (Int32 j = 0; j < a.dim2Size(); ++j) {
381 a(i, j) = (i * 253) + j;
386void _setNumArray3Values(T& a)
388 for (Int32 i = 0; i < a.dim1Size(); ++i) {
389 for (Int32 j = 0; j < a.dim2Size(); ++j) {
390 for (Int32 k = 0; k < a.dim3Size(); ++k) {
391 a(i, j, k) = (i * 253) + (j * 27) + k;
401TEST(NumArray2, Layout)
403 std::cout <<
"TEST_NUMARRAY2 Disposition\n";
407 ASSERT_EQ(a.totalNbElement(), (3 * 5));
408 _setNumArray2Values(a);
409 auto values = a.to1DSpan();
410 std::cout <<
"V=" << values <<
"\n";
411 UniqueArray<Real> ref_value = { 0, 1, 2, 3, 4, 253, 254, 255, 256, 257, 506, 507, 508, 509, 510 };
412 ASSERT_EQ(values.smallView(), ref_value.
view());
417 ASSERT_EQ(a.totalNbElement(), (3 * 5));
418 _setNumArray2Values(a);
419 auto values = a.to1DSpan();
420 std::cout <<
"V=" << values <<
"\n";
421 UniqueArray<Real> ref_value = { 0, 253, 506, 1, 254, 507, 2, 255, 508, 3, 256, 509, 4, 257, 510 };
422 ASSERT_EQ(values.smallView(), ref_value.
view());
429template <
typename NumArray3>
430void _checkRightLayoutDim3(NumArray3& a)
433 ASSERT_EQ(a.totalNbElement(), (2 * 3 * 5));
434 ASSERT_EQ(a.extent0(), 2);
435 ASSERT_EQ(a.extent1(), 3);
436 ASSERT_EQ(a.extent2(), 5);
437 _setNumArray3Values(a);
438 auto values = a.to1DSpan();
439 std::cout <<
"V=" << values <<
"\n";
441 0, 1, 2, 3, 4, 27, 28, 29, 30, 31, 54, 55, 56, 57, 58,
442 253, 254, 255, 256, 257, 280, 281, 282, 283, 284, 307, 308, 309, 310, 311
444 ASSERT_EQ(values.smallView(), ref_value.
view());
447template <
typename NumArray3>
448void _checkLeftLayoutDim3(NumArray3& a)
452 ASSERT_EQ(a.totalNbElement(), (2 * 3 * 5));
453 _setNumArray3Values(a);
454 auto values = a.to1DSpan();
455 std::cout <<
"V=" << values <<
"\n";
457 0, 253, 27, 280, 54, 307, 1, 254, 28, 281, 55, 308, 2, 255, 29,
458 282, 56, 309, 3, 256, 30, 283, 57, 310, 4, 257, 31, 284, 58, 311
460 ASSERT_EQ(values.smallView(), ref_value.
view());
463TEST(NumArray3, Layout)
465 std::cout <<
"TEST_NUMARRAY3 Disposition\n";
469 std::cout <<
"TEST_NUMARRAY3 RightLayout 1\n";
470 _checkRightLayoutDim3(a);
474 std::cout <<
"TEST_NUMARRAY3 RightLayout 2\n";
475 _checkRightLayoutDim3(a);
479 std::cout <<
"TEST_NUMARRAY3 RightLayout 3\n";
480 _checkRightLayoutDim3(a);
484 std::cout <<
"TEST_NUMARRAY3 RightLayout 4\n";
485 _checkRightLayoutDim3(a);
489 std::cout <<
"TEST_NUMARRAY3 RightLayout 5\n";
490 _checkRightLayoutDim3(a);
495 std::cout <<
"TEST_NUMARRAY3 LeftLayout 1\n";
496 _checkLeftLayoutDim3(a);
500 std::cout <<
"TEST_NUMARRAY3 LeftLayout 2\n";
501 _checkLeftLayoutDim3(a);
505 std::cout <<
"TEST_NUMARRAY3 LeftLayout 3\n";
506 _checkLeftLayoutDim3(a);
510 std::cout <<
"TEST_NUMARRAY3 LeftLayout 4\n";
511 _checkLeftLayoutDim3(a);
515 std::cout <<
"TEST_NUMARRAY3 LeftLayout 5\n";
516 _checkLeftLayoutDim3(a);
527 a(2) =
Real2(0.0, 3.2);
529 ASSERT_EQ(a(3).y, 2.0);
534 const Real3 v(0.0, 3.2, 5.6);
537 ASSERT_EQ(a(4).y, 4.0);
543 const Real2 v0(1.2, 1.7);
549 ASSERT_EQ(a(4).y.x, 4.0);
551 ASSERT_EQ(a(3)(1), v0);
556 const Real3 v0(1.2, 3.4, 1.7);
563 ASSERT_EQ(a(4).y.z, 4.0);
565 ASSERT_EQ(a(3)(1), v0);
575 const char* values1_str =
"1 3 -2 \n -7 -5 12 \n 3 9 11\n";
576 NumArray<Int32, MDDim1> ref_value(9, { 1, 3, -2, -7, -5, 12, 3, 9, 11 });
577 std::istringstream istr1(values1_str);
580 ASSERT_EQ(int32_values.
extent0(), 9);
581 ASSERT_EQ(int32_values.
to1DSpan(), ref_value.to1DSpan());
584 const char* values1_str =
"1.1 3.3 -2.5 \n \n 2.1 4.99 12.23 \n 23 \n 45.1 11.9e2 -12.6e4\n";
585 NumArray<Real, MDDim1> ref_value(10, { 1.1, 3.3, -2.5, 2.1, 4.99, 12.23, 23, 45.1, 11.9e2, -12.6e4 });
586 std::istringstream istr1(values1_str);
589 ASSERT_EQ(real_values.
extent0(), 10);
590 ASSERT_EQ(real_values.
to1DSpan(), ref_value.to1DSpan());
593namespace TestCopyNumArray
607 std::cout << w.to1DSpan() <<
"\n";
609 std::cout << b.a_.to1DSpan() <<
"\n";
615 auto tpq = Arcane::NumArray<Real, Arcane::MDDim1>(5);
617 std::cout << tpq.to1DSpan() <<
"\n";
625 using namespace TestCopyNumArray;
628 std::cout <<
"Val 4 = "
630 <<
" " << test4.a_.to1DSpan() <<
"\n";
632 std::cout <<
"Val 5 = " << &test5 <<
" " << test5.to1DSpan() <<
"\n";
633 ASSERT_EQ(test4.a_.to1DSpan(), test5.to1DSpan());
641 std::cout <<
"Test_SpanUsage";
650 a1.
copy(span_ua1,
nullptr);
654 ASSERT_EQ(span_ua1, md_a1.to1DSmallSpan());
657 ASSERT_EQ(const_span_ua1, md_a2.to1DSmallSpan());
660 ASSERT_EQ(span_ua1, md_a3.to1DSmallSpan());
#define ASSERT_TRUE(condition)
Vérifie que condition est vrai.
SmallSpan< const T > smallSpan() const
Vue immutable sur ce tableau.
ArrayView< T > view() const
Vue mutable sur ce tableau.
Classe de base des vues multi-dimensionnelles.
constexpr ExtentIndexType extent1() const
Valeur de la deuxième dimension.
constexpr DataType * ptrAt(ExtentIndexType i, ExtentIndexType j, ExtentIndexType k, ExtentIndexType l) const
Pointeur sur la valeur pour l'élément i,j,k.
__host__ __device__ MDSpan< DataType, RemovedFirstExtentsType, LayoutPolicy > slice(ExtentIndexType i) const
Retourne une vue de dimension (N-1) à partir de l'élément d'indice i.
constexpr ExtentIndexType extent0() const
Valeur de la première dimension.
Tableaux multi-dimensionnels pour les types numériques accessibles sur accélérateurs.
Span< const DataType > to1DSpan() const
Vue 1D constante sur l'instance.
constexpr ExtentIndexType extent2() const
Valeur de la troisième dimension.
constexpr ExtentIndexType extent1() const
Valeur de la deuxième dimension.
constexpr SmallSpan< DataType > to1DSmallSpan()
Vue 1D sur l'instance (uniquement si rank == 1).
void copy(ConstMDSpanType rhs)
Copie dans l'instance les valeurs de rhs.
constexpr ExtentIndexType extent3() const
Valeur de la quatrième dimension.
void fill(const DataType &v)
Remplit les valeurs du tableau par v.
void resize(ExtentIndexType dim1_size)
Modifie la taille du tableau en gardant pas les valeurs actuelles.
constexpr ExtentIndexType extent0() const
Valeur de la première dimension.
Classe gérant un vecteur réel 2-dimensionnel.
Classe gérant une matrice 2x2 de réels.
Classe gérant un vecteur réel de 3 dimensions.
Classe gérant une matrice réelle 3x3.
Vue d'un tableau d'éléments de type T.
Vue d'un tableau d'éléments de type T.
Vecteur 1D de données avec sémantique par valeur (style STL).
void readFromText(NumArray< double, MDDim1 > &v, std::istream &input)
Remplit v avec les valeurs de input.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --