-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_node.c
134 lines (116 loc) · 3.72 KB
/
test_node.c
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
/* This file is part of NodeTrie. */
/* Copyright (C) 2017 Panos Kittenis */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation, version 2.1. */
/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
/* Lesser General Public License for more details. */
/* You should have received a copy of the GNU Lesser General Public */
/* License along with this library; if not, write to the Free Software */
/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
#include <stdio.h>
#include <stdlib.h>
#include <check.h>
#include "../src/node.h"
START_TEST(test_create_index) {
Node *root = init_node();
ck_assert_int_eq(root->children_i, 0);
ck_assert(root->name == NULL);
root = clear(root);
ck_assert(root == NULL);
}
END_TEST
void test_node_children(Node *parent, unsigned char **name) {
Node* child;
while (*name) {
// printf("Checking name %s\n", *name);
child = name_is_child(parent, *name);
ck_assert(child != NULL);
ck_assert_str_eq(child->name, *name);
ck_assert(!is_leaf(parent));
parent = child;
name++;
}
ck_assert(is_leaf(child));
}
START_TEST(test_create_index_tree) {
unsigned char *paths[] = {"b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", NULL};
Node *root = init_node();
insert_paths(root, paths);
ck_assert_int_eq(root->children_i, 1);
for (size_t i = 0; i < 1; i++) {
ck_assert_int_eq(root->children[i].children_i, 1);
}
unsigned char *new_paths[] = {"b1", "b22", "b3", "b44", "b5", "b66", "b7", "b88", "b9", NULL};
insert_paths(root, new_paths);
for (size_t i = 0; i < 1; i++) {
if (i+1 % 2 == 1) {
ck_assert_int_eq(root->children[i].children_i, 2);
}
else if (i+1 % 2 == 0) {
ck_assert_int_eq(root->children[i].children_i, 1);
}
}
ck_assert(!is_leaf(root));
// Check children
test_node_children(root, &paths[0]);
test_node_children(root, &new_paths[0]);
// Clearing
root = clear(root);
ck_assert(root == NULL);
}
END_TEST
START_TEST(test_is_pattern) {
char *patterns[5] = {"*", "?", "[", "{", NULL};
char **pattern = patterns;
while(*pattern) {
ck_assert(is_pattern(*(pattern)++));
}
char *invalid_patterns[6] = {"jfd", "asdf", "arter", "treop", "ertiu", NULL};
pattern = invalid_patterns;
while(*pattern) {
ck_assert(!is_pattern(*(pattern)++));
}
}
END_TEST
/*
START_TEST(test_match_entries) {
unsigned char *keys[] = {"b1", "b2", "b3", "b4", NULL};
unsigned char **matches = match_entries(keys, "b*");
size_t i = 0;
unsigned char *key = keys[i];
while (key) {
ck_assert_str_eq(matches[i], key);
i++;
key = keys[i];
}
}
END_TEST
*/
Suite * node_suite(void)
{
Suite *s;
TCase *tc_core;
s = suite_create("node");
/* Core test case */
tc_core = tcase_create("Core");
tcase_add_test(tc_core, test_create_index);
tcase_add_test(tc_core, test_create_index_tree);
// tcase_add_test(tc_core, test_match_entries);
tcase_add_test(tc_core, test_is_pattern);
suite_add_tcase(s, tc_core);
return s;
}
int main(void) {
int number_failed;
Suite *s;
SRunner *sr;
s = node_suite();
sr = srunner_create(s);
srunner_run_all(sr, CK_VERBOSE);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}