Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
TestArray2.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2023 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
27_Add(Array<Real>& v,Integer new_size)
28{
29 v.resize(new_size);
30}
31}
32
33namespace
34{
35
36template <typename T>
37void _dumpArray2(std::ostream& o, const Array2<T>& a)
38{
39 for (Int32 i = 0; i < a.dim1Size(); ++i)
40 for (Int32 j = 0; j < a.dim2Size(); ++j) {
41 if (i != 0 || j != 0)
42 o << ' ';
43 o << "[" << i << "," << j << "]=\"" << a[i][j] << '"';
44 }
45}
46} // namespace
47
48/*---------------------------------------------------------------------------*/
49/*---------------------------------------------------------------------------*/
50
51template <typename T>
53{
54 public:
55
56 SharedArray2<T> m_test_array;
57 SharedArray2<T> m_test_array2;
58 SharedArray2<T> m_test_array3;
59};
60
61namespace
62{
63template <typename T>
64UniqueArray2<T> _generateSharedArray2(Int32 dim1_size, Int32 dim2_size)
65{
67 a.resize(dim1_size, dim2_size);
68 for (Int32 i = 0; i < a.dim1Size(); ++i)
69 for (Int32 j = 0; j < a.dim2Size(); ++j) {
70 a[i][j] = T{ i + j };
71 }
72 return a;
73}
74} // namespace
75
76/*---------------------------------------------------------------------------*/
77/*---------------------------------------------------------------------------*/
78
79template <typename T>
81{
82 public:
83
84 static void testMisc();
85};
86
87/*---------------------------------------------------------------------------*/
88/*---------------------------------------------------------------------------*/
89
90template <typename T>
93{
94 SharedArray2<T> sh_empty;
95 {
96 SharedArray2<T> sh_d;
97 sh_d.resize(2, 6);
98
99 SharedArray2<T> sh_b;
100 sh_b = _generateSharedArray2<T>(5, 3);
101 ASSERT_EQ(sh_b.dim1Size(), 5);
102 ASSERT_EQ(sh_b.dim2Size(), 3);
103 std::cout << "\nSH_B=";
104 _dumpArray2(std::cout, sh_b);
105 std::cout << "\n";
106
107 SharedArray2<T> sh_c = sh_b;
108 sh_d = sh_c;
109 std::cout << "\nSH_D=";
110 _dumpArray2(std::cout, sh_d);
111 std::cout << "\n";
112 _checkSameInfoArray2(sh_b, sh_c);
113 _checkSameInfoArray2(sh_b, sh_d);
114 }
115
116 {
117 SampleClass1<T> sc1;
118 for (int i = 0; i < 5; ++i) {
119 Int32 size1 = 7 - i;
120 Int32 size2 = 3 + i;
121 {
122 UniqueArray2<T> base_array = _generateSharedArray2<T>(size1, size2);
123 SharedArray2<T> x(base_array);
124 SharedArray2<T> x2(base_array.span());
125 SharedArray2<T> x3(base_array.constView());
126 _checkSameInfoArray2(x, base_array);
127 _checkSameInfoArray2(x2, base_array);
128 _checkSameInfoArray2(x3, base_array);
129 sc1.m_test_array = x;
130 _checkSameInfoArray2(x, sc1.m_test_array);
131 sc1.m_test_array2 = x.span();
132 _checkSameInfoArray2(x, sc1.m_test_array2);
133 sc1.m_test_array3 = x.constView();
134 _checkSameInfoArray2(x, sc1.m_test_array3);
135 }
136 ASSERT_EQ(sc1.m_test_array.dim1Size(), size1);
137 ASSERT_EQ(sc1.m_test_array.dim2Size(), size2);
138 if ((i % 2) == 0)
139 sc1.m_test_array = SharedArray2<T>{};
140 }
141 }
142}
143
144/*---------------------------------------------------------------------------*/
145/*---------------------------------------------------------------------------*/
146
147TEST(SharedArray2, Misc1)
148{
150}
151
152TEST(SharedArray2, Misc2)
153{
155}
156
157TEST(SharedArray2, Misc3)
158{
160}
161
162/*---------------------------------------------------------------------------*/
163/*---------------------------------------------------------------------------*/
164
165TEST(Array2, Misc)
166{
167 using namespace Arccore;
168 PrintableMemoryAllocator printable_allocator2;
169 IMemoryAllocator* allocator2 = &printable_allocator2;
170
172 sh_a.resize(3, 2);
173 ASSERT_EQ(sh_a.totalNbElement(), 6);
174 ASSERT_EQ(sh_a.dim1Size(), 3);
175 ASSERT_EQ(sh_a.dim2Size(), 2);
176
177 {
179 c.resize(3, 5);
180 Integer nb = 15;
181 c.reserve(nb * 2);
182 Int64 current_capacity = c.capacity();
183 ASSERT_EQ(current_capacity, (nb * 2)) << "Bad capacity (test 1)";
184 c.shrink(32);
185 ASSERT_EQ(c.capacity(), current_capacity) << "Bad capacity (test 2)";
186 c.shrink();
187 c.shrink_to_fit();
188 ASSERT_EQ(c.capacity(), c.totalNbElement()) << "Bad capacity (test 3)";
189 ASSERT_EQ(c[1][2], c(1, 2));
190#ifdef ARCCORE_HAS_MULTI_SUBSCRIPT
191 bool is_ok = c[2, 1] == c(2, 1);
192 ASSERT_TRUE(is_ok);
193#endif
194 }
195 {
197 c.resize(2, 1);
198 std::cout << "V1=" << c.to1DSpan() << "\n";
199 c[0][0] = 2;
200 c[1][0] = 3;
201 c.resize(2, 2);
202 std::cout << "V2=" << c.to1DSpan() << "\n";
203 ASSERT_EQ(c[0][0], 2);
204 ASSERT_EQ(c[1][0], 3);
205 ASSERT_EQ(c[0][1], 0);
206 ASSERT_EQ(c[1][1], 0);
208 d.resize(4, 5);
209 ASSERT_EQ(d.totalNbElement(), 20);
210 ASSERT_EQ(d.dim1Size(), 4);
211 ASSERT_EQ(d.dim2Size(), 5);
212
213 d = c;
214 ASSERT_EQ(d.totalNbElement(), c.totalNbElement());
215 ASSERT_EQ(d.dim1Size(), c.dim1Size());
216 ASSERT_EQ(d.dim2Size(), c.dim2Size());
217
218 UniqueArray2<Int32> e(allocator2);
219 ASSERT_EQ(e.allocator(), allocator2);
220 e.resize(7, 6);
221 ASSERT_EQ(e.totalNbElement(), 42);
222 ASSERT_EQ(e.dim1Size(), 7);
223 ASSERT_EQ(e.dim2Size(), 6);
224
225 {
226 e = d;
227 ASSERT_EQ(e.allocator(), d.allocator());
228 ASSERT_EQ(d.totalNbElement(), e.totalNbElement());
229 Int32 dim1_size = e.dim1Size();
230 Int32 dim2_size = e.dim2Size();
231 ASSERT_EQ(d.dim1Size(), dim1_size);
232 ASSERT_EQ(d.dim2Size(), dim2_size);
233 e.add(23);
234 ASSERT_EQ(e.dim1Size(), dim1_size + 1);
235 ASSERT_EQ(e.dim2Size(), dim2_size);
236 ASSERT_EQ(e[dim1_size][0], 23);
237 }
238 {
239 UniqueArray2<Int32> f(allocator2);
241 g.resize(4, 3);
242 g = f;
243 _checkSameInfoArray2(g, f);
244
246 _checkSameInfoArray2(h, f);
247
248 UniqueArray2<Int32> h2(sh_a);
249 _checkSameInfoArray2(h2, sh_a);
250
251 g = sh_a;
252 _checkSameInfoArray2(g, sh_a);
253 }
254 }
255 {
257 c.resizeNoInit(2, 1);
258 c[0][0] = 1;
259 c[1][0] = 2;
260 c.resizeNoInit(2, 2);
261 c[0][1] = 4;
262 c[1][1] = 5;
263 std::cout << "X1=" << c.to1DSpan() << "\n";
264 ASSERT_EQ(c[0][0], 1);
265 ASSERT_EQ(c[1][0], 2);
266 ASSERT_EQ(c[0][1], 4);
267 ASSERT_EQ(c[1][1], 5);
268 c.resize(3, 2);
269 std::cout << "X2=" << c.to1DSpan() << "\n";
270 ASSERT_EQ(c[0][0], 1);
271 ASSERT_EQ(c[1][0], 2);
272 ASSERT_EQ(c[0][1], 4);
273 ASSERT_EQ(c[1][1], 5);
274 ASSERT_EQ(c[2][0], 0);
275 ASSERT_EQ(c[2][1], 0);
276 c[2][0] = 8;
277 c[2][1] = 10;
278 c.resize(6, 5);
279 std::cout << "X3=" << c.to1DSpan() << "\n";
280 ASSERT_EQ(c[0][0], 1);
281 ASSERT_EQ(c[1][0], 2);
282 ASSERT_EQ(c[0][1], 4);
283 ASSERT_EQ(c[1][1], 5);
284 ASSERT_EQ(c[2][0], 8);
285 ASSERT_EQ(c[2][1], 10);
286 for (int i = 0; i < 4; ++i) {
287 ASSERT_EQ(c[i][2], 0);
288 ASSERT_EQ(c[i][3], 0);
289 ASSERT_EQ(c[i][4], 0);
290 }
291 for (int j = 0; j < 5; ++j) {
292 ASSERT_EQ(c[3][j], 0);
293 ASSERT_EQ(c[4][j], 0);
294 ASSERT_EQ(c[5][j], 0);
295 }
296 }
297}
298
299/*---------------------------------------------------------------------------*/
300/*---------------------------------------------------------------------------*/
301
302namespace Arccore
303{
304// Instancie explicitement les classes tableaux pour garantir
305// que toutes les méthodes fonctionnent
306template class UniqueArray2<IntSubClass>;
307template class SharedArray2<IntSubClass>;
308template class Array2<IntSubClass>;
309}
#define ASSERT_TRUE(condition)
Vérifie que condition est vrai.
Definition Assertion.h:126
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Classe représentant un tableau 2D classique.
Integer totalNbElement() const
Nombre total d'éléments (dim1Size()*dim2Size())
void resizeNoInit(Int64 new_size)
Redimensionne uniquement la première dimension en laissant la deuxième à l'identique.
void resize(Int64 new_size)
Redimensionne uniquement la première dimension en laissant la deuxième à l'identique.
void reserve(Int64 new_capacity)
Réserve de la mémoire pour new_capacity éléments.
Integer capacity() const
Capacité (nombre d'éléments alloués) du tableau.
Span< DataType > to1DSpan()
Vue du tableau sous forme de tableau 1D.
Classe de base des vecteurs 1D de données.
void resize(Int64 s)
Change le nombre d'éléments du tableau à s.
Allocateur mémoire via malloc/realloc/free avec impression listing.
Vecteur de données 2D partagées avec sémantique par référence.
Vecteur de données 2D avec sémantique par valeur (style STL).
Espace de nom de Arccore.
Definition ArcaneTypes.h:24
Int32 Integer
Type représentant un entier.