Skip to content

Commit 8ba0bc1

Browse files
author
Julian LALU
committed
Add all test to hashset
1 parent c0fe5e9 commit 8ba0bc1

12 files changed

+23575
-98
lines changed

test/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,18 @@ set( src
9191
hashset/hashset_constructors.cpp
9292
hashset/hashset_contains.cpp
9393
hashset/hashset_copy_assign_operator.cpp
94+
hashset/hashset_copy_constructors.cpp
95+
hashset/hashset_destructeur.cpp
96+
hashset/hashset_find.cpp
9497
hashset/hashset_iterator.cpp
9598
hashset/hashset_misc.cpp
99+
hashset/hashset_move_assign_operator.cpp
100+
hashset/hashset_move_constructors.cpp
101+
hashset/hashset_rehash.cpp
102+
hashset/hashset_remove.cpp
103+
hashset/hashset_reserve.cpp
104+
hashset/hashset_shrink_to_fit.cpp
105+
hashset/hashset_swap.cpp
96106
i128/i128_assign.cpp
97107
i128/i128_cast.cpp
98108
i128/i128_comparisons.cpp

test/hashset/hashset_copy_assign_operator.cpp

Lines changed: 98 additions & 98 deletions
Large diffs are not rendered by default.

test/hashset/hashset_copy_constructors.cpp

Lines changed: 5832 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <core/containers/hashset.h>
2+
#include "../misc/allocator_watcher.h"
3+
4+
GTEST_TEST(hashset, destructor_call_elements_destructors)
5+
{
6+
7+
using key_type = hud_test::non_bitwise_type;
8+
using hashset_type = hud::hashset<key_type>;
9+
10+
const auto test = []()
11+
{
12+
hashset_type set;
13+
14+
const usize COUNT = 4;
15+
i32 *dtor_assigned_key_counter = nullptr;
16+
17+
dtor_assigned_key_counter = hud::memory::allocate_array<i32>(COUNT);
18+
hud::memory::set_memory_zero_safe(dtor_assigned_key_counter, COUNT * sizeof(i32));
19+
20+
hud_test::LeakArrayGuard guard_assigned_key_counter(dtor_assigned_key_counter, COUNT);
21+
22+
bool all_keys_destructor_are_not_called = true;
23+
24+
{
25+
hashset_type set;
26+
set.reserve(COUNT);
27+
for (i32 i = 0; i < COUNT; i++)
28+
{
29+
set.add(hud::tag_piecewise_construct, hud::forward_as_tuple(i, dtor_assigned_key_counter + i));
30+
}
31+
32+
// Ensure element's destructors are not called
33+
for (i32 i = 0; i < COUNT; i++)
34+
{
35+
const auto it = set.find(i);
36+
if (*it->key().destructor_counter() != 0)
37+
{
38+
all_keys_destructor_are_not_called = false;
39+
}
40+
}
41+
}
42+
43+
return std::tuple {
44+
all_keys_destructor_are_not_called, // 0
45+
};
46+
};
47+
48+
// Non constant
49+
{
50+
const auto result = test();
51+
hud_assert_true(std::get<0>(result));
52+
}
53+
54+
// Constant
55+
{
56+
constexpr auto result = test();
57+
hud_assert_true(std::get<0>(result));
58+
}
59+
}

test/hashset/hashset_find.cpp

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#include <core/containers/hashset.h>
2+
#include <core/templates/integer_sequence.h>
3+
4+
GTEST_TEST(hashset, find_in_empty_hashset)
5+
{
6+
hud::hashset<usize> set;
7+
hud_assert_eq(set.find(1), set.end());
8+
}
9+
10+
GTEST_TEST(hashset, find_in_hashset_trivial_type)
11+
{
12+
13+
hud_test::for_each_value<std::make_integer_sequence<usize, 64>>()(
14+
[]<usize maxx>()
15+
{
16+
const auto test = [](usize max)
17+
{
18+
using KeyType = usize;
19+
hud::hashset<KeyType> set;
20+
21+
for (u32 index = 0; index < max; index++)
22+
{
23+
set.add(index);
24+
}
25+
26+
bool is_find_ok = true;
27+
// Find elements that exists
28+
for (u32 index = 0; index < max; index++)
29+
{
30+
const auto it = set.find(index);
31+
if (it == set.end())
32+
{
33+
is_find_ok = false;
34+
}
35+
if (it->key() != index)
36+
{
37+
is_find_ok = false;
38+
}
39+
}
40+
41+
// Find elements that not exists
42+
for (u32 index = max; index < max * 2; index++)
43+
{
44+
const auto it = set.find(index);
45+
if (it != set.end())
46+
{
47+
is_find_ok = false;
48+
}
49+
}
50+
return is_find_ok;
51+
};
52+
53+
// Non constant
54+
{
55+
const auto result = test(maxx);
56+
hud_assert_true(result);
57+
}
58+
59+
// Constant
60+
{
61+
constexpr auto result = test(maxx);
62+
hud_assert_true(result);
63+
}
64+
}
65+
);
66+
}
67+
68+
GTEST_TEST(hashset, find_in_hashset_non_trivial_type)
69+
{
70+
hud_test::for_each_value<std::make_integer_sequence<usize, 64>>()(
71+
[]<usize maxx>()
72+
{
73+
const auto test_1 = [](usize max)
74+
{
75+
using KeyType = hud_test::non_bitwise_type;
76+
hud::hashset<KeyType> set;
77+
78+
for (u32 index = 0; index < max; index++)
79+
{
80+
set.add(index);
81+
}
82+
83+
bool is_find_ok = true;
84+
// Find elements that exists
85+
for (u32 index = 0; index < max; index++)
86+
{
87+
const auto it = set.find(index);
88+
if (it == set.end())
89+
{
90+
is_find_ok = false;
91+
}
92+
if (it->key() != index)
93+
{
94+
is_find_ok = false;
95+
}
96+
}
97+
98+
// Find elements that not exists
99+
for (u32 index = max; index < max * 2; index++)
100+
{
101+
const auto it = set.find(index);
102+
if (it != set.end())
103+
{
104+
is_find_ok = false;
105+
}
106+
}
107+
return is_find_ok;
108+
};
109+
110+
const auto test_2 = [](usize max)
111+
{
112+
using KeyType = hud_test::non_bitwise_type;
113+
hud::hashset<KeyType> set;
114+
115+
for (u32 index = 0; index < max; index++)
116+
{
117+
set.add(index);
118+
}
119+
120+
bool is_find_ok = true;
121+
// Find elements that exists
122+
for (u32 index = 0; index < max; index++)
123+
{
124+
const auto it = set.find(KeyType(index));
125+
if (it == set.end())
126+
{
127+
is_find_ok = false;
128+
}
129+
if (it->key() != index)
130+
{
131+
is_find_ok = false;
132+
}
133+
}
134+
135+
// Find elements that not exists
136+
for (u32 index = max; index < max * 2; index++)
137+
{
138+
const auto it = set.find(KeyType(index));
139+
if (it != set.end())
140+
{
141+
is_find_ok = false;
142+
}
143+
}
144+
return is_find_ok;
145+
};
146+
147+
// Non constant
148+
{
149+
const auto result = test_1(maxx);
150+
hud_assert_true(result);
151+
auto result_2 = test_2(maxx);
152+
hud_assert_true(result_2);
153+
}
154+
155+
// Constant
156+
{
157+
constexpr auto result = test_1(maxx);
158+
hud_assert_true(result);
159+
constexpr auto result_2 = test_2(maxx);
160+
hud_assert_true(result_2);
161+
}
162+
}
163+
);
164+
}

0 commit comments

Comments
 (0)