Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testQueue.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32
33# include <iostream>
34# include <string>
35# include <cstdlib>
36# include <cerrno>
37# include <climits>
38# include <stdexcept>
39# include <tpl_arrayQueue.H>
40
41using namespace std;
42
43int g_count = -1;
44
45struct Foo
46{
47 int * ptr;
48
49 void swap(Foo & f)
50 {
51 std::swap(ptr, f.ptr);
52 }
53
54 Foo() : ptr(nullptr)
55 {
56 ptr = new int;
57 *ptr = ::g_count--;
58 }
59
60 Foo(int i) : ptr(nullptr)
61 {
62 ptr = new int;
63 *ptr = i;
64 }
65
66 Foo(const Foo & f) : ptr(nullptr)
67 {
68 if (f.ptr != nullptr)
69 {
70 ptr = new int;
71 *ptr = *f.ptr;
72 }
73 }
74
75 Foo(Foo && f) : ptr(nullptr)
76 {
77 std::swap(ptr, f.ptr);
78 }
79
80 Foo & operator = (const Foo & f)
81 {
82 if (this == &f)
83 return *this;
84
85 // Handle null pointers safely
86 if (f.ptr == nullptr)
87 {
88 delete ptr;
89 ptr = nullptr;
90 }
91 else if (ptr == nullptr)
92 {
93 ptr = new int;
94 *ptr = *f.ptr;
95 }
96 else
97 {
98 *ptr = *f.ptr;
99 }
100
101 return *this;
102 }
103
105 {
106 swap(f);
107 return *this;
108 }
109
111 {
112 if (ptr != nullptr)
113 {
114 delete ptr;
115 ptr = nullptr;
116 }
117 }
118
119 // operator int () { return *ptr; }
120
121 operator int () const
122 {
123 if (ptr == nullptr)
124 throw std::logic_error("attempt to access value of moved-from Foo");
125 return *ptr;
126 }
127};
128
129template <typename T>
130void print(const ArrayQueue<T> & q)
131{
132 cout << "capacity = " << q.capacity() << endl
133 << "size = " << q.size() << endl;
134
135 for (size_t i = 0; i < q.size(); ++i)
136 cout << (T) q.front(i) << " ";
137 cout << endl ;
138
139 for (size_t i = 0; i < q.size(); ++i)
140 cout << (T) q.rear(i) << " ";
141 cout << endl
142 << endl;
143}
144
145template <typename T>
147{
148 cout << "Creating rval queue ";
150 for (int i = 0; i < n; ++i)
151 cout << q.put(T(i)) << " ";
152 cout << endl;
153
154 return q;
155}
156
157int main(int argc, char * argv[])
158{
159 if (argc == 1)
160 {
161 cout << "testQueue -- exercises ArrayQueue with insert, consult, and delete operations\n"
162 << "\n"
163 << "Creates an ArrayQueue<int> of the given size, fills it, reads back\n"
164 << "elements, drains in steps of 3 until underflow, refills, then deletes\n"
165 << "a specified number of items. Finally tests copy and move constructors\n"
166 << "with both int and a heap-allocating Foo type.\n"
167 << "\n"
168 << "Usage:\n"
169 << " " << argv[0] << " <queue-size> <items-to-delete>\n"
170 << "\n"
171 << "Arguments:\n"
172 << " queue-size Capacity of the queue and number of items to insert\n"
173 << " items-to-delete Number of items to remove in the second phase\n"
174 << "\n"
175 << "Example:\n"
176 << " " << argv[0] << " 20 7\n"
177 << " Creates a queue of capacity 20, inserts 20 items, then deletes 7.\n";
178 return 0;
179 }
180
181 if (argc < 3)
182 {
183 cerr << "Usage: " << argv[0] << " <queue-size> <items-to-delete>" << endl;
184 cerr << "Both arguments must be non-negative integers." << endl;
185 return 1;
186 }
187
188 // Parse first argument (queue size)
189 char *endptr1;
190 errno = 0;
191 long n_long = strtol(argv[1], &endptr1, 10);
192
193 // Validate first argument
195 {
196 cerr << "Error: Invalid queue size argument. Must be a non-negative integer fitting in int." << endl;
197 return 1;
198 }
199
200 int n = static_cast<int>(n_long);
201 ArrayQueue<int> q(n);
202
203 print(q);
204
205 cout << "Inserting " << n << " values ";
206 for (size_t i = 0; i < n; ++i)
207 cout << q.put(i) << " ";
208 cout << " done!" << endl << endl;
209
210 print(q);
211
212 cout << "Consulting all values until underflow ";
213 for (int i = 0; 1; i++)
214 {
215 try
216 {
217 int val = q.rear(i);
218 cout << val << " " ;
219 }
220 catch (std::range_error & exc)
221 {
222 cout << endl << exc.what() << endl;
223 break;
224 }
225 }
226 cout << " done! " << endl << endl;
227
228 cout << "Deleting all values in steps of 3 until underflow ";
229 while (1)
230 {
231 try
232 {
233 printf("%d ", q.getn(3));
234 }
235 catch (std::underflow_error & exc)
236 {
237 cout << endl << exc.what() << endl;
238 break;
239 }
240 }
241 cout << " done! " << endl << endl;
242
243 print(q);
244
245 cout << "Inserting " << n << " values " << endl;
246 for (size_t i = 0; i < n; ++i)
247 cout << q.put(i) << " ";
248 cout << " done!" << endl << endl;
249
250 print(q);
251
252 // Parse second argument (items to delete)
253 char *endptr2;
254 errno = 0;
255 long m_long = strtol(argv[2], &endptr2, 10);
256
257 // Validate second argument
259 {
260 cerr << "Error: Invalid items-to-delete argument. Must be a non-negative integer fitting in int." << endl;
261 return 1;
262 }
263
264 int m = static_cast<int>(m_long);
265
266 cout << "Deleting " << m << " items" << endl;
267 for (int i = 0; i < m; i++)
268 {
269 try
270 {
271 cout << q.get() << " ";
272 }
273 catch (std::underflow_error & exc)
274 {
275 cout << endl << exc.what() << endl;
276 break;
277 }
278 }
279
280 cout << "done!" << endl << endl;
281
282 cout << "q = " << endl;
283 print(q);
284
285 cout << "Testing constructors ... " << endl;
286
287 ArrayQueue<int> q1 = q;
288
289 print(q1);
290
292 print(q2);
293
294
296 print(q3);
297
298 printf("Ended\n");
299}
int main()
Queue implemented with a single dynamic array.
T & front(const size_t i=0) const
Return the i-th oldest item of the queue.
T & getn(const size_t i)
Remove the i oldest items of the queue.
T & put(const T &item)
Copy and put an item in the queue.
T & rear(const size_t i=0) const
Return the i-th youngest item of the queue.
T get()
Remove the oldest item of the queue and return a copy.
size_t size() const noexcept
Return the number of elements.
constexpr size_t capacity() const noexcept
The type of element of array.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
STL namespace.
int i
void swap(Foo &f)
Definition testQueue.C:49
Foo()
Definition testQueue.C:54
std::unique_ptr< int > ptr
Foo(Foo &&f)
Definition testQueue.C:75
Foo(const Foo &f)
Definition testQueue.C:66
~Foo()
Definition testQueue.C:110
int * ptr
Definition testQueue.C:47
Foo & operator=(const Foo &f)
Foo(int i)
Definition testQueue.C:60
int g_count
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
int g_count
Definition testQueue.C:43
ArrayQueue< T > create_queue(int n)
Definition testQueue.C:146
void print(const ArrayQueue< T > &q)
Definition testQueue.C:130
Circular queue implementations backed by arrays.