Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
logical_bounds.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11*/
12
29#include <cstddef>
30#include <cstdlib>
31#include <cstring>
32#include <iostream>
33#include <string>
34#include <utility>
35
36#include <aleph-version.H>
37#include <tpl_array.H>
38#include <tpl_arrayQueue.H>
39#include <tpl_memArray.H>
40
41#ifdef ALEPH_MEMARRAY_ANNOTATIONS
42# include <sanitizer/asan_interface.h>
43#endif
44
45using Aleph::Array;
47using Aleph::MemArray;
48
49// What the package declares is what this translation unit got from the
50// usage requirements of Aleph::Aleph.
51#ifdef ALEPH_MEMARRAY_ANNOTATIONS
52static_assert(EXPECT_CONTAINER_ANNOTATIONS == 1,
53 "MemArray is annotated but the package does not declare it");
54static_assert(Aleph::Build_Info::container_annotations);
55#else
56static_assert(EXPECT_CONTAINER_ANNOTATIONS == 0,
57 "the package declares container annotations but MemArray is not annotated");
58static_assert(not Aleph::Build_Info::container_annotations);
59#endif
60
61namespace
62{
63 int failures = 0;
64
65 void check(const bool condition, const char *step, const char *what)
66 {
67 if (condition)
68 return;
69 ++failures;
70 std::cerr << "FAILED after " << step << ": " << what << '\n';
71 }
72
74 void check_bounds(const MemArray<std::string> & a, const char *step)
75 {
76 check(a.size() <= a.capacity(), step, "size() <= capacity()");
77#ifdef ALEPH_MEMARRAY_ANNOTATIONS
78 const std::string *raw = a.get_ptr();
79 for (size_t i = 0; i < a.capacity(); ++i)
80 {
81 const char *first = reinterpret_cast<const char *>(raw + i);
82 const char *last = reinterpret_cast<const char *>(raw + i + 1) - 1;
83 const bool poisoned = __asan_address_is_poisoned(first) != 0
84 and __asan_address_is_poisoned(last) != 0;
85 const bool addressable = __asan_address_is_poisoned(first) == 0
86 and __asan_address_is_poisoned(last) == 0;
87 if (i < a.size())
88 check(addressable, step, "a live slot is addressable");
89 else
90 check(poisoned, step, "a reserved slot beyond size() is poisoned");
91 }
92#endif
93 }
94
96 [[noreturn]] void read_beyond_size(const MemArray<std::string> & a)
97 {
98 if (a.size() >= a.capacity())
99 {
100 std::cout << "no reserved slot to read: the read was not reported\n";
101 std::exit(EXIT_FAILURE);
102 }
103 const volatile char *byte = reinterpret_cast<const volatile char *>(a.get_ptr() + a.size());
104 volatile char sink = *byte;
105 (void) sink;
106 std::cout << "the read was not reported\n";
107 std::exit(EXIT_FAILURE);
108 }
109
111 void memarray_sequence(const char *stop_at)
112 {
113 const auto at = [stop_at] (const char *step) { return std::strcmp(stop_at, step) == 0; };
114
116 check_bounds(a, "construction");
117
118 for (int i = 0; i < 5; ++i)
119 a.put(std::to_string(i));
120 check(a.size() == 5 and a(4) == "4", "put", "the items are stored");
121 check_bounds(a, "put");
122 if (at("after_put"))
123 read_beyond_size(a);
124
125 for (int i = 5; i < 40; ++i)
126 a.append(std::to_string(i));
127 check(a.size() == 40 and a(39) == "39", "growth", "the items are stored");
128 check_bounds(a, "growth");
129 if (at("after_growth"))
130 read_beyond_size(a);
131
132 for (int i = 0; i < 30; ++i)
133 (void) a.get();
134 check(a.size() == 10 and a(9) == "9", "shrink", "the first items remain");
135 check_bounds(a, "shrink");
136 if (at("after_shrink"))
137 read_beyond_size(a);
138
139 a.putn(3);
140 a(12) = "putn";
141 check_bounds(a, "putn");
142
143 a.push("front");
144 check(a(0) == "front", "push", "the item is first");
145 check(a.remove_first() == "front", "remove_first", "the first item is returned");
146 check_bounds(a, "push and remove_first");
147
148 a.reserve(256);
149 check(a.capacity() >= 256 and a(12) == "putn", "reserve", "the items are kept");
150 check_bounds(a, "reserve");
151
153 check(copy.size() == a.size() and copy(12) == "putn", "copy", "the items are copied");
154 check_bounds(copy, "copy construction");
155
156 MemArray<std::string> assigned(4);
157 assigned = a;
158 check_bounds(assigned, "copy assignment");
159
160 MemArray<std::string> moved(std::move(copy));
161 check(moved.size() == a.size() and moved(12) == "putn", "move", "the items are moved");
162 check_bounds(moved, "move construction");
163 check_bounds(copy, "move construction (source)");
164 if (at("after_move"))
165 read_beyond_size(moved);
166
167 MemArray<std::string> small(8);
168 small.put("small");
169 small.swap(moved);
170 check(small.size() == a.size() and moved.size() == 1 and moved(0) == "small",
171 "swap", "the contents are exchanged");
172 check_bounds(small, "swap");
173 check_bounds(moved, "swap (other)");
174 if (at("after_swap"))
175 read_beyond_size(moved);
176
177 // A moved-from array is reused.
178 copy.put("reused");
179 check(copy.size() == 1 and copy(0) == "reused", "reuse", "a moved-from array is reused");
180 check_bounds(copy, "reuse of a moved-from array");
181
182 a.empty();
183 check(a.size() == 0, "empty", "the array is empty");
184 check_bounds(a, "empty");
185
187 check_bounds(a, "empty_and_release");
188 a.put("again");
189 check(a.size() == 1 and a(0) == "again", "reuse", "a released array is reused");
190 check_bounds(a, "reuse of a released array");
191 if (at("after_reuse"))
192 read_beyond_size(a);
193 }
194
195 void array_sequence()
196 {
198 for (int i = 0; i < 100; ++i)
199 a.append(i);
200 long sum = 0;
201 for (auto it = a.get_it(); it.has_curr(); it.next_ne())
202 sum += it.get_curr();
203 check(a.size() == 100 and sum == 4950, "Array", "append and iteration");
204
205 Array<int> moved(std::move(a));
206 a.append(7);
207 check(moved.size() == 100 and a.size() == 1 and a(0) == 7, "Array", "moved-from reuse");
208 }
209
210 void queue_sequence()
211 {
212 ArrayQueue<int> q(8);
213 for (int i = 0; i < 8; ++i)
214 q.put(i);
215 for (int i = 0; i < 5; ++i)
216 check(q.get() == i, "ArrayQueue", "FIFO order");
217 for (int i = 8; i < 13; ++i)
218 q.put(i); // the storage wraps around
219 check(q.size() == 8 and q.front() == 5 and q.rear() == 12, "ArrayQueue wrap-around",
220 "front and rear");
221 for (size_t i = 0; i < q.size(); ++i)
222 {
223 check(q.front(i) == static_cast<int>(5 + i), "ArrayQueue wrap-around", "front(i)");
224 check(q.rear(i) == static_cast<int>(12 - i), "ArrayQueue wrap-around", "rear(i)");
225 }
226
228 check(copy.size() == 8 and copy.front() == 5 and copy.rear() == 12, "ArrayQueue copy",
229 "a wrapped queue is copied in order");
230
231 q.reserve(64);
232 check(q.size() == 8 and q.front() == 5 and q.rear() == 12, "ArrayQueue reserve",
233 "a wrapped queue is reserved in order");
234 for (int i = 13; i < 60; ++i)
235 q.put(i);
236 for (int i = 5; i < 60; ++i)
237 check(q.get() == i, "ArrayQueue growth", "FIFO order");
238 check(q.size() == 0, "ArrayQueue", "empty after draining");
239
240 ArrayQueue<int> moved(std::move(copy));
241 check(moved.size() == 8 and moved.front() == 5, "ArrayQueue move", "the items are moved");
242 copy.put(1);
243 check(copy.size() == 1 and copy.front() == 1, "ArrayQueue move", "moved-from reuse");
244
245 moved.empty();
246 moved.put(2);
247 check(moved.size() == 1 and moved.front() == 2, "ArrayQueue empty", "reuse");
248 moved.empty_and_release();
249 moved.put(3);
250 moved.put(4);
251 check(moved.size() == 2 and moved.front() == 3 and moved.rear() == 4,
252 "ArrayQueue empty_and_release", "reuse");
253 }
254}
255
256int main(int argc, char **argv)
257{
258 const char *stop_at = argc > 1 ? argv[1] : "";
259
260 memarray_sequence(stop_at);
261 if (stop_at[0] != '\0')
262 {
263 std::cout << "unknown case '" << stop_at << "': the read was not reported\n";
264 return EXIT_FAILURE;
265 }
266
267 array_sequence();
268 queue_sequence();
269
270 if (failures != 0)
271 {
272 std::cerr << failures << " check(s) failed\n";
273 return EXIT_FAILURE;
274 }
275
276 std::cout << "logical bounds: ok (container annotations "
277 << (Aleph::Build_Info::container_annotations ? "on" : "off") << ")\n";
278 return EXIT_SUCCESS;
279}
int main()
Queue implemented with a single dynamic array.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
static Array create_reserved(const size_t cap)
Build an empty array with room for at least cap items.
Definition tpl_array.H:194
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
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 & 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.
void empty() noexcept
Empties the container.
T * get_ptr() noexcept
Return the current base of array.
T remove_first()
Remove the first item. Gap is closed.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
and
Check uniqueness with explicit hash + equality functors.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
bool check(GT &g)
Circular queue implementations backed by arrays.
Dynamic array container with automatic resizing.
Simple, scalable, contiguous dynamic array.