Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
TestArray2.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/collections/Array.h"
10#include "arccore/collections/Array2.h"
11#include "arccore/collections/IMemoryAllocator.h"
12
13#include "arccore/base/FatalErrorException.h"
14#include "arccore/base/Iterator.h"
15
16#include "TestArrayCommon.h"
17
18using namespace Arccore;
19using namespace TestArccore;
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace
25{
26void _Add(Array<Real>& v, Integer new_size)
27{
28 v.resize(new_size);
29}
30} // namespace
31
32namespace
33{
34
35template <typename T>
36void _dumpArray2(std::ostream& o, const Array2<T>& a)
37{
38 for (Int32 i = 0; i < a.dim1Size(); ++i)
39 for (Int32 j = 0; j < a.dim2Size(); ++j) {
40 if (i != 0 || j != 0)
41 o << ' ';
42 o << "[" << i << "," << j << "]=\"" << a[i][j] << '"';
43 }
44}
45} // namespace
46
47/*---------------------------------------------------------------------------*/
48/*---------------------------------------------------------------------------*/
49
50template <typename T>
52{
53 public:
54
55 SharedArray2<T> m_test_array;
56 SharedArray2<T> m_test_array2;
57 SharedArray2<T> m_test_array3;
58};
59
60namespace
61{
62template <typename T>
63UniqueArray2<T> _generateSharedArray2(Int32 dim1_size, Int32 dim2_size)
64{
66 a.resize(dim1_size, dim2_size);
67 for (Int32 i = 0; i < a.dim1Size(); ++i)
68 for (Int32 j = 0; j < a.dim2Size(); ++j) {
69 a[i][j] = T{ i + j };
70 }
71 return a;
72}
73} // namespace
74
75/*---------------------------------------------------------------------------*/
76/*---------------------------------------------------------------------------*/
77
78template <typename T>
80{
81 public:
82
83 static void testMisc();
84};
85
86/*---------------------------------------------------------------------------*/
87/*---------------------------------------------------------------------------*/
88
89template <typename T>
92{
93 SharedArray2<T> sh_empty;
94 {
95 SharedArray2<T> sh_d;
96 sh_d.resize(2, 6);
97
98 SharedArray2<T> sh_b;
99 sh_b = _generateSharedArray2<T>(5, 3);
100 ASSERT_EQ(sh_b.dim1Size(), 5);
101 ASSERT_EQ(sh_b.dim2Size(), 3);
102 std::cout << "\nSH_B=";
103 _dumpArray2(std::cout, sh_b);
104 std::cout << "\n";
105
106 SharedArray2<T> sh_c = sh_b;
107 sh_d = sh_c;
108 std::cout << "\nSH_D=";
109 _dumpArray2(std::cout, sh_d);
110 std::cout << "\n";
111 _checkSameInfoArray2(sh_b, sh_c);
112 _checkSameInfoArray2(sh_b, sh_d);
113 }
114
115 {
116 SampleClass1<T> sc1;
117 for (int i = 0; i < 5; ++i) {
118 Int32 size1 = 7 - i;
119 Int32 size2 = 3 + i;
120 {
121 UniqueArray2<T> base_array = _generateSharedArray2<T>(size1, size2);
122 SharedArray2<T> x(base_array);
123 SharedArray2<T> x2(base_array.span());
124 SharedArray2<T> x3(base_array.constView());
125 _checkSameInfoArray2(x, base_array);
126 _checkSameInfoArray2(x2, base_array);
127 _checkSameInfoArray2(x3, base_array);
128 sc1.m_test_array = x;
129 _checkSameInfoArray2(x, sc1.m_test_array);
130 sc1.m_test_array2 = x.span();
131 _checkSameInfoArray2(x, sc1.m_test_array2);
132 sc1.m_test_array3 = x.constView();
133 _checkSameInfoArray2(x, sc1.m_test_array3);
134 }
135 ASSERT_EQ(sc1.m_test_array.dim1Size(), size1);
136 ASSERT_EQ(sc1.m_test_array.dim2Size(), size2);
137 if ((i % 2) == 0)
138 sc1.m_test_array = SharedArray2<T>{};
139 }
140 }
141}
142
143/*---------------------------------------------------------------------------*/
144/*---------------------------------------------------------------------------*/
145
146TEST(SharedArray2, Misc1)
147{
148 TestSharedArray2<Int32>::testMisc();
149}
150
151TEST(SharedArray2, Misc2)
152{
153 TestSharedArray2<IntSubClass>::testMisc();
154}
155
156TEST(SharedArray2, Misc3)
157{
158 TestSharedArray2<IntSubClassNoPod>::testMisc();
159}
160
161/*---------------------------------------------------------------------------*/
162/*---------------------------------------------------------------------------*/
163
164TEST(Array2, Misc)
165{
166 using namespace Arccore;
167 PrintableMemoryAllocator printable_allocator2;
168 IMemoryAllocator* allocator2 = &printable_allocator2;
169
171 sh_a.resize(3, 2);
172 ASSERT_EQ(sh_a.totalNbElement(), 6);
173 ASSERT_EQ(sh_a.dim1Size(), 3);
174 ASSERT_EQ(sh_a.dim2Size(), 2);
175
176 {
178 c.resize(3, 5);
179 Integer nb = 15;
180 c.reserve(nb * 2);
181 Int64 current_capacity = c.capacity();
182 ASSERT_EQ(current_capacity, (nb * 2)) << "Bad capacity (test 1)";
183 c.shrink(32);
184 ASSERT_EQ(c.capacity(), current_capacity) << "Bad capacity (test 2)";
185 c.shrink();
186 c.shrink_to_fit();
187 ASSERT_EQ(c.capacity(), c.totalNbElement()) << "Bad capacity (test 3)";
188 ASSERT_EQ(c[1][2], c(1, 2));
189#ifdef ARCCORE_HAS_MULTI_SUBSCRIPT
190 bool is_ok = c[2, 1] == c(2, 1);
191 ASSERT_TRUE(is_ok);
192#endif
193 }
194 {
196 c.resize(2, 1);
197 std::cout << "V1=" << c.to1DSpan() << "\n";
198 c[0][0] = 2;
199 c[1][0] = 3;
200 c.resize(2, 2);
201 std::cout << "V2=" << c.to1DSpan() << "\n";
202 ASSERT_EQ(c[0][0], 2);
203 ASSERT_EQ(c[1][0], 3);
204 ASSERT_EQ(c[0][1], 0);
205 ASSERT_EQ(c[1][1], 0);
207 d.resize(4, 5);
208 ASSERT_EQ(d.totalNbElement(), 20);
209 ASSERT_EQ(d.dim1Size(), 4);
210 ASSERT_EQ(d.dim2Size(), 5);
211
212 d = c;
213 ASSERT_EQ(d.totalNbElement(), c.totalNbElement());
214 ASSERT_EQ(d.dim1Size(), c.dim1Size());
215 ASSERT_EQ(d.dim2Size(), c.dim2Size());
216
217 UniqueArray2<Int32> e(allocator2);
218 ASSERT_EQ(e.allocator(), allocator2);
219 e.resize(7, 6);
220 ASSERT_EQ(e.totalNbElement(), 42);
221 ASSERT_EQ(e.dim1Size(), 7);
222 ASSERT_EQ(e.dim2Size(), 6);
223
224 {
225 e = d;
226 ASSERT_EQ(e.allocator(), d.allocator());
227 ASSERT_EQ(d.totalNbElement(), e.totalNbElement());
228 Int32 dim1_size = e.dim1Size();
229 Int32 dim2_size = e.dim2Size();
230 ASSERT_EQ(d.dim1Size(), dim1_size);
231 ASSERT_EQ(d.dim2Size(), dim2_size);
232 e.add(23);
233 ASSERT_EQ(e.dim1Size(), dim1_size + 1);
234 ASSERT_EQ(e.dim2Size(), dim2_size);
235 ASSERT_EQ(e[dim1_size][0], 23);
236 }
237 {
238 UniqueArray2<Int32> f(allocator2);
240 g.resize(4, 3);
241 g = f;
242 _checkSameInfoArray2(g, f);
243
245 _checkSameInfoArray2(h, f);
246
247 UniqueArray2<Int32> h2(sh_a);
248 _checkSameInfoArray2(h2, sh_a);
249
250 g = sh_a;
251 _checkSameInfoArray2(g, sh_a);
252 }
253 }
254 {
256 c.resizeNoInit(2, 1);
257 c[0][0] = 1;
258 c[1][0] = 2;
259 c.resizeNoInit(2, 2);
260 c[0][1] = 4;
261 c[1][1] = 5;
262 std::cout << "X1=" << c.to1DSpan() << "\n";
263 ASSERT_EQ(c[0][0], 1);
264 ASSERT_EQ(c[1][0], 2);
265 ASSERT_EQ(c[0][1], 4);
266 ASSERT_EQ(c[1][1], 5);
267 c.resize(3, 2);
268 std::cout << "X2=" << c.to1DSpan() << "\n";
269 ASSERT_EQ(c[0][0], 1);
270 ASSERT_EQ(c[1][0], 2);
271 ASSERT_EQ(c[0][1], 4);
272 ASSERT_EQ(c[1][1], 5);
273 ASSERT_EQ(c[2][0], 0);
274 ASSERT_EQ(c[2][1], 0);
275 c[2][0] = 8;
276 c[2][1] = 10;
277 c.resize(6, 5);
278 std::cout << "X3=" << c.to1DSpan() << "\n";
279 ASSERT_EQ(c[0][0], 1);
280 ASSERT_EQ(c[1][0], 2);
281 ASSERT_EQ(c[0][1], 4);
282 ASSERT_EQ(c[1][1], 5);
283 ASSERT_EQ(c[2][0], 8);
284 ASSERT_EQ(c[2][1], 10);
285 for (int i = 0; i < 4; ++i) {
286 ASSERT_EQ(c[i][2], 0);
287 ASSERT_EQ(c[i][3], 0);
288 ASSERT_EQ(c[i][4], 0);
289 }
290 for (int j = 0; j < 5; ++j) {
291 ASSERT_EQ(c[3][j], 0);
292 ASSERT_EQ(c[4][j], 0);
293 ASSERT_EQ(c[5][j], 0);
294 }
295 }
296}
297
298/*---------------------------------------------------------------------------*/
299/*---------------------------------------------------------------------------*/
300
301namespace Arcane
302{
303// Explicitly instantiate the array classes to ensure
304// that all methods work
305template class UniqueArray2<IntSubClass>;
306template class SharedArray2<IntSubClass>;
307template class Array2<IntSubClass>;
308} // namespace Arcane
#define ASSERT_TRUE(condition)
Checks that condition is true.
Definition Assertion.h:128
Class representing a classic 2D array.
void reserve(Int64 new_capacity)
Reserves memory for new_capacity elements.
Integer capacity() const
Capacity (number of allocated elements) of the array.
void resizeNoInit(Int64 new_size)
Resizes only the first dimension, leaving the second dimension unchanged.
Span< DataType > to1DSpan()
View of the array as a 1D array.
void resize(Int64 new_size)
Resizes only the first dimension, leaving the second dimension unchanged.
Integer totalNbElement() const
Total number of elements (dim1Size()*dim2Size()).
Base class for 1D data vectors.
void resize(Int64 s)
Changes the number of elements in the array to s.
Memory allocator via malloc/realloc/free with listing output.
Shared 2D data vector with reference semantics.
2D data vector with value semantics (STL style).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
Namespace of Arccore.