jaulib v1.4.1-17-gd77ace3-dirty
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
test_collections01.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2025 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#include <cassert>
25#include <cstring>
26
28#include <jau/test/catch2_ext.hpp>
29
30TEST_CASE( "StringHashMapWrap Test 00", "[hashmap][string][StringHashMapWrap]" ) {
31 {
32 const std::string two = "two";
33 const std::string_view two2 = "two";
34 jau::StringHashMapWrap<int, int, -1> map;
35 REQUIRE( 0 == map.size() );
36 REQUIRE( true == map.put("one", 1) );
37 REQUIRE( true == map.put(two, 2) );
38 REQUIRE( 2 == map.size() );
39
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() ); // string key maintains its own storage
45
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) );
51
52 REQUIRE( 3 == map.put3(two2, 4) );
53 REQUIRE( 2 == map.size() );
54 REQUIRE( 4 == map.get(two2) );
55
56 REQUIRE( -1 == map.put3("new", 100) );
57 REQUIRE( 3 == map.size() );
58 REQUIRE(100 == map.get("new") );
59
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() );
68
69 REQUIRE( false == map.insert("one", 1000) );
70 REQUIRE( 1 == map.size() );
71 REQUIRE( 1 == map.get("one") );
72 REQUIRE( true == map.insert(two, 2) );
73 REQUIRE( 2 == map.size() );
74 REQUIRE( 2 == map.get(two2) );
75
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() );
81
82 {
83 const int v = 42;
84 int &i2_0 = map.put2("i2", v);
85 int &i2_1 = map.get("i2");
86 const int &i2_2 = map.get("i2");
87 REQUIRE( v == i2_0 );
88 REQUIRE( v == i2_1 );
89 REQUIRE( v == i2_2 );
90 REQUIRE( &i2_0 != &v );
91 REQUIRE( &i2_0 == &i2_1 );
92 REQUIRE( &i2_0 == &i2_2 );
93 REQUIRE( 3 == map.size() );
94 }
95
96 map.clear();
97 REQUIRE( 0 == map.size() );
98 }
99}
100
101TEST_CASE( "StringViewHashMapWrap Test 00", "[hashmap][string_view][StringViewHashMapWrap]" ) {
102 {
103 // string_view key must be persistent!
104 const std::string_view one = "one";
105 const std::string_view two = "two";
106 jau::StringViewHashMapWrap<int, int, -1> map;
107 REQUIRE( 0 == map.size() );
108 REQUIRE( true == map.put(one, 1) );
109 REQUIRE( true == map.put(two, 2) );
110 REQUIRE( 2 == map.size() );
111
112 auto pp = map.find(two);
113 REQUIRE( nullptr != pp );
114 REQUIRE( two == pp->first );
115 REQUIRE( two.data() == pp->first.data() ); // string_view key uses external storage two
116
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) );
122
123 REQUIRE( 3 == map.put3(two, 4) );
124 REQUIRE( 2 == map.size() );
125 REQUIRE( 4 == map.get(two) );
126
127 REQUIRE( -1 == map.put3("new", 100) );
128 REQUIRE( 3 == map.size() );
129 REQUIRE(100 == map.get("new") );
130
131 REQUIRE(100 == map.remove2("new") );
132 REQUIRE( 2 == map.size() );
133 REQUIRE( -1 == map.remove2("new") );
134 REQUIRE( 2 == map.size() );
135 REQUIRE(true == map.remove(two) );
136 REQUIRE( 1 == map.size() );
137 REQUIRE(false == map.remove(two) );
138 REQUIRE( 1 == map.size() );
139
140 REQUIRE( false == map.insert("one", 1000) );
141 REQUIRE( 1 == map.size() );
142 REQUIRE( 1 == map.get("one") );
143 REQUIRE( true == map.insert(two, 2) );
144 REQUIRE( 2 == map.size() );
145 REQUIRE( 2 == map.get(two) );
146
147 REQUIRE( true == map.replace(two, 3) );
148 REQUIRE( 2 == map.size() );
149 REQUIRE( 3 == map.get(two) );
150 REQUIRE( false == map.replace("new", 1) );
151 REQUIRE( 2 == map.size() );
152
153 {
154 const int v = 42;
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() );
166 }
167
168 map.clear();
169 REQUIRE( 0 == map.size() );
170 }
171 {
172 // string_view key must be persistent!
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());
183
184 jau::StringViewHashMapWrap<int, int, -1> map;
185 REQUIRE( 0 == map.size() );
186 REQUIRE( true == map.put(one1, 1) );
187 REQUIRE( true == map.put(two1, 2) );
188 REQUIRE( 2 == map.size() );
189
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() ); // string_view key uses external storage two1
195 REQUIRE( two2.data() != pp->first.data() ); // string_view key uses external storage two1
196
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) );
202
203 REQUIRE( 3 == map.put3(two2, 4) );
204 REQUIRE( 2 == map.size() );
205 REQUIRE( 4 == map.get(two2) );
206
207 REQUIRE( -1 == map.put3("new", 100) );
208 REQUIRE( 3 == map.size() );
209 REQUIRE(100 == map.get("new") );
210
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() );
219
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) );
226
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() );
232
233 map.clear();
234 REQUIRE( 0 == map.size() );
235 }
236}
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]")