-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb_tree_test.c
More file actions
157 lines (117 loc) · 4.08 KB
/
b_tree_test.c
File metadata and controls
157 lines (117 loc) · 4.08 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
//
// Created by josoder on 06.11.17.
//
#include "b_tree_test.h"
void RunBtreeTests(){
bTree* tree = CreateBTree();
assert(tree!=NULL);
PrintBTree(tree);
TestInsertAndReplaceString(tree);
TestReplaceWrongType(tree);
TestGetText(tree);
TestGetInt(tree);
TestGetValue(tree);
TestSetValue(tree);
TestEnumerate(tree);
TestGetString(tree);
TestGetType(tree);
TestDelete(tree);
TestSizeIncreasesWhenCapacityIsReached(tree);
// Should print an empty tree
printf("Tree: \n");
PrintBTree(tree);
FreeBTree(tree);
}
void TestDelete(bTree* bt){
char* path[2] = {"strings", END_OF_PATH};
BTreeDelete(bt, path);
path[0] = "config";
BTreeDelete(bt, path);
assert(FindWithPath(bt, path) == NULL);
}
// Capacity should double when the initial capacity C(10) is reached and shrink when (N*2<C && C > 10)
void TestSizeIncreasesWhenCapacityIsReached(bTree* bt){
char tmp[5];
for(int i=0; i<11; i++){
sprintf(tmp, "%d", i);
BTreeInsert(bt, NULL, IS_FOLDER, tmp, NULL);
}
assert(bt->root->childNodeCapacity == 20);
// Should shrink when deleting the nodes again.
char* path[2] = {"", END_OF_PATH};
for(int i=0; i<11; i++){
sprintf(tmp, "%d", i);
path[0] = tmp;
BTreeDelete(bt, path);
}
assert(bt->root->childNodeCapacity == 10);
}
void TestInsertAndReplaceString(bTree* bt){
char* testKey = "key";
char* path[4] = {"en", "something", testKey, END_OF_PATH};
BTreeInsert(bt, path, IS_NUMERIC, testKey, 2);
path[1] = "something else";
BTreeInsert(bt, path, IS_NUMERIC, testKey, 2);
char* val = "123";
BTreeInsert(bt, NULL, IS_STRING, testKey, val);
char* fromNode = Find(bt->root, testKey)->stringVal;
assert(strcmp(fromNode, "123")==0);
}
void TestReplaceWrongType(bTree* bt){
char* testKey = "key";
BTreeInsert(bt, NULL, IS_STRING, testKey, "testing");
char* value = Find(bt->root, testKey)->stringVal;
assert(strcmp(value, "testing")==0);
int val = 123;
BTreeInsert(bt, NULL, IS_NUMERIC, testKey, val);
// Should still be "testing
assert(strcmp(Find(bt->root, testKey)->stringVal, value)==0);
char* path[2] = {"key", END_OF_PATH};
BTreeDelete(bt, path);
}
void TestGetText(bTree* bt){
BTreeInsert(bt, NULL, IS_FOLDER, "strings", NULL);
char* path[3] = {"strings", "en", "greeting", END_OF_PATH};
BTreeInsert(bt, path, IS_STRING, "greeting", "hello");
path[1] = "sv";
BTreeInsert(bt, path, IS_STRING, "greeting", "hej");
char* swedishGreeting = GetText(bt, "greeting", "sv");
assert(strcmp(swedishGreeting, "hej")==0);
// Does not exist, but should return a greeting in different language
char* germanGreeting = GetText(bt, "greeting", "de");
assert(germanGreeting!=NULL);
}
void TestGetType(bTree* bt){
char *path[4] = {"strings", "en", "greeting", END_OF_PATH};
assert(GetType(bt, path)==IS_STRING);
path[2] = "NADA";
assert(GetType(bt, path)==-1);
}
void TestGetString(bTree* bt){
char *path[4] = {"strings", "en", "greeting", END_OF_PATH};
assert(strcmp(GetString(bt, path), "hey")==0);
}
void TestGetInt(bTree* bt){
char *path[4] = {"config", "update", "rate", END_OF_PATH};
BTreeInsert(bt, path, IS_NUMERIC, "rate", 12);
assert(GetInt(bt, path)==12);
}
void TestGetValue(bTree* bt){
char *path[4] = {"strings", "en", "greeting", END_OF_PATH};
assert(strcmp(GetValue(bt, path, IS_STRING), "hello")==0);
}
void TestSetValue(bTree* bt){
char *path[4] = {"strings", "en", "greeting", END_OF_PATH};
SetValue(bt, path, IS_STRING, "hey");
assert(strcmp(GetValue(bt, path, IS_STRING), "hey")==0);
path[0] = "config"; path[1] = "update"; path[2] = "rate";
SetValue(bt, path, IS_NUMERIC, 13);
// Should print error message. Value should still be 13.
SetValue(bt, path, IS_STRING, "thirteen");
assert(GetValue(bt, path, IS_NUMERIC)==13);
}
void TestEnumerate(bTree* bt){
char *path[3] = {"strings", "en", END_OF_PATH};
BTreeInsert(bt, path, IS_STRING, "what", "ever");
Enumerate(bt, path);
}