-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
247 lines (194 loc) · 6.4 KB
/
Copy pathtests.cpp
File metadata and controls
247 lines (194 loc) · 6.4 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#define JLIB_IMPLEMENTATION
#include "jlib.h"
static inline void print_header(const char *h) {
const char *sep = "********************************************";
puts("");
puts(sep);
puts(h);
puts(sep);
puts("");
}
#define JLIB_TEST_AUTO_ARRAY
#if defined(JLIB_TEST_AUTO_ARRAY)
#include <stdio.h>
int main(int argc, char *argv[]) {
jAuto_Array<int> arr = {};
print_header("jAuto_Array");
arr.clear();
arr.append(1);
assert(arr[0] == 1);
arr.remove_unordered(0);
assert(arr.len == 0);
arr.clear();
arr.append(1);
arr.append(2);
arr.remove_unordered(1);
assert(arr[0] == 1 && arr.len == 1);
arr.clear();
arr.append(1);
arr.append(2);
arr.append(3);
arr.remove_ordered(0);
assert(arr[0] == 2 && arr.len == 2);
puts("All went well!");
return 0;
}
#elif defined(JLIB_TEST_STRINGS)
#include <stdio.h>
static inline void test_string_trim(
const char *string_to_trim, char32_t *trim_chars, ssize trim_char_count, jString expected
) {
jSlice<char32_t> chars_to_trim = jSlice<char32_t>(trim_chars, trim_char_count);
jString trimmed_string = jString(string_to_trim).trim_chars(chars_to_trim);
printf("raw string: \"%s\"\n", string_to_trim);
printf("trimmed string: \"%s\"\n", trimmed_string.clone_to_cstring(jAllocator::heap_allocator()));
assert(trimmed_string == expected);
}
static inline void test_string_find_last_char(
const char *string, char32_t find_char, ssize expected
) {
ssize x = jString(string).find_last_char(find_char);
printf("find last char in: %s\n", string);
printf("expected: %lld, got: %lld\n", expected, x);
assert(x == expected);
}
static inline void test_string_find_first_char(
const char *string, char32_t find_char, ssize expected
) {
ssize x = jString(string).find_first_char(find_char);
printf("find first char in: %s\n", string);
printf("expected: %lld, got: %lld\n", expected, x);
assert(x == expected);
}
static inline void test_path_find_dir(
const char *full, const char *expected
) {
jString dir = j_path_dir(full);
const char *dir_string = dir.clone_to_cstring(jAllocator::heap_allocator());
printf("path: %s, dir: %s\n", full, dir_string);
printf("expected: %s, got: %s\n", expected, dir_string);
assert(jString(expected) == dir);
}
static inline void test_path_ext(
const char *path, const char *expected
) {
jString ext = j_path_ext(path);
const char *ext_string = ext.clone_to_cstring(jAllocator::heap_allocator());
printf("path: %s, file: %s\n", path, ext_string);
printf("expected: %s, got: %s\n", expected, ext_string);
assert(jString(expected) == ext);
}
static inline void test_path_file(
const char *path, const char *expected
) {
jString file = j_path_file(path);
const char *file_string = file.clone_to_cstring(jAllocator::heap_allocator());
printf("path: %s, ext: %s\n", path, file_string);
printf("expected: %s, got: %s\n", expected, file_string);
assert(jString(expected) == file);
}
static inline void test_join_strings(
jSlice<const jString> strings, jString sep, const char *expected
) {
jString result = jString::join(strings, sep, jAllocator::heap_allocator());
printf("expected: \"%s\", got: \"%s\"\n", expected, result.clone_to_cstring(jAllocator::heap_allocator()));
assert(result == jString(expected));
}
static inline void test_string_split(
const char *str, jSlice<char32_t> sep
) {
printf("Split string %s:\n", str);
jSlice<jString> result = jString(str).split(sep, jAllocator::heap_allocator(), INT_MAX);
for (jString r : result) {
printf("\"%s\"\n", r.clone_to_cstring(jAllocator::heap_allocator()));
}
}
int main(int argc, char *argv[]) {
print_header("jString::trim");
test_string_trim("&*&Hello, world!*&**&", U"&*", 2, "Hello, world!");
test_string_trim("&*&Hello, world!", U"&*", 2, "Hello, world!");
test_string_trim("Hello, world!*&**&", U"&*", 2, "Hello, world!");
test_string_trim("Hello, world!", U"&*", 2, "Hello, world!");
print_header("jString::find_*_char");
test_string_find_last_char("///", '/', 2);
test_string_find_last_char("///", '4', -1);
test_string_find_first_char("$//", '/', 1);
test_string_find_first_char("///", '4', -1);
print_header("j_path_dir");
#ifdef _WIN32
test_path_find_dir("/foo\\bar/1234", "/foo\\bar");
test_path_find_dir("\\foo\\bar\\", "\\foo\\bar");
test_path_find_dir("\\foo", "\\");
#endif
test_path_find_dir("/foo/bar/1234", "/foo/bar");
test_path_find_dir("/foo", "/");
test_path_find_dir("/foo/bar", "/foo");
test_path_find_dir("a/b/c", "a/b");
test_path_find_dir("////", "///");
test_path_find_dir("foobar", "");
print_header("j_path_ext");
test_path_ext("foo.bar", ".bar");
test_path_ext("foo.", ".");
test_path_ext("foo", "");
test_path_ext(".bar", ".bar");
test_path_ext("foo.bar.baz", ".baz");
test_path_ext("foo.bar/foo.baz", ".baz");
print_header("j_path_file");
test_path_file("foo", "foo");
test_path_file("foo/bar.baz", "bar.baz");
print_header("jString::join");
{
jString strings1[] = {"Foo", "Bar", "Baz"};
test_join_strings({strings1, ARRAY_LEN(strings1)}, ", ", "Foo, Bar, Baz");
jString strings2[] = {"Foo"};
test_join_strings({strings2, ARRAY_LEN(strings2)}, ", ", "Foo");
jString strings3[] = {"Foo", "Bar", "Baz"};
test_join_strings({strings3, ARRAY_LEN(strings3)}, "", "FooBarBaz");
}
print_header("jString::split");
{
test_string_split("Foo, Bar, Baz", {U", ", 2});
test_string_split("Foo, Bar,", {U", ", 2});
test_string_split(",,Foo, Bar,", {U", ", 2});
test_string_split(",,Foo, ,,Bar, Baz,,, ", {U", ", 2});
test_string_split(",,", {U", ", 2});
test_string_split("A,B,C,", {U", ", 2});
}
return 0;
}
#elif defined(JLIB_TEST_FILESYSTEM)
static inline void test_walk_directory(const char *path) {
printf("Iterating directory: %s\n", path);
jDirectory_Iterator *iter = j_directory_iterate(path);
jFile_Info file = {};
if (iter) {
while (iter->iterate(&file)) {
puts(file.full_path.data);
}
}
else {
puts("Not found.");
}
}
static inline void test_file_info(const char *path) {
jFile_Info info = {};
printf("Dumping file info for: %s\n", path);
if (j_file_info_get(path, &info, jAllocator::heap_allocator())) {
printf("Size: %llu\n", info.size);
printf("Type: %d\n", info.type);
printf("Full path: %s\n", info.full_path.data);
}
else {
puts("File not found.");
}
}
int main(int argc, char *argv[]) {
print_header("j_directory_iterate");
test_walk_directory(".");
test_walk_directory("..");
test_walk_directory("");
print_header("j_file_info_get");
test_file_info("jlib.h");
return 0;
}
#endif