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";
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
246 std::vector<Int64> valid_values = {
247 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
248 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
249 24, 25, 26, 27, 28, 29, 20, 21, 22, 23, 24, 25
251 ASSERT_EQ(valid_values.size(), (
size_t)36);
253 v_extents = v.extentsWithOffset();
255 for (Int64 x = 0, xn = v.dim1Size(); x < xn; ++x) {
256 for (Int64 y = 0, yn = v.dim2Size(); y < yn; ++y) {
257 for (Int64 z = 0, zn = v.dim3Size(); z < zn; ++z) {
258 ArrayBoundsIndex<3> idx{ x, y, z };
259 Int64 offset = v_extents.offset(idx);
260 Int64 val1 = v(x, y, z);
261 Int64 val2 = v({ x, y, z });
262 std::cout <<
"XYZ=" << x <<
" " << y <<
" " << z
263 <<
" V=" << val1 <<
" offset=" << offset <<
"\n";
264 ASSERT_EQ(index, offset);
265 ASSERT_EQ(val1, val2);
266 ASSERT_EQ(valid_values.at(offset), val1);
281 std::cout <<
"IN_CREATE_1\n";
283 std::cout <<
"IN_CREATE_2\n";
284 for (Int32 i = 0; i < size; ++i)
305 ASSERT_EQ(vi1.to1DSpan(), vi0.to1DSpan());
306 ASSERT_EQ(vi2.
to1DSpan(), vi1.to1DSpan());
308 ASSERT_EQ(vi3.to1DSpan(), vi0.to1DSpan());
312 ASSERT_EQ(vi0.to1DSpan(), vi0_span);
313 ASSERT_EQ(vi1_span, vi1.to1DSpan());
314 ASSERT_EQ(vi1.to1DSmallSpan(), vi0.to1DSmallSpan());
315 ASSERT_EQ(vi1.to1DConstSmallSpan(), vi0.to1DConstSmallSpan());
318 ASSERT_EQ(vi1_ref_span, vi1.to1DSpan());
326 ASSERT_EQ(vi1.to1DSpan(), vi0.to1DSpan());
327 ASSERT_EQ(vi2.
to1DSpan(), vi1.to1DSpan());
335 std::cout <<
"PART_1\n";
338 Int32 wanted_size1 = 23;
339 test_move = _createNumArray(wanted_size1);
340 std::cout <<
"PART_2\n";
341 ASSERT_EQ(test_move.totalNbElement(), wanted_size1) <<
"Bad size (test move 1)";
342 ASSERT_EQ(test_move[6], wanted_size1 + 8) <<
"Bad size (test move 2)";
343 Int32 wanted_size2 = 17;
344 test_move = _createNumArray(wanted_size2);
345 std::cout <<
"PART_3\n";
346 ASSERT_EQ(test_move.totalNbElement(), wanted_size2) <<
"Bad size (test move 3)";
347 ASSERT_EQ(test_move[3], wanted_size2 + 5) <<
"Bad size (test move 4)";
351 Int32 wanted_size1 = 31;
352 std::cout <<
"PART_4\n";
354 std::cout <<
"PART_5\n";
355 ASSERT_EQ(test_move.totalNbElement(), wanted_size1) <<
"Bad size (test move 1)";
356 ASSERT_EQ(test_move[7], wanted_size1 + 9) <<
"Bad size (test move 2)";
363TEST(NumArray3, Index)
365 ArrayIndex<3> index(1, 4, 2);
366 auto [i, j, k] = index();
376void _setNumArray2Values(T& a)
378 for (Int32 i = 0; i < a.dim1Size(); ++i) {
379 for (Int32 j = 0; j < a.dim2Size(); ++j) {
380 a(i, j) = (i * 253) + j;
385void _setNumArray3Values(T& a)
387 for (Int32 i = 0; i < a.dim1Size(); ++i) {
388 for (Int32 j = 0; j < a.dim2Size(); ++j) {
389 for (Int32 k = 0; k < a.dim3Size(); ++k) {
390 a(i, j, k) = (i * 253) + (j * 27) + k;
400TEST(NumArray2, Layout)
402 std::cout <<
"TEST_NUMARRAY2 Layout\n";
406 ASSERT_EQ(a.totalNbElement(), (3 * 5));
407 _setNumArray2Values(a);
408 auto values = a.to1DSpan();
409 std::cout <<
"V=" << values <<
"\n";
410 UniqueArray<Real> ref_value = { 0, 1, 2, 3, 4, 253, 254, 255, 256, 257, 506, 507, 508, 509, 510 };
411 ASSERT_EQ(values.smallView(), ref_value.
view());
416 ASSERT_EQ(a.totalNbElement(), (3 * 5));
417 _setNumArray2Values(a);
418 auto values = a.to1DSpan();
419 std::cout <<
"V=" << values <<
"\n";
420 UniqueArray<Real> ref_value = { 0, 253, 506, 1, 254, 507, 2, 255, 508, 3, 256, 509, 4, 257, 510 };
421 ASSERT_EQ(values.smallView(), ref_value.
view());
428template <
typename NumArray3>
429void _checkRightLayoutDim3(NumArray3& a)
432 ASSERT_EQ(a.totalNbElement(), (2 * 3 * 5));
433 ASSERT_EQ(a.extent0(), 2);
434 ASSERT_EQ(a.extent1(), 3);
435 ASSERT_EQ(a.extent2(), 5);
436 _setNumArray3Values(a);
437 auto values = a.to1DSpan();
438 std::cout <<
"V=" << values <<
"\n";
440 0, 1, 2, 3, 4, 27, 28, 29, 30, 31, 54, 55, 56, 57, 58,
441 253, 254, 255, 256, 257, 280, 281, 282, 283, 284, 307, 308, 309, 310, 311
443 ASSERT_EQ(values.smallView(), ref_value.
view());
446template <
typename NumArray3>
447void _checkLeftLayoutDim3(NumArray3& a)
451 ASSERT_EQ(a.totalNbElement(), (2 * 3 * 5));
452 _setNumArray3Values(a);
453 auto values = a.to1DSpan();
454 std::cout <<
"V=" << values <<
"\n";
456 0, 253, 27, 280, 54, 307, 1, 254, 28, 281, 55, 308, 2, 255, 29,
457 282, 56, 309, 3, 256, 30, 283, 57, 310, 4, 257, 31, 284, 58, 311
459 ASSERT_EQ(values.smallView(), ref_value.
view());
462TEST(NumArray3, Layout)
464 std::cout <<
"TEST_NUMARRAY3 Layout\n";
468 std::cout <<
"TEST_NUMARRAY3 RightLayout 1\n";
469 _checkRightLayoutDim3(a);
473 std::cout <<
"TEST_NUMARRAY3 RightLayout 2\n";
474 _checkRightLayoutDim3(a);
478 std::cout <<
"TEST_NUMARRAY3 RightLayout 3\n";
479 _checkRightLayoutDim3(a);
483 std::cout <<
"TEST_NUMARRAY3 RightLayout 4\n";
484 _checkRightLayoutDim3(a);
488 std::cout <<
"TEST_NUMARRAY3 RightLayout 5\n";
489 _checkRightLayoutDim3(a);
494 std::cout <<
"TEST_NUMARRAY3 LeftLayout 1\n";
495 _checkLeftLayoutDim3(a);
499 std::cout <<
"TEST_NUMARRAY3 LeftLayout 2\n";
500 _checkLeftLayoutDim3(a);
504 std::cout <<
"TEST_NUMARRAY3 LeftLayout 3\n";
505 _checkLeftLayoutDim3(a);
509 std::cout <<
"TEST_NUMARRAY3 LeftLayout 4\n";
510 _checkLeftLayoutDim3(a);
514 std::cout <<
"TEST_NUMARRAY3 LeftLayout 5\n";
515 _checkLeftLayoutDim3(a);
526 a(2) =
Real2(0.0, 3.2);
528 ASSERT_EQ(a(3).y, 2.0);
533 const Real3 v(0.0, 3.2, 5.6);
536 ASSERT_EQ(a(4).y, 4.0);
542 const Real2 v0(1.2, 1.7);
548 ASSERT_EQ(a(4).y.x, 4.0);
550 ASSERT_EQ(a(3)(1), v0);
555 const Real3 v0(1.2, 3.4, 1.7);
562 ASSERT_EQ(a(4).y.z, 4.0);
564 ASSERT_EQ(a(3)(1), v0);
574 const char* values1_str =
"1 3 -2 \n -7 -5 12 \n 3 9 11\n";
575 NumArray<Int32, MDDim1> ref_value(9, { 1, 3, -2, -7, -5, 12, 3, 9, 11 });
576 std::istringstream istr1(values1_str);
579 ASSERT_EQ(int32_values.
extent0(), 9);
580 ASSERT_EQ(int32_values.
to1DSpan(), ref_value.to1DSpan());
583 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";
584 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 });
585 std::istringstream istr1(values1_str);
588 ASSERT_EQ(real_values.
extent0(), 10);
589 ASSERT_EQ(real_values.
to1DSpan(), ref_value.to1DSpan());
592namespace TestCopyNumArray
606 std::cout << w.to1DSpan() <<
"\n";
608 std::cout << b.a_.to1DSpan() <<
"\n";
614 auto tpq = Arcane::NumArray<Real, Arcane::MDDim1>(5);
616 std::cout << tpq.to1DSpan() <<
"\n";
624 using namespace TestCopyNumArray;
627 std::cout <<
"Val 4 = "
629 <<
" " << test4.a_.to1DSpan() <<
"\n";
631 std::cout <<
"Val 5 = " << &test5 <<
" " << test5.to1DSpan() <<
"\n";
632 ASSERT_EQ(test4.a_.to1DSpan(), test5.to1DSpan());
640 std::cout <<
"Test_SpanUsage";
649 a1.
copy(span_ua1,
nullptr);
653 ASSERT_EQ(span_ua1, md_a1.to1DSmallSpan());
656 ASSERT_EQ(const_span_ua1, md_a2.to1DSmallSpan());
659 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 multidimensional views.
__host__ __device__ MDSpan< DataType, RemovedFirstExtentsType, LayoutPolicy > slice(ExtentIndexType i) const
Returns a dimension (N-1) view starting from index element i.
constexpr ExtentIndexType extent0() const
Value of the first dimension.
constexpr ExtentIndexType extent1() const
Value of the second dimension.
constexpr DataType * ptrAt(ExtentIndexType i, ExtentIndexType j, ExtentIndexType k, ExtentIndexType l) const
Pointer to the value for element i,j,k,l.
Multi-dimensional arrays for numerical types accessible on accelerators.
Span< const DataType > to1DSpan() const
Constant 1D view on the instance.
constexpr ExtentIndexType extent2() const
Value of the third dimension.
void copy(SmallSpan< const DataType > rhs)
Copies the values from rhs into the instance.
constexpr ExtentIndexType extent1() const
Value of the second dimension.
constexpr SmallSpan< DataType > to1DSmallSpan()
1D view on the instance (only if rank == 1)
constexpr ExtentIndexType extent3() const
Value of the fourth dimension.
void fill(const DataType &v)
Fills the array values with v.
void resize(ExtentIndexType dim1_size)
Resizes the array without keeping current values.
constexpr ExtentIndexType extent0() const
Value of the first dimension.
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 --