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 Empty\n";
44 std::cout <<
"TEST_NUMARRAY Basic\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 Basic2\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";
226#if NUMARRAY_HAS_VALID_RESIZE
229 std::vector<Int64> valid_values = {
230 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
231 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
232 24, 25, 26, 27, 28, 29, 20, 21, 22, 23, 24, 25
234 ASSERT_EQ(valid_values.size(), (
size_t)36);
236 v_extents = v.extentsWithOffset();
238 for (Int64 x = 0, xn = v.dim1Size(); x < xn; ++x) {
239 for (Int64 y = 0, yn = v.dim2Size(); y < yn; ++y) {
240 for (Int64 z = 0, zn = v.dim3Size(); z < zn; ++z) {
241 ArrayBoundsIndex<3> idx{ x, y, z };
242 Int64 offset = v_extents.offset(idx);
243 Int64 val1 = v(x, y, z);
244 Int64 val2 = v({ x, y, z });
245 std::cout <<
"XYZ=" << x <<
" " << y <<
" " << z
246 <<
" V=" << val1 <<
" offset=" << offset <<
"\n";
247 ASSERT_EQ(index, offset);
248 ASSERT_EQ(val1, val2);
249 ASSERT_EQ(valid_values.at(offset), val1);
264 std::cout <<
"IN_CREATE_1\n";
266 std::cout <<
"IN_CREATE_2\n";
267 for (Int32 i = 0; i < size; ++i)
288 ASSERT_EQ(vi1.to1DSpan(), vi0.to1DSpan());
289 ASSERT_EQ(vi2.
to1DSpan(), vi1.to1DSpan());
291 ASSERT_EQ(vi3.to1DSpan(), vi0.to1DSpan());
295 ASSERT_EQ(vi0.to1DSpan(), vi0_span);
296 ASSERT_EQ(vi1_span, vi1.to1DSpan());
297 ASSERT_EQ(vi1.to1DSmallSpan(), vi0.to1DSmallSpan());
298 ASSERT_EQ(vi1.to1DConstSmallSpan(), vi0.to1DConstSmallSpan());
301 ASSERT_EQ(vi1_ref_span, vi1.to1DSpan());
309 ASSERT_EQ(vi1.to1DSpan(), vi0.to1DSpan());
310 ASSERT_EQ(vi2.
to1DSpan(), vi1.to1DSpan());
318 std::cout <<
"PART_1\n";
321 Int32 wanted_size1 = 23;
322 test_move = _createNumArray(wanted_size1);
323 std::cout <<
"PART_2\n";
324 ASSERT_EQ(test_move.totalNbElement(), wanted_size1) <<
"Bad size (test move 1)";
325 ASSERT_EQ(test_move[6], wanted_size1 + 8) <<
"Bad size (test move 2)";
326 Int32 wanted_size2 = 17;
327 test_move = _createNumArray(wanted_size2);
328 std::cout <<
"PART_3\n";
329 ASSERT_EQ(test_move.totalNbElement(), wanted_size2) <<
"Bad size (test move 3)";
330 ASSERT_EQ(test_move[3], wanted_size2 + 5) <<
"Bad size (test move 4)";
334 Int32 wanted_size1 = 31;
335 std::cout <<
"PART_4\n";
337 std::cout <<
"PART_5\n";
338 ASSERT_EQ(test_move.totalNbElement(), wanted_size1) <<
"Bad size (test move 1)";
339 ASSERT_EQ(test_move[7], wanted_size1 + 9) <<
"Bad size (test move 2)";
346TEST(NumArray3, Index)
348 ArrayIndex<3> index(1, 4, 2);
349 auto [i, j, k] = index();
359void _setNumArray2Values(T& a)
361 for (Int32 i = 0; i < a.dim1Size(); ++i) {
362 for (Int32 j = 0; j < a.dim2Size(); ++j) {
363 a(i, j) = (i * 253) + j;
368void _setNumArray3Values(T& a)
370 for (Int32 i = 0; i < a.dim1Size(); ++i) {
371 for (Int32 j = 0; j < a.dim2Size(); ++j) {
372 for (Int32 k = 0; k < a.dim3Size(); ++k) {
373 a(i, j, k) = (i * 253) + (j * 27) + k;
383TEST(NumArray2, Layout)
385 std::cout <<
"TEST_NUMARRAY2 Layout\n";
389 ASSERT_EQ(a.totalNbElement(), (3 * 5));
390 _setNumArray2Values(a);
391 auto values = a.to1DSpan();
392 std::cout <<
"V=" << values <<
"\n";
393 UniqueArray<Real> ref_value = { 0, 1, 2, 3, 4, 253, 254, 255, 256, 257, 506, 507, 508, 509, 510 };
394 ASSERT_EQ(values.smallView(), ref_value.
view());
399 ASSERT_EQ(a.totalNbElement(), (3 * 5));
400 _setNumArray2Values(a);
401 auto values = a.to1DSpan();
402 std::cout <<
"V=" << values <<
"\n";
403 UniqueArray<Real> ref_value = { 0, 253, 506, 1, 254, 507, 2, 255, 508, 3, 256, 509, 4, 257, 510 };
404 ASSERT_EQ(values.smallView(), ref_value.
view());
411template <
typename NumArray3>
412void _checkRightLayoutDim3(NumArray3& a)
415 ASSERT_EQ(a.totalNbElement(), (2 * 3 * 5));
416 ASSERT_EQ(a.extent0(), 2);
417 ASSERT_EQ(a.extent1(), 3);
418 ASSERT_EQ(a.extent2(), 5);
419 _setNumArray3Values(a);
420 auto values = a.to1DSpan();
421 std::cout <<
"V=" << values <<
"\n";
423 0, 1, 2, 3, 4, 27, 28, 29, 30, 31, 54, 55, 56, 57, 58,
424 253, 254, 255, 256, 257, 280, 281, 282, 283, 284, 307, 308, 309, 310, 311
426 ASSERT_EQ(values.smallView(), ref_value.
view());
429template <
typename NumArray3>
430void _checkLeftLayoutDim3(NumArray3& a)
434 ASSERT_EQ(a.totalNbElement(), (2 * 3 * 5));
435 _setNumArray3Values(a);
436 auto values = a.to1DSpan();
437 std::cout <<
"V=" << values <<
"\n";
439 0, 253, 27, 280, 54, 307, 1, 254, 28, 281, 55, 308, 2, 255, 29,
440 282, 56, 309, 3, 256, 30, 283, 57, 310, 4, 257, 31, 284, 58, 311
442 ASSERT_EQ(values.smallView(), ref_value.
view());
445TEST(NumArray3, Layout)
447 std::cout <<
"TEST_NUMARRAY3 Layout\n";
451 std::cout <<
"TEST_NUMARRAY3 RightLayout 1\n";
452 _checkRightLayoutDim3(a);
456 std::cout <<
"TEST_NUMARRAY3 RightLayout 2\n";
457 _checkRightLayoutDim3(a);
461 std::cout <<
"TEST_NUMARRAY3 RightLayout 3\n";
462 _checkRightLayoutDim3(a);
466 std::cout <<
"TEST_NUMARRAY3 RightLayout 4\n";
467 _checkRightLayoutDim3(a);
471 std::cout <<
"TEST_NUMARRAY3 RightLayout 5\n";
472 _checkRightLayoutDim3(a);
477 std::cout <<
"TEST_NUMARRAY3 LeftLayout 1\n";
478 _checkLeftLayoutDim3(a);
482 std::cout <<
"TEST_NUMARRAY3 LeftLayout 2\n";
483 _checkLeftLayoutDim3(a);
487 std::cout <<
"TEST_NUMARRAY3 LeftLayout 3\n";
488 _checkLeftLayoutDim3(a);
492 std::cout <<
"TEST_NUMARRAY3 LeftLayout 4\n";
493 _checkLeftLayoutDim3(a);
497 std::cout <<
"TEST_NUMARRAY3 LeftLayout 5\n";
498 _checkLeftLayoutDim3(a);
509 a(2) =
Real2(0.0, 3.2);
511 ASSERT_EQ(a(3).y, 2.0);
516 const Real3 v(0.0, 3.2, 5.6);
519 ASSERT_EQ(a(4).y, 4.0);
525 const Real2 v0(1.2, 1.7);
531 ASSERT_EQ(a(4).y.x, 4.0);
533 ASSERT_EQ(a(3)(1), v0);
538 const Real3 v0(1.2, 3.4, 1.7);
545 ASSERT_EQ(a(4).y.z, 4.0);
547 ASSERT_EQ(a(3)(1), v0);
557 const char* values1_str =
"1 3 -2 \n -7 -5 12 \n 3 9 11\n";
558 NumArray<Int32, MDDim1> ref_value(9, { 1, 3, -2, -7, -5, 12, 3, 9, 11 });
559 std::istringstream istr1(values1_str);
562 ASSERT_EQ(int32_values.
extent0(), 9);
563 ASSERT_EQ(int32_values.
to1DSpan(), ref_value.to1DSpan());
566 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";
567 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 });
568 std::istringstream istr1(values1_str);
571 ASSERT_EQ(real_values.
extent0(), 10);
572 ASSERT_EQ(real_values.
to1DSpan(), ref_value.to1DSpan());
575namespace TestCopyNumArray
589 std::cout << w.to1DSpan() <<
"\n";
591 std::cout << b.a_.to1DSpan() <<
"\n";
597 auto tpq = Arcane::NumArray<Real, Arcane::MDDim1>(5);
599 std::cout << tpq.to1DSpan() <<
"\n";
607 using namespace TestCopyNumArray;
610 std::cout <<
"Val 4 = "
612 <<
" " << test4.a_.to1DSpan() <<
"\n";
614 std::cout <<
"Val 5 = " << &test5 <<
" " << test5.to1DSpan() <<
"\n";
615 ASSERT_EQ(test4.a_.to1DSpan(), test5.to1DSpan());
623 std::cout <<
"Test_SpanUsage";
632 a1.
copy(span_ua1,
nullptr);
636 ASSERT_EQ(span_ua1, md_a1.to1DSmallSpan());
639 ASSERT_EQ(const_span_ua1, md_a2.to1DSmallSpan());
642 ASSERT_EQ(span_ua1, md_a3.to1DSmallSpan());
#define ASSERT_TRUE(condition)
Checks that condition is true.
SmallSpan< const T > smallSpan() const
Immutable view of this array.
ArrayView< T > view() const
Mutable view of this array.
Base class for multi-dimensional views.
__host__ __device__ MDSpan< DataType, RemovedFirstExtentsType, LayoutPolicy > slice(Int32 i) const
Returns a dimension (N-1) view starting from index element i.
constexpr __host__ __device__ DataType * ptrAt(Int32 i, Int32 j, Int32 k, Int32 l) const
Pointer to the value for element i,j,k,l.
constexpr __host__ __device__ Int32 extent1() const
Value of the second dimension.
constexpr __host__ __device__ Int32 extent0() const
Value of the first dimension.
Multi-dimensional arrays for numerical types accessible on accelerators.
constexpr Int32 extent0() const
Value of the first dimension.
Span< const DataType > to1DSpan() const
Constant 1D view on the instance.
void copy(SmallSpan< const DataType > rhs)
Copies the values from rhs into the instance.
constexpr SmallSpan< DataType > to1DSmallSpan()
1D view on the instance (only if rank == 1)
void fill(const DataType &v)
Fills the array values with v.
constexpr Int32 extent1() const
Value of the second dimension.
constexpr Int32 extent3() const
Value of the fourth dimension.
constexpr Int32 extent2() const
Value of the third dimension.
void resize(Int32 dim1_size)
Resizes the array without keeping current values.
Class managing a 2-dimensional real vector.
Class managing a 2x2 matrix of reals.
Class managing a 3-dimensional real vector.
Class managing a 3x3 real matrix.
View of an array of elements of type T.
View of an array of elements of type T.
1D data vector with value semantics (STL style).
void readFromText(NumArray< double, MDDim1 > &v, std::istream &input)
Fills v with the values from input.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --