6217745 |
Jamozed |
2021-12-08 20:00:24 |
0
|
// test_map.c |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
1
|
// Map unit test for libutil |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
2
|
// Copyright (C) 2021, Jakob Wakeling |
7f427d9 |
Jamozed |
2022-03-06 12:55:13 |
3
|
// MIT Licence |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
4
|
|
6217745 |
Jamozed |
2021-12-08 20:00:24 |
5
|
#include "../map.h" |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
6
|
#include "../util.h" |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
7
|
#include "unit.h" |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
8
|
|
6217745 |
Jamozed |
2021-12-08 20:00:24 |
9
|
#include <stdio.h> |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
10
|
#include <string.h> |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
11
|
|
6217745 |
Jamozed |
2021-12-08 20:00:24 |
12
|
int main(void) { |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
13
|
{ |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
14
|
map_initial_capacity = 4; |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
15
|
map m = map_init(NULL); |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
16
|
|
6217745 |
Jamozed |
2021-12-08 20:00:24 |
17
|
map_insert(&m, "key0", "value0"); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
18
|
map_insert(&m, "key4", "value4"); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
19
|
map_insert(&m, "key3", "value3"); |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
20
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
21
|
ASSERT("T000 INSERT AND LOOKUP STRING 0", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
22
|
strcmp(map_lookup(&m, "key0"), "value0") == 0); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
23
|
ASSERT("T001 INSERT AND LOOKUP STRING 1", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
24
|
strcmp(map_lookup(&m, "key4"), "value4") == 0); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
25
|
ASSERT("T002 INSERT AND LOOKUP STRING 2", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
26
|
strcmp(map_lookup(&m, "key3"), "value3") == 0); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
27
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
28
|
ASSERT("T003 LOOKUP NONEXISTENT STRING", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
29
|
map_lookup(&m, "key1") == NULL); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
30
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
31
|
map_remove(&m, "key4"); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
32
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
33
|
ASSERT("T004 REMOVE AND LOOKUP STRING", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
34
|
map_lookup(&m, "key4") == NULL); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
35
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
36
|
ASSERT("T006 LOOKUP STRING AFTER REMOVE 0", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
37
|
strcmp(map_lookup(&m, "key0"), "value0") == 0); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
38
|
ASSERT("T007 LOOKUP STRING AFTER REMOVE 1", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
39
|
strcmp(map_lookup(&m, "key3"), "value3") == 0); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
40
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
41
|
ASSERT("T008 REMOVE NONEXISTENT STRING", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
42
|
map_remove(&m, "key4") == NULL); |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
43
|
|
6217745 |
Jamozed |
2021-12-08 20:00:24 |
44
|
map_free(&m); |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
45
|
} |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
46
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
47
|
{ |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
48
|
map_initial_capacity = 4; |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
49
|
map m = map_init(NULL); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
50
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
51
|
map_insert(&m, "key0", "value0"); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
52
|
map_insert(&m, "key3", "value3"); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
53
|
map_insert(&m, "key4", "value4"); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
54
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
55
|
ASSERT("T100 INSERT AND LOOKUP STRING 0", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
56
|
strcmp(map_lookup(&m, "key0"), "value0") == 0); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
57
|
ASSERT("T101 INSERT AND LOOKUP STRING 1", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
58
|
strcmp(map_lookup(&m, "key3"), "value3") == 0); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
59
|
ASSERT("T102 INSERT AND LOOKUP STRING 2", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
60
|
strcmp(map_lookup(&m, "key4"), "value4") == 0); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
61
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
62
|
ASSERT("T103 LOOKUP NONEXISTENT STRING", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
63
|
map_lookup(&m, "key1") == NULL); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
64
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
65
|
map_remove(&m, "key4"); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
66
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
67
|
ASSERT("T104 REMOVE AND LOOKUP STRING", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
68
|
map_lookup(&m, "key4") == NULL); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
69
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
70
|
ASSERT("T106 LOOKUP STRING AFTER REMOVE 0", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
71
|
strcmp(map_lookup(&m, "key0"), "value0") == 0); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
72
|
ASSERT("T107 LOOKUP STRING AFTER REMOVE 1", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
73
|
strcmp(map_lookup(&m, "key3"), "value3") == 0); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
74
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
75
|
ASSERT("T108 REMOVE NONEXISTENT STRING", |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
76
|
map_remove(&m, "key4") == NULL); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
77
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
78
|
map_free(&m); |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
79
|
} |
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
80
|
|
7af2131 |
Jamozed |
2022-01-06 00:39:34 |
81
|
printf("%d of %d tests passed\n", testspassed, testsrun); |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
82
|
return testsfailed; |
6217745 |
Jamozed |
2021-12-08 20:00:24 |
83
|
} |
|
|
|
84
|
|