Debian Codegraph @

src/tests/c_hashmap_test.c

raw symbols SCIP
1 #include <cbehave/cbehave.h>
2
3 #include <stdlib.h>
4 #include <string.h>
5 #include <c_hashmap/hashmap.h>
6
7
8 // All tests in this file adapted from example code in the original c_hashmap
9 // package's main.c
10
11
12 FEATURE(hashmap_put, "Hashmap put")
13 SCENARIO("Put one value")
14 GIVEN("a new hashmap")
15 map_t map = hashmap_new();
16
17 WHEN("I put a new value in")
18 int value = 42;
19 int error = hashmap_put(map, "somekey", &value);
20
21 THEN("the operation should be successful");
22 SHOULD_INT_EQUAL(error, (int)MAP_OK);
23
24 hashmap_free(map);
25 SCENARIO_END
26 FEATURE_END
27
28 FEATURE(hashmap_get, "Hashmap get")
29 SCENARIO("Get an existing value")
30 GIVEN("a hashmap with a value")
31 map_t map = hashmap_new();
32 int value = 42;
33 hashmap_put(map, "somekey", &value);
34
35 WHEN("I get it via its key")
36 int *valueOut;
37 int error = hashmap_get(map, "somekey", (void **)&valueOut);
38
39 THEN("the operation should be successful")
40 SHOULD_INT_EQUAL(error, (int)MAP_OK);
41 AND("the value should match");
42 SHOULD_INT_EQUAL(value, *valueOut);
43
44 hashmap_free(map);
45 SCENARIO_END
46
47 SCENARIO("Get non-existing value")
48 GIVEN("a hashmap with a value")
49 map_t map = hashmap_new();
50 int value = 42;
51 hashmap_put(map, "somekey", &value);
52
53 WHEN("I get a non-existing key")
54 int *valueOut;
55 int error = hashmap_get(map, "notkey", (void **)&valueOut);
56
57 THEN("the operation should be unsuccessful")
58 SHOULD_INT_EQUAL(error, (int)MAP_MISSING);
59
60 hashmap_free(map);
61 SCENARIO_END
62 FEATURE_END
63
64 FEATURE(hashmap_remove, "Hashmap remove")
65 SCENARIO("Remove an existing value")
66 GIVEN("a hashmap with a value")
67 map_t map = hashmap_new();
68 int value = 42;
69 hashmap_put(map, "somekey", &value);
70
71 WHEN("I remove it via its key")
72 int error = hashmap_remove(map, "somekey");
73
74 THEN("the operation should be successful")
75 SHOULD_INT_EQUAL(error, (int)MAP_OK);
76
77 hashmap_free(map);
78 SCENARIO_END
79 FEATURE_END
80
81 static int copy_key(any_t data, any_t key)
82 {
83 char **keys = (char **)data;
84 while (*keys != NULL) keys++;
85 *keys = strdup(key);
86 return MAP_OK;
87 }
88 FEATURE(hashmap_iterate_keys_sorted, "Hashmap iterate keys sorted")
89 SCENARIO("Iterate a hashmap sorted by keys")
90 GIVEN("a hashmap with keys a, b, c")
91 map_t map = hashmap_new();
92 int value3 = 3;
93 hashmap_put(map, "c", &value3);
94 int value2 = 2;
95 hashmap_put(map, "b", &value2);
96 int value1 = 1;
97 hashmap_put(map, "a", &value1);
98
99 WHEN("I iterate it by keys sorted")
100 char *keys[3];
101 memset(keys, 0, sizeof keys);
102 const int error = hashmap_iterate_keys_sorted(map, copy_key, keys);
103
104 THEN("the keys should be in order")
105 SHOULD_INT_EQUAL(error, MAP_OK);
106 SHOULD_STR_EQUAL(keys[0], "a");
107 SHOULD_STR_EQUAL(keys[1], "b");
108 SHOULD_STR_EQUAL(keys[2], "c");
109
110 for (int i = 0; i < 3; i++) free(keys[i]);
111 hashmap_free(map);
112 SCENARIO_END
113 FEATURE_END
114
115 CBEHAVE_RUN(
116 "c_hashmap features are:",
117 TEST_FEATURE(hashmap_put),
118 TEST_FEATURE(hashmap_get),
119 TEST_FEATURE(hashmap_remove),
120 TEST_FEATURE(hashmap_iterate_keys_sorted)
121 )