Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
TestArray.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 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/IMemoryAllocator.h"
11
12#include "arccore/base/FatalErrorException.h"
13#include "arccore/base/Iterator.h"
14
15#include "TestArrayCommon.h"
16
17using namespace Arccore;
18using namespace TestArccore;
19
20namespace
21{
22void _testArraySwap(bool use_own_swap)
23{
24 std::cout << "** TestArraySwap is_own=" << use_own_swap << "\n";
25
27 IntSubClass* x1 = c1.unguardedBasePointer();
28 std::cout << "** C1_this = " << &c1 << "\n";
29 std::cout << "** C1_BASE = " << x1 << "\n";
31 IntSubClass* x2 = c2.unguardedBasePointer();
32 std::cout << "** C2_this = " << &c2 << "\n";
33 std::cout << "** C2_BASE = " << x2 << "\n";
34
35 if (use_own_swap) {
36 swap(c1, c2);
37 }
38 else
39 std::swap(c1, c2);
40
41 IntSubClass* after_x1 = c1.data();
42 IntSubClass* after_x2 = c2.data();
43 std::cout << "** C1_BASE_AFTER = " << after_x1 << " size=" << c1.size() << "\n";
44 std::cout << "** C2_BASE_AFTER = " << after_x2 << " size=" << c2.size() << "\n";
45
46 ASSERT_TRUE(x1 == after_x2) << "Bad value after swap [1]";
47 ASSERT_TRUE(x2 == after_x1) << "Bad value after swap [2]";
48}
49} // namespace
50
51TEST(Array, Swap1)
52{
53 _testArraySwap(true);
54}
55
56TEST(Array, Swap2)
57{
58 _testArraySwap(false);
59}
60
61/*---------------------------------------------------------------------------*/
62/*---------------------------------------------------------------------------*/
63
64Integer IntPtrSubClass::count = 0;
65
66/*---------------------------------------------------------------------------*/
67/*---------------------------------------------------------------------------*/
68
69template <typename Container, typename SubClass>
71{
72 public:
73
74 void test()
75 {
76 {
77 Container c;
78 c.add(SubClass(1));
79 c.add(SubClass(2));
80 c.add(SubClass(3));
81 ARCCORE_UT_CHECK((c.size() == 3), "Bad size (3)");
82 ARCCORE_UT_CHECK((c[0] == 1), "Bad value [0]");
83 ARCCORE_UT_CHECK((c[1] == 2), "Bad value [1]");
84 ARCCORE_UT_CHECK((c[2] == 3), "Bad value [2]");
85 c.resize(0);
86 ARCCORE_UT_CHECK((c.size() == 0), "Bad size (0)");
87 c.resize(5);
88 ARCCORE_UT_CHECK((c.size() == 5), "Bad size");
89 c.add(SubClass(6));
90 ARCCORE_UT_CHECK((c.size() == 6), "Bad size");
91 ARCCORE_UT_CHECK((c[5] == 6), "Bad value [5]");
92 c.shrink();
93 ASSERT_EQ(c.size(), c.capacity()) << "Bad capacity (test 1)";
94 c.resize(12);
95 c.shrink();
96 ASSERT_EQ(c.size(), c.capacity()) << "Bad capacity (test 2)";
97 }
98 {
99 Container c;
100 c.shrink();
101 ASSERT_EQ(c.capacity(), 0) << "Bad capacity (test 3)";
102 }
103 {
104 Container c;
105 Integer nb = 20;
106 for (Integer i = 0; i < nb; ++i)
107 c.add(SubClass(i));
108 c.reserve(nb * 2);
109 Int64 current_capacity = c.capacity();
110 ASSERT_EQ(current_capacity, (nb * 2)) << "Bad capacity (test 4)";
111 c.shrink(c.capacity() + 5);
112 ASSERT_EQ(c.capacity(), current_capacity) << "Bad capacity (test 5)";
113 c.shrink(32);
114 ASSERT_EQ(c.capacity(), 32) << "Bad capacity (test 6)";
115 c.shrink();
116 ASSERT_EQ(c.capacity(), c.size()) << "Bad capacity (test 7)";
117 }
118 {
120 Integer nb = 1000000;
121 uc.resize(1000);
122 for (Container& c : uc) {
123 c.reserve(nb);
124 c.shrink_to_fit();
125 }
126 }
127 {
128 Container c;
129 for (Integer i = 0; i < 50; ++i)
130 c.add(SubClass(i + 2));
131 {
132 Container c2 = c;
133 for (Integer i = 50; i < 100; ++i) {
134 c2.add(SubClass(i + 2));
135 }
136 Container c4 = c2;
137 Container c3;
138 c3.add(5);
139 c3.add(6);
140 c3.add(7);
141 c3 = c2;
142 for (Integer i = 100; i < 150; ++i) {
143 c4.add(SubClass(i + 2));
144 }
145 }
146 ARCCORE_UT_CHECK((c.size() == 150), "Bad size (150)");
147 c.reserve(300);
148 ARCCORE_UT_CHECK((c.capacity() == 300), "Bad capacity (300)");
149 for (Integer i = 0; i < 50; ++i) {
150 c.remove(i);
151 }
152 ARCCORE_UT_CHECK((c.size() == 100), "Bad size (100)");
153 for (Integer i = 0; i < 50; ++i) {
154 //cout << "** VAL: " << i << " c=" << c[i] << " expected=" << ((i*2)+3) << '\n';
155 ARCCORE_UT_CHECK((c[i] == ((i * 2) + 3)), "Bad value");
156 }
157 for (Integer i = 50; i < 100; ++i) {
158 //cout << "** VAL: " << i << " c=" << c[i] << " expected=" << (i+52) << '\n';
159 ARCCORE_UT_CHECK((c[i] == (i + 52)), "Bad value");
160 }
161 }
162 }
163};
164
165namespace
166{
167void _testArrayNewInternal()
168{
169 using namespace Arccore;
170 std::cout << "** TEST VECTOR NEW\n";
171
172 std::cout << "** wanted_size = " << AlignedMemoryAllocator3::simdAlignment() << "\n";
173 //if (impl_size!=wanted_size)
174 //ARCCORE_FATAL("Bad sizeof(ArrayImplBase) v={0} expected={1}",impl_size,wanted_size);
175 {
177 rvt.test();
178 }
179 std::cout << "** TEST VECTOR NEW 2\n";
180 {
182 rvt.test();
183 std::cout << "** COUNT = " << IntPtrSubClass::count << "\n";
184 }
185 std::cout << "** TEST VECTOR NEW 3\n";
186 {
188 rvt.test();
189 std::cout << "** COUNT = " << IntPtrSubClass::count << "\n";
190 }
191 {
193 c.add(5);
194 c.add(7);
196 c2.add(3);
197 ARCCORE_UT_CHECK((c2.size() == 3), "Bad value [3]");
198 ARCCORE_UT_CHECK((c.size() == 2), "Bad value [2]");
199 ARCCORE_UT_CHECK((c[0] == 5), "Bad value [5]");
200 ARCCORE_UT_CHECK((c[1] == 7), "Bad value [7]");
201 ARCCORE_UT_CHECK((c2[0] == 5), "Bad value [5]");
202 ARCCORE_UT_CHECK((c2[1] == 7), "Bad value [7]");
203 ARCCORE_UT_CHECK((c2[2] == 3), "Bad value [7]");
204 }
205 {
207 c.add(5);
208 c.add(7);
210 c2.add(3);
211 ARCCORE_UT_CHECK((c2.size() == 3), "Bad value [3]");
212 ARCCORE_UT_CHECK((c.size() == 2), "Bad value [2]");
213 ARCCORE_UT_CHECK((c[0] == 5), "Bad value [5]");
214 ARCCORE_UT_CHECK((c[1] == 7), "Bad value [7]");
215 ARCCORE_UT_CHECK((c2[0] == 5), "Bad value [5]");
216 ARCCORE_UT_CHECK((c2[1] == 7), "Bad value [7]");
217 ARCCORE_UT_CHECK((c2[2] == 3), "Bad value [7]");
218 }
219 {
220 UniqueArray<IntSubClass> c{ 5, 7 };
222 c2.add(3);
223 ARCCORE_UT_CHECK((c2.size() == 3), "Bad value [3]");
224 ARCCORE_UT_CHECK((c.size() == 2), "Bad value [2]");
225 ARCCORE_UT_CHECK((c[0] == 5), "Bad value [5]");
226 ARCCORE_UT_CHECK((c[1] == 7), "Bad value [7]");
227 ARCCORE_UT_CHECK((c2[0] == 5), "Bad value [5]");
228 ARCCORE_UT_CHECK((c2[1] == 7), "Bad value [7]");
229 ARCCORE_UT_CHECK((c2[2] == 3), "Bad value [7]");
230 }
231 {
232 UniqueArray<IntSubClass> c{ 5, 7 };
233 ARCCORE_UT_CHECK((c.size() == 2), "Bad value [2]");
234 ARCCORE_UT_CHECK((c[0] == 5), "Bad value [5]");
235 ARCCORE_UT_CHECK((c[1] == 7), "Bad value [7]");
236 }
237 {
238 SharedArray<IntSubClass> c{ 5, 7 };
239 ARCCORE_UT_CHECK((c.size() == 2), "Bad value [2]");
240 ARCCORE_UT_CHECK((c[0] == 5), "Bad value [5]");
241 ARCCORE_UT_CHECK((c[1] == 7), "Bad value [7]");
242 }
243 {
244 PrintableMemoryAllocator allocator;
245 UniqueArray<IntSubClass> c(&allocator);
246 UniqueArray<IntSubClass> cx(&allocator, 5);
247 ARCCORE_UT_CHECK((cx.size() == 5), "Bad value [5]");
248 c.add(5);
249 c.add(7);
251 c2.add(3);
252 ARCCORE_UT_CHECK((c2.size() == 3), "Bad value [3]");
253 ARCCORE_UT_CHECK((c.size() == 2), "Bad value [2]");
254 ARCCORE_UT_CHECK((c[0] == 5), "Bad value [5]");
255 ARCCORE_UT_CHECK((c[1] == 7), "Bad value [7]");
256 ARCCORE_UT_CHECK((c2[0] == 5), "Bad value [5]");
257 ARCCORE_UT_CHECK((c2[1] == 7), "Bad value [7]");
258 ARCCORE_UT_CHECK((c2[2] == 3), "Bad value [7]");
259 for (Integer i = 0; i < 50; ++i)
260 c.add(i + 3);
261 c.resize(24);
262 c.reserve(70);
263 }
264 {
266 c2.add(3);
267 c2.add(5);
269 ASSERT_EQ(c22.size(),33);
270 c22 = c2;
271 {
273 c.add(5);
274 c.add(7);
275 c2 = c;
278 c2.resize(125);
279 ASSERT_EQ(c2.size(),c3.size());
280 ASSERT_EQ(c2.size(),c4.size());
281 c3.resize(459);
282 ASSERT_EQ(c2.size(),c3.size());
283 ASSERT_EQ(c2.size(),c4.size());
284 c4.resize(932);
285 c.resize(32);
286 }
287 c2.add(3);
288 ARCCORE_UT_CHECK((c2.size() == 33), "Bad value [3]");
289 ARCCORE_UT_CHECK((c2[0] == 5), "Bad value [5]");
290 ARCCORE_UT_CHECK((c2[1] == 7), "Bad value [7]");
291 ARCCORE_UT_CHECK((c2[32] == 3), "Bad value [7]");
292 c2.resize(1293);
293 ASSERT_EQ(c2.size(),1293);
294 ASSERT_EQ(c22.size(),2);
295
296 {
297 SharedArray<IntSubClass> values1 = { -7, 3, 4 };
298 ASSERT_EQ(values1.size(),3);
299 ASSERT_EQ(values1[0],-7);
300 ASSERT_EQ(values1[1],3);
301 ASSERT_EQ(values1[2],4);
302 values1 = { 2, -1, 9, 13 };
303 ASSERT_EQ(values1.size(),4);
304 ASSERT_EQ(values1[0],2);
305 ASSERT_EQ(values1[1],-1);
306 ASSERT_EQ(values1[2],9);
307 ASSERT_EQ(values1[3],13);
308 SharedArray<IntSubClass> values2 = values1;
309 ASSERT_EQ(values2,values1);
310
311 values1 = {};
312 ASSERT_EQ(values1.size(),0);
313 ASSERT_EQ(values2.size(),0);
314 }
315
316 }
317 {
318 UniqueArray<Int32> values1 = { 2, 5 };
319 UniqueArray<Int32> values2 = { 4, 9, 7 };
320 // Copie les valeurs de values2 à la fin de values1.
321 std::copy(std::begin(values2), std::end(values2), std::back_inserter(values1));
322 std::cout << "** VALUES1 = " << values1 << "\n";
323 ARCCORE_UT_CHECK((values1.size() == 5), "BI: Bad size");
324 ARCCORE_UT_CHECK((values1[0] == 2), "BI: Bad value [0]");
325 ARCCORE_UT_CHECK((values1[1] == 5), "BI: Bad value [1]");
326 ARCCORE_UT_CHECK((values1[2] == 4), "BI: Bad value [2]");
327 ARCCORE_UT_CHECK((values1[3] == 9), "BI: Bad value [3]");
328 ARCCORE_UT_CHECK((values1[4] == 7), "BI: Bad value [4]");
329
331 vx.add(IntPtrSubClass(5));
332 UniqueArray<IntPtrSubClass>::iterator i = std::begin(vx);
334 std::cout << "V=" << i->m_v << " " << ci->m_v << '\n';
335
336 values1 = { -7, 3 };
337 ASSERT_EQ(values1.size(),2);
338 ASSERT_EQ(values1[0],-7);
339 ASSERT_EQ(values1[1],3);
340
341 values1 = {};
342 ASSERT_EQ(values1.size(),0);
343 }
344 {
345 UniqueArray<Int32> values1;
346 UniqueArray<Int32> values2 = { 4, 9, 7, 6, 3 };
347 // Copie les valeurs de values2 à la fin de values1.
348 values1.copy(values2);
349 std::cout << "** VALUES1 = " << values1 << "\n";
350 ARCCORE_UT_CHECK((values1.size() == 5), "BI: Bad size");
351 ARCCORE_UT_CHECK((values1[0] == 4), "BI2: Bad value [0]");
352 ARCCORE_UT_CHECK((values1[1] == 9), "BI2: Bad value [1]");
353 ARCCORE_UT_CHECK((values1[2] == 7), "BI2: Bad value [2]");
354 ARCCORE_UT_CHECK((values1[3] == 6), "BI2: Bad value [3]");
355 ARCCORE_UT_CHECK((values1[4] == 3), "BI2: Bad value [4]");
356
358 vx.add(IntPtrSubClass(5));
359 UniqueArray<IntPtrSubClass>::iterator i = std::begin(vx);
361 std::cout << "V=" << i->m_v << " " << ci->m_v << '\n';
362 }
363 {
365 vx.add(IntPtrSubClass(5));
368 std::cout << "V=" << i->m_v << " " << ci->m_v << '\n';
371 std::cout << "V=" << cicvx->m_v << '\n';
372 }
373 {
375 vx.add(IntPtrSubClass(5));
376 UniqueArray<IntPtrSubClass>::iterator i = std::begin(vx);
377 UniqueArray<IntPtrSubClass>::iterator iend = std::end(vx);
379 std::cout << "V=" << i->m_v << " " << ci->m_v << " " << (iend - i) << '\n';
381 UniqueArray<IntPtrSubClass>::const_iterator cicvx = std::begin(cvx);
382 std::cout << "V=" << cicvx->m_v << '\n';
383 std::copy(std::begin(vx), std::end(vx), std::begin(vx));
384 }
385 {
386 UniqueArray<Int32> values = { 4, 9, 7 };
387 for (typename ArrayView<Int32>::const_iter i(values.view()); i(); ++i) {
388 std::cout << *i << '\n';
389 }
390 for (typename ConstArrayView<Int32>::const_iter i(values.view()); i(); ++i) {
391 std::cout << *i << '\n';
392 }
393 for (auto i : values.range()) {
394 std::cout << i << '\n';
395 }
396 for (auto i : values.constView().range()) {
397 std::cout << i << '\n';
398 }
399
400 {
401 auto r1 = std::make_reverse_iterator(values.end());
402 auto r2 = std::make_reverse_iterator(values.begin());
403 for (; r1 != r2; ++r1) {
404 std::cout << "RVALUE = " << *r1 << '\n';
405 }
406 }
407 {
408 auto r1 = values.rbegin();
409 ASSERT_EQ((*r1), 7);
410 ++r1;
411 ASSERT_EQ((*r1), 9);
412 ++r1;
413 ASSERT_EQ((*r1), 4);
414 ++r1;
415 ASSERT_TRUE((r1 == values.rend()));
416 }
417 {
420 c2.add(IntSubClassNoPod{ 3 });
421 ARCCORE_UT_CHECK((c2.size() == 3), "Bad value [3]");
422 ARCCORE_UT_CHECK((c.size() == 2), "Bad value [2]");
423 ARCCORE_UT_CHECK((c[0] == 5), "Bad value [5]");
424 ARCCORE_UT_CHECK((c[1] == 7), "Bad value [7]");
425 ARCCORE_UT_CHECK((c2[0] == 5), "Bad value [5]");
426 ARCCORE_UT_CHECK((c2[1] == 7), "Bad value [7]");
427 ARCCORE_UT_CHECK((c2[2] == 3), "Bad value [7]");
428 c = c2.span();
429 SmallSpan<const IntSubClassNoPod> c2_small_span = c2;
430 ASSERT_EQ(c.constSpan(), c2.constSpan());
431 ASSERT_EQ(c.constSmallSpan(), c2_small_span);
432 ASSERT_EQ(c.smallSpan(), c2.smallSpan());
433 }
434 }
435}
436} // namespace
437
438/*---------------------------------------------------------------------------*/
439/*---------------------------------------------------------------------------*/
440
441TEST(Array, Misc)
442{
443 try {
444 _testArrayNewInternal();
445 }
446 catch (const Exception& ex) {
447 std::cerr << "Exception ex=" << ex << "\n";
448 throw;
449 }
450}
451
452/*---------------------------------------------------------------------------*/
453/*---------------------------------------------------------------------------*/
454
455namespace
456{
457void
458_Add(Array<Real>& v,Integer new_size)
459{
460 v.resize(new_size);
461}
462}
463
464/*---------------------------------------------------------------------------*/
465/*---------------------------------------------------------------------------*/
466
467TEST(Array, Misc2)
468{
469 using namespace Arccore;
470 {
472 v.resize(3);
473 v[0] = 1.2;
474 v[1] = -1.3;
475 v[2] = 7.6;
476 v.add(4.3);
477 for (Real x : v) {
478 std::cout << " Value: " << x << '\n';
479 }
480 v.printInfos(std::cout);
481 }
482 {
484 v.add(4.3);
485 v.add(5.3);
486 v.add(4.6);
487 v.printInfos(std::cout);
488 v.add(14.3);
489 v.add(25.3);
490 v.add(34.6);
491 v.printInfos(std::cout);
492 v.resize(4);
493 v.printInfos(std::cout);
494 v.add(12.2);
495 v.add(2.4);
496 v.add(3.4);
497 v.add(3.6);
498 v.printInfos(std::cout);
499 for (Integer i = 0, is = v.size(); i < is; ++i) {
500 std::cout << " Value: " << v[i] << '\n';
501 }
502 }
503 {
505 v.reserve(5);
506 for (int i = 0; i < 10; ++i)
507 v.add((Real)i);
508 for (Integer i = 0, is = v.size(); i < is; ++i) {
509 std::cout << " Value: " << v[i] << '\n';
510 }
511 }
512 {
514 v.reserve(175);
515 for (int i = 0; i < 27500; ++i) {
516 Real z = (Real)i;
517 v.add(z * z);
518 }
519 for (int i = 0; i < 5000; ++i) {
520 v.remove(i * 2);
521 }
522 v.reserve(150);
523 for (int i = 0; i < 27500; ++i) {
524 Real z = (Real)i;
525 v.add(z * z);
526 }
527 std::cout << " ValueSize= " << v.size() << " values=" << v << '\n';
528 }
529 for (Integer i = 0; i < 100; ++i) {
531 _Add(v, 500000);
532 _Add(v, 1000000);
533 _Add(v, 0);
534 _Add(v, 100000);
535 _Add(v, 0);
536 _Add(v, 0);
537 _Add(v, 230000);
538 std::cout << " Size: " << v.size() << '\n';
539 ASSERT_EQ(v.size(), 230000);
540 }
541}
542
543/*---------------------------------------------------------------------------*/
544/*---------------------------------------------------------------------------*/
545
546TEST(Array, SubViews)
547{
548 using namespace Arccore;
549
550 {
551 // Test Array::subView() et Array::subConstView()
553 v.resize(23);
554 for (Int32 i = 0, n = v.size(); i < n; ++i)
555 v[i] = (i + 1);
556
557 auto sub_view1 = v.subView(50, 5);
558 ASSERT_EQ(sub_view1.data(), nullptr);
559 ASSERT_EQ(sub_view1.size(), 0);
560
561 auto sub_view2 = v.subView(2, 8);
562 ASSERT_EQ(sub_view2.size(), 8);
563 for (Int32 i = 0, n = sub_view2.size(); i < n; ++i)
564 ASSERT_EQ(sub_view2[i], v[2 + i]);
565
566 auto sub_view3 = v.subView(20, 8);
567 ASSERT_EQ(sub_view3.size(), 3);
568 for (Int32 i = 0, n = sub_view3.size(); i < n; ++i)
569 ASSERT_EQ(sub_view3[i], v[20 + i]);
570
571 auto sub_const_view1 = v.subConstView(50, 5);
572 ASSERT_EQ(sub_const_view1.data(), nullptr);
573 ASSERT_EQ(sub_const_view1.size(), 0);
574
575 auto sub_const_view2 = v.subConstView(2, 8);
576 ASSERT_EQ(sub_const_view2.size(), 8);
577 for (Int32 i = 0, n = sub_const_view2.size(); i < n; ++i)
578 ASSERT_EQ(sub_const_view2[i], v[2 + i]);
579
580 auto sub_const_view3 = v.subConstView(20, 8);
581 ASSERT_EQ(sub_const_view3.size(), 3);
582 for (Int32 i = 0, n = sub_const_view3.size(); i < n; ++i)
583 ASSERT_EQ(sub_const_view3[i], v[20 + i]);
584 }
585}
586
587/*---------------------------------------------------------------------------*/
588/*---------------------------------------------------------------------------*/
589
591{
592 public:
593 NoCopyData(const NoCopyData& x) = delete;
594};
595
596template <typename DataType>
598: public Arccore::Array<DataType>
599{
600 public:
601
603};
604
605/*---------------------------------------------------------------------------*/
606/*---------------------------------------------------------------------------*/
607
608TEST(Array, Misc3)
609{
610 using namespace Arccore;
611
612 {
614 const Int32 ref_value1 = 12;
615 const Int32 ref_value2 = 7;
616
617 c.resize(9, IntSubClassNoPod(ref_value1));
618 std::cout << "C1=" << c << "\n";
619 for (IntSubClassNoPod x : c)
620 ASSERT_EQ(x, ref_value1);
621
622 c.resize(21, IntSubClassNoPod(ref_value2));
623 ASSERT_EQ(c.size(), 21);
624 std::cout << "C2=" << c << "\n";
625
626 // Redimensionne sans initialiser. Les valeurs pour les éléments
627 // de 9 à 18 doivent valoir \a ref_value2
628 c.resizeNoInit(18);
629 std::cout << "C4=" << c << "\n";
630 for (Int32 i = 9, s = c.size(); i < s; ++i)
631 ASSERT_EQ(c[i], ref_value2);
632 for (Int32 i = 9, s = c.size(); i < s; ++i)
633 new (c.data() + i) IntSubClassNoPod(i + 2);
634 for (Int32 i = 9, s = c.size(); i < s; ++i)
635 ASSERT_EQ(c[i], (i + 2));
636 }
637 {
639 c2.resizeNoInit(25);
640 }
641}
642
643/*---------------------------------------------------------------------------*/
644/*---------------------------------------------------------------------------*/
645
646template<typename ArrayType>
648{
649 public:
650 static void doTestBase()
651 {
652 using namespace Arccore;
653
655 PrintableMemoryAllocator printable_allocator2;
656 IMemoryAllocator* allocator2 = &printable_allocator2;
657
658 {
659 std::cout << "Array a\n";
660 ArrayType a(allocator1);
661 ASSERT_EQ(a.allocator(),allocator1);
662 a.add(27);
663 a.add(38);
664 a.add(13);
665 a.add(-5);
666
667 std::cout << "Array b\n";
668 ArrayType b(allocator2);
669 ASSERT_EQ(b.capacity(),0);
670 ASSERT_EQ(b.size(),0);
671 ASSERT_EQ(b.allocator(),allocator2);
672
673 b = a;
674 ASSERT_EQ(b.size(),a.size());
675 ASSERT_EQ(b.allocator(),a.allocator());
676
677 std::cout << "Array c\n";
678 ArrayType c(a.clone());
679 ASSERT_EQ(c.allocator(),a.allocator());
680 ASSERT_EQ(c.size(),a.size());
681 ASSERT_EQ(c.constSpan(),a.constSpan());
682
683 std::cout << "Array d\n";
684 ArrayType d(allocator2,a);
685 ASSERT_EQ(d.allocator(),allocator2);
686 ASSERT_EQ(d.size(),a.size());
687 ASSERT_EQ(d.constSpan(),a.constSpan());
688
689 std::cout << "Array e\n";
690 ArrayType e(allocator2,25);
691 ASSERT_EQ(e.allocator(),allocator2);
692 ASSERT_EQ(e.size(),25);
693
694 ArrayType f(allocator2);
695 f = e;
696 ASSERT_EQ(f.allocator(),e.allocator());
697 ASSERT_EQ(f.size(),e.size());
698
699 f = f;
700 ASSERT_EQ(f.allocator(),e.allocator());
701 ASSERT_EQ(f.size(),e.size());
702 }
703 }
704};
705
706TEST(UniqueArray, AllocatorBase)
707{
709}
710
711TEST(SharedArray, AllocatorBase)
712{
714}
715
716/*---------------------------------------------------------------------------*/
717/*---------------------------------------------------------------------------*/
718
720{
721 using namespace Arccore;
722 std::cout << "Sizeof(MemoryAllocationOptions)=" << sizeof(MemoryAllocationOptions) << "\n";
723 std::cout << "Sizeof(ArrayMetaData)=" << sizeof(ArrayMetaData) << "\n";
724 std::cout << "Sizeof(UniqueArray<Int32>)=" << sizeof(UniqueArray<Int32>) << "\n";
725 std::cout << "Sizeof(SharedArray<Int32>)=" << sizeof(SharedArray<Int32>) << "\n";
726
727 PrintableMemoryAllocator printable_allocator;
728 PrintableMemoryAllocator printable_allocator2;
731 {
732 std::cout << "Array a1\n";
733 UniqueArray<Int32> a1(allocator2);
734 ASSERT_EQ(a1.allocator(), allocator2);
735 ASSERT_EQ(a1.size(), 0);
736 ASSERT_EQ(a1.capacity(), 0);
737 ASSERT_EQ(a1.data(), nullptr);
738
739 std::cout << "Array a2\n";
740 UniqueArray<Int32> a2(a1);
741 ASSERT_EQ(a1.allocator(), a2.allocator());
742 ASSERT_EQ(a2.capacity(), 0);
743 ASSERT_EQ(a2.data(), nullptr);
744 a1.reserve(3);
745 a1.add(5);
746 a1.add(7);
747 a1.add(12);
748 a1.add(3);
749 a1.add(1);
750 ASSERT_EQ(a1.size(), 5);
751
752 std::cout << "Array a3\n";
753 UniqueArray<Int32> a3(allocator1);
754 a3.add(4);
755 a3.add(6);
756 a3.add(2);
757 ASSERT_EQ(a3.size(), 3);
758 a3 = a1;
759 ASSERT_EQ(a3.allocator(), a1.allocator());
760 ASSERT_EQ(a3.size(), a1.size());
761 ASSERT_EQ(a3.constSpan(), a1.constSpan());
762
763 std::cout << "Array a4\n";
764 UniqueArray<Int32> a4(allocator1);
765 a4.add(4);
766 a4.add(6);
767 a4.add(2);
768 ASSERT_EQ(a4.size(), 3);
769 a4 = a1.span();
770 ASSERT_EQ(a4.allocator(), allocator1);
771
772 a4 = UniqueArray<Int32>(&printable_allocator2);
773
774 UniqueArray<Int32> array[2];
775 IMemoryAllocator* allocator3 = allocator1;
776 for( Integer i=0; i<2; ++i ){
777 array[i] = UniqueArray<Int32>(allocator3);
778 }
779 ASSERT_EQ(array[0].allocator(), allocator3);
780 ASSERT_EQ(array[1].allocator(), allocator3);
781 }
782}
783
784/*---------------------------------------------------------------------------*/
785/*---------------------------------------------------------------------------*/
786
788{
789 using namespace Arccore;
790 std::cout << "Sizeof(MemoryAllocationOptions)=" << sizeof(MemoryAllocationOptions) << "\n";
791 std::cout << "Sizeof(ArrayMetaData)=" << sizeof(ArrayMetaData) << "\n";
792 std::cout << "Sizeof(UniqueArray<Int32>)=" << sizeof(UniqueArray<Int32>) << "\n";
793 std::cout << "Sizeof(SharedArray<Int32>)=" << sizeof(SharedArray<Int32>) << "\n";
794
795 PrintableMemoryAllocator printable_allocator;
796 PrintableMemoryAllocator printable_allocator2;
799 {
800 std::cout << "Array a1\n";
801 SharedArray<Int32> a1(allocator2);
802 ASSERT_EQ(a1.allocator(), allocator2);
803 ASSERT_EQ(a1.size(), 0);
804 ASSERT_EQ(a1.capacity(), 0);
805 ASSERT_EQ(a1.data(), nullptr);
806
807 std::cout << "Array a2\n";
808 SharedArray<Int32> a2(a1);
809 ASSERT_EQ(a1.allocator(), a2.allocator());
810 ASSERT_EQ(a2.capacity(), 0);
811 ASSERT_EQ(a2.data(), nullptr);
812 a1.reserve(3);
813 a1.add(5);
814 a1.add(7);
815 a1.add(12);
816 a1.add(3);
817 a1.add(1);
818 ASSERT_EQ(a1.size(), 5);
819
820 std::cout << "Array a3\n";
821 SharedArray<Int32> a3(allocator1);
822 a3.add(4);
823 a3.add(6);
824 a3.add(2);
825 ASSERT_EQ(a3.size(), 3);
826 a3 = a1;
827 ASSERT_EQ(a3.allocator(), a1.allocator());
828 ASSERT_EQ(a3.size(), a1.size());
829 ASSERT_EQ(a3.constSpan(), a1.constSpan());
830
831 std::cout << "Array a4\n";
832 SharedArray<Int32> a4(allocator1);
833 a4.add(4);
834 a4.add(6);
835 a4.add(2);
836 ASSERT_EQ(a4.size(), 3);
837 a4 = a1.span();
838 ASSERT_EQ(a4.allocator(), allocator1);
839
840 a4 = SharedArray<Int32>(&printable_allocator2);
841
842 SharedArray<Int32> array[2];
843 IMemoryAllocator* allocator3 = allocator1;
844 for( Integer i=0; i<2; ++i ){
845 array[i] = SharedArray<Int32>(allocator3);
846 }
847 ASSERT_EQ(array[0].allocator(), allocator3);
848 ASSERT_EQ(array[1].allocator(), allocator3);
849 }
850}
851
860: public IMemoryAllocator3
861{
862 public:
863
864 bool hasRealloc(MemoryAllocationArgs args) const override
865 {
866 _checkValid(args);
867 return m_default_allocator.hasRealloc(args);
868 }
870 {
871 _checkValid(args);
872 return m_default_allocator.allocate(args,new_size);
873 }
875 {
876 _checkValid(args);
877 return m_default_allocator.reallocate(args, current_ptr, new_size);
878 }
880 {
881 _checkValid(args);
882 m_default_allocator.deallocate(args, ptr);
883 }
884 Int64 adjustedCapacity(MemoryAllocationArgs args, Int64 wanted_capacity, Int64 element_size) const override
885 {
886 _checkValid(args);
887 return m_default_allocator.adjustedCapacity(args, wanted_capacity, element_size);
888 }
889 size_t guarantedAlignment(MemoryAllocationArgs args) const override
890 {
891 _checkValid(args);
892 return m_default_allocator.guarantedAlignment(args);
893 }
894
896 {
897 // Cette méthode n'est appelée qu'une seule fois donc on teste directement les valeurs attendues
898 ASSERT_EQ(old_args.memoryLocationHint(), eMemoryLocationHint::None);
899 ASSERT_EQ(new_args.memoryLocationHint(), eMemoryLocationHint::MainlyHost);
900 ASSERT_EQ(ptr.size(), 8);
901 m_default_allocator.notifyMemoryArgsChanged(old_args, new_args, ptr);
902 }
903
904 private:
905
906 DefaultMemoryAllocator3 m_default_allocator;
907
908 private:
909
910 static void _checkValid(MemoryAllocationArgs args)
911 {
912 bool v1 = args.memoryLocationHint() == eMemoryLocationHint::None;
913 bool v2 = args.memoryLocationHint() == eMemoryLocationHint::MainlyHost;
914 bool v3 = args.memoryLocationHint() == eMemoryLocationHint::HostAndDeviceMostlyRead;
915 bool is_valid = v1 || v2 || v3;
916 ASSERT_TRUE(is_valid);
917 }
918};
919
920#define ASSERT_SAME_ARRAY_INFOS(a,b) \
921 ASSERT_EQ(a.allocationOptions(), b.allocationOptions());\
922 ASSERT_EQ(a.size(), b.size());\
923 ASSERT_EQ(a.capacity(), b.capacity())
924
925TEST(Array, AllocatorV2)
926{
927 using namespace Arccore;
928 TesterMemoryAllocatorV3 testerv2_allocator;
929 TesterMemoryAllocatorV3 testerv2_allocator2;
930 MemoryAllocationOptions allocate_options1(&testerv2_allocator, eMemoryLocationHint::HostAndDeviceMostlyRead);
931 MemoryAllocationOptions allocate_options2(&testerv2_allocator, eMemoryLocationHint::None, 0);
932 {
933 MemoryAllocationOptions opt3(&testerv2_allocator);
934 opt3.setMemoryLocationHint(eMemoryLocationHint::HostAndDeviceMostlyRead);
935 ASSERT_EQ(opt3, allocate_options1);
936 }
937 {
938 std::cout << "Array a1\n";
939 UniqueArray<Int32> a1(allocate_options2);
940 ASSERT_EQ(a1.allocationOptions(), allocate_options2);
941 ASSERT_EQ(a1.size(), 0);
942 ASSERT_EQ(a1.capacity(), 0);
943 ASSERT_EQ(a1.data(), nullptr);
944
945 std::cout << "Array a2\n";
946 UniqueArray<Int32> a2(a1);
947 ASSERT_SAME_ARRAY_INFOS(a2, a1);
948 ASSERT_EQ(a2.data(), nullptr);
949 a1.reserve(3);
950 a1.add(5);
951 a1.add(7);
952 a1.add(12);
953 a1.add(3);
954 a1.add(1);
955 ASSERT_EQ(a1.size(), 5);
956 a2.add(9);
957 a2.add(17);
958 // Pour tester notifyMemoryArgsChanged()
959 a2.setMemoryLocationHint(eMemoryLocationHint::MainlyHost);
960
961 std::cout << "Array a3\n";
962 UniqueArray<Int32> a3(allocate_options1);
963 a3.add(4);
964 a3.add(6);
965 a3.add(2);
966 ASSERT_EQ(a3.size(), 3);
967 a3 = a1;
968 ASSERT_EQ(a3.allocator(), a1.allocator());
969 ASSERT_EQ(a3.size(), a1.size());
970 ASSERT_EQ(a3.constSpan(), a1.constSpan());
971
972 std::cout << "Array a4\n";
973 UniqueArray<Int32> a4(allocate_options1);
974 a4.add(4);
975 a4.add(6);
976 a4.add(2);
977 ASSERT_EQ(a4.size(), 3);
978 a4 = a1.span();
979 ASSERT_EQ(a4.allocationOptions(), allocate_options1);
980
981 a4 = UniqueArray<Int32>(&testerv2_allocator2);
982
983 UniqueArray<Int32> array[2];
984 MemoryAllocationOptions allocator3 = allocate_options1;
985 for (Integer i = 0; i < 2; ++i) {
986 array[i] = UniqueArray<Int32>(allocator3);
987 }
988 ASSERT_EQ(array[0].allocationOptions(), allocator3);
989 ASSERT_EQ(array[1].allocationOptions(), allocator3);
990 }
991}
992
993TEST(Array, DebugInfo)
994{
995 using namespace Arccore;
996 DefaultMemoryAllocator3 m_default_allocator;
997 MemoryAllocationOptions allocate_options2(&m_default_allocator, eMemoryLocationHint::None, 0);
998
999 String a1_name("Array1");
1001 {
1002 std::cout << "Array a1\n";
1003 UniqueArray<Int32> a1(allocate_options2);
1004 a1.setDebugName(a1_name);
1005 ASSERT_EQ(a1.allocationOptions(), allocate_options2);
1006 ASSERT_EQ(a1.size(), 0);
1007 ASSERT_EQ(a1.capacity(), 0);
1008 ASSERT_EQ(a1.data(), nullptr);
1009 ASSERT_EQ(a1.debugName(), a1_name);
1010
1011 std::cout << "Array a2\n";
1012 UniqueArray<Int32> a2(a1);
1013 ASSERT_SAME_ARRAY_INFOS(a2, a1);
1014 ASSERT_EQ(a2.data(), nullptr);
1015 ASSERT_EQ(a2.debugName(), a1_name);
1016 a1.reserve(3);
1017 a1.add(5);
1018 a1.add(7);
1019 a1.add(12);
1020 a1.add(3);
1021 a1.add(1);
1022 ASSERT_EQ(a1.size(), 5);
1023 a2.add(9);
1024 a2.add(17);
1025 ASSERT_EQ(a2.debugName(), a1_name);
1026 ASSERT_EQ(a2.size(), 2);
1027
1028 a3 = a2;
1029 }
1030 ASSERT_EQ(a3.debugName(), a1_name);
1031 ASSERT_EQ(a3.size(), 2);
1032}
1033
1034/*---------------------------------------------------------------------------*/
1035/*---------------------------------------------------------------------------*/
1036
1037namespace Arccore
1038{
1039// Instancie explicitement les classes tableaux pour garantir
1040// que toutes les méthodes fonctionnent
1041template class UniqueArray<IntSubClass>;
1042template class SharedArray<IntSubClass>;
1043template class Array<IntSubClass>;
1044template class AbstractArray<IntSubClass>;
1045} // namespace Arccore
#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
String debugName() const
Nom de debug (nul si aucun nom spécifié)
Integer size() const
Nombre d'éléments du vecteur.
static constexpr Integer simdAlignment()
Alignement pour les structures utilisant la vectorisation.
static AlignedMemoryAllocator3 * Simd()
Allocateur garantissant l'alignement pour utiliser la vectorisation sur la plateforme cible.
static AlignedMemoryAllocator * Simd()
Allocateur garantissant l'alignement pour utiliser la vectorisation sur la plateforme cible.
Informations sur une zone mémoire allouée.
Int64 size() const
Taille en octets de la zone mémoire utilisée. (-1) si inconnue.
Classe de base des vecteurs 1D de données.
Span< const T > span() const
Vue immutable sur ce tableau.
iterator begin()
Itérateur sur le premier élément du tableau.
void copy(Span< const T > rhs)
Copie les valeurs de rhs dans l'instance.
reverse_iterator rbegin()
Itérateur inverse sur le premier élément du tableau.
void resizeNoInit(Int64 s)
Redimensionne sans initialiser les nouvelles valeurs.
void reserve(Int64 new_capacity)
Réserve le mémoire pour new_capacity éléments.
SmallSpan< const T > smallSpan() const
Vue immutable sur ce tableau.
void resize(Int64 s)
Change le nombre d'éléments du tableau à s.
ArrayView< T > subView(Int64 abegin, Integer asize)
Sous-vue à partir de l'élément abegin et contenant asize éléments.
void remove(Int64 index)
Supprime l'entité ayant l'indice index.
ConstArrayView< T > constView() const
Vue constante sur ce tableau.
ArrayView< T > view() const
Vue mutable sur ce tableau.
SmallSpan< const T > constSmallSpan() const
Vue immutable sur ce tableau.
void add(ConstReferenceType val)
Ajoute l'élément val à la fin du tableau.
Span< const T > constSpan() const
Vue constante sur ce tableau.
reverse_iterator rend()
Itérateur inverse sur le premier élément après la fin du tableau.
ConstArrayView< T > subConstView(Int64 abegin, Int32 asize) const
Sous-vue à partir de l'élément abegin et contenant asize éléments.
iterator end()
Itérateur sur le premier élément après la fin du tableau.
interval d'itérateurs constant
bool hasRealloc(MemoryAllocationArgs) const override
Indique si l'allocateur supporte la sémantique de realloc.
AllocatedMemoryInfo reallocate(MemoryAllocationArgs, AllocatedMemoryInfo current_ptr, Int64 new_size) override
Réalloue de la mémoire pour new_size octets et retourne le pointeur.
Int64 adjustedCapacity(MemoryAllocationArgs, Int64 wanted_capacity, Int64 element_size) const override
Ajuste la capacité suivant la taille d'élément.
void deallocate(MemoryAllocationArgs, AllocatedMemoryInfo ptr) override
Libère la mémoire dont l'adresse de base est ptr.
size_t guarantedAlignment(MemoryAllocationArgs) const override
Valeur de l'alignement garanti par l'allocateur.
AllocatedMemoryInfo allocate(MemoryAllocationArgs, Int64 new_size) override
Alloue de la mémoire pour new_size octets et retourne le pointeur.
Classe de base d'une exception.
virtual void notifyMemoryArgsChanged(MemoryAllocationArgs old_args, MemoryAllocationArgs new_args, AllocatedMemoryInfo ptr)
Notifie du changement des arguments spécifiques à l'instance.
Classe contenant des informations pour spécialiser les allocations.
Options pour configurer les allocations.
Allocateur mémoire via malloc/realloc/free avec impression listing.
Vecteur 1D de données avec sémantique par référence.
SharedArray< T > clone() const
Clone le tableau.
Vue d'un tableau d'éléments de type T.
Definition Span.h:670
Chaîne de caractères unicode.
Vecteur 1D de données avec sémantique par valeur (style STL).
Allocateur pour tester les arguments.
Definition TestArray.cc:861
void notifyMemoryArgsChanged(MemoryAllocationArgs old_args, MemoryAllocationArgs new_args, AllocatedMemoryInfo ptr) override
Notifie du changement des arguments spécifiques à l'instance.
Definition TestArray.cc:895
AllocatedMemoryInfo reallocate(MemoryAllocationArgs args, AllocatedMemoryInfo current_ptr, Int64 new_size) override
Réalloue de la mémoire pour new_size octets et retourne le pointeur.
Definition TestArray.cc:874
size_t guarantedAlignment(MemoryAllocationArgs args) const override
Valeur de l'alignement garanti par l'allocateur.
Definition TestArray.cc:889
void deallocate(MemoryAllocationArgs args, AllocatedMemoryInfo ptr) override
Libère la mémoire dont l'adresse de base est ptr.
Definition TestArray.cc:879
AllocatedMemoryInfo allocate(MemoryAllocationArgs args, Int64 new_size) override
Alloue de la mémoire pour new_size octets et retourne le pointeur.
Definition TestArray.cc:869
Int64 adjustedCapacity(MemoryAllocationArgs args, Int64 wanted_capacity, Int64 element_size) const override
Ajuste la capacité suivant la taille d'élément.
Definition TestArray.cc:884
bool hasRealloc(MemoryAllocationArgs args) const override
Indique si l'allocateur supporte la sémantique de realloc.
Definition TestArray.cc:864
Concept for allocating, resizing and freeing memory block.
Espace de nom de Arccore.
Definition ArcaneTypes.h:24
void swap(UniqueArray< T > &v1, UniqueArray< T > &v2)
Échange les valeurs de v1 et v2.
double Real
Type représentant un réel.
Int32 Integer
Type représentant un entier.
std::int64_t Int64
Type entier signé sur 64 bits.