Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
array.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
38#include <gtest/gtest.h>
39
40#include <tpl_array.H>
41
42#include <array>
43#include <numeric>
44#include <string>
45#include <type_traits>
46#include <vector>
47
48using namespace Aleph;
49
50namespace {
51
53{
54 Array<int> arr;
55 EXPECT_TRUE(arr.is_empty());
56 EXPECT_EQ(arr.size(), 0u);
57 EXPECT_THROW(arr.base(), std::underflow_error);
58
60 EXPECT_THROW(empty_const.base(), std::underflow_error);
61
62 arr.append(10);
63 arr.append(20);
65 EXPECT_EQ(arr.size(), 2u);
66 EXPECT_EQ(arr.base(), 10);
67 EXPECT_EQ(arr.get_first(), 10);
68 EXPECT_EQ(arr.get_last(), 20);
69
70 const auto & carr = arr;
72 EXPECT_EQ(carr.get_last(), 20);
73}
74
76{
77 Array<int> arr;
78 arr.append(1);
79 arr.append(2);
80 arr.insert(-1);
81
82 ASSERT_EQ(arr.size(), 3u);
83 EXPECT_EQ(arr.get_first(), -1);
84 EXPECT_EQ(arr.get_last(), 2);
85
86 EXPECT_EQ(arr.remove_first(), -1);
87 EXPECT_EQ(arr.remove_last(), 2);
88 EXPECT_EQ(arr.size(), 1u);
89 EXPECT_EQ(arr.base(), 1);
90
91 arr.empty();
92 EXPECT_TRUE(arr.is_empty());
93}
94
96{
97 Array<int> original = {1, 2, 3, 4};
99 ASSERT_EQ(copy.size(), original.size());
100 copy[0] = 100;
101 EXPECT_EQ(original[0], 1);
102
104 assigned = copy;
105 EXPECT_EQ(assigned.size(), copy.size());
106 EXPECT_EQ(assigned[0], 100);
107
108 Array<int> moved(std::move(copy));
109 EXPECT_EQ(moved.size(), 4u);
110 EXPECT_EQ(moved[0], 100);
111
114 move_assigned = std::move(moved);
117}
118
120{
121 Array<int> arr;
122 const auto initial_cap = arr.capacity();
123 arr.reserve(initial_cap + 50);
124 EXPECT_GE(arr.capacity(), initial_cap + 50);
125
126 arr.putn(5);
127 ASSERT_EQ(arr.size(), 5u);
128 for (size_t i = 0; i < arr.size(); ++i)
129 arr[i] = static_cast<int>(i * 10);
130
132 other.append(-1);
133 arr.swap(other);
134 EXPECT_EQ(arr.size(), 1u);
135 EXPECT_EQ(arr[0], -1);
136 EXPECT_EQ(other.size(), 5u);
137 EXPECT_EQ(other[2], 20);
138}
139
141{
143 arr.append("hello");
144 arr.append("world");
145
146 EXPECT_EQ(arr[0], "hello");
147 EXPECT_EQ(arr(1), "world");
148 EXPECT_THROW(arr[2], std::out_of_range);
149
150 const Array<std::string> carr = arr;
151 EXPECT_EQ(carr[0], "hello");
152 EXPECT_EQ(carr(1), "world");
153 EXPECT_THROW(carr[3], std::out_of_range);
154}
155
157{
158 Array<int> arr;
159 for (int i = 1; i <= 5; ++i)
160 arr.append(i);
161
162 const std::array<int, 5> ascending = {1, 2, 3, 4, 5};
163 const std::array<int, 5> descending = {5, 4, 3, 2, 1};
164
165 arr.reverse();
166 for (size_t i = 0; i < descending.size(); ++i)
167 EXPECT_EQ(arr[i], descending[i]) << "reverse() should mutate in place";
168
169 const Array<int> &carr = arr;
170 const auto copy = carr.reverse();
171 for (size_t i = 0; i < ascending.size(); ++i)
172 EXPECT_EQ(copy[i], ascending[i]) << "const reverse() should return new copy";
173
174 arr.rev();
175 for (size_t i = 0; i < ascending.size(); ++i)
176 EXPECT_EQ(arr[i], ascending[i]) << "rev() alias should behave like reverse()";
177
178 const auto copy_rev = carr.rev();
179 for (size_t i = 0; i < descending.size(); ++i)
180 EXPECT_EQ(copy_rev[i], descending[i]) << "const rev() should return reversed copy";
181}
182
183struct MoveOnlyOp
184{
185 bool *called;
186 explicit MoveOnlyOp(bool *c) : called(c) {}
187 MoveOnlyOp(const MoveOnlyOp &) = delete;
188 MoveOnlyOp & operator=(const MoveOnlyOp &) = delete;
189 MoveOnlyOp(MoveOnlyOp &&) = default;
190 MoveOnlyOp & operator=(MoveOnlyOp &&) = default;
191 bool operator()(int)
192 {
193 *called = true;
194 return true;
195 }
196};
197
199{
200 Array<int> arr = {1, 2, 3, 4};
201
202 int sum = 0;
203 auto accumulate = [&sum](int value)
204 {
205 sum += value;
206 return true;
207 };
209 EXPECT_EQ(sum, 10);
210
211 int visited = 0;
212 auto stop_at_three = [&visited](int value)
213 {
214 ++visited;
215 return value < 3;
216 };
218 EXPECT_EQ(visited, 3);
219
220 bool called = false;
221 EXPECT_TRUE(arr.traverse(MoveOnlyOp(&called)));
222 EXPECT_TRUE(called);
223}
224
226{
227 Array<int> arr = {0, 1, 2, 3};
228 Array<int>::Iterator it(arr);
229
230 int expected = 0;
231 for (; it.has_curr(); it.next())
232 {
233 EXPECT_EQ(it.get_curr(), expected);
234 ++expected;
235 }
237}
238
240{
241 auto arr = build_array<int>(5, 4, 3, 2, 1);
242 EXPECT_EQ(arr.size(), 5u);
243 EXPECT_EQ(arr[0], 5);
244 EXPECT_EQ(arr[4], 1);
245
246 const auto vec = to_stdvector(arr);
247 ASSERT_EQ(vec.size(), arr.size());
248 for (size_t i = 0; i < vec.size(); ++i)
249 EXPECT_EQ(vec[i], arr(i));
250}
251
252namespace
253{
254struct DefaultInit
255{
256 int v;
257 DefaultInit() : v(123) {}
258 explicit DefaultInit(int x) : v(x) {}
259 bool operator==(const DefaultInit &o) const { return v == o.v; }
260};
261}
262
264{
265 const size_t n = 8;
266 const int value = 42;
267 Array<int> arr(n, value);
268 ASSERT_EQ(arr.size(), n);
269 for (size_t i = 0; i < n; ++i)
270 EXPECT_EQ(arr[i], value);
271}
272
274{
275 const size_t n = 6;
276 const std::string value = "abc";
277 Array<std::string> arr(n, value);
278 ASSERT_EQ(arr.size(), n);
279 for (size_t i = 0; i < n; ++i)
280 EXPECT_EQ(arr[i], value);
281}
282
284{
285 const size_t n = 10;
286 auto arr = Array<int>::create(n);
287 static_assert(std::is_trivially_default_constructible_v<int>);
288 ASSERT_EQ(arr.size(), n);
289
290 for (size_t i = 0; i < arr.size(); ++i)
291 arr[i] = static_cast<int>(i * 3);
292 for (size_t i = 0; i < arr.size(); ++i)
293 EXPECT_EQ(arr[i], static_cast<int>(i * 3));
294}
295
297{
298 const size_t n = 7;
299 auto arr = Array<DefaultInit>::create(n);
300 static_assert(!std::is_trivially_default_constructible_v<DefaultInit>);
301 ASSERT_EQ(arr.size(), n);
302 for (size_t i = 0; i < n; ++i)
303 EXPECT_EQ(arr[i].v, 123);
304
305 arr[0] = DefaultInit(7);
306 EXPECT_EQ(arr[0].v, 7);
307}
308
310{
312 pod.putn(3);
313 pod[0] = 1;
314 pod[1] = 2;
315 pod[2] = 3;
316 pod.append(4);
317 ASSERT_EQ(pod.size(), 4u);
318 EXPECT_EQ(pod[3], 4);
319
321 nonpod.putn(2);
322 nonpod[0] = "x";
323 nonpod[1] = "y";
324 nonpod.append("z");
325 ASSERT_EQ(nonpod.size(), 3u);
326 EXPECT_EQ(nonpod[2], "z");
327}
328
329} // namespace
bool operator==(const Time &l, const Time &r)
Definition ah-time.H:133
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:138
static Array create(size_t n)
Create an array with n logical elements.
Definition tpl_array.H:191
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:333
void empty() noexcept
Empty the stack.
Definition tpl_array.H:327
constexpr bool is_empty() const noexcept
Return true if stack is empty.
Definition tpl_array.H:330
T & base()
Return a reference to the first element of array.
Definition tpl_array.H:314
T & insert(const T &data)
insert a copy of data at the beginning of the array.
Definition tpl_array.H:274
Array & rev()
Definition tpl_array.H:401
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:221
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:239
bool traverse(Operation &operation)
Traverse all the items of the stack from the youngest to the oldest and conditionally performs an ope...
Definition tpl_array.H:413
T & get_first() noexcept
return a modifiable reference to the first element.
Definition tpl_array.H:340
Array & reverse()
Reverse the order of items in array.
Definition tpl_array.H:385
T & get_last() noexcept
return a modifiable reference to the last element.
Definition tpl_array.H:348
constexpr size_t capacity() const noexcept
Return the internal capacity.
Definition tpl_array.H:336
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:308
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
Definition tpl_array.H:298
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
T & get_last() const
Return the last item of the list.
Definition htlist.H:1663
T & get_first() const
Return the first item of the list.
Definition htlist.H:1675
DynList & rev() noexcept
Definition htlist.H:1769
DynList & reverse() noexcept
Definition htlist.H:1763
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
LocateFunctions< Container, Type > * base() const
Definition ah-dry.H:172
#define TEST(name)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
T accumulate(Itor beg, Itor end, T initValue)
Accumulate values in a range.
Definition ahAlgo.H:1493
std::vector< typename Container::Item_Type > to_stdvector(const Container &c)
Definition tpl_array.H:464
DynList< T > maps(const C &c, Op op)
Classic map operation.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Iterator on the items of an array.
Definition tpl_array.H:446
Dynamic array container with automatic resizing.