Arcane  v3.15.0.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
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18using namespace Arcane;
19
20TEST(Collections, Basic)
21{
22 std::cout << "TEST_Collection Basic\n";
23 std::cout << "STRUCT_ARRAY=" << sizeof(UniqueArray<Int32>) << "\n";
24
26 String str1 = "TotoTiti";
27 String str2 = "Tata";
28 String str3 = "Hello";
29 String str4 = "MyStringToTest";
30
31 string_list.add(str1);
32 ASSERT_EQ(string_list.count(), 1);
33
34 string_list.add(str2);
35 ASSERT_EQ(string_list.count(), 2);
36
37 string_list.add(str3);
38 ASSERT_EQ(string_list.count(), 3);
39
40 ASSERT_TRUE(string_list.contains("Tata"));
41 ASSERT_FALSE(string_list.contains("NotTata"));
43 ASSERT_EQ(string_list[1], "Tata");
45
46 string_list.remove("Tata");
47 ASSERT_EQ(string_list.count(), 2);
50
51 string_list.clear();
52 ASSERT_EQ(string_list.count(), 0);
53
54 string_list.add(str4);
55 ASSERT_EQ(string_list.count(), 1);
56 string_list.add(str2);
57 ASSERT_EQ(string_list.count(), 2);
58 string_list.add(str1);
59 ASSERT_EQ(string_list.count(), 3);
60
61 ASSERT_TRUE(string_list.contains("Tata"));
62 ASSERT_FALSE(string_list.contains("NotTata"));
63 ASSERT_TRUE(string_list.contains(str2));
64 ASSERT_FALSE(string_list.contains(str3));
65 ASSERT_TRUE(string_list.contains(str1));
66}
67
68void
69_checkSmallArrayValues(Span<const Int32> view)
70{
71 for( Int64 i=0, n=view.size(); i<n; ++i )
72 ASSERT_EQ(view[i],i+1);
73}
74
75void
76_checkSmallArrayValues(Span<const Int32> view1,Span<const Int32> view2)
77{
78 Int64 n1 = view1.size();
79 Int64 n2 = view2.size();
81 for( Int64 i=0; i<n1; ++i )
82 ASSERT_EQ(view1[i],view2[i]);
83}
84
86{
87 {
88 constexpr int N = 934;
89 char buf[N];
91 ASSERT_EQ(b.guarantedAlignment({}),0);
92 }
93 {
95 for( Int32 i=0; i<200; ++i )
96 buf1.add(i+1);
97 ASSERT_EQ(buf1.size(),200);
98 _checkSmallArrayValues(buf1);
99
100 buf1.resize(50);
101 buf1.shrink();
102 ASSERT_EQ(buf1.size(),50);
103 _checkSmallArrayValues(buf1);
104
105 for( Int32 i=0; i<200; ++i )
106 buf1.add(50+i+1);
107 ASSERT_EQ(buf1.size(),250);
108 _checkSmallArrayValues(buf1);
109 }
110 for( int z=1; z<10; ++z ) {
112 for(Int32 i=0, n=ref_buf.size(); i<n; ++i )
113 ref_buf[i] = (i+1)*2;
114
116 for(Int32 i=0, n=ref_buf2.size(); i<n; ++i )
117 ref_buf2[i] = (i+13)*3;
118
120 _checkSmallArrayValues(buf2,ref_buf);
122 _checkSmallArrayValues(buf3,ref_buf);
124 _checkSmallArrayValues(buf4,ref_buf);
126 _checkSmallArrayValues(buf5,ref_buf);
128 _checkSmallArrayValues(buf6,ref_buf);
129
130 buf2 = ref_buf2;
131 _checkSmallArrayValues(buf2,ref_buf2);
132 buf3 = ref_buf2.span();
133 _checkSmallArrayValues(buf3,ref_buf2);
134 buf4 = ref_buf2.constSpan();
135 _checkSmallArrayValues(buf4,ref_buf2);
136 buf5 = ref_buf2.view();
137 _checkSmallArrayValues(buf5,ref_buf2);
138 buf6 = ref_buf2.constView();
139 _checkSmallArrayValues(buf6,ref_buf2);
140 }
141 {
142 for( int z=1; z<10; ++z ) {
143 Int32 n = 5+(z*100);
145 ASSERT_EQ(buf3.size(),n);
146 for(Int32 i=0; i<n; ++i )
147 buf3[i] = (i*22)+1;
148 for(Int32 i=0; i<n; ++i )
149 ASSERT_EQ(buf3[i],((i*22)+1));
150 }
151 }
152 {
153 std::cout << "Test initializer_list 1\n";
154 SmallArray<Int32,20> buf = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25,
155 27, 29, 31, 33, 35, 37, 39, 41 };
156 Int32 n = 21;
157 ASSERT_EQ(buf.size(),n);
158 for(Int32 i=0; i<n; ++i )
159 ASSERT_EQ(buf[i],((i*2)+1));
160 }
161 {
162 std::cout << "Test initializer_list 2\n";
163 SmallArray<Int32,100> buf = { 1, 3, 5, 7, 9, 11 };
164 Int32 n = 6;
165 ASSERT_EQ(buf.size(),n);
166 for(Int32 i=0; i<n; ++i )
167 ASSERT_EQ(buf[i],((i*2)+1));
168 }
169 {
170 size_t s1 = 513;
172 ASSERT_EQ(buf1.size(),s1);
173
174 Int64 s2 = 217;
176 ASSERT_EQ(buf2.size(),s2);
177 }
178}
179
180/*---------------------------------------------------------------------------*/
181/*---------------------------------------------------------------------------*/
182
184{
185 {
187 ASSERT_EQ(empty_array.size(), 0);
188 ASSERT_EQ(empty_array.span().size(), 0);
189 }
190
191 {
192 static constexpr Int32 Size = 4;
195 ASSERT_EQ(array1.size(), Size);
196 ASSERT_EQ(array1.span().size(), Size);
197 ASSERT_EQ(array1.view().size(), Size);
198 ASSERT_EQ(const_array1.span().size(), Size);
199 ASSERT_EQ(const_array1.view().size(), Size);
200 for (Int32 i = 0; i < Size; ++i) {
201 ASSERT_EQ(array1[i], 0);
202 ASSERT_EQ(array1.span()[i], 0);
203 ASSERT_EQ(array1.view()[i], 0);
204 ASSERT_EQ(const_array1.view()[i], 0);
205 }
206
207 array1[0] = 3;
208 array1[1] = 5;
209 array1[2] = -1;
210 array1[3] = 8;
211 ASSERT_EQ(array1[0], 3);
212 ASSERT_EQ(array1[1], 5);
213 ASSERT_EQ(const_array1[1], 5);
214 std::cout << "V[2]=" << array1[2] << "\n";
215 {
216 auto iter = array1.begin();
217 ASSERT_EQ(*iter, 3);
218 ASSERT_EQ(*iter, *const_array1.begin());
219 ++iter;
220 ASSERT_EQ(*iter, 5);
221 ++iter;
222 ASSERT_EQ(*iter, -1);
223 ++iter;
224 ASSERT_EQ(*iter, 8);
225 ++iter;
226 ASSERT_EQ(iter, array1.end());
227 ASSERT_EQ(iter, const_array1.end());
228 }
229 }
230}
231
232/*---------------------------------------------------------------------------*/
233/*---------------------------------------------------------------------------*/
234
235namespace Arcane
236{
237template class List<String>;
238template class ListImplBase<String>;
239template class ListImplT<String>;
240template class Collection<String>;
241template class CollectionImplT<String>;
242
243template class SmallArray<Int32>;
244template class FixedArray<Int32,3>;
245template class FixedArray<double, 21>;
246}
247
248/*---------------------------------------------------------------------------*/
249/*---------------------------------------------------------------------------*/
#define ASSERT_FALSE(condition)
Vérifie que condition est faux.
Definition Assertion.h:138
#define ASSERT_TRUE(condition)
Vérifie que condition est vrai.
Definition Assertion.h:126
Tableau 1D de taille fixe.
Definition FixedArray.h:45
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:149
Tableau 1D de données avec buffer pré-alloué sur la pile.
Definition SmallArray.h:89
Allocateur avec buffer pré-alloué.
Definition SmallArray.h:40
Chaîne de caractères unicode.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-