Repository navigation
Expand file tree
/
Copy pathtest.c
More file actions
189 lines (165 loc) · 5.93 KB
/
Copy pathtest.c
File metadata and controls
189 lines (165 loc) · 5.93 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
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
185
186
187
188
189
// need to run through `make run` so argc and argv are the expected ones
#include <string.h>
#define AHB_LIB_IMPLEMENTATION
#define AHB_STRIP_PREFIX
#include "ahb_lib.h"
#define OUTPUT_SIZE 256
int total_assertions = 0;
int passed_assertions = 0;
int failed_assertions = 0;
void assert_equal_str(char *expected, char *got, char *name)
{
total_assertions += 1;
if (strcmp(expected, got) != 0) {
failed_assertions += 1;
color_printf(COLOR_RED, " FAIL: ");
printf("%s:\n Expected: %s, Got: %s\n", name, expected, got);
}
else {
passed_assertions += 1;
color_printf(COLOR_GREEN, " PASS: ");
printf("%s\n", name);
}
}
void assert_equal_int(int expected, int got, char *name)
{
total_assertions += 1;
if (expected != got) {
failed_assertions += 1;
color_printf(COLOR_RED, " FAIL: ");
printf("%s:\n Expected: %d, Got: %d\n", name, expected, got);
}
else {
passed_assertions += 1;
color_printf(COLOR_GREEN, " PASS: ");
printf("%s\n", name);
}
}
void assert_not_equal_int(int expected, int got, char *name)
{
total_assertions += 1;
if (expected == got) {
failed_assertions += 1;
color_printf(COLOR_RED, " FAIL: ");
printf("%s:\n Expected: %d, Got: %d\n", name, expected, got);
}
else {
passed_assertions += 1;
color_printf(COLOR_GREEN, " PASS: ");
printf("%s\n", name);
}
}
void assert_is_null(void *got, char *name)
{
total_assertions += 1;
if (NULL != got) {
failed_assertions += 1;
color_printf(COLOR_RED, " FAIL: ");
printf("%s:\n Expected: NULL, Got: %p\n", name, got);
}
else {
passed_assertions += 1;
color_printf(COLOR_GREEN, " PASS: ");
printf("%s\n", name);
}
}
// need to run through `make run` so argc and argv are the expected ones
int main(int argc, char **argv) {
ahb_args_init(argc, argv);
char *arg = NULL;
arg = args_next();
assert_equal_str("first", arg, "ahb_args_next 1");
arg = args_next();
assert_equal_str("second", arg, "ahb_args_next 2");
arg = args_next();
assert_equal_str("3", arg, "ahb_args_next 3");
ExecResult res = {0};
bool got = ahb_exec(&res, "ls -lah");
assert_equal_int(true, got, "ahb_exec");
StringList list = {0};
ahb_split(res.output, '\n', &list);
assert_equal_int(true, list.count > 0, "ahb_split");
StringList words = {0};
ahb_split(list.data[1].data, ' ', &words);
assert_equal_int(true, words.count > 0, "ahb_split 2");
assert_equal_str(".", words.data[words.count - 1].data, "ahb_split 3");
exec_free(&res);
assert_is_null(res.output, "exec_free 1");
assert_is_null(res.error, "exec_free 2");
split_free(&words);
assert_is_null(words.data, "split_free 1");
split_free(&list);
assert_is_null(list.data, "split_free 2");
bool exist = ahb_file_exits("./test.c");
assert_equal_int(true, exist, "ahb_file_exist true");
exist = ahb_file_exits("./doesnotexist.c");
assert_equal_int(false, exist, "ahb_file_exist false");
// update this with the current size of README.md
long size = ahb_get_file_size("README.md");
assert_equal_int(126, size, "ahb_get_file_size");
char *content = read_entire_file("./test.c");
assert_equal_int(true, strlen(content) > 0, "ahb_read_entire_file");
free(content);
char *cstr = "Test String";
String str = ahb_cstr_to_String(cstr);
assert_equal_str(cstr, str.data, "ahb_cstr_to_String");
char* cstr2 = ahb_String_to_cstr(str);
assert_equal_str(cstr, cstr2, "ahb_String_to_cstr");
String totrim = ahb_cstr_to_String(" test ");
assert_equal_int(10, totrim.count, "ahb_cstr_to_String");
String trimmed = ahb_String_trim(totrim);
assert_equal_int(4, trimmed.count, "ahb_cstr_to_String");
StringBuilder sb = {0};
ahb_sb_append(&sb, "hello");
assert_equal_str("hello", sb.data, "ahb_sb_append");
ahb_sb_append(&sb, " world");
assert_equal_str("hello world", sb.data, "ahb_sb_append 2");
ahb_sb_free(&sb);
assert_is_null(sb.data, "ahb_sb_free");
// dynamic arrays tests
typedef struct {
int *data;
size_t count;
size_t capacity;
} List;
List dyn = {0};
assert_is_null(dyn.data, "dynamic array");
da_append(&dyn, 1);
assert_equal_int(1, dyn.count, "da_append");
da_append(&dyn, 2);
da_append(&dyn, 3);
da_append(&dyn, 4);
assert_equal_int(4, dyn.count, "da_append");
da_free(&dyn);
assert_is_null(dyn.data, "da_free");
assert_equal_int(0, dyn.count, "da_free 2");
assert_equal_int(0, dyn.capacity, "da_free 3");
// hashmap tests
Map hashmap = {0};
ahb_map_insert(&hashmap, "key1", "first item", strlen("first item"));
assert_equal_int(1, hashmap.count, "ahb_map_insert");
ahb_map_insert(&hashmap, "key2", "second item", strlen("first item"));
assert_equal_int(2, hashmap.count, "ahb_map_insert 2");
char *hash_value = ahb_map_search(&hashmap, "key1");
assert_equal_str("first item", hash_value, "ahb_map_search");
ahb_map_remove(&hashmap, "key2", NULL);
assert_equal_int(1, hashmap.count, "ahb_map_remove");
ahb_map_free(&hashmap, NULL);
assert_is_null(hashmap.data, "ahb_map_free");
/* still to implement */
// arena_init, arena_alloc, arena_reset, arena_free, arena_strdup, arena_strndup
// ahb_read_entire_file_arena
// ahb_temp_alloc, ahb_temp_alloc_free
// ahb_sb_to_cstr, ahb_str_replace_all, ahb_print_String
// ahb_Log, ahb_Log_nonl
// ahb_is_valid_date, date_to_lld
printf("\n");
printf("Summary:\n");
printf(" Assertions: %3d\n", total_assertions);
if (passed_assertions > 0) color_printf(COLOR_GREEN, " Passed: %3d\n", passed_assertions);
if (failed_assertions > 0) {
color_printf(COLOR_RED, " Failed: %3d\n", failed_assertions);
return 1;
}
return 0;
}