28#include <jau/test/catch2_ext.hpp>
30TEST_CASE(
"StringHashMapWrap Test 00",
"[hashmap][string][StringHashMapWrap]" ) {
32 const std::string
two =
"two";
33 const std::string_view two2 =
"two";
35 REQUIRE( 0 == map.
size() );
36 REQUIRE(
true == map.
put(
"one", 1) );
37 REQUIRE(
true == map.
put(
two, 2) );
38 REQUIRE( 2 == map.
size() );
40 auto pp = map.
find(two2);
41 REQUIRE(
nullptr != pp );
42 REQUIRE(
two == pp->first );
43 REQUIRE( two2 == pp->first );
44 REQUIRE(
two.data() != pp->first.data() );
46 REQUIRE( 1 == map.
get(
"one") );
47 REQUIRE( 2 == map.
get(two2) );
48 REQUIRE(
false == map.
put(two2, 3) );
49 REQUIRE( 2 == map.
size() );
50 REQUIRE( 3 == map.
get(two2) );
52 REQUIRE( 3 == map.
put3(two2, 4) );
53 REQUIRE( 2 == map.
size() );
54 REQUIRE( 4 == map.
get(two2) );
56 REQUIRE( -1 == map.
put3(
"new", 100) );
57 REQUIRE( 3 == map.
size() );
58 REQUIRE(100 == map.
get(
"new") );
60 REQUIRE(100 == map.
remove2(
"new") );
61 REQUIRE( 2 == map.
size() );
62 REQUIRE( -1 == map.
remove2(
"new") );
63 REQUIRE( 2 == map.
size() );
64 REQUIRE(
true == map.
remove(two2) );
65 REQUIRE( 1 == map.
size() );
66 REQUIRE(
false == map.
remove(two2) );
67 REQUIRE( 1 == map.
size() );
69 REQUIRE(
false == map.
insert(
"one", 1000) );
70 REQUIRE( 1 == map.
size() );
71 REQUIRE( 1 == map.
get(
"one") );
73 REQUIRE( 2 == map.
size() );
74 REQUIRE( 2 == map.
get(two2) );
76 REQUIRE(
true == map.
replace(two2, 3) );
77 REQUIRE( 2 == map.
size() );
78 REQUIRE( 3 == map.
get(two2) );
79 REQUIRE(
false == map.
replace(
"new", 1) );
80 REQUIRE( 2 == map.
size() );
84 int &i2_0 = map.
put2(
"i2", v);
85 int &i2_1 = map.
get(
"i2");
86 const int &i2_2 = map.
get(
"i2");
90 REQUIRE( &i2_0 != &v );
91 REQUIRE( &i2_0 == &i2_1 );
92 REQUIRE( &i2_0 == &i2_2 );
93 REQUIRE( 3 == map.
size() );
97 REQUIRE( 0 == map.
size() );
101TEST_CASE(
"StringViewHashMapWrap Test 00",
"[hashmap][string_view][StringViewHashMapWrap]" ) {
104 const std::string_view
one =
"one";
105 const std::string_view
two =
"two";
107 REQUIRE( 0 == map.
size() );
108 REQUIRE(
true == map.
put(
one, 1) );
109 REQUIRE(
true == map.
put(
two, 2) );
110 REQUIRE( 2 == map.
size() );
113 REQUIRE(
nullptr != pp );
114 REQUIRE(
two == pp->first );
115 REQUIRE(
two.data() == pp->first.data() );
117 REQUIRE( 1 == map.
get(
one) );
118 REQUIRE( 2 == map.
get(
two) );
119 REQUIRE(
false == map.
put(
two, 3) );
120 REQUIRE( 2 == map.
size() );
121 REQUIRE( 3 == map.
get(
two) );
123 REQUIRE( 3 == map.
put3(
two, 4) );
124 REQUIRE( 2 == map.
size() );
125 REQUIRE( 4 == map.
get(
two) );
127 REQUIRE( -1 == map.
put3(
"new", 100) );
128 REQUIRE( 3 == map.
size() );
129 REQUIRE(100 == map.
get(
"new") );
131 REQUIRE(100 == map.
remove2(
"new") );
132 REQUIRE( 2 == map.
size() );
133 REQUIRE( -1 == map.
remove2(
"new") );
134 REQUIRE( 2 == map.
size() );
136 REQUIRE( 1 == map.
size() );
138 REQUIRE( 1 == map.
size() );
140 REQUIRE(
false == map.
insert(
"one", 1000) );
141 REQUIRE( 1 == map.
size() );
142 REQUIRE( 1 == map.
get(
"one") );
144 REQUIRE( 2 == map.
size() );
145 REQUIRE( 2 == map.
get(
two) );
148 REQUIRE( 2 == map.
size() );
149 REQUIRE( 3 == map.
get(
two) );
150 REQUIRE(
false == map.
replace(
"new", 1) );
151 REQUIRE( 2 == map.
size() );
155 const std::string_view i2_k =
"i2";
156 int &i2_0 = map.
put2(i2_k, v);
157 int &i2_1 = map.
get(i2_k);
158 const int &i2_2 = map.
get(i2_k);
159 REQUIRE( v == i2_0 );
160 REQUIRE( v == i2_1 );
161 REQUIRE( v == i2_2 );
162 REQUIRE( &i2_0 != &v );
163 REQUIRE( &i2_0 == &i2_1 );
164 REQUIRE( &i2_0 == &i2_2 );
165 REQUIRE( 3 == map.
size() );
169 REQUIRE( 0 == map.
size() );
173 const std::string one1_storage =
"one";
174 const std::string one2_storage =
"one";
175 const std::string two1_storage =
"two";
176 const std::string two2_storage =
"two";
177 const std::string_view one1(one1_storage);
178 const std::string_view one2(one2_storage);
179 const std::string_view two1(two1_storage);
180 const std::string_view two2(two2_storage);
181 REQUIRE(one1.data() != one2.data());
182 REQUIRE(two1.data() != two2.data());
185 REQUIRE( 0 == map.
size() );
186 REQUIRE(
true == map.
put(one1, 1) );
187 REQUIRE(
true == map.
put(two1, 2) );
188 REQUIRE( 2 == map.
size() );
190 auto pp = map.
find(two2);
191 REQUIRE(
nullptr != pp );
192 REQUIRE( two1 == pp->first );
193 REQUIRE( two2 == pp->first );
194 REQUIRE( two1.data() == pp->first.data() );
195 REQUIRE( two2.data() != pp->first.data() );
197 REQUIRE( 1 == map.
get(one2) );
198 REQUIRE( 2 == map.
get(two2) );
199 REQUIRE(
false == map.
put(two2, 3) );
200 REQUIRE( 2 == map.
size() );
201 REQUIRE( 3 == map.
get(two2) );
203 REQUIRE( 3 == map.
put3(two2, 4) );
204 REQUIRE( 2 == map.
size() );
205 REQUIRE( 4 == map.
get(two2) );
207 REQUIRE( -1 == map.
put3(
"new", 100) );
208 REQUIRE( 3 == map.
size() );
209 REQUIRE(100 == map.
get(
"new") );
211 REQUIRE(100 == map.
remove2(
"new") );
212 REQUIRE( 2 == map.
size() );
213 REQUIRE( -1 == map.
remove2(
"new") );
214 REQUIRE( 2 == map.
size() );
215 REQUIRE(
true == map.
remove(two2) );
216 REQUIRE( 1 == map.
size() );
217 REQUIRE(
false == map.
remove(two2) );
218 REQUIRE( 1 == map.
size() );
220 REQUIRE(
false == map.
insert(
"one", 1000) );
221 REQUIRE( 1 == map.
size() );
222 REQUIRE( 1 == map.
get(
"one") );
223 REQUIRE(
true == map.
insert(two1, 2) );
224 REQUIRE( 2 == map.
size() );
225 REQUIRE( 2 == map.
get(two2) );
227 REQUIRE(
true == map.
replace(two2, 3) );
228 REQUIRE( 2 == map.
size() );
229 REQUIRE( 3 == map.
get(two2) );
230 REQUIRE(
false == map.
replace(
"new", 1) );
231 REQUIRE( 2 == map.
size() );
234 REQUIRE( 0 == map.
size() );
value_type remove2(const query_type &key)
Removes value if mapped and returns it, otherwise returns no_value.
const_reference get(const query_type &key) const
Returns the immutable mapped value reference for the given key or novalue()
reference put2(const key_type &key, const_reference obj)
Maps the value for the given key, overwrites old mapping if exists.
bool put(const key_type &key, const_reference obj)
Maps the value for the given key, overwrites old mapping if exists.
void clear()
Clears the hash map.
bool replace(const key_type &key, const_reference obj)
Replaces the already mapped value for the given key, does nothing if no mapping exists.
size_type size() const noexcept
bool insert(const key_type &key, const_reference obj)
Adds a new mapping of the value for the given key, does nothing if a mapping exists.
value_type put3(const key_type &key, const_reference obj)
Maps the value for the given key, overwrites old mapping if exists.
const pair_type * find(const query_type &key) const
Returns the immutable pair_type pointer for the given key or nullptr.
bool remove(const query_type &key)
Removes value if mapped and returns true, otherwise returns false.
HashMapWrap< std::string, Value_type, Novalue_type, no_value, jau::string_hash, std::string_view > StringHashMapWrap
StringHashMapWrap, generic std::unordered_map exposing a more higher level API.
HashMapWrap< std::string_view, Value_type, Novalue_type, no_value, jau::string_hash > StringViewHashMapWrap
StringHashViewMapWrap, generic std::unordered_map exposing a more higher level API,...
TEST_CASE("StringHashMapWrap Test 00", "[hashmap][string][StringHashMapWrap]")