Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dynarray.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_dynArray.H>
41
42using namespace Aleph;
43using namespace std;
44
45namespace {
46
48{
49 DynArray<int> arr;
50 EXPECT_TRUE(arr.is_empty());
51 EXPECT_EQ(arr.size(), 0u);
52
53 arr.append(42);
54 EXPECT_EQ(arr.size(), 1u);
56 EXPECT_EQ(arr.access(0), 42);
57
58 arr.append(7);
59 EXPECT_EQ(arr.size(), 2u);
60 EXPECT_EQ(arr.access(1), 7);
61}
62
64{
65 DynArray<int> arr;
67
68 arr.reserve(0, 3);
69 ASSERT_EQ(arr.size(), 4u);
70 for (size_t i = 0; i < arr.size(); ++i)
71 EXPECT_EQ(arr.access(i), 123);
72
73 arr.access(2) = 77;
74 EXPECT_EQ(arr.access(2), 77);
75
76 auto & ref = arr.touch(10);
77 ref = 99;
78 EXPECT_EQ(arr.size(), 11u);
79 EXPECT_EQ(arr.access(10), 99);
80}
81
83{
84 DynArray<int> arr;
85
86 EXPECT_THROW(arr.pop(), std::underflow_error);
87 EXPECT_THROW(arr.top(), std::underflow_error);
88 EXPECT_THROW(arr.get_first(), std::underflow_error);
89 EXPECT_THROW(arr.get_last(), std::underflow_error);
90
91 int dummy = 0;
92 EXPECT_THROW(arr.remove(dummy), std::underflow_error);
93}
94
96{
97 DynArray<int> arr;
98 EXPECT_THROW(arr.reserve(5, 4), std::domain_error);
99}
100
102{
103 DynArray<int> arr;
104 arr.reserve(5);
105 ASSERT_EQ(arr.size(), 5u);
106 for (size_t i = 0; i < arr.size(); ++i)
107 arr.access(i) = static_cast<int>(i * 2);
108 for (size_t i = 0; i < arr.size(); ++i)
109 EXPECT_EQ(arr.access(i), static_cast<int>(i * 2));
110}
111
113{
114 DynArray<int> arr;
115 for (int i = 0; i < 6; ++i)
116 arr.append(i);
117
118 auto it = arr.get_it(3);
119 ASSERT_TRUE(it.has_curr());
120 EXPECT_EQ(it.get_curr(), 3);
121 it.next();
122 EXPECT_EQ(it.get_curr(), 4);
123
124 const auto & carr = arr;
125 auto cit = carr.get_it(5);
126 EXPECT_EQ(cit.get_curr(), 5);
127 EXPECT_THROW(carr.get_it(6), std::out_of_range);
128}
129
131{
132 DynArray<int> arr;
133 arr.adjust(10);
134 EXPECT_EQ(arr.size(), 10u);
135 arr.cut(3);
136 EXPECT_EQ(arr.size(), 3u);
137 arr.empty();
138 EXPECT_TRUE(arr.is_empty());
139 arr.append(1);
140 arr.append(2);
141 arr.cut(2);
142 EXPECT_EQ(arr.size(), 2u);
143}
144
146{
147 DynArray<int> arr;
148 EXPECT_THROW(arr.reserve(4, 3), std::domain_error);
149 EXPECT_EQ(arr.size(), 0u);
150}
151
153{
154 DynArray<int> arr;
155 arr.reserve(2, 5);
156 ASSERT_EQ(arr.size(), 6u);
157 arr.touch(20) = 100;
158 EXPECT_EQ(arr.size(), 21u);
159 arr.cut(6);
160 EXPECT_EQ(arr.size(), 6u);
161}
162
164{
165 DynArray<int> arr;
166 for (int i = 0; i < 5; ++i)
167 arr.push(i);
168 EXPECT_EQ(arr.get_first(), 0);
169 EXPECT_EQ(arr.get_last(), 4);
170
171 arr.insert(-1);
172 EXPECT_EQ(arr.get_first(), -1);
173
174 EXPECT_EQ(arr.pop(), 4);
175 EXPECT_EQ(arr.size(), 5u);
176 EXPECT_EQ(arr.top(), arr.get_last());
177}
178
179} // namespace
void push(const T &data)
void adjust(const size_t dim)
Set a new dimension.
Iterator get_it()
void cut(const size_t new_dim=0)
Cut the array to a new dimension; that is, it reduces the dimension of array and frees the remaining ...
void remove(T &item)
Given a valid reference to an item in the array, it removes it and decrease the dimension.
T & insert(const T &item)
T & get_last() const
Return a modifiable reference to the last item of array (as if this was a queue)
T & get_first() const
Return a modifiable reference to the first item of array (as if this was a queue)
void set_default_initial_value(const T &value) noexcept
Set the default value.
T & touch(const size_t i)
Touch the entry i.
size_t size() const noexcept
Return the current dimension of array.
T pop()
Remove the last item of array (as if this was a stack)
T & access(const size_t i) const noexcept
Fast access without checking allocation and bound_min_clock checking.
T & top() const
Return a modifiable reference to the last item of stack.
T & append()
Allocate a new entry to the end of array.
bool is_empty() const noexcept
Return true if the array is empty.
void empty() noexcept
Empty the array.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:190
#define TEST(name)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Lazy and scalable dynamic array implementation.