12
12
#pragma warning(pop)
13
13
#endif
14
14
15
+ #include < dirent.h>
16
+ #include < experimental/filesystem>
17
+ #include < iterator>
18
+ #include < random>
15
19
#include < stdio.h>
16
20
#include < stdlib.h>
17
21
#include < string.h>
22
+ #include < sys/stat.h>
18
23
19
24
#include " pkg_editor/pkg_editor.h"
20
25
#include < assert.h>
40
45
#define PACK_UNPACK_FILE " .pack_unpack"
41
46
#define PACK_UNPACK_DIR " .pack_unpack_dir"
42
47
48
+ namespace fs = std::experimental::filesystem::v1;
49
+
43
50
static void l_remove_file (const char *filename) {
44
51
#ifdef _WIN32
45
52
_unlink (filename);
46
53
#else
47
54
unlink (filename);
48
55
#endif
49
- }
56
+ } // namespace
57
+ // fs=std::experimental::filesystem::v1staticvoidl_remove_file(constchar*filename)
50
58
51
59
static void l_remove_sample_file (void ) { l_remove_file (SAMPLE_FILE); }
52
60
@@ -382,6 +390,8 @@ TEST(package, pack) {
382
390
static bool files_same (const char *f1, const char *f2) {
383
391
std::ifstream file1 (f1, std::ifstream::ate | std::ifstream::binary);
384
392
std::ifstream file2 (f2, std::ifstream::ate | std::ifstream::binary);
393
+ file1.exceptions (std::ifstream::failbit | std::ifstream::badbit);
394
+ file2.exceptions (std::ifstream::failbit | std::ifstream::badbit);
385
395
const std::ifstream::pos_type fileSize = file1.tellg ();
386
396
387
397
if (fileSize != file2.tellg ()) {
@@ -396,11 +406,54 @@ static bool files_same(const char *f1, const char *f2) {
396
406
return std::equal (begin1, std::istreambuf_iterator<char >(), begin2);
397
407
}
398
408
409
+ static void generate_random_file (std::string name, size_t size) {
410
+ std::mt19937 gen{std::random_device ()()};
411
+ std::uniform_int_distribution<> dis (0 , 255 );
412
+ std::ofstream file (name);
413
+ std::generate_n (std::ostream_iterator<char >(file, " " ), size,
414
+ [&] { return dis (gen); });
415
+ }
416
+
417
+ /* *
418
+ * Generates a "tmp" folder in the cwd with num_files files.
419
+ * Puts their file paths relative to the cwd in generated_files.
420
+ */
421
+ static void generate_tmp_folder (int num_files, std::string generated_files[]) {
422
+ fs::remove_all (" tmp" );
423
+ fs::create_directory (" tmp" );
424
+
425
+ generated_files[num_files - 1 ] = " ./tmp/empty_file" ;
426
+ generate_random_file (generated_files[num_files - 1 ], 0 );
427
+ if (num_files == 1 ) {
428
+ return ;
429
+ }
430
+
431
+ generated_files[num_files - 2 ] = " ./tmp/large_file" ;
432
+ generate_random_file (generated_files[num_files - 2 ], 10000000 ); // ~10MB
433
+
434
+ num_files -= 2 ;
435
+
436
+ for (int i = 0 ; i < num_files; i++) {
437
+ generated_files[i] = " ." + std::string (tmpnam (NULL ));
438
+ generate_random_file (generated_files[i], rand () % 100000 );
439
+ }
440
+ }
441
+
442
+ static void check_tmp_folder (int num_files, std::string generated_files[],
443
+ std::string unpack_dir) {
444
+ for (int i = 0 ; i < num_files; i++) { //
445
+ std::string unpacked_file_path = unpack_dir + " /" + generated_files[i];
446
+ files_same (generated_files[i].c_str (), unpacked_file_path.c_str ());
447
+ }
448
+ }
449
+
399
450
TEST (package, unpack) {
400
451
int result;
401
-
452
+ int num_rand_files = 10 ;
453
+ std::string files[num_rand_files];
454
+ generate_tmp_folder (num_rand_files, files);
402
455
// Create a known good input.
403
- const char *test_input[] = {" include " , " src " , " test " , NULL };
456
+ const char *test_input[] = {" tmp " , NULL };
404
457
result = acl_pkg_pack (PACK_UNPACK_FILE, test_input);
405
458
CHECK_EQUAL (1 , result);
406
459
@@ -409,17 +462,17 @@ TEST(package, unpack) {
409
462
CHECK_EQUAL (1 , result);
410
463
411
464
// Compare some files to be sure that they are the same.
412
- CHECK_EQUAL (true ,
413
- files_same (" CMakeLists.txt" , PACK_UNPACK_DIR " /CMakeLists.txt" ));
414
- CHECK_EQUAL (true , files_same (" test/pkg_editor_test.cpp" ,
415
- PACK_UNPACK_DIR " /test/pkg_editor_test.cpp" ));
465
+ check_tmp_folder (num_rand_files, files, PACK_UNPACK_DIR);
416
466
}
417
467
418
468
TEST (package, unpack_buffer) {
419
469
int result;
470
+ int num_rand_files = 10 ;
471
+ std::string files[num_rand_files];
472
+ generate_tmp_folder (num_rand_files, files);
420
473
421
474
// Create a known good input.
422
- const char *test_input[] = {" include" , " src" , " test" , NULL };
475
+ const char *test_input[] = {" include" , " src" , " test" , " tmp " , NULL };
423
476
result = acl_pkg_pack (PACK_UNPACK_FILE, test_input);
424
477
CHECK_EQUAL (1 , result);
425
478
@@ -445,13 +498,16 @@ TEST(package, unpack_buffer) {
445
498
PACK_UNPACK_DIR " /src/pkg_editor.c" ));
446
499
CHECK_EQUAL (true , files_same (" test/pkg_editor_test.cpp" ,
447
500
PACK_UNPACK_DIR " /test/pkg_editor_test.cpp" ));
501
+ check_tmp_folder (num_rand_files, files, PACK_UNPACK_DIR);
448
502
}
449
503
450
504
TEST (package, unpack_buffer_stdin) {
451
505
int result;
452
-
506
+ int num_rand_files = 10 ;
507
+ std::string files[num_rand_files];
508
+ generate_tmp_folder (num_rand_files, files);
453
509
// Create a known good input.
454
- const char *test_input[] = {" include" , " src" , " test" , NULL };
510
+ const char *test_input[] = {" include" , " src" , " test" , " tmp " , NULL };
455
511
result = acl_pkg_pack (PACK_UNPACK_FILE, test_input);
456
512
CHECK_EQUAL (1 , result);
457
513
@@ -494,23 +550,6 @@ TEST(package, unpack_buffer_stdin) {
494
550
PACK_UNPACK_DIR " /src/pkg_editor.c" ));
495
551
CHECK_EQUAL (true , files_same (" test/pkg_editor_test.cpp" ,
496
552
PACK_UNPACK_DIR " /test/pkg_editor_test.cpp" ));
553
+ check_tmp_folder (num_rand_files, files, PACK_UNPACK_DIR);
497
554
}
498
-
499
- TEST (package, unpack_empty) {
500
- int result;
501
-
502
- // Create a known good input.
503
- const char *test_input[] = {" test" , NULL };
504
- FILE *f = fopen (" test/empty" , " wb" );
505
- fclose (f);
506
- result = acl_pkg_pack (PACK_UNPACK_FILE, test_input);
507
- CHECK_EQUAL (1 , result);
508
-
509
- // Now read it back.
510
- result = acl_pkg_unpack (PACK_UNPACK_FILE, PACK_UNPACK_DIR);
511
- CHECK_EQUAL (1 , result);
512
-
513
- CHECK_EQUAL (true , files_same (" test/empty" , PACK_UNPACK_DIR " /test/empty" ));
514
- }
515
-
516
555
#endif
0 commit comments