Skip to content

Commit 68c652c

Browse files
committed
Visualisation of working with LinkedHashMap
1 parent de7a7b4 commit 68c652c

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ project(linked_hash_map C)
33

44
set(CMAKE_C_STANDARD 11)
55

6-
add_executable(linked_hash_map main.c linked_hash_map_entry.h linked_hash_map_struct.h linked_hash_map_put.c linked_hash_map_put.h linked_hash_map_utils.c linked_hash_map_utils.h linked_hash_map.c linked_hash_map.h linked_hash_map_get.c linked_hash_map_get.h linked_hash_map_remove.c linked_hash_map_remove.h)
6+
add_executable(linked_hash_map main.c linked_hash_map_entry.h linked_hash_map_struct.h linked_hash_map_put.c linked_hash_map_put.h linked_hash_map_utils.c linked_hash_map_utils.h linked_hash_map.c linked_hash_map.h linked_hash_map_get.c linked_hash_map_get.h linked_hash_map_remove.c linked_hash_map_remove.h tests.c tests.h)

main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include <stdio.h>
1+
#include "tests.h"
22

33
int main() {
4-
printf("Hello, World!\n");
4+
test();
55
return 0;
66
}

tests.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "tests.h"
2+
3+
void test() {
4+
struct LinkedHashMap *map = createHashMap();
5+
6+
testAdd(map);
7+
testGet(map);
8+
testRemove(map);
9+
10+
deleteMap(map);
11+
}
12+
13+
void testAdd(struct LinkedHashMap *map) {
14+
printf("\nRunning put test on the following map:\n");
15+
print(map);
16+
17+
for (int i = 0; i < 10; ++i) {
18+
printf("Adding pair (%d, %d) to the map\n", i, 10 - i);
19+
put(map, i, 10 - i);
20+
print(map);
21+
}
22+
int present = map->head->after->after->after->key;
23+
int value = 25;
24+
printf("Adding pair with existing key (%d, %d) to the map\n", present, value);
25+
put(map, present, value);
26+
print(map);
27+
}
28+
29+
void testGet(struct LinkedHashMap *map) {
30+
printf("\nRunning get test on the following map:\n");
31+
print(map);
32+
33+
int key = 8;
34+
printf("Requested key: %d; value for the key: %d\n", key, get(map, key)->value);
35+
key = 2;
36+
printf("Requested key: %d; value for the key: %d\n", key, get(map, key)->value);
37+
key = 9;
38+
printf("Requested key: %d; value for the key: %d\n", key, get(map, key)->value);
39+
}
40+
41+
void testRemove(struct LinkedHashMap *map) {
42+
printf("\nRunning remove test on the following map:\n");
43+
print(map);
44+
45+
int key = 4;
46+
printf("Removing entry with key %d:\n", key);
47+
removeEntry(map, key);
48+
print(map);
49+
50+
key = 0;
51+
printf("Removing entry with key %d:\n", key);
52+
removeEntry(map, key);
53+
print(map);
54+
55+
key = 9;
56+
printf("Removing entry with key %d:\n", key);
57+
removeEntry(map, key);
58+
print(map);
59+
60+
printf("Removing all entries:\n");
61+
while (!isEmpty(map)) {
62+
removeEntry(map, map->head->key);
63+
}
64+
65+
print(map);
66+
}

tests.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef LINKED_HASH_MAP_TESTS_H
2+
#define LINKED_HASH_MAP_TESTS_H
3+
4+
#include "linked_hash_map.h"
5+
#include <time.h>
6+
7+
void test();
8+
9+
void testAdd(struct LinkedHashMap *map);
10+
11+
void testGet(struct LinkedHashMap *map);
12+
13+
void testRemove(struct LinkedHashMap *map);
14+
15+
#endif //LINKED_HASH_MAP_TESTS_H

0 commit comments

Comments
 (0)