850_Dpkg_Compression.t 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # -*- mode: cperl -*-
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use Test::More tests => 9;
  16. use strict;
  17. use warnings;
  18. use_ok('Dpkg::Compression::FileHandle');
  19. my $tmpdir = "t.tmp/850_Dpkg_Compression";
  20. mkdir $tmpdir;
  21. my @lines = ("One\n", "Two\n", "Three\n");
  22. my $fh;
  23. sub test_write {
  24. my ($filename, $check_result) = @_;
  25. $fh = Dpkg::Compression::FileHandle->new();
  26. open $fh, ">", $filename or die "open failed";
  27. print $fh $lines[0];
  28. syswrite($fh, $lines[1]);
  29. printf $fh "%s", $lines[2];
  30. close $fh or die "close failed";
  31. &$check_result($filename, "std functions");
  32. unlink $filename or die "cannot unlink $filename";
  33. $fh = Dpkg::Compression::FileHandle->new();
  34. $fh->open($filename, "w");
  35. $fh->print($lines[0]);
  36. $fh->write($lines[1], length($lines[1]));
  37. $fh->printf("%s", $lines[2]);
  38. $fh->close() or die "close failed";
  39. &$check_result($filename, "IO::Handle methods");
  40. }
  41. sub check_uncompressed {
  42. my ($filename, $method) = @_;
  43. open(READ, "<", $filename) or die "cannot read $filename";
  44. my @read = <READ>;
  45. close READ or die "cannot close";
  46. is_deeply(\@lines, \@read, "$filename correctly written ($method)");
  47. }
  48. sub check_compressed {
  49. my ($filename, $method) = @_;
  50. open(READ, "-|", "zcat $tmpdir/myfile.gz") or die "cannot fork zcat";
  51. my @read = <READ>;
  52. close READ or die "cannot close";
  53. is_deeply(\@lines, \@read, "$filename correctly written ($method)");
  54. }
  55. sub test_read {
  56. my ($filename) = @_;
  57. $fh = Dpkg::Compression::FileHandle->new();
  58. open($fh, "<", $filename) or die "open failed";
  59. my @read = <$fh>;
  60. close $fh or die "close failed";
  61. is_deeply(\@lines, \@read, "$filename correctly read (std functions)");
  62. @read = ();
  63. $fh = Dpkg::Compression::FileHandle->new();
  64. $fh->open($filename, "r") or die "open failed";
  65. @read = $fh->getlines();
  66. $fh->close() or die "close failed";
  67. is_deeply(\@lines, \@read, "$filename correctly read (IO::Handle methods)");
  68. }
  69. # Test write on uncompressed file
  70. test_write("$tmpdir/myfile", \&check_uncompressed);
  71. # Test write on compressed file
  72. test_write("$tmpdir/myfile.gz", \&check_compressed);
  73. # Test read on uncompressed file
  74. test_read("$tmpdir/myfile");
  75. # Test read on compressed file
  76. test_read("$tmpdir/myfile.gz");