Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
memarray.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
33
38# include <gtest/gtest.h>
39
40# include <tpl_memArray.H>
41# include <htlist.H>
42# include <concepts>
43# include <limits>
44# include <memory>
45# include <new>
46# include <string>
47# include <type_traits>
48
49using namespace std;
50using namespace testing;
51using namespace Aleph;
52
53bool is_power_of_two(size_t x)
54{
55 return ((x != 0) && !(x & (x - 1))) != 0;
56}
57
58struct Default_MemArray : public Test
59{
60 const size_t n = 64;
62};
63
65{
68 {
69 for (size_t i = 0; i < 30; ++i)
70 m.append(i);
71 }
72};
73
74TEST(MemArray, Basic_initialization)
75{
76 {
78 EXPECT_TRUE(is_power_of_two(m1.capacity()));
79 EXPECT_EQ(m1.size(), 0);
80 EXPECT_TRUE(m1.is_empty());
81 EXPECT_THROW(m1.get(), underflow_error);
82 }
83
84 {
85 MemArray<int> m1(32);
86 EXPECT_TRUE(is_power_of_two(m1.capacity()));
87 EXPECT_EQ(m1.size(), 0);
88 EXPECT_TRUE(m1.is_empty());
89 EXPECT_THROW(m1.get(), underflow_error);
90
91 MemArray<int> m2(31);
92 MemArray<int> m3(17);
93 EXPECT_TRUE(is_power_of_two(m2.capacity()));
94 EXPECT_TRUE(m2.is_empty());
95 EXPECT_TRUE(m3.is_empty());
96 EXPECT_EQ(m2.size(), 0);
97 EXPECT_EQ(m3.size(), 0);
98 EXPECT_EQ(m1.capacity(), m2.capacity());
99 EXPECT_EQ(m1.capacity(), m3.capacity());
100 }
101
102 {
103 MemArray<int> m1(512);
104 EXPECT_TRUE(is_power_of_two(m1.capacity()));
105 EXPECT_EQ(m1.size(), 0);
106 EXPECT_TRUE(m1.is_empty());
107 EXPECT_THROW(m1.get(), underflow_error);
108
109 MemArray<int> m2(257);
110 MemArray<int> m3(316);
111 EXPECT_TRUE(is_power_of_two(m2.capacity()));
112 EXPECT_TRUE(m2.is_empty());
113 EXPECT_TRUE(m3.is_empty());
114 EXPECT_EQ(m2.size(), 0);
115 EXPECT_EQ(m3.size(), 0);
116 EXPECT_EQ(m1.capacity(), m2.capacity());
117 EXPECT_EQ(m1.capacity(), m3.capacity());
118 }
119
120 {
121 MemArray<int> m1(4096);
122 EXPECT_TRUE(is_power_of_two(m1.capacity()));
123 EXPECT_EQ(m1.size(), 0);
124 EXPECT_TRUE(m1.is_empty());
125 EXPECT_THROW(m1.get(), underflow_error);
126
127 MemArray<int> m2(2049);
128 MemArray<int> m3(3000);
129 EXPECT_TRUE(is_power_of_two(m2.capacity()));
130 EXPECT_TRUE(m2.is_empty());
131 EXPECT_TRUE(m3.is_empty());
132 EXPECT_EQ(m2.size(), 0);
133 EXPECT_EQ(m3.size(), 0);
134 EXPECT_EQ(m1.capacity(), m2.capacity());
135 EXPECT_EQ(m1.capacity(), m3.capacity());
136 }
137}
138
139TEST_F(Default_MemArray, growing_in_2_powers)
140{
141 const size_t n = m.capacity();
142 for (size_t i = 0; i < n; ++i)
143 m.append(i);
144
145 EXPECT_EQ(m.size(), n);
146 EXPECT_EQ(m.capacity(), n);
147
148 m.append(n); // this append would cause expansion
149 EXPECT_EQ(m.capacity(), 2 * n);
150 EXPECT_EQ(m.size(), n + 1);
151 EXPECT_EQ(m.get_first(), 0);
152 EXPECT_EQ(m.get_last(), n);
153
154 // testing gap opening
155 m.insert(-1);
156 EXPECT_EQ(m.get_first(), -1);
157 EXPECT_EQ(m.get_last(), n);
158
159 EXPECT_THROW(m[m.size()], out_of_range);
160 EXPECT_THROW(m[m.capacity()], out_of_range);
161
162 { // Testing operator [] in read mode
163 int k = -1;
164 for (size_t i = 0; i < m.size(); ++i, ++k)
165 EXPECT_EQ(m[i], k);
166 EXPECT_EQ(k, n + 1);
167 }
168
169 { // Testing operator [] in write mode
170 for (size_t i = 0; i < m.size(); ++i)
171 m[i]++;
172
173 int k = 0;
174 for (size_t i = 0; i < m.size(); ++i, ++k)
175 EXPECT_EQ(m[i], k);
176 EXPECT_EQ(k, n + 2);
177 }
178}
179
181{
182 const size_t dim = m.capacity();
183
184 m.putn(dim + 1); // This will cause expansion
185
186 EXPECT_EQ(m.capacity(), 2 * dim); // Verify expansion
187 EXPECT_FALSE(m.is_empty());
188 EXPECT_EQ(m.size(), dim + 1);
189
190 for (size_t i = 0; i < m.size(); ++i)
191 {
192 EXPECT_NO_THROW(m[i]);
193 m[i] = i;
194 }
195
196 EXPECT_THROW(m[m.size()], out_of_range);
197 EXPECT_THROW(m.get(m.size() + 1), underflow_error);
198
199 size_t k = 0;
200 EXPECT_NE(m.size(), k);
201 for (size_t i = 0; i < m.size(); ++i, ++k)
202 EXPECT_EQ(m[i], i);
203 EXPECT_EQ(k, m.size()); // TEST that loop has been executed
204
205 const size_t curr_cap = m.capacity();
206 const size_t avail = m.capacity() - m.size();
207 m.putn(avail); // this shouldn't cause expansion
208
209 EXPECT_EQ(m.capacity(), curr_cap);
210 EXPECT_EQ(m.size(), m.capacity());
211
212 int item;
213 EXPECT_NO_THROW(item = m.get(m.size())); // it must take out all items
214
215 EXPECT_EQ(item, 0);
216 EXPECT_TRUE(m.is_empty());
217 EXPECT_EQ(m.size(), 0);
218}
219
220TEST(MemArrayMoveOnly, AppendAndRemoveUniquePtr)
221{
223
224 auto p1 = std::make_unique<int>(5);
225 auto p2 = std::make_unique<int>(7);
226
227 m.append(std::move(p1));
228 m.append(std::move(p2));
229
230 ASSERT_EQ(m.size(), 2u);
231 EXPECT_EQ(*m[0], 5);
232 EXPECT_EQ(*m[1], 7);
233
234 auto last = m.remove_last();
235 EXPECT_EQ(*last, 7);
236 EXPECT_EQ(m.size(), 1u);
237
238 auto first = m.remove_first();
239 EXPECT_EQ(*first, 5);
240 EXPECT_TRUE(m.is_empty());
241}
242
243TEST_F(Default_MemArray, access_operator)
244{
245 EXPECT_TRUE(m.is_empty());
246 EXPECT_EQ(m.size(), 0);
247 EXPECT_NE(m.capacity(), 0);
248
249 const size_t cap1 = m.capacity();
250
251 // Test invalid accesses without insertion neither expansion
252 size_t k = 0;
253 for (size_t i = 0; i < m.capacity(); ++i, ++k)
254 EXPECT_THROW(m[i], out_of_range);
255 EXPECT_EQ(k, m.capacity());
256 EXPECT_EQ(m.capacity(), cap1); // capacity has not changed
257 EXPECT_TRUE(m.is_empty());
258 EXPECT_EQ(m.size(), 0);
259
260 // Insert until capacity (no expansion)
261 k = 0;
262 for (size_t i = 0; i < m.capacity(); ++i, ++k)
263 m.append(i);
264 EXPECT_EQ(k, m.capacity());
265 EXPECT_EQ(m.size(), m.capacity());
266
267 // Test that item were inserted
268 k = 0;
269 for (size_t i = 0; i < m.capacity(); ++i, ++k)
270 {
271 EXPECT_NO_THROW(m[i]);
272 EXPECT_EQ(m[i], i);
273 }
274 EXPECT_EQ(k, m.capacity());
275
276 // Now we cause an expansion
277 k = 0;
278 for (size_t i = m.size(); i < 2 * cap1; ++i, ++k)
279 m.append(i);
280
281 EXPECT_EQ(k, cap1);
282 EXPECT_EQ(m.capacity(), 2 * cap1);
283 EXPECT_EQ(m.size(), 2 * cap1);
284
285 k = 0;
286 for (size_t i = 0; i < m.size(); ++i, ++k)
287 {
288 EXPECT_NO_THROW(m[i]);
289 EXPECT_EQ(m[i], i);
290 }
291 EXPECT_EQ(k, m.capacity());
292}
293
295{
296 const size_t cap = m.capacity();
297 EXPECT_TRUE(m.is_empty());
298 EXPECT_NE(m.capacity(), 0);
299 EXPECT_EQ(m.size(), 0);
300
301 m.reserve(2 * cap + 1); // this should expand to 4*cap
302 EXPECT_EQ(m.capacity(), 4 * cap);
303}
304
305TEST_F(MemArray_with_30_items, copy_and_assigment)
306{
307 EXPECT_FALSE(m.is_empty());
308 EXPECT_EQ(m.size(), 30);
309 EXPECT_EQ(m.capacity(), 32);
310 size_t k = 0;
311 for (size_t i = 0; i < m.size(); ++i, ++k)
312 EXPECT_EQ(m[i], i);
313 EXPECT_EQ(k, m.size());
314
315 { // Copy constructor
316 MemArray<int> aux = m;
317 EXPECT_FALSE(aux.is_empty());
318 EXPECT_EQ(aux.size(), 30);
319 EXPECT_EQ(aux.capacity(), 32);
320 size_t k = 0;
321 for (size_t i = 0; i < m.size(); ++i, ++k)
322 EXPECT_EQ(aux[i], i);
323 EXPECT_EQ(k, m.size());
324 EXPECT_NE(m.get_ptr(), aux.get_ptr());
325 }
326
327 { // move constructor
328 auto ptr = m.get_ptr();
329 MemArray<int> aux = move(m);
330 EXPECT_EQ(aux.get_ptr(), ptr);
331 EXPECT_FALSE(aux.is_empty());
332 EXPECT_EQ(aux.size(), 30);
333 EXPECT_EQ(aux.capacity(), 32);
334 EXPECT_TRUE(m.is_empty());
335 EXPECT_EQ(m.size(), 0);
336 EXPECT_EQ(m.capacity(), 0);
337 EXPECT_EQ(m.get_ptr(), nullptr); // array of zero must be nullptr
338 size_t k = 0;
339 for (size_t i = 0; i < m.size(); ++i, ++k)
340 EXPECT_EQ(aux[i], i);
341 EXPECT_EQ(k, m.size());
342 EXPECT_NE(m.get_ptr(), aux.get_ptr());
343
344 m.swap(aux); // restore m to previous initialized state
345 EXPECT_EQ(m.get_ptr(), ptr);
346 EXPECT_TRUE(aux.is_empty());
347 EXPECT_EQ(aux.size(), 0);
348 EXPECT_EQ(aux.capacity(), 0);
349 EXPECT_FALSE(m.is_empty());
350 EXPECT_EQ(m.size(), 30);
351 EXPECT_EQ(m.capacity(), 32);
352 k = 0;
353 for (size_t i = 0; i < m.size(); ++i, ++k)
354 EXPECT_EQ(m[i], i);
355 EXPECT_EQ(k, m.size());
356 }
357
358 // copy assigment
359 MemArray<int> aux;
360 EXPECT_TRUE(aux.is_empty());
361 EXPECT_EQ(aux.size(), 0);
362 EXPECT_NE(aux.capacity(), 0);
363 EXPECT_NE(aux.get_ptr(), nullptr);
364
365 aux = m;
366 EXPECT_FALSE(aux.is_empty());
367 EXPECT_NE(m.size(), 0);
368 EXPECT_EQ(aux.size(), m.size());
369 EXPECT_EQ(aux.capacity(), m.capacity());
370 EXPECT_FALSE(m.is_empty());
371 EXPECT_NE(m.size(), 0);
372 EXPECT_NE(m.capacity(), 0);
373 EXPECT_NE(m.get_ptr(), aux.get_ptr()); // array of zero must be nullptr
374 k = 0;
375 for (size_t i = 0; i < m.size(); ++i, ++k)
376 EXPECT_EQ(aux[i], m[i]);
377 EXPECT_EQ(k, m.size());
378
379 // TODO move assigment
380}
381
382TEST(MemArray, zero_capacity)
383{
384 MemArray<int> m(0);
385 EXPECT_NE(m.capacity(), 0);
386 EXPECT_EQ(m.size(), 0);
387 EXPECT_NE(m.get_ptr(), nullptr);
388 EXPECT_TRUE(m.is_empty());
389}
390
391TEST(MemArray, insertion_with_rvalues)
392{
394 EXPECT_EQ(m.size(), 0);
395 EXPECT_TRUE(m.is_empty());
396
397 size_t N = 0;
398 for (size_t i = 0; i < 10; ++i)
399 {
401 EXPECT_TRUE(l.is_empty());
402 for (size_t k = 0; k < 10; ++k, ++N)
403 l.append(N);
404 EXPECT_FALSE(l.is_empty());
405 m.insert(move(l));
406 EXPECT_TRUE(l.is_empty());
407 }
408
409 size_t n = 0;
410 for (long i = 9; i >= 0; --i) // descending for matching values of N
411 {
412 const DynList<int> &l = m[i];
413 EXPECT_FALSE(l.is_empty());
414 for (auto it = l.get_it(); it.has_curr(); it.next(), ++n)
415 EXPECT_EQ(it.get_curr(), n);
416 }
417}
418
419TEST(MemArray, remove_with_rvalues)
420{
421 constexpr size_t num_items = 10;
423 size_t N = 0;
424 for (size_t i = 0; i < num_items; ++i)
425 {
427 EXPECT_TRUE(l.is_empty());
428 for (size_t k = 0; k < num_items; ++k, ++N)
429 l.insert(N);
430 EXPECT_FALSE(l.is_empty());
431 m.insert(move(l));
432 EXPECT_TRUE(l.is_empty());
433 }
434
435 size_t n = N - 1;
436 for (size_t i = 0; i < num_items; ++i)
437 {
438 DynList<int> l = m.remove_first();
439 auto it = l.get_it();
440 for (size_t k = 0; k < 10; ++k, it.next(), --n)
441 EXPECT_EQ(it.get_curr(), n);
442 assert(num_items - i < m.capacity()); // hard assert. Better leave it!
443 // The removed slot lies beyond the logical size: inspect it with
444 // access(), since operator() only reaches live items. Container
445 // annotations report any read of that slot, so skip it then.
446#ifndef ALEPH_MEMARRAY_ANNOTATIONS
447 EXPECT_TRUE(m.access(num_items - i - 1).is_empty()); // verify moving
448#endif
449 }
450}
451
453{
454 for (size_t i = 0; i < n; ++i)
455 m.append(i);
456
457 EXPECT_EQ(m.capacity(), n);
458 EXPECT_EQ(m.capacity(), m.size());
459
460 size_t N = m.capacity();
461 for (size_t i = 0; i < n; ++i)
462 {
463 EXPECT_EQ(m.remove_last(), n - i - 1);
464 if (m.size() == N / 4 - 1 and m.size() > m.contract_threshold)
465 {
466 N /= 2;
467 EXPECT_EQ(m.capacity(), N); // valid if contraction was done!
468 }
469 }
470}
471
472TEST_F(Default_MemArray, remove_on_empty)
473{
474 EXPECT_THROW(m.remove_last(), underflow_error);
475 EXPECT_THROW(m.remove_first(), underflow_error);
476 EXPECT_THROW(m.get(0), underflow_error);
477 EXPECT_THROW(m.get(), underflow_error);
478 EXPECT_THROW(m.get(2), underflow_error);
479}
480
481TEST(MemArray, reverse_empty_and_single_item)
482{
484 EXPECT_NO_THROW(m.reverse());
485 EXPECT_TRUE(m.is_empty());
486
487 m.append(7);
488 EXPECT_NO_THROW(m.reverse());
489 EXPECT_EQ(m.size(), 1u);
490 EXPECT_EQ(m[0], 7);
491}
492
493TEST(MemArray, putn_rejects_capacity_overflow)
494{
496 EXPECT_THROW(m.putn(std::numeric_limits<size_t>::max()), overflow_error);
497}
498
499TEST(MemArray, as_stack)
500{
502
503 EXPECT_THROW(m.top(), underflow_error);
504
505 for (size_t i = 0; i < 100; ++i)
506 EXPECT_EQ(m.push(i), i);
507
508 for (size_t i = 100; i > 0; --i)
509 EXPECT_EQ(m.pop(), i - 1);
510
511 EXPECT_TRUE(m.is_empty());
512 ASSERT_EQ(m.size(), 0);
513 EXPECT_THROW(m.top(), underflow_error);
514 EXPECT_THROW(m.pop(), underflow_error);
515}
516
517TEST(MemArray, Iterator_on_empty_container)
518{
519 MemArray<int> empty_m;
520 MemArray<int>::Iterator it = empty_m;
521 EXPECT_FALSE(it.has_curr());
522 EXPECT_THROW(it.get_curr(), overflow_error);
523 EXPECT_THROW(it.next(), overflow_error);
524 EXPECT_THROW(it.prev(), underflow_error);
525 it.reset();
526 EXPECT_FALSE(it.has_curr());
527 EXPECT_THROW(it.get_curr(), overflow_error);
528 EXPECT_THROW(it.next(), overflow_error);
529 EXPECT_THROW(it.prev(), underflow_error);
530 it.reset_last();
531 EXPECT_FALSE(it.has_curr());
532 EXPECT_THROW(it.get_curr(), underflow_error);
533 EXPECT_THROW(it.next(), overflow_error);
534 EXPECT_THROW(it.prev(), underflow_error);
535}
536
538{
539 int i = 0;
540 for (MemArray<int>::Iterator it = m; it.has_curr(); it.next(), ++i)
541 EXPECT_EQ(it.get_curr(), i);
542
544 it.reset_last();
545 i = n - 1;
546 for (MemArray<int>::Iterator it = m; it.has_curr(); it.prev(), --i)
547 EXPECT_EQ(it.get_curr(), i);
548}
549
550TEST(MemArray, traverse_on_empty_container)
551{
553 size_t n = 0;
554 auto ret = m.traverse([&n](int)
555 {
556 ++n;
557 return true;
558 });
559 EXPECT_TRUE(ret);
560 EXPECT_EQ(n, 0);
561}
562
564{
565 const size_t cap = m.capacity();
566
567 // Test 1: clear on empty
568 EXPECT_TRUE(m.is_empty());
569 EXPECT_EQ(m.size(), 0);
570
571 static_assert(noexcept(m.clear()), "clear() must be noexcept");
572 m.clear();
573
574 EXPECT_TRUE(m.is_empty());
575 EXPECT_EQ(m.size(), 0);
576 EXPECT_EQ(m.capacity(), cap);
577 EXPECT_NE(m.get_ptr(), nullptr);
578
579 // Test 2: clear on populated
580 for (size_t i = 0; i < 10; ++i)
581 m.append(i);
582
583 EXPECT_FALSE(m.is_empty());
584 EXPECT_EQ(m.size(), 10);
585 const size_t cap_before = m.capacity();
586 const int* ptr_before = m.get_ptr();
587
588 m.clear();
589
590 EXPECT_TRUE(m.is_empty());
591 EXPECT_EQ(m.size(), 0);
592 EXPECT_EQ(m.capacity(), cap_before);
593 EXPECT_EQ(m.get_ptr(), ptr_before);
594
595 // Verify it can be reused
596 m.append(100);
597 EXPECT_EQ(m.size(), 1);
598 EXPECT_EQ(m[0], 100);
599}
600
602{
603 int N = 0;
604 auto ret = m.traverse([&N, this](int i)
605 {
606 ++N;
607 return i == n / 2;
608 }); // m is empty
609 EXPECT_TRUE(ret);
610 EXPECT_EQ(N, 0);
611
612 for (size_t i = 0; i < n; ++i)
613 m.append(i);
614
615 EXPECT_EQ(N, 0);
616 EXPECT_TRUE(m.size() > 0);
617 EXPECT_EQ(m.size(), n);
618 ret = m.traverse([&N, this](int i)
619 {
620 ++N;
621 return i < n / 2;
622 });
623 EXPECT_FALSE(ret);
624 EXPECT_EQ(N, n / 2 + 1);
625}
626
627// operator() reaches live items only: debug builds assert i < n. access()
628// still reaches every reserved slot (ArrayQueue relies on it).
629#ifndef NDEBUG
630TEST(MemArray, call_operator_asserts_a_live_index_in_debug_builds)
631{
632 MemArray<int> a(16);
633 a.put(1);
634 a.put(2);
635 EXPECT_EQ(a(1), 2);
636 EXPECT_EQ(a.capacity(), 16U);
637 EXPECT_DEATH((void) a(2), "i < n");
638 EXPECT_NO_FATAL_FAILURE((void) a.access(2));
639}
640#endif
641
642#ifdef ALEPH_MEMARRAY_ANNOTATIONS
643// With ALEPH_ANNOTATE_CONTAINERS and AddressSanitizer, the reserved slots
644// beyond the logical size are poisoned: a raw read of slot size() is reported
645// as container-overflow after every operation that changes the size or the
646// buffer, while the legitimate accesses of those operations are not.
647
649static void read_slot(const string *raw, size_t i)
650{
651 const volatile char *byte = reinterpret_cast<const volatile char *>(raw + i);
652 volatile char sink = *byte;
653 (void) sink;
654}
655
657static void expect_overflow_at_size(const MemArray<string> &a)
658{
659 ASSERT_LT(a.size(), a.capacity());
660 if (a.size() > 0)
661 read_slot(a.get_ptr(), a.size() - 1); // live: no report
662 EXPECT_DEATH(read_slot(a.get_ptr(), a.size()), "container-overflow");
663}
664
665TEST(MemArray, asan_annotations_follow_size_and_buffer_changes)
666{
667 MemArray<string> a(16);
668 expect_overflow_at_size(a); // empty after construction
669 for (int i = 0; i < 5; ++i)
670 a.put(to_string(i)); // put
671 EXPECT_EQ(a(4), "4");
672 expect_overflow_at_size(a);
673
674 for (int i = 5; i < 40; ++i)
675 a.append(to_string(i)); // growth by expand()
676 expect_overflow_at_size(a);
677
678 for (int i = 0; i < 30; ++i)
679 (void) a.get(); // shrink by contract()
680 EXPECT_EQ(a.size(), 10U);
681 expect_overflow_at_size(a);
682
683 a.putn(3); // logical growth in place
684 a(12) = "putn";
685 expect_overflow_at_size(a);
686
687 a.push("front"); // open_gap()
688 EXPECT_EQ(a(0), "front");
689 EXPECT_EQ(a.remove_first(), "front"); // close_gap()
690 expect_overflow_at_size(a);
691
692 a.reserve(256); // reallocation
693 EXPECT_EQ(a(12), "putn");
694 expect_overflow_at_size(a);
695
696 MemArray<string> copy(a); // copy construction
697 expect_overflow_at_size(copy);
698 MemArray<string> assigned(4);
699 assigned = a; // copy assignment
700 expect_overflow_at_size(assigned);
701 MemArray<string> moved(std::move(copy)); // move construction
702 expect_overflow_at_size(moved);
703 assigned.swap(moved);
704 expect_overflow_at_size(assigned);
705 expect_overflow_at_size(moved);
706
707 a.empty(); // logical emptying
708 expect_overflow_at_size(a);
709 a.empty_and_release(); // release and reallocation
710 expect_overflow_at_size(a);
711 a.put("again");
712 EXPECT_EQ(a(0), "again");
713}
714#endif
715
716static_assert(std::is_nothrow_move_constructible_v<MemArray<int>>);
717
718TEST(MemArray, moved_from_storage_recovers_on_every_insertion_path)
719{
720 for (int operation = 0; operation < 10; ++operation)
721 {
722 SCOPED_TRACE(operation);
723 MemArray<int> source(16);
724 source.append(17);
725 MemArray<int> moved(std::move(source));
726 EXPECT_EQ(source.size(), 0u);
727 EXPECT_EQ(source.capacity(), 0u);
728 EXPECT_FALSE(MemArray<int>::Iterator(source).has_curr());
729 EXPECT_TRUE(source.traverse([](int) { return false; }));
730 const int value = 9;
731 switch (operation)
732 {
733 case 0: source.put(value); break;
734 case 1: source.put(9); break;
735 case 2: source.append(value); break;
736 case 3: source.append(9); break;
737 case 4: source.insert(value); break;
738 case 5: source.insert(9); break;
739 case 6: source.push(value); break;
740 case 7: source.putn(1); source(0) = 9; break;
741 case 8: source.reserve(16); source.put(9); break;
742 case 9: source.empty_and_release(); source.put(9); break;
743 }
744 ASSERT_EQ(source.size(), 1u);
745 EXPECT_GE(source.capacity(), MemArray<int>::Min_Dim);
746 EXPECT_EQ(source(0), 9);
747 EXPECT_EQ(moved(0), 17);
748 }
749}
750
751TEST(MemArray, copying_and_moving_an_empty_moved_from_array)
752{
753 MemArray<int> source;
754 source.put(7);
755 MemArray<int> moved(std::move(source));
756 MemArray<int> copy(source);
757 MemArray<int> moved_again(std::move(source));
758 MemArray<int> assigned;
759 assigned = source;
760 EXPECT_TRUE(copy.is_empty());
761 EXPECT_TRUE(moved_again.is_empty());
762 EXPECT_TRUE(assigned.is_empty());
763 EXPECT_THROW(source[0], out_of_range);
764 EXPECT_THROW(source.get(), underflow_error);
765 moved_again.put(8);
766 EXPECT_EQ(moved_again(0), 8);
767 EXPECT_EQ(moved(0), 7);
768}
769
770TEST(MemArray, boolean_relocation_reads_only_logical_initialized_elements)
771{
772 MemArray<bool> a(32);
773 a.put(true);
774 a.reserve(64);
775 EXPECT_TRUE(a(0));
776 EXPECT_TRUE(a.get()); // contraction with no remaining logical elements
777 a.put(true);
778 a.putn(64); // growth must not read fresh uninitialized slots
779 for (size_t i = 1; i < a.size(); ++i)
780 a(i) = false;
781 EXPECT_TRUE(a(0));
782 EXPECT_FALSE(a(64));
783}
784
785namespace
786{
788 struct ThrowingArrayItem
789 {
790 static inline int live = 0;
791 static inline int assignments_left = -1;
792 int value = 0;
793
795 ThrowingArrayItem() { ++live; }
797 ThrowingArrayItem(const ThrowingArrayItem &other) : value(other.value) { ++live; }
799 ThrowingArrayItem(ThrowingArrayItem &&other) noexcept : value(other.value) { ++live; }
801 ~ThrowingArrayItem() { --live; }
802
804 ThrowingArrayItem &operator=(const ThrowingArrayItem &other)
805 {
806 if (assignments_left == 0)
807 throw std::runtime_error("element assignment failed");
808 if (assignments_left > 0)
809 --assignments_left;
810 value = other.value;
811 return *this;
812 }
813
815 ThrowingArrayItem &operator=(ThrowingArrayItem &&other)
816 {
817 return *this = static_cast<const ThrowingArrayItem &>(other);
818 }
819 };
820}
821
822TEST(MemArray, failed_assignment_releases_temporary_storage_and_restores_annotations)
823{
824 ASSERT_EQ(ThrowingArrayItem::live, 0);
825 {
827 a.putn(3);
828 a(0).value = 7;
829 const int original_live = ThrowingArrayItem::live;
830
831 ThrowingArrayItem::assignments_left = 1;
832 EXPECT_THROW(a.reserve(128), std::runtime_error);
833 EXPECT_EQ(ThrowingArrayItem::live, original_live);
834 EXPECT_EQ(a.size(), 3u);
835 EXPECT_EQ(a.capacity(), 16u);
836
837 ThrowingArrayItem::assignments_left = 1;
838 EXPECT_THROW({ MemArray<ThrowingArrayItem> copy(a); }, std::runtime_error);
839 EXPECT_EQ(ThrowingArrayItem::live, original_live);
840
841 ThrowingArrayItem::assignments_left = 0;
842 EXPECT_THROW(a.put(a(0)), std::runtime_error);
843 EXPECT_EQ(a.size(), 3u);
844#ifdef ALEPH_MEMARRAY_ANNOTATIONS
845 EXPECT_EQ(__sanitizer_verify_contiguous_container(
846 a.get_ptr(), a.get_ptr() + a.size(), a.get_ptr() + a.capacity()), 1);
847#endif
848 ThrowingArrayItem::assignments_left = -1;
849 a.put(a(0));
850 EXPECT_EQ(a(3).value, 7);
851 }
852 EXPECT_EQ(ThrowingArrayItem::live, 0);
853}
854
855namespace
856{
859 struct Failing_Slot
860 {
861 static inline int constructions_left = -1;
862 std::string value;
863
866 {
867 if (constructions_left == 0)
868 throw std::bad_alloc();
869 if (constructions_left > 0)
871 }
872
874 explicit Failing_Slot(std::string v) : value(std::move(v)) {}
875 };
876
878 std::string long_string(const char c)
879 {
880 return std::string(40, c);
881 }
882}
883
884TEST(MemArray, put_of_own_element_copies_it_before_growing)
885{
886 MemArray<int> a(4);
887 for (int i : {10, 20, 30, 40})
888 a.put(i);
889 ASSERT_EQ(a.size(), a.capacity()); // the next put grows the array
890
891 EXPECT_EQ(a.put(a(0)), 10); // used to read the freed buffer
892 ASSERT_EQ(a.size(), 5u);
893 for (int i = 0; i < 4; ++i)
894 EXPECT_EQ(a(i), 10 * (i + 1));
895 EXPECT_EQ(a(4), 10);
896
897 EXPECT_EQ(a.put(a(1)), 20); // with free capacity
898 EXPECT_EQ(a(5), 20);
899}
900
901TEST(MemArray, put_of_own_moved_element_inserts_its_value)
902{
904 for (char c : {'a', 'b', 'c', 'd'})
905 a.put(long_string(c));
906 ASSERT_EQ(a.size(), a.capacity());
907
908 EXPECT_EQ(a.put(std::move(a(1))), long_string('b'));
909 ASSERT_EQ(a.size(), 5u);
910 EXPECT_EQ(a(0), long_string('a'));
911 EXPECT_EQ(a(3), long_string('d'));
912
913 EXPECT_EQ(a.put(std::move(a(2))), long_string('c')); // with free capacity
914 EXPECT_EQ(a(4), long_string('b'));
915}
916
917TEST(MemArray, push_of_own_element_inserts_its_value)
918{
919 MemArray<int> a(4);
920 for (int i : {10, 20, 30})
921 a.put(i);
922
923 EXPECT_EQ(a.push(a(1)), 20); // the gap used to overwrite the 20 first
924 ASSERT_EQ(a.size(), 4u);
925 const int with_room[] = {20, 10, 20, 30};
926 for (size_t i = 0; i < 4; ++i)
927 EXPECT_EQ(a(i), with_room[i]);
928
929 ASSERT_EQ(a.size(), a.capacity());
930 EXPECT_EQ(a.push(a(3)), 30); // used to read the freed buffer
931 ASSERT_EQ(a.size(), 5u);
932 const int grown[] = {30, 20, 10, 20, 30};
933 for (size_t i = 0; i < 5; ++i)
934 EXPECT_EQ(a(i), grown[i]);
935}
936
937TEST(MemArray, push_of_own_moved_element_inserts_its_value)
938{
940 for (char c : {'a', 'b', 'c'})
941 a.put(long_string(c));
942
943 EXPECT_EQ(a.push(std::move(a(1))), long_string('b'));
944 ASSERT_EQ(a.size(), 4u);
945 EXPECT_EQ(a(1), long_string('a'));
946 EXPECT_EQ(a(3), long_string('c'));
947
948 ASSERT_EQ(a.size(), a.capacity());
949 EXPECT_EQ(a.push(std::move(a(3))), long_string('c'));
950 ASSERT_EQ(a.size(), 5u);
951 EXPECT_EQ(a(1), long_string('b'));
952 EXPECT_EQ(a(2), long_string('a'));
953}
954
955TEST(MemArray, failed_copy_while_growing_leaves_the_array_unchanged)
956{
957 ASSERT_EQ(ThrowingArrayItem::live, 0);
958 {
960 for (int v = 1; v <= 4; ++v)
961 {
962 ThrowingArrayItem item;
963 item.value = v;
964 a.put(item);
965 }
966 ASSERT_EQ(a.size(), a.capacity());
967 const int original_live = ThrowingArrayItem::live;
968
969 ThrowingArrayItem::assignments_left = 0; // copying the item fails
970 EXPECT_THROW(a.put(a(0)), std::runtime_error);
971 EXPECT_THROW(a.push(a(0)), std::runtime_error);
972 ThrowingArrayItem::assignments_left = -1;
973
974 EXPECT_EQ(ThrowingArrayItem::live, original_live);
975 ASSERT_EQ(a.size(), 4u);
976 EXPECT_EQ(a.capacity(), 4u);
977 for (size_t i = 0; i < 4; ++i)
978 EXPECT_EQ(a(i).value, static_cast<int>(i) + 1);
979#ifdef ALEPH_MEMARRAY_ANNOTATIONS
980 EXPECT_EQ(__sanitizer_verify_contiguous_container(
981 a.get_ptr(), a.get_ptr() + a.size(), a.get_ptr() + a.capacity()), 1);
982#endif
983 a.put(a(0));
984 EXPECT_EQ(a(4).value, 1);
985 }
986 EXPECT_EQ(ThrowingArrayItem::live, 0);
987}
988
989TEST(MemArray, failed_growth_leaves_a_moved_argument_untouched)
990{
992 for (char c : {'a', 'b', 'c', 'd'})
994 ASSERT_EQ(a.size(), a.capacity());
995
997 Failing_Slot::constructions_left = 0; // the new buffer cannot be built
998 EXPECT_THROW(a.put(std::move(x)), std::bad_alloc);
999 EXPECT_THROW(a.push(std::move(x)), std::bad_alloc);
1001
1002 EXPECT_EQ(x.value, long_string('x'));
1003 ASSERT_EQ(a.size(), 4u);
1004 for (size_t i = 0; i < 4; ++i)
1005 EXPECT_EQ(a(i).value, long_string(static_cast<char>('a' + i)));
1006
1007 a.put(std::move(x));
1008 EXPECT_EQ(a(4).value, long_string('x'));
1009}
1010
1011namespace
1012{
1014 template <class T>
1015 struct Full_Ring : public MemArray<T>
1016 {
1018
1021 static T of(const char c)
1022 {
1023 if constexpr (std::is_scalar_v<T>)
1024 return c;
1025 else if constexpr (std::is_trivially_copyable_v<T>)
1026 return T{double(c), -double(c)};
1027 else
1028 return T(1, c);
1029 }
1030
1032 static T item(const size_t i) { return of(static_cast<char>('a' + i)); }
1033
1035 Full_Ring(const size_t cap, const size_t first) : MemArray<T>(cap)
1036 {
1037 for (size_t i = 0; i < cap; ++i)
1038 this->put(T());
1039 for (size_t i = 0; i < cap; ++i)
1040 (*this)((first + i) % cap) = item(i);
1041 }
1042 };
1043
1045 template <class T>
1046 void check_growth_of_rings()
1047 {
1048 const T inserted = Full_Ring<T>::of('#');
1049 for (size_t first = 0; first < 8; ++first)
1050 for (size_t pos = 0; pos <= 8; ++pos)
1051 {
1052 Full_Ring<T> ring(8, first);
1053 ASSERT_EQ(ring.size(), ring.capacity());
1054 T &ret = ring.grow_and_insert(pos, inserted, first);
1055 EXPECT_EQ(&ret, &ring(pos));
1056 ASSERT_EQ(ring.size(), 9u);
1057 EXPECT_EQ(ring.capacity(), 16u);
1058 for (size_t i = 0; i < 9; ++i)
1059 EXPECT_EQ(ring(i), i == pos ? inserted :
1060 Full_Ring<T>::item(i < pos ? i : i - 1))
1061 << "first " << first << ", pos " << pos << ", i " << i;
1062 }
1063 }
1064}
1065
1066// The queues grow from any slot and at their end, and the arrays, from
1067// the first slot and at any end. The growth is one for all of them.
1068TEST(MemArray, growth_inserts_at_any_position_of_a_sequence_from_any_slot)
1069{
1070 check_growth_of_rings<char>(); // a scalar goes to the growth as a copy
1071 check_growth_of_rings<string>(); // and what is not, as it comes
1072}
1073
1074// A copy has the items and the capacity of its original, whatever its size:
1075// a trivially copyable type goes by memcpy() from 5 items on, and the
1076// others item by item
1077namespace
1078{
1079 struct Pair_Of_Doubles
1080 {
1081 double x = 0, y = 0;
1082 bool operator == (const Pair_Of_Doubles &) const = default;
1083 };
1084
1085 template <class T, class Make>
1086 void check_copies_of_every_size(Make make)
1087 {
1088 for (size_t n = 0; n <= 40; ++n)
1089 {
1090 MemArray<T> a;
1091 for (size_t i = 0; i < n; ++i)
1092 a.append(make(i));
1093 const MemArray<T> copy(a);
1094 ASSERT_EQ(copy.size(), n);
1095 EXPECT_EQ(copy.capacity(), a.capacity()) << n << " items";
1096 for (size_t i = 0; i < n; ++i)
1097 EXPECT_EQ(copy(i), a(i)) << n << " items, item " << i;
1098 EXPECT_TRUE(n == 0 or &copy(0) != &a(0)) << n << " items";
1099 }
1100 }
1101}
1102
1103TEST(MemArray, copies_have_the_items_and_the_capacity_of_every_size)
1104{
1105 static_assert(std::is_trivially_copyable_v<Pair_Of_Doubles>);
1106 check_copies_of_every_size<int>([] (size_t i) { return int(i * 7); });
1107 check_copies_of_every_size<char>([] (size_t i) { return char('a' + i % 26); });
1108 check_copies_of_every_size<Pair_Of_Doubles>([] (size_t i)
1109 {
1110 return Pair_Of_Doubles{double(i), -double(i) / 4};
1111 });
1112 check_copies_of_every_size<string>([] (size_t i) { return string(i + 20, 'x'); });
1113}
1114
1115// The growth moves a struct with memcpy(); the struct goes to it by
1116// reference, so that the item that it inserts may be one of the array
1117TEST(MemArray, growth_of_a_trivially_copyable_struct_from_any_slot)
1118{
1119 check_growth_of_rings<Pair_Of_Doubles>();
1120}
1121
1122namespace
1123{
1127 template <int K>
1128 struct Assigned_By_Template
1129 {
1130 int value = 0;
1131 static inline int assignments = 0;
1132
1133 Assigned_By_Template() = default;
1134
1136 Assigned_By_Template(const int v) : value(v) {}
1137
1138 Assigned_By_Template(const Assigned_By_Template &) = default;
1139 Assigned_By_Template(Assigned_By_Template &&) = default;
1140 Assigned_By_Template &operator = (const Assigned_By_Template &)
1141 requires (K < 0) = default;
1142 Assigned_By_Template &operator = (Assigned_By_Template &&)
1143 requires (K < 0) = default;
1144
1146 template <class U>
1147 requires std::same_as<std::remove_cvref_t<U>, Assigned_By_Template>
1148 Assigned_By_Template &operator = (U && a)
1149 {
1150 ++assignments;
1151 value = a.value;
1152 return *this;
1153 }
1154 };
1155}
1156
1157// A copy and a growth go through an assignment that is not trivial, as
1158// with a few items, whatever the type traits say of the copy
1159TEST(MemArray, a_type_whose_assignment_is_not_trivial_is_copied_and_grown_through_it)
1160{
1161 using T = Assigned_By_Template<0>;
1162 static_assert(not std::is_trivially_copy_assignable_v<T>);
1163 MemArray<T> a(8);
1164 for (int i = 0; i < 8; ++i)
1165 a.append(T(i));
1166 T::assignments = 0;
1167 const MemArray<T> copy(a);
1168 EXPECT_EQ(T::assignments, 8);
1169 T::assignments = 0;
1170 a.append(T(8)); // full: it grows
1171 EXPECT_EQ(T::assignments, 9);
1172 for (int i = 0; i < 9; ++i)
1173 EXPECT_EQ(a(i).value, i);
1174}
static string long_string(const char c)
A string too long for the small-string buffer.
size_t size_t int32_t value
Definition ca-c-api.h:116
T & get_curr() const
Get the current item with bounds checking.
Definition array_it.H:268
void reset_last() noexcept
Reset the iterator to the last item.
Definition array_it.H:330
void reset() noexcept
Reset the iterator to the first item.
Definition array_it.H:317
void prev()
Move to the previous item with bounds checking.
Definition array_it.H:310
void next()
Advance to the next item with bounds checking.
Definition array_it.H:290
bool has_curr() const noexcept
Check if there is a current valid item.
Definition array_it.H:231
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
T & insert(const T &item)
Definition htlist.H:1308
constexpr bool is_empty() const noexcept
Definition htlist.H:434
Simple, scalable and fast dynamic array.
T & put(const T &item)
Put a copy of item at the end of the sequence.
void reserve(const size_t cap)
Reserves cap cells into the array.
size_t size() const noexcept
Return the number of elements.
void putn(const size_t more)
Reserve more additional logical slots in the array.
T & grow_and_insert(const size_t pos, U &&item, const size_t first=0)
Grow a full array and insert one item: relocate_and_insert().
T & append(const T &item)
See put(const T & item).
void empty_and_release()
Clear the array and reduce allocated storage to Min_Dim.
T get(const size_t i=1)
Remove i elements from the end.
T & push(const T &item)
Push a copy of item at the beginning of the sequence.
constexpr size_t capacity() const noexcept
The type of element of array.
bool is_empty() const noexcept
Return true is the array is empty.
void empty() noexcept
Empties the container.
T & access(const size_t i) noexcept
Return a modifiable reference to the ith element.
T * get_ptr() noexcept
Return the current base of array.
T remove_first()
Remove the first item. Gap is closed.
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
void swap(ODhashTable &other) noexcept
Swaps this table with other in constant time.
Definition tpl_odhash.H:470
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
Key * append(const Key &key)
Alias for insert() (copy version).
Definition hashDry.H:733
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Definition hashDry.H:954
constexpr size_t capacity() const noexcept
Returns the current capacity of the table.
Definition hashDry.H:964
void clear()
Empties the container.
Definition hashDry.H:949
constexpr bool is_empty() const noexcept
Checks if the table is empty.
Definition hashDry.H:959
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Definition hashDry.H:507
#define TEST(name)
#define N
Definition fib.C:294
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_dim_function > > dim(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4130
bool traverse(Node *root, Op op)
See infix_traverse().
Singly linked list implementations with head-tail access.
bool is_power_of_two(size_t x)
Definition memarray.cc:53
TEST_F(Default_MemArray, growing_in_2_powers)
Definition memarray.cc:139
Main namespace for Aleph-w library functions.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
and
Check uniqueness with explicit hash + equality functors.
bool has_curr() const noexcept
Return true if all underlying iterators are positioned on a valid item.
Definition ah-zip.H:173
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
STL namespace.
Simple iterator on elements of array.
const size_t n
Definition memarray.cc:60
MemArray< int > m
Definition memarray.cc:61
Element whose default construction, that of every slot of a new buffer, can fail as an allocation doe...
static int constructions_left
Failing_Slot()
Fail at the configured default construction.
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation, typename Aleph::concepts_detail::traverse_arg< Container, Operation >::type, Container & >())
Traverse the container via its iterator and performs a conditioned operation on each item.
Definition ah-dry.H:411
MemArray< int > m
Definition memarray.cc:66
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dnode< int > Test
Definition testDnode.C:42
static int * k
Simple, scalable, contiguous dynamic array.
DynList< int > l