-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHashMapTest.Mod
More file actions
503 lines (409 loc) · 15.9 KB
/
Copy pathHashMapTest.Mod
File metadata and controls
503 lines (409 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
(** HashMapTest.mod - Tests for HashMap.mod.
Copyright (C) 2025
Released under The 3-Clause BSD License.
*)
MODULE HashMapTest;
IMPORT HashMap, Collections, CollectionKeys, Tests;
TYPE
(* Test item extending Collections.Item *)
TestItem = RECORD(Collections.Item)
value: INTEGER
END;
TestItemPtr = POINTER TO TestItem;
(* Integer item for testing - simple wrapper around INTEGER *)
IntegerItem = RECORD(Collections.Item)
value: INTEGER
END;
IntegerItemPtr = POINTER TO IntegerItem;
(* Visitor state for testing iteration *)
TestVisitorState = RECORD(Collections.VisitorState)
sum: INTEGER;
count: INTEGER
END;
VAR
ts: Tests.TestSet;
(** Create a new test item *)
PROCEDURE NewTestItem(value: INTEGER): TestItemPtr;
VAR item: TestItemPtr;
BEGIN
NEW(item);
item.value := value;
RETURN item
END NewTestItem;
(** Create a new integer item *)
PROCEDURE NewIntegerItem(value: INTEGER): IntegerItemPtr;
VAR item: IntegerItemPtr;
BEGIN
NEW(item);
item.value := value;
RETURN item
END NewIntegerItem;
(** Visitor procedure for testing iteration *)
PROCEDURE Visitor(item: Collections.ItemPtr; VAR state: Collections.VisitorState): BOOLEAN;
VAR
pair: HashMap.KeyValuePairPtr;
testItem: TestItemPtr;
value: Collections.ItemPtr;
BEGIN
pair := item(HashMap.KeyValuePairPtr);
value := HashMap.PairValue(pair);
testItem := value(TestItemPtr);
state(TestVisitorState).sum := state(TestVisitorState).sum + testItem.value;
INC(state(TestVisitorState).count);
(* Continue iteration *)
RETURN TRUE
END Visitor;
PROCEDURE TestNewAndFree*(): BOOLEAN;
VAR
map: HashMap.HashMap;
pass: BOOLEAN;
BEGIN
pass := TRUE;
map := HashMap.New();
Tests.ExpectedBool(TRUE, map # NIL, "HashMap.New should return non-nil", pass);
Tests.ExpectedBool(TRUE, HashMap.IsEmpty(map), "New hashmap should be empty", pass);
Tests.ExpectedInt(0, HashMap.Count(map), "New hashmap should have count 0", pass);
HashMap.Free(map);
Tests.ExpectedBool(TRUE, map = NIL, "HashMap.Free should set map to NIL", pass);
RETURN pass
END TestNewAndFree;
PROCEDURE TestNewWithSize*(): BOOLEAN;
VAR
map: HashMap.HashMap;
ops: CollectionKeys.KeyOps;
pass: BOOLEAN;
BEGIN
pass := TRUE;
CollectionKeys.IntegerKeyOps(ops);
map := HashMap.NewWithSize(32, ops);
Tests.ExpectedBool(TRUE, map # NIL, "HashMap.NewWithSize should return non-nil", pass);
Tests.ExpectedBool(TRUE, HashMap.IsEmpty(map), "New hashmap should be empty", pass);
Tests.ExpectedInt(0, HashMap.Count(map), "New hashmap should have count 0", pass);
HashMap.Free(map);
RETURN pass
END TestNewWithSize;
PROCEDURE TestPutAndGet*(): BOOLEAN;
VAR
map: HashMap.HashMap;
item1, item2, item3, retrieved: TestItemPtr;
value: Collections.ItemPtr;
pass: BOOLEAN;
BEGIN
pass := TRUE;
map := HashMap.New();
item1 := NewTestItem(100);
item2 := NewTestItem(200);
item3 := NewTestItem(300);
(* Put some values *)
HashMap.Put(map, 1, item1);
HashMap.Put(map, 2, item2);
HashMap.Put(map, 3, item3);
Tests.ExpectedInt(3, HashMap.Count(map), "Count should be 3 after 3 puts", pass);
Tests.ExpectedBool(FALSE, HashMap.IsEmpty(map), "Map should not be empty", pass);
(* Get the values *)
IF HashMap.Get(map, 1, value) THEN
retrieved := value(TestItemPtr);
Tests.ExpectedInt(100, retrieved.value, "Get key 1 should return item with value 100", pass);
ELSE
Tests.ExpectedBool(TRUE, FALSE, "Get key 1 should succeed", pass);
END;
IF HashMap.Get(map, 2, value) THEN
retrieved := value(TestItemPtr);
Tests.ExpectedInt(200, retrieved.value, "Get key 2 should return item with value 200", pass);
ELSE
Tests.ExpectedBool(TRUE, FALSE, "Get key 2 should succeed", pass);
END;
IF HashMap.Get(map, 3, value) THEN
retrieved := value(TestItemPtr);
Tests.ExpectedInt(300, retrieved.value, "Get key 3 should return item with value 300", pass);
ELSE
Tests.ExpectedBool(TRUE, FALSE, "Get key 3 should succeed", pass);
END;
(* Try to get non-existent key *)
Tests.ExpectedBool(FALSE, HashMap.Get(map, 999, value), "Get non-existent key should return FALSE", pass);
HashMap.Free(map);
RETURN pass
END TestPutAndGet;
PROCEDURE TestUpdateExistingKey*(): BOOLEAN;
VAR
map: HashMap.HashMap;
item1, item2, retrieved: TestItemPtr;
value: Collections.ItemPtr;
pass: BOOLEAN;
BEGIN
pass := TRUE;
map := HashMap.New();
item1 := NewTestItem(100);
item2 := NewTestItem(999);
(* Put initial value *)
HashMap.Put(map, 42, item1);
Tests.ExpectedInt(1, HashMap.Count(map), "Count should be 1 after first put", pass);
(* Update with new value *)
HashMap.Put(map, 42, item2);
Tests.ExpectedInt(1, HashMap.Count(map), "Count should still be 1 after update", pass);
(* Verify the updated value *)
IF HashMap.Get(map, 42, value) THEN
retrieved := value(TestItemPtr);
Tests.ExpectedInt(999, retrieved.value, "Updated value should be 999", pass);
ELSE
Tests.ExpectedBool(TRUE, FALSE, "Get after update should succeed", pass);
END;
HashMap.Free(map);
RETURN pass
END TestUpdateExistingKey;
PROCEDURE TestContains*(): BOOLEAN;
VAR
map: HashMap.HashMap;
item1: TestItemPtr;
pass: BOOLEAN;
BEGIN
pass := TRUE;
map := HashMap.New();
item1 := NewTestItem(100);
(* Test non-existent key *)
Tests.ExpectedBool(FALSE, HashMap.Contains(map, 1), "Contains should return FALSE for non-existent key", pass);
(* Add a key and test *)
HashMap.Put(map, 1, item1);
Tests.ExpectedBool(TRUE, HashMap.Contains(map, 1), "Contains should return TRUE for existing key", pass);
Tests.ExpectedBool(FALSE, HashMap.Contains(map, 2), "Contains should return FALSE for different key", pass);
HashMap.Free(map);
RETURN pass
END TestContains;
PROCEDURE TestRemove*(): BOOLEAN;
VAR
map: HashMap.HashMap;
item1, item2: TestItemPtr;
pass: BOOLEAN;
BEGIN
pass := TRUE;
map := HashMap.New();
item1 := NewTestItem(100);
item2 := NewTestItem(200);
(* Put some values *)
HashMap.Put(map, 1, item1);
HashMap.Put(map, 2, item2);
Tests.ExpectedInt(2, HashMap.Count(map), "Count should be 2 after puts", pass);
(* Remove existing key *)
Tests.ExpectedBool(TRUE, HashMap.Remove(map, 1), "Remove existing key should return TRUE", pass);
Tests.ExpectedInt(1, HashMap.Count(map), "Count should be 1 after remove", pass);
Tests.ExpectedBool(FALSE, HashMap.Contains(map, 1), "Removed key should not exist", pass);
Tests.ExpectedBool(TRUE, HashMap.Contains(map, 2), "Other key should still exist", pass);
(* Try to remove non-existent key *)
Tests.ExpectedBool(FALSE, HashMap.Remove(map, 999), "Remove non-existent key should return FALSE", pass);
Tests.ExpectedInt(1, HashMap.Count(map), "Count should be unchanged", pass);
(* Remove last key *)
Tests.ExpectedBool(TRUE, HashMap.Remove(map, 2), "Remove last key should return TRUE", pass);
Tests.ExpectedInt(0, HashMap.Count(map), "Count should be 0 after removing all", pass);
Tests.ExpectedBool(TRUE, HashMap.IsEmpty(map), "Map should be empty after removing all", pass);
HashMap.Free(map);
RETURN pass
END TestRemove;
PROCEDURE TestLoadFactor*(): BOOLEAN;
VAR
map: HashMap.HashMap;
item1, item2: TestItemPtr;
loadFactor: INTEGER;
ops: CollectionKeys.KeyOps;
pass: BOOLEAN;
BEGIN
pass := TRUE;
CollectionKeys.IntegerKeyOps(ops);
map := HashMap.NewWithSize(4, ops); (* Small size for testing *)
Tests.ExpectedInt(0, HashMap.LoadFactor(map), "Empty map should have 0 load factor", pass);
item1 := NewTestItem(100);
(* Add one item: 1/4 = 25% *)
HashMap.Put(map, 1, item1);
loadFactor := HashMap.LoadFactor(map);
Tests.ExpectedInt(25, loadFactor, "Load factor should be 25% with 1/4 buckets used", pass);
item2 := NewTestItem(200);
(* Add more items *)
HashMap.Put(map, 2, item2);
loadFactor := HashMap.LoadFactor(map);
Tests.ExpectedInt(50, loadFactor, "Load factor should be 50% with 2/4 buckets used", pass);
HashMap.Free(map);
RETURN pass
END TestLoadFactor;
PROCEDURE TestForeach*(): BOOLEAN;
VAR
map: HashMap.HashMap;
item1, item2, item3: TestItemPtr;
state: TestVisitorState;
pass: BOOLEAN;
BEGIN
pass := TRUE;
map := HashMap.New();
item1 := NewTestItem(10);
item2 := NewTestItem(20);
item3 := NewTestItem(30);
HashMap.Put(map, 1, item1);
HashMap.Put(map, 2, item2);
HashMap.Put(map, 3, item3);
state.sum := 0;
state.count := 0;
HashMap.Foreach(map, Visitor, state);
Tests.ExpectedInt(60, state.sum, "Sum of all values should be 60", pass);
Tests.ExpectedInt(3, state.count, "Should visit 3 items", pass);
HashMap.Free(map);
RETURN pass
END TestForeach;
PROCEDURE TestCollisionHandling*(): BOOLEAN;
VAR
map: HashMap.HashMap;
item1, item2, item3: TestItemPtr;
value: Collections.ItemPtr;
retrieved: TestItemPtr;
ops: CollectionKeys.KeyOps;
pass: BOOLEAN;
BEGIN
pass := TRUE;
CollectionKeys.IntegerKeyOps(ops);
map := HashMap.NewWithSize(2, ops); (* Very small size to force collisions *)
item1 := NewTestItem(100);
item2 := NewTestItem(200);
item3 := NewTestItem(300);
(* Put multiple items that will likely collide *)
HashMap.Put(map, 1, item1);
HashMap.Put(map, 3, item2); (* Should hash to same bucket in size-2 map *)
HashMap.Put(map, 5, item3); (* Should also collide *)
Tests.ExpectedInt(3, HashMap.Count(map), "Count should be 3 despite collisions", pass);
(* Verify we can retrieve all items *)
IF HashMap.Get(map, 1, value) THEN
retrieved := value(TestItemPtr);
Tests.ExpectedInt(100, retrieved.value, "Should retrieve first item correctly", pass)
ELSE
Tests.ExpectedBool(TRUE, FALSE, "Should be able to get first item", pass)
END;
IF HashMap.Get(map, 3, value) THEN
retrieved := value(TestItemPtr);
Tests.ExpectedInt(200, retrieved.value, "Should retrieve second item correctly", pass)
ELSE
Tests.ExpectedBool(TRUE, FALSE, "Should be able to get second item", pass)
END;
IF HashMap.Get(map, 5, value) THEN
retrieved := value(TestItemPtr);
Tests.ExpectedInt(300, retrieved.value, "Should retrieve third item correctly", pass)
ELSE
Tests.ExpectedBool(TRUE, FALSE, "Should be able to get third item", pass)
END;
(* Test removal with collisions *)
Tests.ExpectedBool(TRUE, HashMap.Remove(map, 3), "Should be able to remove middle colliding item", pass);
Tests.ExpectedInt(2, HashMap.Count(map), "Count should be 2 after removal", pass);
Tests.ExpectedBool(FALSE, HashMap.Contains(map, 3), "Removed item should not be found", pass);
Tests.ExpectedBool(TRUE, HashMap.Contains(map, 1), "Other items should still exist", pass);
Tests.ExpectedBool(TRUE, HashMap.Contains(map, 5), "Other items should still exist", pass);
HashMap.Free(map);
RETURN pass
END TestCollisionHandling;
PROCEDURE TestStringKeys*(): BOOLEAN;
VAR
map: HashMap.HashMap;
value: Collections.ItemPtr;
intValue: IntegerItemPtr;
success: BOOLEAN;
pass: BOOLEAN;
BEGIN
pass := TRUE;
(* Create string-based hashmap *)
map := HashMap.NewStringMap();
(* Test empty map *)
Tests.ExpectedInt(0, HashMap.Count(map), "New string map should be empty", pass);
Tests.ExpectedBool(TRUE, HashMap.IsEmpty(map), "New string map should report as empty", pass);
(* Add some string key-value pairs *)
intValue := NewIntegerItem(42);
HashMap.PutString(map, "hello", intValue);
intValue := NewIntegerItem(100);
HashMap.PutString(map, "world", intValue);
intValue := NewIntegerItem(7);
HashMap.PutString(map, "test", intValue);
(* Test count *)
Tests.ExpectedInt(3, HashMap.Count(map), "String map should contain 3 items", pass);
(* Test retrieval *)
success := HashMap.GetString(map, "hello", value);
Tests.ExpectedBool(TRUE, success, "Should find 'hello' key", pass);
IF success THEN
intValue := value(IntegerItemPtr);
Tests.ExpectedInt(42, intValue.value, "'hello' should map to 42", pass)
END;
success := HashMap.GetString(map, "world", value);
Tests.ExpectedBool(TRUE, success, "Should find 'world' key", pass);
IF success THEN
intValue := value(IntegerItemPtr);
Tests.ExpectedInt(100, intValue.value, "'world' should map to 100", pass)
END;
(* Test Contains *)
Tests.ExpectedBool(TRUE, HashMap.ContainsString(map, "test"), "Should contain 'test' key", pass);
Tests.ExpectedBool(FALSE, HashMap.ContainsString(map, "missing"), "Should not contain 'missing' key", pass);
(* Test key update *)
intValue := NewIntegerItem(999);
HashMap.PutString(map, "hello", intValue);
success := HashMap.GetString(map, "hello", value);
Tests.ExpectedBool(TRUE, success, "Should still find 'hello' key after update", pass);
IF success THEN
intValue := value(IntegerItemPtr);
Tests.ExpectedInt(999, intValue.value, "'hello' should now map to 999", pass)
END;
(* Count should still be 3 after update *)
Tests.ExpectedInt(3, HashMap.Count(map), "Count should still be 3 after update", pass);
(* Test removal *)
success := HashMap.RemoveString(map, "world");
Tests.ExpectedBool(TRUE, success, "Should successfully remove 'world' key", pass);
Tests.ExpectedBool(FALSE, HashMap.ContainsString(map, "world"), "Should not contain 'world' key after removal", pass);
Tests.ExpectedInt(2, HashMap.Count(map), "Count should be 2 after removal", pass);
(* Test removal of non-existent key *)
success := HashMap.RemoveString(map, "missing");
Tests.ExpectedBool(FALSE, success, "Should not successfully remove non-existent key", pass);
HashMap.Free(map);
RETURN pass
END TestStringKeys;
PROCEDURE TestClear(): BOOLEAN;
VAR
map: HashMap.HashMap;
value: Collections.ItemPtr;
pass, found: BOOLEAN;
i: INTEGER;
BEGIN
pass := TRUE;
map := HashMap.New();
(* Add some key-value pairs *)
FOR i := 1 TO 5 DO
HashMap.Put(map, i, NewTestItem(i * 10))
END;
IF HashMap.Count(map) # 5 THEN pass := FALSE END;
IF HashMap.IsEmpty(map) THEN pass := FALSE END;
(* Clear the map *)
HashMap.Clear(map);
IF HashMap.Count(map) # 0 THEN pass := FALSE END;
IF ~HashMap.IsEmpty(map) THEN pass := FALSE END;
(* Lookups should fail *)
FOR i := 1 TO 5 DO
found := HashMap.Get(map, i, value);
IF found OR (value # NIL) THEN pass := FALSE END
END;
(* Add after clearing *)
HashMap.Put(map, 42, NewTestItem(123));
IF HashMap.Count(map) # 1 THEN pass := FALSE END;
found := HashMap.Get(map, 42, value);
IF ~found OR (value = NIL) THEN pass := FALSE END;
IF value(TestItemPtr).value # 123 THEN pass := FALSE END;
(* Clear again, should be empty *)
HashMap.Clear(map);
IF HashMap.Count(map) # 0 THEN pass := FALSE END;
IF ~HashMap.IsEmpty(map) THEN pass := FALSE END;
HashMap.Free(map);
RETURN pass
END TestClear;
BEGIN
Tests.Init(ts, "HashMap Tests");
Tests.Add(ts, TestNewAndFree);
Tests.Add(ts, TestNewWithSize);
Tests.Add(ts, TestPutAndGet);
Tests.Add(ts, TestUpdateExistingKey);
Tests.Add(ts, TestContains);
Tests.Add(ts, TestRemove);
Tests.Add(ts, TestLoadFactor);
Tests.Add(ts, TestForeach);
Tests.Add(ts, TestCollisionHandling);
Tests.Add(ts, TestStringKeys);
Tests.Add(ts, TestClear);
ASSERT(Tests.Run(ts));
END HashMapTest.