38#include <gtest/gtest.h>
51 bool operator()(
int a,
int b)
const noexcept
53 auto aa = a < 0 ? -a : a;
54 auto bb = b < 0 ? -b : b;
63 bool operator()(
int a,
int b)
const noexcept
76TEST(DynArraySet, DefaultConstructEmpty)
79 EXPECT_EQ(s.
size(), 0u);
81 EXPECT_EQ(s.
search(1),
nullptr);
83 EXPECT_EQ(s.
count(1), 0u);
86TEST(DynArraySet, InsertSearchAndContains)
91 ASSERT_NE(p1,
nullptr);
93 EXPECT_EQ(s.
size(), 1u);
96 ASSERT_NE(p2,
nullptr);
98 EXPECT_EQ(s.
size(), 2u);
101 ASSERT_NE(f10,
nullptr);
107TEST(DynArraySet, DuplicatesAreAllowed)
114 EXPECT_EQ(s.
size(), 3u);
115 EXPECT_EQ(s.
count(7), 3u);
116 EXPECT_NE(s.
search(7),
nullptr);
119TEST(DynArraySet, FindThrowsWhenMissing)
124 EXPECT_NO_THROW((
void)s.
find(1));
125 EXPECT_THROW((
void)s.
find(2), std::domain_error);
135 EXPECT_EQ(s.
count(1), 2u);
137 EXPECT_EQ(s.
count(1), 1u);
138 EXPECT_EQ(s.
size(), 2u);
141 EXPECT_EQ(s.
count(1), 0u);
142 EXPECT_EQ(s.
size(), 1u);
145 EXPECT_EQ(s.
size(), 1u);
157 EXPECT_EQ(s.
count(5), 3u);
159 EXPECT_EQ(removed, 3u);
160 EXPECT_EQ(s.
count(5), 0u);
161 EXPECT_EQ(s.
size(), 2u);
166TEST(DynArraySet, CustomEqualityPredicate)
174 EXPECT_TRUE(s.contains(10));
175 EXPECT_TRUE(s.contains(-10));
178 EXPECT_EQ(s.count(10), 2u);
181 auto *p = s.search(10);
182 ASSERT_NE(p,
nullptr);
183 EXPECT_TRUE(*p == 10 || *p == -10);
186TEST(DynArraySet, SetEqualChangesSearchSemantics)
194 EXPECT_TRUE(s.contains(20));
195 EXPECT_EQ(s.count(20), 1u);
197 s.set_equal(ModEqual{11});
200 EXPECT_FALSE(s.contains(20));
203TEST(DynArraySet, InheritedDynArrayApiStillWorks)
210 EXPECT_EQ(s.
size(), 3u);
217 EXPECT_EQ(s.
size(), 2u);
Set-like container backed by a dynamic array.
T & find(const T &key)
Find and return a reference to the first matching element.
T * search(const T &key) noexcept
Search for the first element equal to key.
size_t count(const T &key) const noexcept
Count how many occurrences of key exist.
bool remove_one(const T &key)
Remove a single occurrence of key.
size_t remove_all(const T &key)
Remove all occurrences of key.
bool contains(const T &key) const noexcept
Check whether key is present.
T * insert(const T &item)
Insert item (duplicates are allowed).
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) noexcept
Fast access without checking allocation and bound_min_clock checking.
T & get_first()
Return a modifiable reference to the first item of array (as if this was a queue)
T & get_last()
Return a modifiable reference to the last item of array (as if this was a queue)
T & append()
Allocate a new entry to the end of array.
bool is_empty() const noexcept
Return true if the array is empty.
void remove(T &item)
Given a valid reference to an item in the array, it removes it and decrease the dimension.
Main namespace for Aleph-w library functions.
double mod(double a, double b)