Arcane  v3.14.10.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-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
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
25 String str1 = "TotoTiti";
26 String str2 = "Tata";
27 String str3 = "Hello";
28 String str4 = "MyStringToTest";
29
30 string_list.add(str1);
31 ASSERT_EQ(string_list.count(), 1);
32
33 string_list.add(str2);
34 ASSERT_EQ(string_list.count(), 2);
35
36 string_list.add(str3);
37 ASSERT_EQ(string_list.count(), 3);
38
39 ASSERT_TRUE(string_list.contains("Tata"));
40 ASSERT_FALSE(string_list.contains("NotTata"));
42 ASSERT_EQ(string_list[1], "Tata");
44
45 string_list.remove("Tata");
46 ASSERT_EQ(string_list.count(), 2);
49
50 string_list.clear();
51 ASSERT_EQ(string_list.count(), 0);
52
53 string_list.add(str4);
54 ASSERT_EQ(string_list.count(), 1);
55 string_list.add(str2);
56 ASSERT_EQ(string_list.count(), 2);
57 string_list.add(str1);
58 ASSERT_EQ(string_list.count(), 3);
59
60 ASSERT_TRUE(string_list.contains("Tata"));
61 ASSERT_FALSE(string_list.contains("NotTata"));
62 ASSERT_TRUE(string_list.contains(str2));
63 ASSERT_FALSE(string_list.contains(str3));
64 ASSERT_TRUE(string_list.contains(str1));
65}
66
67void
68_checkSmallArrayValues(Span<const Int32> view)
69{
70 for( Int64 i=0, n=view.size(); i<n; ++i )
71 ASSERT_EQ(view[i],i+1);
72}
73
74void
75_checkSmallArrayValues(Span<const Int32> view1,Span<const Int32> view2)
76{
77 Int64 n1 = view1.size();
78 Int64 n2 = view2.size();
80 for( Int64 i=0; i<n1; ++i )
81 ASSERT_EQ(view1[i],view2[i]);
82}
83
85{
86 {
87 constexpr int N = 934;
88 char buf[N];
90 ASSERT_EQ(b.guarantedAlignment(),0);
91 }
92 {
94 for( Int32 i=0; i<200; ++i )
95 buf1.add(i+1);
96 ASSERT_EQ(buf1.size(),200);
97 _checkSmallArrayValues(buf1);
98
99 buf1.resize(50);
100 buf1.shrink();
101 ASSERT_EQ(buf1.size(),50);
102 _checkSmallArrayValues(buf1);
103
104 for( Int32 i=0; i<200; ++i )
105 buf1.add(50+i+1);
106 ASSERT_EQ(buf1.size(),250);
107 _checkSmallArrayValues(buf1);
108 }
109 for( int z=1; z<10; ++z ) {
111 for(Int32 i=0, n=ref_buf.size(); i<n; ++i )
112 ref_buf[i] = (i+1)*2;
113
115 for(Int32 i=0, n=ref_buf2.size(); i<n; ++i )
116 ref_buf2[i] = (i+13)*3;
117
119 _checkSmallArrayValues(buf2,ref_buf);
121 _checkSmallArrayValues(buf3,ref_buf);
123 _checkSmallArrayValues(buf4,ref_buf);
125 _checkSmallArrayValues(buf5,ref_buf);
127 _checkSmallArrayValues(buf6,ref_buf);
128
129 buf2 = ref_buf2;
130 _checkSmallArrayValues(buf2,ref_buf2);
131 buf3 = ref_buf2.span();
132 _checkSmallArrayValues(buf3,ref_buf2);
133 buf4 = ref_buf2.constSpan();
134 _checkSmallArrayValues(buf4,ref_buf2);
135 buf5 = ref_buf2.view();
136 _checkSmallArrayValues(buf5,ref_buf2);
137 buf6 = ref_buf2.constView();
138 _checkSmallArrayValues(buf6,ref_buf2);
139 }
140 {
141 for( int z=1; z<10; ++z ) {
142 Int32 n = 5+(z*100);
144 ASSERT_EQ(buf3.size(),n);
145 for(Int32 i=0; i<n; ++i )
146 buf3[i] = (i*22)+1;
147 for(Int32 i=0; i<n; ++i )
148 ASSERT_EQ(buf3[i],((i*22)+1));
149 }
150 }
151 {
152 std::cout << "Test initializer_list 1\n";
153 SmallArray<Int32,20> buf = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25,
154 27, 29, 31, 33, 35, 37, 39, 41 };
155 Int32 n = 21;
156 ASSERT_EQ(buf.size(),n);
157 for(Int32 i=0; i<n; ++i )
158 ASSERT_EQ(buf[i],((i*2)+1));
159 }
160 {
161 std::cout << "Test initializer_list 2\n";
162 SmallArray<Int32,100> buf = { 1, 3, 5, 7, 9, 11 };
163 Int32 n = 6;
164 ASSERT_EQ(buf.size(),n);
165 for(Int32 i=0; i<n; ++i )
166 ASSERT_EQ(buf[i],((i*2)+1));
167 }
168 {
169 size_t s1 = 513;
171 ASSERT_EQ(buf1.size(),s1);
172
173 Int64 s2 = 217;
175 ASSERT_EQ(buf2.size(),s2);
176 }
177}
178
179/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
181
183{
184 {
186 ASSERT_EQ(empty_array.size(), 0);
187 ASSERT_EQ(empty_array.span().size(), 0);
188 }
189
190 {
191 static constexpr Int32 Size = 4;
194 ASSERT_EQ(array1.size(), Size);
195 ASSERT_EQ(array1.span().size(), Size);
196 ASSERT_EQ(array1.view().size(), Size);
197 ASSERT_EQ(const_array1.span().size(), Size);
198 ASSERT_EQ(const_array1.view().size(), Size);
199 for (Int32 i = 0; i < Size; ++i) {
200 ASSERT_EQ(array1[i], 0);
201 ASSERT_EQ(array1.span()[i], 0);
202 ASSERT_EQ(array1.view()[i], 0);
203 ASSERT_EQ(const_array1.view()[i], 0);
204 }
205
206 array1[0] = 3;
207 array1[1] = 5;
208 array1[2] = -1;
209 array1[3] = 8;
210 ASSERT_EQ(array1[0], 3);
211 ASSERT_EQ(array1[1], 5);
212 ASSERT_EQ(const_array1[1], 5);
213 std::cout << "V[2]=" << array1[2] << "\n";
214 {
215 auto iter = array1.begin();
216 ASSERT_EQ(*iter, 3);
217 ASSERT_EQ(*iter, *const_array1.begin());
218 ++iter;
219 ASSERT_EQ(*iter, 5);
220 ++iter;
221 ASSERT_EQ(*iter, -1);
222 ++iter;
223 ASSERT_EQ(*iter, 8);
224 ++iter;
225 ASSERT_EQ(iter, array1.end());
226 ASSERT_EQ(iter, const_array1.end());
227 }
228 }
229}
230
231/*---------------------------------------------------------------------------*/
232/*---------------------------------------------------------------------------*/
233
234namespace Arcane
235{
236template class List<String>;
237template class ListImplBase<String>;
238template class ListImplT<String>;
239template class Collection<String>;
240template class CollectionImplT<String>;
241
242template class SmallArray<Int32>;
243template class FixedArray<Int32,3>;
244template class FixedArray<double, 21>;
245}
246
247/*---------------------------------------------------------------------------*/
248/*---------------------------------------------------------------------------*/
#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:120
Tableau 1D de données avec buffer pré-alloué sur la pile.
Definition SmallArray.h:92
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 -*-