Arcane  v4.1.2.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
TestCollections.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 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
8#include <gtest/gtest.h>
9
10#include "arcane/utils/List.h"
11#include "arcane/utils/String.h"
12#include "arcane/utils/SmallArray.h"
13#include "arcane/utils/FixedArray.h"
14#include "arcane/utils/MultiArray2.h"
15
16#include <ranges>
17
18/*---------------------------------------------------------------------------*/
19/*---------------------------------------------------------------------------*/
20
21using namespace Arcane;
22
23TEST(Collections, Basic)
24{
25 std::cout << "TEST_Collection Basic\n";
26 std::cout << "STRUCT_ARRAY=" << sizeof(UniqueArray<Int32>) << "\n";
27
28 StringList string_list;
29 String str1 = "TotoTiti";
30 String str2 = "Tata";
31 String str3 = "Hello";
32 String str4 = "MyStringToTest";
33
34 string_list.add(str1);
35 ASSERT_EQ(string_list.count(), 1);
36
37 string_list.add(str2);
38 ASSERT_EQ(string_list.count(), 2);
39
40 string_list.add(str3);
41 ASSERT_EQ(string_list.count(), 3);
42
43 ASSERT_TRUE(string_list.contains("Tata"));
44 ASSERT_FALSE(string_list.contains("NotTata"));
45 ASSERT_EQ(string_list[0], str1);
46 ASSERT_EQ(string_list[1], "Tata");
47 ASSERT_EQ(string_list[2], str3);
48
49 string_list.remove("Tata");
50 ASSERT_EQ(string_list.count(), 2);
51 ASSERT_EQ(string_list[0], str1);
52 ASSERT_EQ(string_list[1], str3);
53
54 string_list.clear();
55 ASSERT_EQ(string_list.count(), 0);
56
57 string_list.add(str4);
58 ASSERT_EQ(string_list.count(), 1);
59 string_list.add(str2);
60 ASSERT_EQ(string_list.count(), 2);
61 string_list.add(str1);
62 ASSERT_EQ(string_list.count(), 3);
63
64 ASSERT_TRUE(string_list.contains("Tata"));
65 ASSERT_FALSE(string_list.contains("NotTata"));
66 ASSERT_TRUE(string_list.contains(str2));
67 ASSERT_FALSE(string_list.contains(str3));
68 ASSERT_TRUE(string_list.contains(str1));
69}
70
71void
72_checkSmallArrayValues(Span<const Int32> view)
73{
74 for( Int64 i=0, n=view.size(); i<n; ++i )
75 ASSERT_EQ(view[i],i+1);
76}
77
78void
79_checkSmallArrayValues(Span<const Int32> view1,Span<const Int32> view2)
80{
81 Int64 n1 = view1.size();
82 Int64 n2 = view2.size();
83 ASSERT_EQ(n1,n2);
84 for( Int64 i=0; i<n1; ++i )
85 ASSERT_EQ(view1[i],view2[i]);
86}
87
88TEST(Collections,SmallArray)
89{
90 {
91 constexpr int N = 934;
92 char buf[N];
94 ASSERT_EQ(b.guarantedAlignment({}),0);
95 }
96 {
98 for( Int32 i=0; i<200; ++i )
99 buf1.add(i+1);
100 ASSERT_EQ(buf1.size(),200);
101 _checkSmallArrayValues(buf1);
102
103 buf1.resize(50);
104 buf1.shrink();
105 ASSERT_EQ(buf1.size(),50);
106 _checkSmallArrayValues(buf1);
107
108 for( Int32 i=0; i<200; ++i )
109 buf1.add(50+i+1);
110 ASSERT_EQ(buf1.size(),250);
111 _checkSmallArrayValues(buf1);
112 }
113 for( int z=1; z<10; ++z ) {
114 UniqueArray<Int32> ref_buf(250*z);
115 for(Int32 i=0, n=ref_buf.size(); i<n; ++i )
116 ref_buf[i] = (i+1)*2;
117
118 UniqueArray<Int32> ref_buf2(100*z*z);
119 for(Int32 i=0, n=ref_buf2.size(); i<n; ++i )
120 ref_buf2[i] = (i+13)*3;
121
122 SmallArray<Int32,1024> buf2(ref_buf);
123 _checkSmallArrayValues(buf2,ref_buf);
124 SmallArray<Int32,1024> buf3(ref_buf.span());
125 _checkSmallArrayValues(buf3,ref_buf);
126 SmallArray<Int32,1024> buf4(ref_buf.constSpan());
127 _checkSmallArrayValues(buf4,ref_buf);
128 SmallArray<Int32,1024> buf5(ref_buf.view());
129 _checkSmallArrayValues(buf5,ref_buf);
130 SmallArray<Int32,1024> buf6(ref_buf.constView());
131 _checkSmallArrayValues(buf6,ref_buf);
132
133 buf2 = ref_buf2;
134 _checkSmallArrayValues(buf2,ref_buf2);
135 buf3 = ref_buf2.span();
136 _checkSmallArrayValues(buf3,ref_buf2);
137 buf4 = ref_buf2.constSpan();
138 _checkSmallArrayValues(buf4,ref_buf2);
139 buf5 = ref_buf2.view();
140 _checkSmallArrayValues(buf5,ref_buf2);
141 buf6 = ref_buf2.constView();
142 _checkSmallArrayValues(buf6,ref_buf2);
143 }
144 {
145 for( int z=1; z<10; ++z ) {
146 Int32 n = 5+(z*100);
147 SmallArray<Int32> buf3(n);
148 ASSERT_EQ(buf3.size(),n);
149 for(Int32 i=0; i<n; ++i )
150 buf3[i] = (i*22)+1;
151 for(Int32 i=0; i<n; ++i )
152 ASSERT_EQ(buf3[i],((i*22)+1));
153 }
154 }
155 {
156 std::cout << "Test initializer_list 1\n";
157 SmallArray<Int32,20> buf = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25,
158 27, 29, 31, 33, 35, 37, 39, 41 };
159 Int32 n = 21;
160 ASSERT_EQ(buf.size(),n);
161 for(Int32 i=0; i<n; ++i )
162 ASSERT_EQ(buf[i],((i*2)+1));
163 }
164 {
165 std::cout << "Test initializer_list 2\n";
166 SmallArray<Int32,100> buf = { 1, 3, 5, 7, 9, 11 };
167 Int32 n = 6;
168 ASSERT_EQ(buf.size(),n);
169 for(Int32 i=0; i<n; ++i )
170 ASSERT_EQ(buf[i],((i*2)+1));
171 }
172 {
173 size_t s1 = 513;
174 SmallArray<Int32> buf1(s1);
175 ASSERT_EQ(buf1.size(),s1);
176
177 Int64 s2 = 217;
178 SmallArray<Int32> buf2(s2);
179 ASSERT_EQ(buf2.size(),s2);
180 }
181}
182
183/*---------------------------------------------------------------------------*/
184/*---------------------------------------------------------------------------*/
185
186TEST(Collections, FixedArray)
187{
188 static_assert(std::ranges::contiguous_range<FixedArray<Int32, 2>>);
189
190 {
191 FixedArray<Int32, 0> empty_array;
192 ASSERT_EQ(empty_array.size(), 0);
193 ASSERT_EQ(empty_array.span().size(), 0);
194 }
195
196 {
197 static constexpr Int32 Size = 4;
199 const FixedArray<Int32, Size>& const_array1(array1);
200 ASSERT_EQ(array1.size(), Size);
201 ASSERT_EQ(array1.span().size(), Size);
202 ASSERT_EQ(array1.view().size(), Size);
203 ASSERT_EQ(const_array1.span().size(), Size);
204 ASSERT_EQ(const_array1.view().size(), Size);
205 for (Int32 i = 0; i < Size; ++i) {
206 ASSERT_EQ(array1[i], 0);
207 ASSERT_EQ(array1.span()[i], 0);
208 ASSERT_EQ(array1.view()[i], 0);
209 ASSERT_EQ(const_array1.view()[i], 0);
210 }
211
212 array1[0] = 3;
213 array1[1] = 5;
214 array1[2] = -1;
215 array1[3] = 8;
216 ASSERT_EQ(array1[0], 3);
217 ASSERT_EQ(array1[1], 5);
218 ASSERT_EQ(const_array1[1], 5);
219 std::cout << "V[2]=" << array1[2] << "\n";
220 {
221 auto iter = array1.begin();
222 ASSERT_EQ(*iter, 3);
223 ASSERT_EQ(*iter, *const_array1.begin());
224 ++iter;
225 ASSERT_EQ(*iter, 5);
226 ++iter;
227 ASSERT_EQ(*iter, -1);
228 ++iter;
229 ASSERT_EQ(*iter, 8);
230 ++iter;
231 ASSERT_EQ(iter, array1.end());
232 ASSERT_EQ(iter, const_array1.end());
233 }
234 }
235 {
236 FixedArray<Int32, 2> v({ 1, 2 });
237 ASSERT_EQ(v[0], 1);
238 ASSERT_EQ(v[1], 2);
239 }
240 {
241 FixedArray<Int32, 2> v({ 3 });
242 ASSERT_EQ(v[0], 3);
243 ASSERT_EQ(v[1], 0);
244 }
245 {
247 a1.add(3);
248 a1.add(5);
250 a2.add(27);
251 a2.add(32);
252 a2.add(21);
253 FixedArray<UniqueArray<Int32>, 2> v({ a1, a2 });
254 ASSERT_EQ(v[0].size(), 2);
255 ASSERT_EQ(v[0][1], 5);
256 ASSERT_EQ(v[0][1], 5);
257 ASSERT_EQ(v[1].size(), 3);
258 ASSERT_EQ(v[1][0], 27);
259 ASSERT_EQ(v[1][1], 32);
260 ASSERT_EQ(v[1][2], 21);
261 FixedArray<UniqueArray<Int32>, 2> v2({ a1 });
262 v2 = { a2 };
263 ASSERT_EQ(v2[0].size(), 3);
264 ASSERT_EQ(v2[0][0], 27);
265 ASSERT_EQ(v2[0][1], 32);
266 ASSERT_EQ(v2[0][2], 21);
267 ASSERT_EQ(v2[1].size(), 0);
268 }
269}
270
271/*---------------------------------------------------------------------------*/
272/*---------------------------------------------------------------------------*/
273
274namespace Arcane
275{
276template class List<String>;
277template class ListImplBase<String>;
278template class ListImplT<String>;
279template class Collection<String>;
280template class CollectionImplT<String>;
281
282template class SmallArray<Int32>;
283template class FixedArray<Int32,3>;
284template class FixedArray<double, 21>;
285
286template class MultiArray2<Int32>;
287template class UniqueMultiArray2<Int32>;
288template class SharedMultiArray2<Int32>;
289}
290
291/*---------------------------------------------------------------------------*/
292/*---------------------------------------------------------------------------*/
#define ASSERT_FALSE(condition)
Vérifie que condition est faux.
Definition Assertion.h:137
#define ASSERT_TRUE(condition)
Vérifie que condition est vrai.
Definition Assertion.h:125
Integer size() const
Nombre d'éléments du vecteur.
void shrink()
Réalloue pour libérer la mémoire non utilisée.
void resize(Int64 s)
Change le nombre d'éléments du tableau à s.
void add(ConstReferenceType val)
Ajoute l'élément val à la fin du tableau.
classe de base d'implémentation d'une collection typée.
Classe de base d'une collection fortement typée.
constexpr __host__ __device__ ArrayView< T > view()
Vue modifiable sur le tableau.
constexpr __host__ __device__ SmallSpan< T, NbElement > span()
Vue modifiable sur le tableau.
static constexpr Int32 size()
Nombre d'éléments tu tableau.
Tableau avec allocateur virtuel.
Implémentation d'une collection d'éléments sous forme de vecteur.
Classe de base des tableau 2D à taille multiple.
Definition MultiArray2.h:61
Tableau 2D à taille multiple avec sémantique par référence.
Tableau 1D de données avec buffer pré-alloué sur la pile.
constexpr __host__ __device__ SizeType size() const noexcept
Retourne la taille du tableau.
Definition Span.h:325
Vue d'un tableau d'éléments de type T.
Definition Span.h:633
Chaîne de caractères unicode.
Vecteur 1D de données avec sémantique par valeur (style STL).
Tableau 2D à taille multiple avec sémantique par valeur.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
List< String > StringList
Tableau de chaînes de caractères unicode.
Definition UtilsTypes.h:509