Lolly 1.4.27
Loading...
Searching...
No Matches
hashmap_test.cpp
Go to the documentation of this file.
1#include "a_lolly_test.hpp"
2#include "hashmap.hpp"
3#include <string>
4
5TEST_CASE ("test_resize") {
6 auto hm= hashmap<int, int> (0, 10);
7 hm (1) = 10;
8 hm (2) = 20;
9
10 hm->resize (1);
11 CHECK_EQ (hm[1] == 10, true);
12 CHECK_EQ (hm[2] == 20, true);
13
14 hm->resize (20);
15 CHECK_EQ (hm[1] == 10, true);
16 CHECK_EQ (hm[2] == 20, true);
17}
18
19TEST_CASE ("test reset") {
20 auto hm= hashmap<int, int> (0, 10);
21 hm (1) = 10;
22 hm (11)= 20;
23 hm->reset (1);
24
25 CHECK_EQ (hm->contains (1), false);
26 CHECK_EQ (hm->contains (11), true);
27}
28
30void
31routine (int key) {
32 CHECK_EQ (hm_generate->contains (key), true);
33}
34
35TEST_CASE ("test generate") {
36 hm_generate (1)= 10;
37 hm_generate (2)= 20;
38 hm_generate->generate (routine);
39}
40
41TEST_CASE ("test contains") {
42 auto hm= hashmap<int, void*> (nullptr, 2, 2);
43 hm (1) = nullptr;
44 CHECK_EQ (hm->contains (1), true);
45 CHECK_EQ (hm->contains (3), false);
46}
47
48TEST_CASE ("test empty") {
49 auto hm= hashmap<int, int> ();
50 CHECK_EQ (hm->empty (), true);
51
52 hm (1);
53 CHECK_EQ (hm->empty (), false);
54}
55
56TEST_CASE ("test join") {
57 auto hm1= hashmap<int, int> ();
58 auto hm2= hashmap<int, int> ();
59 hm1 (1) = 10;
60 hm1 (2) = 20;
61 hm2 (2) = -20;
62 hm2 (3) = -30;
63 hm1->join (hm2);
64
65 CHECK_EQ (hm1[1] == 10, true);
66 CHECK_EQ (hm1[2] == -20, true);
67 CHECK_EQ (hm1[3] == -30, true);
68}
69
70TEST_CASE ("test write back") {
71 auto hm1= hashmap<int, int> (0, 10);
72 auto hm2= hashmap<int, int> (0, 10);
73 hm1 (1) = 10;
74 hm1 (2) = 20;
75 hm2 (2) = -20;
76
77 hm1->write_back (2, hm2);
78 CHECK_EQ (hm1[2] == 20, true);
79
80 hm1->write_back (3, hm2);
81 CHECK_EQ (hm1[3] == 0, true);
82
83 hm2 (4)= -40;
84 hm1->write_back (4, hm2);
85 CHECK_EQ (hm2[4] == -40, true);
86}
87
88TEST_CASE ("test pre patch") {
89 auto hm = hashmap<int, int> ();
91 auto hm_base = hashmap<int, int> ();
92
93 hm (1)= 10;
94 hm_patch (1);
95 hm->pre_patch (hm_patch, hm_base);
96 CHECK_EQ (hm[1] == 10, true);
97
98 hm (2) = 20;
99 hm_patch (2)= -20;
100 hm_base (2) = 20;
101 hm->pre_patch (hm_patch, hm_base);
102 CHECK_EQ (hm[2] == 0, true);
103
104 hm_patch (3)= -30;
105 hm->pre_patch (hm_patch, hm_base);
106 CHECK_EQ (hm[3] == -30, true);
107}
108
109TEST_CASE ("test post patch") {
110 auto hm = hashmap<int, int> ();
112 auto hm_base = hashmap<int, int> ();
113
114 hm (1)= 10;
115 hm_patch (1);
116 hm->post_patch (hm_patch, hm_base);
117 CHECK_EQ (hm[1] == 0, true);
118
119 hm_patch (2)= -20;
120 hm->pre_patch (hm_patch, hm_base);
121 CHECK_EQ (hm[2] == -20, true);
122}
123
124TEST_CASE ("test copy") {
125 auto hm = hashmap<int, int> ();
126 auto hm_c= hashmap<int, int> (0, 10, 2);
127 hm_c (1) = 10;
128 hm_c (11)= 110;
129 hm_c (2) = 20;
130
131 auto res_hm= copy (hm_c);
132 CHECK_EQ (res_hm[1] == 10, true);
133 CHECK_EQ (res_hm[11] == 110, true);
134 CHECK_EQ (res_hm[2] == 20, true);
135}
136
137TEST_CASE ("test equality") {
138 auto hm1= hashmap<int, int> (0, 10, 3);
139 auto hm2= hashmap<int, int> (0, 100, 30);
140 hm1 (1) = 10;
141 hm2 (1) = 10;
142 CHECK_EQ (hm1 == hm2, true);
143
144 hm2 (2)= 20;
145 CHECK_EQ (hm1 != hm2, true);
146}
147
148TEST_CASE ("test changes") {
149 auto base_m = hashmap<int, int> ();
150 auto patch_m= hashmap<int, int> ();
151 base_m (1) = 10;
152 base_m (2) = 20;
153 patch_m (2) = -20;
154 patch_m (3) = -30;
155 auto res = changes (patch_m, base_m);
156 CHECK_EQ (N (res) == 2, true);
157 CHECK_EQ (res[2] == -20, true);
158 CHECK_EQ (res[3] == -30, true);
159}
160
161TEST_CASE ("test invert") {
162 auto base_m = hashmap<int, int> ();
163 auto patch_m= hashmap<int, int> ();
164 base_m (1) = 10;
165 base_m (2) = 20;
166 patch_m (2) = -20;
167 patch_m (3) = -30;
168 auto res = invert (patch_m, base_m);
169 CHECK_EQ (N (res) == 2, true);
170 CHECK_EQ (res[2] == 20, true);
171 CHECK_EQ (res[3] == 0, true);
172}
173
174TEST_CASE ("test size") {
176 CHECK_EQ (N (empty_hm) == 0, true);
177
179 non_empty_hm (1) = nullptr;
180 CHECK_EQ (N (non_empty_hm) == 1, true);
181}
182
183TEST_CASE ("hashmap default") {
184 // Create a hashmap object with integer keys and string values
185 hashmap<int, std::string> map ("default");
186 // Test the comparison operators
188 equal_map (1)= "one";
189 equal_map (2)= "two";
191 not_equal_map (1)= "one";
192 not_equal_map (2)= "three";
193 CHECK_EQ (map == equal_map, false);
195}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
int copy(int x)
Returns a copy of an integer.
Definition basic.hpp:249
The list class represents a linked list.
Definition list.hpp:48
hashmap< T, U > changes(hashmap< T, U > p, hashmap< T, U > b)
Creates a new hashmap containing entries that have changed in the 'patch' compared to 'base'.
hashmap< T, U > invert(hashmap< T, U > p, hashmap< T, U > b)
Creates a new hashmap containing entries that are different in the 'patch' compared to 'base',...
void routine(int key)
auto hm_generate
TEST_CASE("test for operator+= and advance()")