Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
TestCollections.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
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 _checkSmallArrayValues(Span<const Int32> view)
72{
73 for (Int64 i = 0, n = view.size(); i < n; ++i)
74 ASSERT_EQ(view[i], i + 1);
75}
76
77void _checkSmallArrayValues(Span<const Int32> view1, Span<const Int32> view2)
78{
79 Int64 n1 = view1.size();
80 Int64 n2 = view2.size();
81 ASSERT_EQ(n1, n2);
82 for (Int64 i = 0; i < n1; ++i)
83 ASSERT_EQ(view1[i], view2[i]);
84}
85
86TEST(Collections, SmallArray)
87{
88 {
89 constexpr int N = 934;
90 char buf[N];
92 ASSERT_EQ(b.guarantedAlignment({}), 0);
93 }
94 {
96 for (Int32 i = 0; i < 200; ++i)
97 buf1.add(i + 1);
98 ASSERT_EQ(buf1.size(), 200);
99 _checkSmallArrayValues(buf1);
100
101 buf1.resize(50);
102 buf1.shrink();
103 ASSERT_EQ(buf1.size(), 50);
104 _checkSmallArrayValues(buf1);
105
106 for (Int32 i = 0; i < 200; ++i)
107 buf1.add(50 + i + 1);
108 ASSERT_EQ(buf1.size(), 250);
109 _checkSmallArrayValues(buf1);
110 }
111 for (int z = 1; z < 10; ++z) {
112 UniqueArray<Int32> ref_buf(250 * z);
113 for (Int32 i = 0, n = ref_buf.size(); i < n; ++i)
114 ref_buf[i] = (i + 1) * 2;
115
116 UniqueArray<Int32> ref_buf2(100 * z * z);
117 for (Int32 i = 0, n = ref_buf2.size(); i < n; ++i)
118 ref_buf2[i] = (i + 13) * 3;
119
120 SmallArray<Int32, 1024> buf2(ref_buf);
121 _checkSmallArrayValues(buf2, ref_buf);
122 SmallArray<Int32, 1024> buf3(ref_buf.span());
123 _checkSmallArrayValues(buf3, ref_buf);
124 SmallArray<Int32, 1024> buf4(ref_buf.constSpan());
125 _checkSmallArrayValues(buf4, ref_buf);
126 SmallArray<Int32, 1024> buf5(ref_buf.view());
127 _checkSmallArrayValues(buf5, ref_buf);
128 SmallArray<Int32, 1024> buf6(ref_buf.constView());
129 _checkSmallArrayValues(buf6, ref_buf);
130
131 buf2 = ref_buf2;
132 _checkSmallArrayValues(buf2, ref_buf2);
133 buf3 = ref_buf2.span();
134 _checkSmallArrayValues(buf3, ref_buf2);
135 buf4 = ref_buf2.constSpan();
136 _checkSmallArrayValues(buf4, ref_buf2);
137 buf5 = ref_buf2.view();
138 _checkSmallArrayValues(buf5, ref_buf2);
139 buf6 = ref_buf2.constView();
140 _checkSmallArrayValues(buf6, ref_buf2);
141 }
142 {
143 for (int z = 1; z < 10; ++z) {
144 Int32 n = 5 + (z * 100);
145 SmallArray<Int32> buf3(n);
146 ASSERT_EQ(buf3.size(), n);
147 for (Int32 i = 0; i < n; ++i)
148 buf3[i] = (i * 22) + 1;
149 for (Int32 i = 0; i < n; ++i)
150 ASSERT_EQ(buf3[i], ((i * 22) + 1));
151 }
152 }
153 {
154 std::cout << "Test initializer_list 1\n";
155 SmallArray<Int32, 20> buf = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25,
156 27, 29, 31, 33, 35, 37, 39, 41 };
157 Int32 n = 21;
158 ASSERT_EQ(buf.size(), n);
159 for (Int32 i = 0; i < n; ++i)
160 ASSERT_EQ(buf[i], ((i * 2) + 1));
161 }
162 {
163 std::cout << "Test initializer_list 2\n";
164 SmallArray<Int32, 100> buf = { 1, 3, 5, 7, 9, 11 };
165 Int32 n = 6;
166 ASSERT_EQ(buf.size(), n);
167 for (Int32 i = 0; i < n; ++i)
168 ASSERT_EQ(buf[i], ((i * 2) + 1));
169 }
170 {
171 size_t s1 = 513;
172 SmallArray<Int32> buf1(s1);
173 ASSERT_EQ(buf1.size(), s1);
174
175 Int64 s2 = 217;
176 SmallArray<Int32> buf2(s2);
177 ASSERT_EQ(buf2.size(), s2);
178 }
179}
180
181/*---------------------------------------------------------------------------*/
182/*---------------------------------------------------------------------------*/
183
184TEST(Collections, FixedArray)
185{
186 static_assert(std::ranges::contiguous_range<FixedArray<Int32, 2>>);
187
188 {
189 FixedArray<Int32, 0> empty_array;
190 ASSERT_EQ(empty_array.size(), 0);
191 ASSERT_EQ(empty_array.span().size(), 0);
192 }
193
194 {
195 static constexpr Int32 Size = 4;
197 const FixedArray<Int32, Size>& const_array1(array1);
198 ASSERT_EQ(array1.size(), Size);
199 ASSERT_EQ(array1.span().size(), Size);
200 ASSERT_EQ(array1.view().size(), Size);
201 ASSERT_EQ(const_array1.span().size(), Size);
202 ASSERT_EQ(const_array1.view().size(), Size);
203 for (Int32 i = 0; i < Size; ++i) {
204 ASSERT_EQ(array1[i], 0);
205 ASSERT_EQ(array1.span()[i], 0);
206 ASSERT_EQ(array1.view()[i], 0);
207 ASSERT_EQ(const_array1.view()[i], 0);
208 }
209
210 array1[0] = 3;
211 array1[1] = 5;
212 array1[2] = -1;
213 array1[3] = 8;
214 ASSERT_EQ(array1[0], 3);
215 ASSERT_EQ(array1[1], 5);
216 ASSERT_EQ(const_array1[1], 5);
217 std::cout << "V[2]=" << array1[2] << "\n";
218 {
219 auto iter = array1.begin();
220 ASSERT_EQ(*iter, 3);
221 ASSERT_EQ(*iter, *const_array1.begin());
222 ++iter;
223 ASSERT_EQ(*iter, 5);
224 ++iter;
225 ASSERT_EQ(*iter, -1);
226 ++iter;
227 ASSERT_EQ(*iter, 8);
228 ++iter;
229 ASSERT_EQ(iter, array1.end());
230 ASSERT_EQ(iter, const_array1.end());
231 }
232 }
233 {
234 FixedArray<Int32, 2> v({ 1, 2 });
235 ASSERT_EQ(v[0], 1);
236 ASSERT_EQ(v[1], 2);
237 }
238 {
239 FixedArray<Int32, 2> v({ 3 });
240 ASSERT_EQ(v[0], 3);
241 ASSERT_EQ(v[1], 0);
242 }
243 {
245 a1.add(3);
246 a1.add(5);
248 a2.add(27);
249 a2.add(32);
250 a2.add(21);
251 FixedArray<UniqueArray<Int32>, 2> v({ a1, a2 });
252 ASSERT_EQ(v[0].size(), 2);
253 ASSERT_EQ(v[0][1], 5);
254 ASSERT_EQ(v[0][1], 5);
255 ASSERT_EQ(v[1].size(), 3);
256 ASSERT_EQ(v[1][0], 27);
257 ASSERT_EQ(v[1][1], 32);
258 ASSERT_EQ(v[1][2], 21);
259 FixedArray<UniqueArray<Int32>, 2> v2({ a1 });
260 v2 = { a2 };
261 ASSERT_EQ(v2[0].size(), 3);
262 ASSERT_EQ(v2[0][0], 27);
263 ASSERT_EQ(v2[0][1], 32);
264 ASSERT_EQ(v2[0][2], 21);
265 ASSERT_EQ(v2[1].size(), 0);
266 }
267}
268
269/*---------------------------------------------------------------------------*/
270/*---------------------------------------------------------------------------*/
271
272namespace Arcane
273{
274template class List<String>;
275template class ListImplBase<String>;
276template class ListImplT<String>;
277template class Collection<String>;
278template class CollectionImplT<String>;
279
280template class SmallArray<Int32>;
281template class FixedArray<Int32, 3>;
282template class FixedArray<double, 21>;
283
284template class MultiArray2<Int32>;
285template class UniqueMultiArray2<Int32>;
286template class SharedMultiArray2<Int32>;
287} // namespace Arcane
288
289/*---------------------------------------------------------------------------*/
290/*---------------------------------------------------------------------------*/
#define ASSERT_FALSE(condition)
Checks that condition is false.
Definition Assertion.h:140
#define ASSERT_TRUE(condition)
Checks that condition is true.
Definition Assertion.h:128
Integer size() const
Number of elements in the vector.
void shrink()
Reallocates to free unused memory.
void resize(Int64 s)
Changes the number of elements in the array to s.
void add(ConstReferenceType val)
Adds element val to the end of the array.
base class for implementation of a typed collection.
Base class for a strongly typed collection.
constexpr __host__ __device__ ArrayView< T > view()
Modifiable view of the array.
constexpr __host__ __device__ SmallSpan< T, NbElement > span()
Modifiable view of the array.
static constexpr Int32 size()
Number of elements in the array.
Implementation of a collection of elements in vector form.
Base class for multi-sized 2D arrays.
Definition MultiArray2.h:60
Multi-sized 2D array with reference semantics.
1D data array with pre-allocated stack buffer.
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:327
View of an array of elements of type T.
Definition Span.h:635
1D data vector with value semantics (STL style).
Multi-sized 2D array with value semantics.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
List< String > StringList
Unicode string list.
Definition UtilsTypes.h:509