comparison libinterp/dldfcn/xzip.cc @ 22194:b1ebad209360

xzip.cc: modify tests for readability.
author Carnë Draug <carandraug@octave.org>
date Sun, 31 Jul 2016 02:16:29 +0100
parents 1344509a480c
children 15b9d7cb3098
comparison
equal deleted inserted replaced
22193:e35866e6a2e0 22194:b1ebad209360
602 ## Takes a single argument, a function handle for the test. This other 602 ## Takes a single argument, a function handle for the test. This other
603 ## function must accept two arguments, a directory for the tests, and 603 ## function must accept two arguments, a directory for the tests, and
604 ## a cell array with zip function, unzip function, and file extension. 604 ## a cell array with zip function, unzip function, and file extension.
605 605
606 %!function run_test_function (test_function) 606 %!function run_test_function (test_function)
607 %! enabled_zippers = cell (0, 0); 607 %! enabled_zippers = struct ("zip", {}, "unzip", {}, "ext", {});
608 %! if (__octave_config_info__ ().build_features.BZ2) 608 %! if (__octave_config_info__ ().build_features.BZ2)
609 %! enabled_zippers(1, end+1) = @bzip2; 609 %! enabled_zippers(end+1).zip = @bzip2;
610 %! enabled_zippers(2, end) = @bunzip2; 610 %! enabled_zippers(end).unzip = @bunzip2;
611 %! enabled_zippers(3, end) = ".bz2"; 611 %! enabled_zippers(end).ext = ".bz2";
612 %! endif 612 %! endif
613 %! if (__octave_config_info__ ().build_features.Z) 613 %! if (__octave_config_info__ ().build_features.Z)
614 %! enabled_zippers(1, end+1) = @gzip; 614 %! enabled_zippers(end+1).zip = @gzip;
615 %! enabled_zippers(2, end) = @gunzip; 615 %! enabled_zippers(end).unzip = @gunzip;
616 %! enabled_zippers(3, end) = ".gz"; 616 %! enabled_zippers(end).ext = ".gz";
617 %! endif 617 %! endif
618 %! 618 %!
619 %! for z = enabled_zippers 619 %! for z = enabled_zippers
620 %! test_dir = tempname (); 620 %! test_dir = tempname ();
621 %! if (! mkdir (test_dir)) 621 %! if (! mkdir (test_dir))
641 %! if (fflush (fid) || fclose (fid)) 641 %! if (fflush (fid) || fclose (fid))
642 %! error ("unable to flush or close file"); 642 %! error ("unable to flush or close file");
643 %! endif 643 %! endif
644 %!endfunction 644 %!endfunction
645 645
646 %!function unlink_or_error (filepath)
647 %! [err, msg] = unlink (filepath);
648 %! if (err)
649 %! error ("unable to remove file required for the test");
650 %! endif
651 %!endfunction
652
646 ## Test with large files because of varied buffer size 653 ## Test with large files because of varied buffer size
647 %!function test_large_file (test_dir, z) 654 %!function test_large_file (test_dir, z)
648 %! test_file = tempname (test_dir); 655 %! test_file = tempname (test_dir);
649 %! create_file (test_file, rand (500000, 1)); 656 %! create_file (test_file, rand (500000, 1));
650 %! md5 = hash ("md5", fileread (test_file)); 657 %! md5 = hash ("md5", fileread (test_file));
651 %! 658 %!
652 %! expected_z_file = [test_file z{3}]; 659 %! z_file = [test_file z.ext];
653 %! z_files = z{1} (test_file); 660 %! z_filelist = z.zip (test_file);
654 %! assert (z_files, {expected_z_file}) 661 %! assert (z_filelist, {z_file})
655 %! 662 %!
656 %! unlink (test_file); 663 %! unlink_or_error (test_file);
657 %! assert (z{2} (z_files{1}), {test_file}) 664 %! uz_filelist = z.unzip (z_file);
665 %! assert (uz_filelist, {test_file})
666 %!
658 %! assert (hash ("md5", fileread (test_file)), md5) 667 %! assert (hash ("md5", fileread (test_file)), md5)
659 %!endfunction 668 %!endfunction
660 %!test run_test_function (@test_large_file) 669 %!test run_test_function (@test_large_file)
661 670
662 ## Test that xzipped files are rexzipped 671 ## Test that xzipped files are rexzipped
663 %!function test_z_z (test_dir, z) 672 %!function test_z_z (test_dir, z)
664 %! ori_file = tempname (test_dir); 673 %! ori_file = tempname (test_dir);
665 %! create_file (ori_file, rand (100, 1)); 674 %! create_file (ori_file, rand (100, 1));
666 %! md5_ori = hash ("md5", fileread (ori_file)); 675 %! md5_ori = hash ("md5", fileread (ori_file));
667 %! 676 %!
668 %! z_file = [ori_file z{3}]; 677 %! z_file = [ori_file z.ext];
669 %! filelist = z{1} (ori_file); 678 %! z_filelist = z.zip (ori_file);
670 %! assert (filelist, {z_file}) # check output 679 %! assert (z_filelist, {z_file}) # check output
671 %! assert (exist (z_file), 2) # confirm file exists 680 %! assert (exist (z_file), 2) # confirm file exists
672 %! assert (exist (z_file), 2) # and did not remove original file 681 %! assert (exist (ori_file), 2) # and did not remove original file
682 %!
683 %! unlink_or_error (ori_file);
684 %! uz_filelist = z.unzip (z_file);
685 %! assert (uz_filelist, {ori_file}) # bug #48598
686 %! assert (hash ("md5", fileread (ori_file)), md5_ori)
687 %! assert (exist (z_file), 2) # bug #48597
688 %!
689 %! ## xzip should dutifully re-xzip files even if they already are zipped
690 %! z_z_file = [z_file z.ext];
691 %! z_z_filelist = z.zip (z_file);
692 %! assert (z_z_filelist, {z_z_file}) # check output
693 %! assert (exist (z_z_file), 2) # confirm file exists
694 %! assert (exist (z_file), 2)
695 %!
673 %! md5_z = hash ("md5", fileread (z_file)); 696 %! md5_z = hash ("md5", fileread (z_file));
674 %! 697 %! unlink_or_error (z_file);
675 %! unlink (ori_file); 698 %! uz_z_filelist = z.unzip (z_z_file);
676 %! assert (z{2} (z_file), {ori_file}) 699 %! assert (uz_z_filelist, {z_file}) # bug #48598
677 %! ## bug #48597 700 %! assert (exist (z_z_file), 2) # bug #48597
678 %! assert (exist (ori_file), 2) # bug #48597 (Xunzip should not remove file)
679 %! assert (hash ("md5", fileread (ori_file)), md5_ori)
680 %!
681 %! ## xzip should dutifully re-xzip files even if they already are zipped
682 %! z_z_file = [z_file z{3}];
683 %!
684 %! filelist = z{1} (z_file);
685 %! assert (filelist, {z_z_file}) # check output
686 %! assert (exist (z_z_file), 2) # confirm file exists
687 %! assert (exist (z_z_file), 2) # and did not remove original file
688 %!
689 %! unlink (z_file);
690 %! assert (z{2} (z_z_file), {z_file})
691 %! assert (hash ("md5", fileread (z_file)), md5_z) 701 %! assert (hash ("md5", fileread (z_file)), md5_z)
692 %!endfunction 702 %!endfunction
693 %!test run_test_function (@test_z_z) 703 %!test run_test_function (@test_z_z)
694 704
695 %!function test_xzip_dir (test_dir, z) 705 %!function test_xzip_dir (test_dir, z) # bug #43431
696 %! fpaths = fullfile (test_dir, {"test1", "test2", "test3"}); 706 %! fpaths = fullfile (test_dir, {"test1", "test2", "test3"});
697 %! z_files = strcat (fpaths, z{3});
698 %! md5s = cell (1, 3); 707 %! md5s = cell (1, 3);
699 %! for idx = 1:numel(fpaths) 708 %! for idx = 1:numel(fpaths)
700 %! create_file (fpaths{idx}, rand (100, 1)); 709 %! create_file (fpaths{idx}, rand (100, 1));
701 %! md5s(idx) = hash ("md5", fileread (fpaths{idx})); 710 %! md5s(idx) = hash ("md5", fileread (fpaths{idx}));
702 %! endfor 711 %! endfor
703 %! 712 %!
704 %! assert (sort (z{1} ([test_dir filesep()])), z_files(:)) 713 %! test_dir = [test_dir filesep()];
714 %!
715 %! z_files = strcat (fpaths, z.ext);
716 %! z_filelist = z.zip (test_dir);
717 %! assert (sort (z_filelist), z_files(:))
705 %! for idx = 1:numel(fpaths) 718 %! for idx = 1:numel(fpaths)
706 %! assert (exist (z_files{idx}), 2) 719 %! assert (exist (z_files{idx}), 2)
707 %! unlink (fpaths{idx}); 720 %! unlink_or_error (fpaths{idx});
708 %! endfor 721 %! endfor
722 %!
723 %! ## only gunzip handles directory (bunzip2 should too though)
724 %! if (z.unzip == @gunzip)
725 %! uz_filelist = z.unzip (test_dir);
726 %! else
727 %! uz_filelist = cell (1, numel (z_filelist));
728 %! for idx = 1:numel(z_filelist)
729 %! uz_filelist(idx) = z.unzip (z_filelist{idx});
730 %! endfor
731 %! endif
732 %! assert (sort (uz_filelist), fpaths(:)) # bug #48598
709 %! for idx = 1:numel(fpaths) 733 %! for idx = 1:numel(fpaths)
710 %! assert (z{2} (z_files{idx}), fpaths{idx}); # bug #48598
711 %! assert (hash ("md5", fileread (fpaths{idx})), md5s{idx}) 734 %! assert (hash ("md5", fileread (fpaths{idx})), md5s{idx})
712 %! endfor 735 %! endfor
713 %!endfunction 736 %!endfunction
714 %!test run_test_function (@test_xzip_dir) 737 %!test run_test_function (@test_xzip_dir)
715 738
724 %! if (! mkdir (out_dirs{1})) 747 %! if (! mkdir (out_dirs{1}))
725 %! error ("unable to create directory for test"); 748 %! error ("unable to create directory for test");
726 %! endif 749 %! endif
727 %! for idx = 1:numel(out_dirs) 750 %! for idx = 1:numel(out_dirs)
728 %! out_dir = out_dirs{idx}; 751 %! out_dir = out_dirs{idx};
729 %! z_file = fullfile (out_dir, [filename z{3}]); 752 %! uz_file = fullfile (out_dir, filename);
730 %! assert (z{1} (filepath, out_dir), {z_file}) 753 %! z_file = [uz_file z.ext];
754 %!
755 %! z_filelist = z.zip (filepath, out_dir);
756 %! assert (z_filelist, {z_file})
731 %! assert (exist (z_file, "file"), 2) 757 %! assert (exist (z_file, "file"), 2)
732 %! uz_file = z_file(1:(end-numel(z{3}))); 758 %!
733 %! assert (z{2} (z_file), uz_file); # bug #48598 759 %! uz_filelist = z.unzip (z_file);
760 %! assert (uz_filelist, {uz_file}) # bug #48598
761 %!
734 %! assert (hash ("md5", fileread (uz_file)), md5) 762 %! assert (hash ("md5", fileread (uz_file)), md5)
735 %! endfor 763 %! endfor
736 %!endfunction 764 %!endfunction
737 %!test run_test_function (@test_save_to_dir) 765 %!test run_test_function (@test_save_to_dir)
738 */ 766 */