Lolly 1.4.28
Loading...
Searching...
No Matches
Functions | Variables
file_test.cpp File Reference
#include "a_lolly_test.hpp"
#include "file.hpp"
#include "sys_utils.hpp"
#include "tbox/tbox.h"
#include <cstring>
Include dependency graph for file_test.cpp:

Go to the source code of this file.

Functions

url get_lolly_tmp ()
 
void remove_if_exist (const url &u1)
 
TEST_MEMORY_LEAK_INIT TEST_CASE ("is_directory/is_regular")
 
 TEST_CASE ("is_newer")
 
 TEST_CASE ("is_of_type")
 
 TEST_CASE ("file_size")
 
 TEST_CASE ("last_modified")
 
 TEST_CASE ("mkdir/rmdir")
 
 TEST_CASE ("chdir")
 
 TEST_CASE ("remove")
 
 TEST_CASE ("move")
 
 TEST_CASE ("copy")
 
TEST_MEMORY_LEAK_ALL TEST_CASE ("url_temp")
 
TEST_MEMORY_LEAK_RESET TEST_CASE ("read_directory")
 
 TEST_CASE ("subdirectories")
 
 TEST_CASE ("load_string from empty file")
 
 TEST_CASE ("load_string from newly created file")
 
 TEST_CASE ("load_string from 3 local files and check exception")
 
 TEST_CASE ("load_string from url with :")
 
 TEST_CASE ("save to empty file")
 
 TEST_CASE ("save to empty file with unicode filename")
 
 TEST_CASE ("create and save to file")
 
 TEST_CASE ("save to exist file")
 
 TEST_CASE ("append to empty file")
 
 TEST_CASE ("append file not empty")
 

Variables

url unix_root = url_system ("/")
 
url xmake_lua = url_pwd () * "xmake.lua"
 

Function Documentation

◆ get_lolly_tmp()

url get_lolly_tmp ( )

Definition at line 17 of file file_test.cpp.

17 {
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}
Definition url.hpp:37
url url_system(string name)
Definition url.cpp:306

◆ remove_if_exist()

void remove_if_exist ( const url & u1)

Definition at line 28 of file file_test.cpp.

28 {
31 if (tb_file_info (path1, &info)) {
33 };
34}
The list class represents a linked list.
Definition list.hpp:48
string as_string(int16_t i)
Definition string.cpp:272

◆ TEST_CASE() [1/23]

TEST_MEMORY_LEAK_INIT TEST_CASE ( "is_directory/is_regular" )

Definition at line 45 of file file_test.cpp.

45 {
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}
bool is_regular(url u)
Definition file.cpp:57
bool is_directory(url u)
Definition file.cpp:38
bool is_symbolic_link(url u)
Definition file.cpp:76
url xmake_lua
Definition file_test.cpp:14
url unix_root
Definition file_test.cpp:13
url reroot(url u, string protocol)
Definition url.cpp:490
url url_pwd()
Definition url.cpp:326

◆ TEST_CASE() [2/23]

TEST_CASE ( "is_newer" )

Definition at line 63 of file file_test.cpp.

63 {
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}
bool is_newer(url which, url than)
Definition file.cpp:90
void rmdir(url u)
Definition file.cpp:249
void mkdir(url u)
Definition file.cpp:226
url get_lolly_tmp()
Definition file_test.cpp:17

◆ TEST_CASE() [3/23]

TEST_CASE ( "is_of_type" )

Definition at line 75 of file file_test.cpp.

75 {
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}
bool is_of_type(url name, string filter)
Definition file.cpp:105

◆ TEST_CASE() [4/23]

TEST_CASE ( "file_size" )

Definition at line 85 of file file_test.cpp.

85 {
87 CHECK (file_size (reroot (xmake_lua, "file")) > 0);
88}
int file_size(url u)
Definition file.cpp:155

◆ TEST_CASE() [5/23]

TEST_CASE ( "last_modified" )

Definition at line 90 of file file_test.cpp.

90 {
92 CHECK (last_modified (reroot (xmake_lua, "file")) > 0);
93}
int last_modified(url u)
Definition file.cpp:169

◆ TEST_CASE() [6/23]

TEST_CASE ( "mkdir/rmdir" )

Definition at line 95 of file file_test.cpp.

