36#include <aleph-version.H>
41#ifdef ALEPH_MEMARRAY_ANNOTATIONS
42# include <sanitizer/asan_interface.h>
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);
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);
65 void check(
const bool condition,
const char *step,
const char *what)
70 std::cerr <<
"FAILED after " << step <<
": " << what <<
'\n';
77#ifdef ALEPH_MEMARRAY_ANNOTATIONS
78 const std::string *raw = a.
get_ptr();
79 for (
size_t i = 0; i < a.
capacity(); ++i)
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;
88 check(addressable, step,
"a live slot is addressable");
90 check(poisoned, step,
"a reserved slot beyond size() is poisoned");
100 std::cout <<
"no reserved slot to read: the read was not reported\n";
101 std::exit(EXIT_FAILURE);
103 const volatile char *
byte =
reinterpret_cast<const volatile char *
>(a.
get_ptr() + a.
size());
104 volatile char sink = *byte;
106 std::cout <<
"the read was not reported\n";
107 std::exit(EXIT_FAILURE);
111 void memarray_sequence(
const char *stop_at)
113 const auto at = [stop_at] (
const char *step) {
return std::strcmp(stop_at, step) == 0; };
116 check_bounds(a,
"construction");
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");
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"))
132 for (
int i = 0; i < 30; ++i)
134 check(a.
size() == 10
and a(9) ==
"9",
"shrink",
"the first items remain");
135 check_bounds(a,
"shrink");
136 if (at(
"after_shrink"))
141 check_bounds(a,
"putn");
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");
149 check(a.
capacity() >= 256
and a(12) ==
"putn",
"reserve",
"the items are kept");
150 check_bounds(a,
"reserve");
154 check_bounds(
copy,
"copy construction");
158 check_bounds(assigned,
"copy assignment");
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);
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);
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");
183 check(a.
size() == 0,
"empty",
"the array is empty");
184 check_bounds(a,
"empty");
187 check_bounds(a,
"empty_and_release");
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"))
195 void array_sequence()
198 for (
int i = 0; i < 100; ++i)
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");
207 check(moved.size() == 100
and a.
size() == 1
and a(0) == 7,
"Array",
"moved-from reuse");
210 void queue_sequence()
213 for (
int i = 0; i < 8; ++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)
219 check(q.size() == 8
and q.front() == 5
and q.rear() == 12,
"ArrayQueue wrap-around",
221 for (
size_t i = 0; i < q.size(); ++i)
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)");
229 "a wrapped queue is copied in order");
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)
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");
241 check(moved.size() == 8
and moved.front() == 5,
"ArrayQueue move",
"the items are moved");
243 check(
copy.size() == 1
and copy.front() == 1,
"ArrayQueue move",
"moved-from reuse");
247 check(moved.size() == 1
and moved.front() == 2,
"ArrayQueue empty",
"reuse");
248 moved.empty_and_release();
251 check(moved.size() == 2
and moved.front() == 3
and moved.rear() == 4,
252 "ArrayQueue empty_and_release",
"reuse");
258 const char *stop_at = argc > 1 ? argv[1] :
"";
260 memarray_sequence(stop_at);
261 if (stop_at[0] !=
'\0')
263 std::cout <<
"unknown case '" << stop_at <<
"': the read was not reported\n";
272 std::cerr << failures <<
" check(s) failed\n";
276 std::cout <<
"logical bounds: ok (container annotations "
277 << (Aleph::Build_Info::container_annotations ?
"on" :
"off") <<
")\n";
Queue implemented with a single dynamic array.
Simple dynamic array with automatic resizing and functional operations.
static Array create_reserved(const size_t cap)
Build an empty array with room for at least cap items.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
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.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
and
Check uniqueness with explicit hash + equality functors.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Circular queue implementations backed by arrays.
Dynamic array container with automatic resizing.
Simple, scalable, contiguous dynamic array.