-
Notifications
You must be signed in to change notification settings - Fork 5
/
unit_tests.c
184 lines (169 loc) · 9.21 KB
/
unit_tests.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Unit tests for parser of the
* C Programming Language (ISO/IEC 9899:2018).
*
* @authors: Denis Chernikov, Vladislav Kuleykin
*/
#include <stddef.h>
#include <stdio.h>
#include "ast.h"
#include "string_tools.h"
#include "typedef_name.h"
/// Conversion function for AST node content.
/// Copied from `main.c'.
///
/// \param obj Object of AST content
/// \return String representation of the given object, NULL if not AST_NODE
char *content_to_str(AST_NODE *node)
{
return alloc_const_str((char *) node->content);
}
int passed = 0;
int failed = 0;
void pass_test(_Bool result, char *label)
{
if (result)
{
++passed;
}
else
{
++failed;
printf("Failed test #%d:\n%s\n", passed + failed, label);
}
}
int main()
{
printf("Start of testing.\n\n");
// Test `str_eq'
pass_test(str_eq(NULL, NULL), "str_eq(NULL, NULL)");
pass_test(!str_eq(NULL, ""), "!str_eq(NULL, \"\")");
pass_test(str_eq("", ""), "str_eq(\"\", \"\")");
pass_test(!str_eq("b", "a"), "!str_eq(\"b\", \"a\")");
pass_test(str_eq("aaa", "aaa"), "str_eq(\"aaa\", \"aaa\")");
// Test `repeat'
pass_test(str_eq(repeat(4, "a"), "aaaa"), "repeat(4, \"a\")");
pass_test(str_eq(repeat(0, "a"), ""), "repeat(0, \"a\")");
pass_test(str_eq(repeat(-10, "a"), NULL), "repeat(-10, \"a\")");
pass_test(str_eq(repeat(1, "a"), "a"), "repeat(1, \"a\")");
pass_test(str_eq(repeat(3, ""), ""), "repeat(3, \"\")");
pass_test(str_eq(repeat(2, NULL), NULL), "repeat(2, NULL)");
// Test `concat_array'
pass_test(str_eq(concat_array((char *[]) {"a", "b", "c", "d"}, 4, ", "), "a, b, c, d"),
"concat_array((char*[]){\"a\", \"b\", \"c\", \"d\"}, 4, \", \")");
pass_test(str_eq(concat_array((char *[]) {"a", "", "c"}, 3, ", "), "a, , c"),
"concat_array((char*[]){\"a\", \"\", \"c\"}, 3, \", \")");
pass_test(str_eq(concat_array((char *[]) {"a", NULL}, 2, ", "), "a, "),
"concat_array((char*[]){\"a\", NULL}, 2, \", \")");
pass_test(str_eq(concat_array(NULL, 2, ", "), NULL),
"concat_array(NULL, 2, \", \")");
pass_test(str_eq(concat_array((char *[]) {"a", NULL}, -1, ", "), NULL),
"concat_array((char*[]){\"a\", NULL}, 2, \", \")");
pass_test(str_eq(concat_array((char *[]) {"a", NULL}, 2, NULL), NULL),
"concat_array((char*[]){\"a\", NULL}, 2, NULL)");
pass_test(str_eq(concat_array((char *[]) {"a"}, 0, ", "), ""),
"concat_array((char*[]){\"a\", NULL}, 2, NULL)");
// Test `wrap_by_quotes'
pass_test(str_eq(wrap_by_quotes("\" \\ \b \f \n \r \t"), "\"\\\" \\\\ \\b \\f \\n \\r \\t\""),
"wrap_by_quotes(\"\\\" \\\\ \\b \\f \\n \\r \\t\")");
pass_test(str_eq(wrap_by_quotes(NULL), NULL), "wrap_by_quotes(NULL)");
pass_test(str_eq(wrap_by_quotes(""), "\"\""), "wrap_by_quotes(\"\")");
// Test `is_typedef_name'
put_typedef_name("a");
pass_test(is_typedef_name("a"), "put_typedef_name(\"a\");\nis_typedef_name(\"a\")");
pass_test(!is_typedef_name("b"), "put_typedef_name(\"a\");\nis_typedef_name(\"b\")");
add_std_typedef("math.h");
pass_test(is_typedef_name("float_t"), "add_std_typedef(\"math.h\");\nis_typedef_name(\"float_t\")");
pass_test(!is_typedef_name("real_t"), "add_std_typedef(\"math.h\");\nis_typedef_name(\"real_t\")");
// Test `ast_create_node'
AST_NODE *node1 = ast_create_node(Identifier, "node1", 0);
pass_test(node1->type == Identifier,
"ast_create_node(Identifier, \"node1\", 0); node1->type == Identifier;");
pass_test(str_eq(node1->content, "node1"),
"ast_create_node(Identifier, \"node1\", 0); node1->content == \"node1\";");
pass_test(node1->children_number == 0,
"ast_create_node(Identifier, \"node1\", 0); node1->children_number == 0;");
pass_test(node1->children == NULL,
"ast_create_node(Identifier, \"node1\", 0); node1->children == NULL;");
AST_NODE *node2 = ast_create_node(Identifier, "node2", 1, node1);
pass_test(node2->children_number == 1,
"ast_create_node(Identifier, \"node2\", 1, node1); node2->children_number == 1;");
pass_test(node2->children != NULL,
"ast_create_node(Identifier, \"node2\", 1, node1); node2->children != NULL;");
pass_test(node2->children[0] == node1,
"ast_create_node(Identifier, \"node2\", 1, node1); node2->children[0] == node1;");
AST_NODE *node3 = ast_create_node(Identifier, "node3", 2, node1, node2);
pass_test(node3->children_number == 2,
"ast_create_node(Identifier, \"node3\", 2, node1, node2); node3->children_number == 2;");
pass_test(node3->children != NULL,
"ast_create_node(Identifier, \"node3\", 2, node1, node2); node3->children != NULL;");
pass_test(node3->children[0] == node1,
"ast_create_node(Identifier, \"node3\", 2, node1, node2); node3->children[0] == node1;");
pass_test(node3->children[1] == node2,
"ast_create_node(Identifier, \"node3\", 2, node1, node2); node3->children[1] == node2;");
// Test `ast_expand_node'
AST_NODE* ast_node = ast_create_node(TranslationUnit, NULL, 0);
AST_NODE* ast_node_to_add = ast_create_node(TranslationUnit, NULL, 0);
AST_NODE* ast_node_expanded = ast_create_node(TranslationUnit, NULL, 0);
ast_expand_node(ast_node_expanded, ast_node_to_add);
pass_test(ast_expand_node(ast_node, ast_node_to_add)->type == ast_node_expanded->type,
"AST_NODE* ast_node = ast_create_node(TranslationUnit, NULL, 0);\n"
"AST_NODE* ast_node_to_add = ast_create_node(TranslationUnit, NULL, 0);\n"
"AST_NODE* ast_node_expanded = ast_expand_node(ast_node, ast_node_to_add);\n"
"ast_expand_node(ast_node, ast_node_to_add)->type == ast_node_expanded->type");
ast_node = ast_create_node(TranslationUnit, NULL, 0);
pass_test(ast_expand_node(ast_node, ast_node_to_add)->content == ast_node_expanded->content,
"AST_NODE* ast_node = ast_create_node(TranslationUnit, NULL, 0);\n"
"AST_NODE* ast_node_to_add = ast_create_node(TranslationUnit, NULL, 0);\n"
"AST_NODE* ast_node_expanded = ast_expand_node(ast_node, ast_node_to_add);\n"
"ast_expand_node(ast_node, ast_node_to_add)->content == ast_node_expanded->content");
ast_node = ast_create_node(TranslationUnit, NULL, 0);
pass_test(ast_expand_node(ast_node, ast_node_to_add)->children_number == ast_node_expanded->children_number,
"AST_NODE* ast_node = ast_create_node(TranslationUnit, NULL, 0);\n"
"AST_NODE* ast_node_to_add = ast_create_node(TranslationUnit, NULL, 0);\n"
"AST_NODE* ast_node_expanded = ast_expand_node(ast_node, ast_node_to_add);\n"
"ast_expand_node(ast_node, ast_node_to_add)->children_number == ast_node_expanded->children_number");
ast_node = ast_create_node(TranslationUnit, NULL, 0);
pass_test(ast_expand_node(ast_node, ast_node_to_add)->children == ast_node->children,
"AST_NODE* ast_node = ast_create_node(TranslationUnit, NULL, 0);\n"
"AST_NODE* ast_node_to_add = ast_create_node(TranslationUnit, NULL, 0);\n"
"AST_NODE* ast_node_expanded = ast_expand_node(ast_node, ast_node_to_add);\n"
"ast_expand_node(ast_node, ast_node_to_add)->children == ast_node->children");
pass_test(ast_expand_node(NULL, ast_node_to_add) == NULL,
"AST_NODE* ast_node = ast_create_node(TranslationUnit, NULL, 0);\n"
"AST_NODE* ast_node_to_add = ast_create_node(TranslationUnit, NULL, 0);\n"
"ast_expand_node(NULL, ast_node_to_add) == NULL");
ast_node = ast_create_node(TranslationUnit, NULL, 0);
pass_test(ast_expand_node(ast_node, NULL)->children_number == 0,
"AST_NODE* ast_node = ast_create_node(TranslationUnit, NULL, 0);\n"
"AST_NODE* ast_node_to_add = ast_create_node(TranslationUnit, NULL, 0);\n"
"ast_expand_node(ast_node, NULL)->children_number == 0");
ast_node = ast_create_node(TranslationUnit, NULL, 0);
pass_test(ast_expand_node(ast_node, NULL)->children == NULL,
"AST_NODE* ast_node = ast_create_node(TranslationUnit, NULL, 0);\n"
"AST_NODE* ast_node_to_add = ast_create_node(TranslationUnit, NULL, 0);\n"
"ast_expand_node(ast_node, NULL)->children == NULL");
// Test `ast_type_to_str'
pass_test(str_eq(ast_type_to_str(TranslationUnit), "TranslationUnit"), "ast_type_to_str(TranslationUnit)");
pass_test(str_eq(ast_type_to_str(-1),NULL), "ast_type_to_str(-1)");
// Test `ast_to_json'
char *json1 = "{\n"
" \"type\": \"Identifier\",\n"
" \"content\": \"node1\",\n"
" \"children_number\": 0,\n"
" \"children\": null\n"
"}";
pass_test(str_eq(ast_to_json(node1, 0, " ", content_to_str), json1),
"ast_to_json(node1, 0, \" \", content_to_str)");
char *json2 = " {\n"
" \"type\": \"Identifier\",\n"
" \"content\": \"node1\",\n"
" \"children_number\": 0,\n"
" \"children\": null\n"
" }";
pass_test(str_eq(ast_to_json(node1, 2, " ", content_to_str), json2),
"ast_to_json(node1, 2, \" \", content_to_str)");
printf("\nResults:\n Total number of tests: %d\n Passed: %d\n Failed: %d\n",
passed + failed, passed, failed);
return 0;
}