38# include <gtest/gtest.h>
45using namespace testing;
53 for (
size_t i = 0; i < 25; ++i)
102 EXPECT_EQ(fst->to_snodenc<
int>()->get_data(), 0);
104 EXPECT_EQ(list.
size(), 3);
108 EXPECT_EQ(fst->to_data<
int>(), 1);
110 EXPECT_EQ(list.
size(), 2);
114 EXPECT_EQ(fst->to_data<
int>(), 2);
116 EXPECT_EQ(list.
size(), 1);
127 EXPECT_EQ(fst->to_data<
int>(), 3);
130 EXPECT_EQ(list.
size(), 0);
132 EXPECT_THROW(list.
rotate_left(1), std::domain_error);
148 EXPECT_EQ(list.
size(), 2);
159 EXPECT_EQ(list.get_first(), list.get_head());
160 EXPECT_EQ(list.get_last(), list.get_tail());
161 EXPECT_EQ(list.size(), 25);
162 EXPECT_FALSE(list.is_empty());
163 EXPECT_FALSE(list.is_unitarian());
164 EXPECT_FALSE(list.is_unitarian_or_empty());
172 EXPECT_THROW(it.
get_curr(), std::overflow_error);
178 EXPECT_THROW(it.
get_curr(), std::overflow_error);
188 EXPECT_EQ(it.get_curr()->to_snodenc<
int>()->get_data(), i);
189 EXPECT_EQ(it.get_pos(), i - 1);
196 static_assert(
noexcept(it.
del_ne()));
198 EXPECT_EQ(p->
to_data<
int>(), 1);
201 EXPECT_EQ(list.get_first()->to_data<
int>(), 2);
205 EXPECT_EQ(p->
to_data<
int>(), 3);
212 EXPECT_EQ(p->
to_data<
int>(), 25);
215 EXPECT_EQ(list.get_last()->to_data<
int>(), 24);
216 EXPECT_EQ(list.size(), 22u);
220 EXPECT_TRUE(list.is_empty());
228TEST(HTListDeathTest, Iterator_del_ne_asserts_that_there_is_a_current_item)
232 EXPECT_DEATH((
void) on_empty.
del_ne(),
"has_curr");
241 EXPECT_DEATH((
void) past_the_end.
del_ne(),
"has_curr");
250 EXPECT_TRUE(list.is_empty());
251 EXPECT_EQ(
l.
size(), 13);
252 EXPECT_EQ(
r.size(), 12);
254 EXPECT_EQ(
l.
get_last()->to_data<
int>(), 13);
255 EXPECT_EQ(
r.get_first()->to_data<
int>(), 14);
256 EXPECT_EQ(
r.get_last()->to_data<
int>(), 25);
260 EXPECT_EQ(it.get_curr()->to_data<
int>(), i);
263 EXPECT_EQ(it.get_curr()->to_data<
int>(), i);
269 EXPECT_EQ(it.get_curr()->to_data<
int>(), i);
277 EXPECT_TRUE(list.is_empty());
278 EXPECT_EQ(list.size(), 0);
283 EXPECT_EQ(it.get_curr()->to_data<
int>(), i);
291 for (
size_t i = 1; i < 13; ++i, it.
next())
314 EXPECT_EQ(list.reverse(), 25);
317 EXPECT_EQ(it.get_curr()->to_data<
int>(), i);
319 EXPECT_EQ(list.reverse(), 25);
323 EXPECT_EQ(it.get_curr()->to_data<
int>(), i);
328 EXPECT_TRUE(list.is_empty());
335 EXPECT_TRUE(
r.is_empty());
340 EXPECT_EQ(it.get_curr()->to_data<
int>(), i);
349 for (
int i = 0, n = 4; i < 3; ++i, ++n, it.
next())
353 list.rotate_left(22);
356 EXPECT_EQ(it.get_curr()->to_data<
int>(), i);
363 ASSERT_THROW(stack.
top(), underflow_error);
364 ASSERT_THROW(stack.
pop(), underflow_error);
374 ASSERT_EQ(stack.
top(), &n1);
375 ASSERT_EQ(stack.
pop(), &n1);
376 ASSERT_EQ(stack.
top(), &n2);
377 ASSERT_EQ(stack.
pop(), &n2);
381 ASSERT_EQ(stack.
top(), &n3);
382 ASSERT_EQ(stack.
pop(), &n3);
392 static inline bool fail =
false;
393 static inline int moves = 0;
396 Fragile_Move(
int v = 0) noexcept : value(v) {}
397 Fragile_Move(
const Fragile_Move &) =
default;
398 Fragile_Move(Fragile_Move && c) :
value(c.
value)
402 throw std::runtime_error(
"move failed");
404 Fragile_Move & operator = (
const Fragile_Move &) =
default;
405 Fragile_Move & operator = (Fragile_Move &&) =
default;
411static_assert(
noexcept(std::declval<DynList<int> &>().remove_ne()));
412static_assert(not
noexcept(std::declval<DynList<Fragile_Move> &>().remove_ne()));
419 for (
int i = 1; i <= 3; ++i)
420 list.
append(Fragile_Move(i));
422 Fragile_Move::fail =
true;
423 EXPECT_THROW((
void) list.
remove_ne(), std::runtime_error);
424 EXPECT_THROW((
void) list.
remove(), std::runtime_error);
425 EXPECT_THROW((
void) list.
remove_first(), std::runtime_error);
426 EXPECT_THROW((
void) list.
pop(), std::runtime_error);
427 EXPECT_THROW((
void) list.
get(), std::runtime_error);
428 Fragile_Move::fail =
false;
430 ASSERT_EQ(list.
size(), 3u);
432 EXPECT_EQ(list.
remove().value, 1);
433 EXPECT_EQ(list.
size(), 2u);
443 for (
int i = 1; i <= 3; ++i)
444 list.
append(Fragile_Move(i));
446 Fragile_Move::moves = 0;
447 const Fragile_Move first = list.
remove_ne();
448 EXPECT_EQ(Fragile_Move::moves, 1);
449 const Fragile_Move second = list.
remove();
450 EXPECT_EQ(Fragile_Move::moves, 2);
451 EXPECT_EQ(first.value, 1);
452 EXPECT_EQ(second.value, 2);
453 EXPECT_EQ(list.
size(), 1u);
461 for (
int pos = 0; pos < 3; ++pos)
464 for (
int i = 1; i <= 3; ++i)
465 list.
append(Fragile_Move(i));
468 for (
int k = 0;
k < pos; ++
k)
471 Fragile_Move::fail =
true;
472 EXPECT_THROW((
void) it.
del(), std::runtime_error);
473 Fragile_Move::fail =
false;
475 ASSERT_EQ(list.
size(), 3u);
477 EXPECT_EQ(it.
get_curr().value, pos + 1);
479 Fragile_Move::moves = 0;
480 EXPECT_EQ(it.
del().value, pos + 1);
481 EXPECT_EQ(Fragile_Move::moves, 1);
482 ASSERT_EQ(list.
size(), 2u);
483 EXPECT_EQ(list.
get_first().value, pos == 0 ? 2 : 1);
484 EXPECT_EQ(list.
get_last().value, pos == 2 ? 2 : 3);
488 EXPECT_EQ(it.
get_curr().value, pos + 2);
494 EXPECT_THROW((
void) it.
del(), std::overflow_error);
size_t size_t int32_t value
Iterator on the items of list.
T & get_curr() const
Return the current item.
T del()
Remove the current item of the iterator.
Doubly-linked list (defined in tpl_dynList.H).
T & get_first()
Return the first item of the list.
T & append(const T &item)
T remove_ne() noexcept(std::is_nothrow_move_constructible_v< T >)
Remove the first item of the list, which must not be empty.
T & get_last()
Return the last item of the list.
T remove()
Remove the first item of the list.
T remove_first()
See remove().
size_t size() const noexcept
Return the number of items of the list, in constant time.
DynList & reverse() noexcept
std::uint32_t next() noexcept
Slinknc * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
void next_ne() noexcept
Move the iterator one position forward guaranteeing no exception.
void reset_first() noexcept
See reset().
bool is_in_last() const noexcept
Return true if the iterator is positioned on the last item.
bool is_last() const noexcept
Slinknc * get_curr() const
bool has_curr() const noexcept
bool is_in_first() const noexcept
Return true if the iterator is positioned on the first item.
Slinknc * del_ne() noexcept
Remove the current item and move the iterator one position forward, without checking that there is a ...
Single linked list of nodes.
Slinknc * get_first() const noexcept
void push(Slinknc *link) noexcept
void remove_all_and_delete() noexcept
Slinknc * get_tail() const noexcept
void cut(Slinknc *link, HTList &list) noexcept
It cuts 'this' over 'link' element and it puts all.
constexpr bool is_empty() const noexcept
Slinknc * get_head() const noexcept
void rotate_left(size_t n)
Rotate to left the list n positions.
HTList & swap(HTList &l) noexcept
Slinknc * pop()
Remove the first item of this.
void append(Slinknc *link) noexcept
size_t size() const noexcept
Count the number of elements of the list.
void insert(Slinknc *link) noexcept
bool is_unitarian() const noexcept
Return true if the list contains exactly just one.
Slinknc * get_last() const noexcept
Return the last item of the list (nullptr if the list is empty)
bool is_unitarian_or_empty() const noexcept
Return true if list contains one element or is empty.
Link of a single linked list non-circular and without header node.
T & to_data() noexcept
Return the item of the Snodenc<T> that this is.
Snodenc< T > * to_snodenc() noexcept
Convert this to a Snodenc<T>.
T & get_data() noexcept
Return a modifiable reference to the data.
Singly linked list implementations with head-tail access.
TEST_F(List_of_25_nodes, Basic_operations)
Main namespace for Aleph-w library functions.
void reverse(Itor beg, Itor end)
Reverse elements in a range.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
void rotate_left(T *ptr, const size_t n, size_t m) noexcept
Rotate array elements left by m positions.
Fw_Itor remove(Fw_Itor __first, const Fw_Itor &__last, const T &__value)
Remove elements equal to a value.