Lolly 1.4.27
Loading...
Searching...
No Matches
file_test.cpp
Go to the documentation of this file.
1/** \file file.cpp
2 * \copyright GPLv3
3 * \details Unitests for file.
4 * \author Darcy
5 * \date 2019-2023
6 */
7#include "a_lolly_test.hpp"
8#include "file.hpp"
9#include "sys_utils.hpp"
10#include "tbox/tbox.h"
11#include <cstring>
12
14url xmake_lua= url_pwd () * "xmake.lua";
15
16url
18#if defined(OS_WIN) || defined(OS_MINGW)
19 return url_system ("$TMP") * url (".lolly");
20#elif defined(OS_MACOS)
21 return url_system ("/private/tmp") * url (".lolly");
22#else
23 return url_system ("/tmp") * url (".lolly");
24#endif
25}
26
27void
35
37
38#if defined(OS_WIN) || defined(OS_MINGW)
39TEST_CASE ("is_directory on Windows") {
40 CHECK (is_directory (url_system ("C:/Windows")));
41 CHECK (is_directory (url_system ("file:///C:/Windows")));
42}
43#endif
44
45TEST_CASE ("is_directory/is_regular") {
46#if defined(OS_LINUX) || defined(OS_MACOS) || defined(OS_WASM)
48#endif
49 CHECK (is_directory (url_pwd () * "tests"));
50
52 CHECK (is_directory (reroot (url_pwd (), "file")));
53 CHECK (!is_regular (url_pwd ()));
54 CHECK (!is_regular (reroot (url_pwd (), "file")));
56 CHECK (!is_symbolic_link (reroot (url_pwd (), "file")));
57
61}
62
63TEST_CASE ("is_newer") {
66 url old_dir= lolly_tmp * url ("old");
67 url new_dir= lolly_tmp * url ("new");
68 mkdir (old_dir);
69 tb_sleep (1);
70 mkdir (new_dir);
73}
74
75TEST_CASE ("is_of_type") {
76 CHECK (is_of_type (url_pwd (), "d"));
77 CHECK (is_of_type (reroot (url_pwd (), "file"), "d"));
78 CHECK (!is_of_type (url_pwd (), "f"));
79 CHECK (is_of_type (xmake_lua, "fr"));
80#if defined(OS_MINGW) || defined(OS_WIN)
81 CHECK (is_of_type (url_pwd () * url ("bin/format.bat"), "x"));
82#endif
83}
84
85TEST_CASE ("file_size") {
87 CHECK (file_size (reroot (xmake_lua, "file")) > 0);
88}
89
90TEST_CASE ("last_modified") {
92 CHECK (last_modified (reroot (xmake_lua, "file")) > 0);
93}
94
95TEST_CASE ("mkdir/rmdir") {
97 url test_mkdir= lolly_tmp * url ("tmp_dir");
102}
103
104TEST_CASE ("chdir") {
106 url old = url_pwd ();
107
108 SUBCASE ("tmp directory") {
109 url test_mkdir= lolly_tmp * url ("ch_dir");
110
113
115 url cur= url_pwd ();
116 CHECK (cur == test_mkdir);
117
118 // restore the test dir
119 chdir (old);
120 }
121
122 SUBCASE ("Root directory") {
123#if defined(OS_MINGW) || defined(OS_WIN)
124 url test_mkdir= url_system ("C:\\");
125#else
127#endif
128
130 url cur= url_pwd ();
132
133 // restore the test dir
134 chdir (old);
135 }
136
137 SUBCASE ("chinese") {
138 url test_mkdir= lolly_tmp * url ("中文");
139
142
144 url cur= url_pwd ();
145 CHECK (cur == test_mkdir);
146
147 // restore the test dir
148 chdir (old);
149 }
150
151 SUBCASE ("dir with Space") {
152 url test_mkdir= lolly_tmp * url (" spa ce");
153
156
158 url cur= url_pwd ();
159 CHECK (cur == test_mkdir);
160
161 // restore the test dir
162 chdir (old);
163 }
164}
165
166TEST_CASE ("remove") {
169
170 SUBCASE ("single file") {
171 url xyz_txt= lolly_tmp * url ("xyz.txt");
173 CHECK (file_size (xyz_txt) == 0);
174 remove (xyz_txt);
175 CHECK (file_size (xyz_txt) == -1);
176 }
177
178 SUBCASE ("multiple files") {
179 url xyz1_txt= lolly_tmp * url ("xyz1.txt");
180 url xyz2_txt= lolly_tmp * url ("xyz2.txt");
181 url xyz3_txt= lolly_tmp * url ("xyz3.txt");
185 CHECK (file_size (xyz1_txt) == 0);
186 CHECK (file_size (xyz2_txt) == 0);
187 CHECK (file_size (xyz3_txt) == 0);
189 CHECK (file_size (xyz1_txt) == -1);
190 CHECK (file_size (xyz2_txt) == -1);
191 CHECK (file_size (xyz3_txt) == -1);
192 }
193}
194
195TEST_CASE ("move") {
198 url m1 = lolly_tmp * url ("move_1.txt");
199 url m2 = lolly_tmp * url ("move_2.txt");
201 move (m1, m2);
202 CHECK (file_size (m1) == -1);
203 CHECK (file_size (m2) == 0);
204 remove (m2);
205}
206
207TEST_CASE ("copy") {
210 url c1 = lolly_tmp * url ("copy_1.txt");
211 url c2 = lolly_tmp * url ("copy_2.txt");
213 CHECK (file_size (c2) == -1);
214 copy (c1, c2);
215 CHECK (file_size (c1) == 0);
216 CHECK (file_size (c2) == 0);
217 remove (c1);
218 remove (c2);
219}
220
222
223TEST_CASE ("url_temp") {
224 url tmp1= url_temp ("png");
227 url tmp2= url_temp ("png");
228 CHECK_EQ (tmp1 == tmp2, false);
230}
231// because url_temp_dir will initialize static variable, occupied memory will
232// increase
234
235TEST_CASE ("read_directory") {
236 bool flag1= false;
237 CHECK (N (read_directory (url_pwd () * "tests", flag1)) > 0);
238 CHECK (!flag1); // no error
239 bool flag2= false;
240 CHECK (N (read_directory (url_system ("no_such_dir"), flag2)) == 0);
241 CHECK (flag2); // error
242}
243
244TEST_CASE ("subdirectories") {
246 if (!os_wasm ()) {
247 url_eq (subdirectories (url_pwd () * "bin"), url_pwd () * "bin");
248 }
249}
250
251TEST_CASE ("load_string from empty file") {
253 url u1 = lolly_tmp * url ("load_empty.txt");
254 string s1;
256 CHECK (!load_string (u1, s1, false));
257 CHECK_EQ (s1 == string (""), true);
258}
259
260TEST_CASE ("load_string from newly created file") {
262 url u1 = lolly_tmp * url ("load_string_1.txt");
264 // can access file?
266 while (!tb_file_remove (s1)) {
267 tb_sleep (1);
268 }
269 }
270 // create test file
271 if (tb_file_create (s1)) {
272 const char* s2 = "hello world";
273 tb_size_t size = strlen (s2);
274 tb_byte_t* buffer= (tb_byte_t*) tb_malloc_bytes (size);
275 memcpy (buffer, s2, size);
276
278 tb_file_writ (file_ref, buffer, size);
279 string s;
280 CHECK (!load_string (u1, s, false));
281 string_eq (s, string ("hello world"));
282 }
283}
284
285#ifdef OS_WASM
286// because exception throw, some object will not be released.
288#endif
289
290TEST_CASE ("load_string from 3 local files and check exception") {
291 url u1= url_pwd () * url ("tests/System/Files/sample_file.txt");
292 url u2= url_pwd () * url ("tests/System/Files/sample_file_copy.txt");
293 url u3= url_pwd () * url ("tests/System/Files/sample_file_throw.txt");
294 string s1, s2, s3;
295 CHECK (!load_string (u1, s1, false));
296 CHECK (!load_string (u2, s2, false));
297 string_eq (s1, s2);
298
299 TM_CHECK_THROWS (load_string (u3, s3, true));
300}
301
302#ifdef OS_WASM
303// because exception throw, some object will not be released.
305#endif
306
307TEST_CASE ("load_string from url with :") {
308 url u=
309 url_unix ("tests/Kernel/Containers:tests/Kernel/Types", "list_test.cpp");
310 string s;
311 load_string (u, s, false);
312 CHECK (N (s) > 0);
313}
314
315#if defined(OS_MINGW) || defined(OS_WIN)
316TEST_CASE ("load_string from read only file") {
317 url hosts= url_system ("C:\\Windows\\System32\\drivers\\etc\\hosts");
318 string s;
319 load_string (hosts, s, false);
320 CHECK (N (s) > 0);
321}
322#endif
323
324#if defined(OS_LINUX) || defined(OS_MACOS)
325TEST_CASE ("load_string from read only file") {
326 url hosts= url_system ("/etc/hosts");
327 string s;
328 load_string (hosts, s, false);
329 CHECK (N (s) > 0);
330}
331#endif
332
333TEST_CASE ("save to empty file") {
335 url u1 = lolly_tmp * url ("save_empty.txt");
336 string s1 ("test");
338 CHECK (!save_string (u1, s1, false));
339 string s2;
340 CHECK (!load_string (u1, s2, false));
341 string_eq (s1, s2);
342}
343
344TEST_CASE ("save to empty file with unicode filename") {
346 url u1 = lolly_tmp * url ("保存到空文件.txt");
347 string s1 ("测试内容");
349 CHECK (!save_string (u1, s1, false));
350 string s2;
351 CHECK (!load_string (u1, s2, false));
352 string_eq (s1, s2);
353}
354
355TEST_CASE ("create and save to file") {
357 url u1 = lolly_tmp * url ("save_nonexist.txt");
359 string s1 ("test");
360 CHECK (!save_string (u1, s1, true));
361 string s2;
362 CHECK (!load_string (u1, s2, true));
363 string_eq (s1, s2);
364}
365
366TEST_CASE ("save to exist file") {
368 url u1 = lolly_tmp * url ("save_exist.txt");
370 tb_file_touch (path1, 0, 0);
371 tb_file_ref_t file=
374 file, reinterpret_cast<const tb_uint8_t*> ("longer text for test"), 20);
375 tb_file_exit (file);
376 string s1 ("test");
377 CHECK (!save_string (u1, s1, false));
378 string s2;
379 CHECK (!load_string (u1, s2, false));
380 string_eq (s1, s2);
381}
382
383TEST_CASE ("append to empty file") {
385
386 SUBCASE ("file not exist") {
387 url u1= lolly_tmp * url ("append_not_exist.txt");
388 string s1 ("file not exist");
389 string s2;
391
392 CHECK (!append_string (u1, s1, false));
393 CHECK (!load_string (u1, s2, false));
394 string_eq (s1, s2);
395 }
396
397 SUBCASE ("file empty") {
398 url u1= lolly_tmp * url ("append_empty.txt");
399 string s1 ("file empty");
400 string s2;
401
403 CHECK (!append_string (u1, s1, false));
404
405 CHECK (!load_string (u1, s2, false));
406 string_eq (s1, s2);
407 }
408}
409
410TEST_CASE ("append file not empty") {
412 SUBCASE ("test file not empty") {
413 url u1= lolly_tmp * url ("append_not_empty.txt");
415
416 string s1 ("head-");
417 string s2 ("file not empty");
418 string s3;
419
420 CHECK (!save_string (u1, s1, false));
421 CHECK (!append_string (u1, s2, false));
422
423 CHECK (!load_string (u1, s3, false));
424 string_eq ("head-file not empty", s3);
425 }
426
427 SUBCASE ("test file include unicode") {
428 url u1= lolly_tmp * url ("unicode文件.txt");
430
431 string s1 ("头-");
432 string s2 ("unicode文件");
433 string s3;
434
435 CHECK (!save_string (u1, s1, false));
436 CHECK (!append_string (u1, s2, false));
437
438 CHECK (!load_string (u1, s3, false));
439 string_eq ("头-unicode文件", s3);
440 }
441}
442
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
int copy(int x)
Returns a copy of an integer.
Definition basic.hpp:249
The list class represents a linked list.
Definition list.hpp:48
Definition url.hpp:37
int last_modified(url u)
Definition file.cpp:169
bool is_regular(url u)
Definition file.cpp:57
bool is_directory(url u)
Definition file.cpp:38
bool load_string(url u, string &s, bool fatal)
Definition file.cpp:434
url url_temp(string suffix)
Definition file.cpp:274
bool is_newer(url which, url than)
Definition file.cpp:90
url subdirectories(url u)
Definition file.cpp:211
void rmdir(url u)
Definition file.cpp:249
void move(url u1, url u2)
Definition file.cpp:310
bool append_string(url u, const string &s, bool fatal)
Definition file.cpp:543
void chdir(url u)
Definition file.cpp:263
bool is_symbolic_link(url u)
Definition file.cpp:76
url url_temp_dir()
Definition file.cpp:300
int file_size(url u)
Definition file.cpp:155
bool save_string(url u, const string &s, bool fatal)
Definition file.cpp:490
void mkdir(url u)
Definition file.cpp:226
bool is_of_type(url name, string filter)
Definition file.cpp:105
array< string > read_directory(url u, bool &error_flag)
Definition file.cpp:194
url xmake_lua
Definition file_test.cpp:14
void remove_if_exist(const url &u1)
Definition file_test.cpp:28
url get_lolly_tmp()
Definition file_test.cpp:17
url unix_root
Definition file_test.cpp:13
url url_unix(string name)
Definition file_url.cpp:31
list< T > remove(list< T > l, T what)
Create a new list with a specific item removed.
Definition list.ipp:214
#define TEST_MEMORY_LEAK_RESET
void url_eq(url left, url right)
#define TM_CHECK_THROWS(statements)
#define TEST_MEMORY_LEAK_INIT
#define TEST_MEMORY_LEAK_ALL
void string_eq(string left, string right)
TEST_CASE("test for operator+= and advance()")
string as_string(int16_t i)
Definition string.cpp:310
bool os_wasm()
url url_none()
Definition url.cpp:91
url url_system(string name)
Definition url.cpp:306
url reroot(url u, string protocol)
Definition url.cpp:490
url url_pwd()
Definition url.cpp:326