Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dynarrayheap_allocation_test.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
42#include <gtest/gtest.h>
43
44#include "allocation_failure.H"
45
46#include <new>
47#include <string>
48#include <vector>
49
50#include <tpl_dynArrayHeap.H>
51
52using namespace Aleph;
53
54namespace
55{
56 // The tests need the operator new of allocation_failure.H
57 struct DynArrayHeapWithoutMemory : public ::testing::Test
58 {
59 void SetUp() override
60 {
62 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
63 }
64 };
65
66 using Heap = DynArrayHeap<std::string>;
67
68 // Longer than the buffer of a short string: copying it allocates
69 std::string item(const size_t k)
70 {
71 return std::string(40, 'x') + std::to_string(k);
72 }
73
74 // The array of the heap, in its order
75 std::vector<std::string> layout(const Heap & h)
76 {
77 std::vector<std::string> out;
78 h.traverse([&out](const std::string & s) { out.push_back(s); return true; });
79 return out;
80 }
81
82 // On a heap of n items, the allocation k of add fails, for each k until
83 // add needs no more. The heap must stay as it was, with the key, and
84 // then take it with memory. Returns how many allocations add needs
85 template <class Add>
86 size_t allocations_as_it_was(const size_t n, Add add)
87 {
88 for (size_t k = 1; ; ++k)
89 {
90 SCOPED_TRACE(testing::Message() << "size " << n << ", allocation " << k);
91 Heap heap;
92 for (size_t i = 1; i <= n; ++i)
93 heap.insert(item(i));
94 const std::vector<std::string> before = layout(heap);
95 std::string key = item(0);
96
97 bool thrown = false;
98 {
99 Fail_At failing(k);
100 try
101 {
102 add(heap, key);
103 }
104 catch (const std::bad_alloc &)
105 {
106 thrown = true;
107 }
108 }
109
110 if (not thrown)
111 {
112 EXPECT_EQ(heap.size(), n + 1);
113 return k - 1;
114 }
115 EXPECT_EQ(heap.size(), n);
116 EXPECT_EQ(layout(heap), before);
117 EXPECT_EQ(key, item(0));
118 add(heap, key);
119 EXPECT_EQ(heap.size(), n + 1);
120 EXPECT_EQ(heap.top(), item(0));
121 }
122 }
123
124 // The sizes up to 70 are in the first block of the array, which the
125 // first insertion allocates with its segment. A heap that fills that
126 // block is the other case: its next item is the first of another block,
127 // one allocation more, which fails with the items in the heap
128 template <class Add>
129 void expect_as_it_was(Add add)
130 {
131 size_t in_the_block = 0;
132 for (size_t n = 0; n <= 70; ++n)
133 in_the_block = allocations_as_it_was(n, add);
134
135 const size_t block_size = DynArray<std::string>().get_block_size();
136 ASSERT_GT(block_size, 71U); // the items are the entries from 1 on
137 EXPECT_GT(allocations_as_it_was(block_size - 1, add), in_the_block);
138 }
139}
140
141TEST_F(DynArrayHeapWithoutMemory, InsertOfACopyLeavesTheHeapAsItWas)
142{
143 expect_as_it_was([](Heap & h, std::string & key) { h.insert(key); });
144}
145
146TEST_F(DynArrayHeapWithoutMemory, InsertOfAMoveLeavesTheHeapAsItWas)
147{
148 expect_as_it_was([](Heap & h, std::string & key) { h.insert(std::move(key)); });
149}
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
long double h
Definition btreepic.C:154
size_t size_t int32_t * out
Definition ca-c-api.h:120
Dynamic heap (priority queue) backed by DynArray.
size_t get_block_size() const noexcept
Return the block size.
TEST_F(DynArrayHeapWithoutMemory, InsertOfACopyLeavesTheHeapAsItWas)
Main namespace for Aleph-w library functions.
double add(double a, double b)
static int * k
Array-based dynamic binary heap.