9 static bool get(Map
*map
, const char *key
, const void *data
) {
10 return map_get(map
, key
) == data
&& map_closest(map
, key
) == data
;
13 static bool compare(const char *key
, void *value
, void *data
) {
15 ok(map_get(map
, key
) == value
, "Compare map content");
19 static bool once(const char *key
, void *value
, void *data
) {
25 static bool visit(const char *key
, void *value
, void *data
) {
32 static int order_counter
;
34 static bool order(const char *key
, void *value
, void *data
) {
37 order
[*index
] = ++order_counter
;
41 int main(int argc
, char *argv
[]) {
42 const char *key
= "404";
43 const int values
[3] = { 0, 1, 2 };
49 ok(map
&& map_empty(map
), "Creation");
50 ok(map_first(map
, &key
) == NULL
&& strcmp(key
, "404") == 0, "First on empty map");
51 ok(map_empty(map_prefix(map
, "404")), "Empty prefix map");
53 ok(!map_get(map
, "404"), "Get non-existing key");
54 ok(!map_contains(map
, "404"), "Contains non-existing key");
55 ok(!map_closest(map
, "404") && errno
== ENOENT
, "Closest non-existing key");
57 ok(!map_put(map
, "a", NULL
) && errno
== EINVAL
&& map_empty(map
) && !map_get(map
, "a"), "Put NULL value");
58 ok(map_put(map
, "a", &values
[0]) && !map_empty(map
) && get(map
, "a", &values
[0]), "Put 1");
59 ok(map_first(map
, &key
) == &values
[0] && strcmp(key
, "a") == 0, "First on map with 1 value");
61 ok(map_first(map_prefix(map
, "a"), &key
) == &values
[0] && strcmp(key
, "a") == 0, "First on prefix map");
62 ok(map_contains(map
, "a"), "Contains existing key");
63 ok(map_closest(map
, "a") == &values
[0], "Closest match existing key");
64 ok(!map_put(map
, "a", &values
[1]) && errno
== EEXIST
&& get(map
, "a", &values
[0]), "Put duplicate");
65 ok(map_put(map
, "cafebabe", &values
[2]) && get(map
, "cafebabe", &values
[2]), "Put 2");
66 ok(map_put(map
, "cafe", &values
[1]) && get(map
, "cafe", &values
[1]), "Put 3");
68 ok(map_first(map_prefix(map
, "cafe"), &key
) == &values
[1] && strcmp(key
, "cafe") == 0, "First on prefix map with multiple suffixes");
70 Map
*copy
= map_new();
71 ok(map_copy(copy
, map
), "Copy");
72 ok(!map_empty(copy
), "Not empty after copying");
73 map_iterate(copy
, compare
, map
);
74 map_iterate(map
, compare
, copy
);
77 map_iterate(copy
, once
, &counter
);
78 ok(counter
== 1, "Iterate stop condition");
80 ok(!map_get(map
, "ca") && !map_closest(map
, "ca") && errno
== 0, "Closest ambigious");
82 int visited
[] = { 0, 0, 0 };
84 map_iterate(map
, visit
, &visited
);
85 ok(visited
[0] == 1 && visited
[1] == 1 && visited
[2] == 1, "Iterate map");
87 memset(visited
, 0, sizeof visited
);
89 map_iterate(map
, order
, &visited
);
90 ok(visited
[0] == 1 && visited
[1] == 2 && visited
[2] == 3, "Ordered iteration");
92 memset(visited
, 0, sizeof visited
);
93 map_iterate(map_prefix(map
, "ca"), visit
, &visited
);
94 ok(visited
[0] == 0 && visited
[1] == 1 && visited
[2] == 1, "Iterate sub map");
96 memset(visited
, 0, sizeof visited
);
98 map_iterate(map_prefix(map
, "ca"), order
, &visited
);
99 ok(visited
[0] == 0 && visited
[1] == 1 && visited
[2] == 2, "Ordered sub map iteration");
101 ok(map_empty(map_prefix(map
, "404")), "Empty map for non-existing prefix");
103 ok(!map_delete(map
, "404"), "Delete non-existing key");
104 ok(map_delete(map
, "cafe") == &values
[1] && !map_get(map
, "cafe"), "Delete existing key");
105 ok(map_closest(map
, "cafe") == &values
[2], "Closest unambigious");
106 ok(map_put(map
, "cafe", &values
[1]) && get(map
, "cafe", &values
[1]), "Put 3 again");
109 ok(map_empty(map
), "Empty after clean");
114 return exit_status();