◆ TEST_CASE() [7/23]

TEST_CASE ( "chdir" )

Definition at line 104 of file file_test.cpp.

104 {
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}
void chdir(url u)
Definition file.cpp:263
void url_eq(url left, url right)

◆ TEST_CASE() [8/23]

TEST_CASE ( "remove" )

Definition at line 166 of file file_test.cpp.

166 {
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}
list< T > remove(list< T > l, T what)
Create a new list with a specific item removed.
Definition list.ipp:214

◆ TEST_CASE() [9/23]

TEST_CASE ( "move" )

Definition at line 195 of file file_test.cpp.

195 {
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}
void move(url u1, url u2)
Definition file.cpp:310

◆ TEST_CASE() [10/23]

TEST_CASE ( "copy" )

Definition at line 207 of file file_test.cpp.

207 {
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}
int copy(int x)
Returns a copy of an integer.
Definition basic.hpp:249

◆ TEST_CASE() [11/23]

TEST_MEMORY_LEAK_ALL TEST_CASE ( "url_temp" )

Definition at line 223 of file file_test.cpp.

223 {
224 url tmp1= url_temp ("png");
227 url tmp2= url_temp ("png");
228 CHECK_EQ (tmp1 == tmp2, false);
230}
url url_temp(string suffix)
Definition file.cpp:274
url url_temp_dir()
Definition file.cpp:300

◆ TEST_CASE() [12/23]

TEST_MEMORY_LEAK_RESET TEST_CASE ( "read_directory" )

Definition at line 235 of file file_test.cpp.

235 {
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}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
array< string > read_directory(url u, bool &error_flag)
Definition file.cpp:194

◆ TEST_CASE() [13/23]

TEST_CASE ( "subdirectories" )

Definition at line 244 of file file_test.cpp.

244 {
246 if (!os_wasm ()) {
247 url_eq (subdirectories (url_pwd () * "bin"), url_pwd () * "bin");
248 }
249}
url subdirectories(url u)
Definition file.cpp:211
bool os_wasm()
url url_none()
Definition url.cpp:91

◆ TEST_CASE() [14/23]

TEST_CASE ( "load_string from empty file" )

Definition at line 251 of file file_test.cpp.

251 {
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}
bool load_string(url u, string &s, bool fatal)
Definition file.cpp:434

◆ TEST_CASE() [15/23]

TEST_CASE ( "load_string from newly created file" )

Definition at line 260 of file file_test.cpp.

260 {
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}
void string_eq(string left, string right)

◆ TEST_CASE() [16/23]

TEST_CASE ( "load_string from 3 local files and check exception" )

Definition at line 290 of file file_test.cpp.

290 {
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}
#define TM_CHECK_THROWS(statements)

◆ TEST_CASE() [17/23]

TEST_CASE ( "load_string from url with :" )

Definition at line 307 of file file_test.cpp.

307 :") {
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}

◆ TEST_CASE() [18/23]

TEST_CASE ( "save to empty file" )

Definition at line 333 of file file_test.cpp.

333 {
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}
bool save_string(url u, const string &s, bool fatal)
Definition file.cpp:490

◆ TEST_CASE() [19/23]

TEST_CASE ( "save to empty file with unicode filename" )

Definition at line 344 of file file_test.cpp.

344 {
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}

◆ TEST_CASE() [20/23]

TEST_CASE ( "create and save to file" )

Definition at line 355 of file file_test.cpp.

355 {
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}
void remove_if_exist(const url &u1)
Definition file_test.cpp:28

◆ TEST_CASE() [21/23]

TEST_CASE ( "save to exist file" )

Definition at line 366 of file file_test.cpp.

366 {
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}

◆ TEST_CASE() [22/23]

TEST_CASE ( "append to empty file" )

Definition at line 383 of file file_test.cpp.

383 {
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}
bool append_string(url u, const string &s, bool fatal)
Definition file.cpp:543

◆ TEST_CASE() [23/23]

TEST_CASE ( "append file not empty" )

Definition at line 410 of file file_test.cpp.

410 {
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}

Variable Documentation

◆ unix_root

url unix_root = url_system ("/")

Definition at line 13 of file file_test.cpp.

◆ xmake_lua

url xmake_lua = url_pwd () * "xmake.lua"

Definition at line 14 of file file_test.cpp.