diff options
Diffstat (limited to 'spec/ruby/library/zlib')
42 files changed, 1737 insertions, 0 deletions
diff --git a/spec/ruby/library/zlib/adler32_spec.rb b/spec/ruby/library/zlib/adler32_spec.rb new file mode 100644 index 0000000000..887c22d059 --- /dev/null +++ b/spec/ruby/library/zlib/adler32_spec.rb @@ -0,0 +1,46 @@ +require_relative '../../spec_helper' +require 'zlib' + +describe "Zlib.adler32" do + it "calculates Adler checksum for string" do + Zlib.adler32("").should == 1 + Zlib.adler32(" ").should == 2162721 + Zlib.adler32("123456789").should == 152961502 + Zlib.adler32("!@#\{$\}%^&**()").should == 365495023 + Zlib.adler32("to be or not to be" * 22).should == 3979904837 + Zlib.adler32("0").should == 3211313 + Zlib.adler32((2**32).to_s).should == 193331739 + Zlib.adler32((2**64).to_s).should == 723452953 + end + + it "calculates Adler checksum for string and initial Adler value" do + test_string = "This is a test string! How exciting!%?" + Zlib.adler32(test_string, 0).should == 63900955 + Zlib.adler32(test_string, 1).should == 66391324 + Zlib.adler32(test_string, 2**8).should == 701435419 + Zlib.adler32(test_string, 2**16).should == 63966491 + -> { Zlib.adler32(test_string, 2**128) }.should.raise(RangeError) + end + + it "calculates the Adler checksum for string and initial Adler value for Integers" do + test_string = "This is a test string! How exciting!%?" + Zlib.adler32(test_string, 2**30).should == 1137642779 + end + + it "assumes that the initial value is given to adler, if adler is omitted" do + orig_crc = Zlib.adler32 + Zlib.adler32("").should == Zlib.adler32("", orig_crc) + Zlib.adler32(" ").should == Zlib.adler32(" ", orig_crc) + Zlib.adler32("123456789").should == Zlib.adler32("123456789", orig_crc) + Zlib.adler32("!@#\{$\}%^&**()").should == Zlib.adler32("!@#\{$\}%^&**()", orig_crc) + Zlib.adler32("to be or not to be" * 22).should == Zlib.adler32("to be or not to be" * 22, orig_crc) + Zlib.adler32("0").should == Zlib.adler32("0", orig_crc) + Zlib.adler32((2**32).to_s).should == Zlib.adler32((2**32).to_s, orig_crc) + Zlib.adler32((2**64).to_s).should == Zlib.adler32((2**64).to_s, orig_crc) + end + + it "it returns the CRC initial value, if string is omitted" do + Zlib.adler32.should == 1 + end + +end diff --git a/spec/ruby/library/zlib/crc32_spec.rb b/spec/ruby/library/zlib/crc32_spec.rb new file mode 100644 index 0000000000..b94b5c627c --- /dev/null +++ b/spec/ruby/library/zlib/crc32_spec.rb @@ -0,0 +1,54 @@ +require_relative '../../spec_helper' +require 'zlib' + +describe "Zlib.crc32" do + it "calculates CRC checksum for string" do + Zlib.crc32("").should == 0 + Zlib.crc32(" ").should == 3916222277 + Zlib.crc32("123456789").should == 3421780262 + Zlib.crc32("!@#\{$\}%^&**()").should == 2824518887 + Zlib.crc32("to be or not to be" * 22).should == 1832379978 + Zlib.crc32("0").should == 4108050209 + Zlib.crc32((2**32).to_s).should == 3267533297 + Zlib.crc32((2**64).to_s).should == 653721760 + end + + it "calculates CRC checksum for string and initial CRC value" do + test_string = "This is a test string! How exciting!%?" + # Zlib.crc32(test_string, -2**28).should == 3230195786 + # Zlib.crc32(test_string, -2**20).should == 2770207303 + # Zlib.crc32(test_string, -2**16).should == 2299432960 + # Zlib.crc32(test_string, -2**8).should == 861809849 + # Zlib.crc32(test_string, -1).should == 2170124077 + Zlib.crc32(test_string, 0).should == 3864990561 + Zlib.crc32(test_string, 1).should == 1809313411 + Zlib.crc32(test_string, 2**8).should == 1722745982 + Zlib.crc32(test_string, 2**16).should == 1932511220 + Zlib.crc32("p", ~305419896).should == 4046865307 + Zlib.crc32("p", -305419897).should == 4046865307 + -> { Zlib.crc32(test_string, 2**128) }.should.raise(RangeError) + end + + it "calculates the CRC checksum for string and initial CRC value for Integers" do + test_string = "This is a test string! How exciting!%?" + # Zlib.crc32(test_string, -2**30).should == 277228695 + Zlib.crc32(test_string, 2**30).should == 46597132 + end + + it "assumes that the initial value is given to crc, if crc is omitted" do + orig_crc = Zlib.crc32 + Zlib.crc32("").should == Zlib.crc32("", orig_crc) + Zlib.crc32(" ").should == Zlib.crc32(" ", orig_crc) + Zlib.crc32("123456789").should == Zlib.crc32("123456789", orig_crc) + Zlib.crc32("!@#\{$\}%^&**()").should == Zlib.crc32("!@#\{$\}%^&**()", orig_crc) + Zlib.crc32("to be or not to be" * 22).should == Zlib.crc32("to be or not to be" * 22, orig_crc) + Zlib.crc32("0").should == Zlib.crc32("0", orig_crc) + Zlib.crc32((2**32).to_s).should == Zlib.crc32((2**32).to_s, orig_crc) + Zlib.crc32((2**64).to_s).should == Zlib.crc32((2**64).to_s, orig_crc) + end + + it "it returns the CRC initial value, if string is omitted" do + Zlib.crc32.should == 0 + end + +end diff --git a/spec/ruby/library/zlib/crc_table_spec.rb b/spec/ruby/library/zlib/crc_table_spec.rb new file mode 100644 index 0000000000..de8876086b --- /dev/null +++ b/spec/ruby/library/zlib/crc_table_spec.rb @@ -0,0 +1,80 @@ +require_relative '../../spec_helper' +require "zlib" + +describe "Zlib.crc_table" do + # This spec fails when zlib.h and libz.so are not from the same version. + # In older zlib (< 1.2.7 it seems), get_crc_table() is stored as u64[], + # but in newer zlib, get_crc_table() is stored as u32[]. + # Technically, there is ABI breakage between those zlib versions, + # but get_crc_table() is an "undocumented function" according to zlib.h. + guard -> { ENV["RUBY_SPEC_TEST_ZLIB_CRC_TABLE"] != "false" } do + it "returns the same value as zlib's get_crc_table()" do + Zlib.crc_table.should == [ + 0, 1996959894, 3993919788, 2567524794, + 124634137, 1886057615, 3915621685, 2657392035, + 249268274, 2044508324, 3772115230, 2547177864, + 162941995, 2125561021, 3887607047, 2428444049, + 498536548, 1789927666, 4089016648, 2227061214, + 450548861, 1843258603, 4107580753, 2211677639, + 325883990, 1684777152, 4251122042, 2321926636, + 335633487, 1661365465, 4195302755, 2366115317, + 997073096, 1281953886, 3579855332, 2724688242, + 1006888145, 1258607687, 3524101629, 2768942443, + 901097722, 1119000684, 3686517206, 2898065728, + 853044451, 1172266101, 3705015759, 2882616665, + 651767980, 1373503546, 3369554304, 3218104598, + 565507253, 1454621731, 3485111705, 3099436303, + 671266974, 1594198024, 3322730930, 2970347812, + 795835527, 1483230225, 3244367275, 3060149565, + 1994146192, 31158534, 2563907772, 4023717930, + 1907459465, 112637215, 2680153253, 3904427059, + 2013776290, 251722036, 2517215374, 3775830040, + 2137656763, 141376813, 2439277719, 3865271297, + 1802195444, 476864866, 2238001368, 4066508878, + 1812370925, 453092731, 2181625025, 4111451223, + 1706088902, 314042704, 2344532202, 4240017532, + 1658658271, 366619977, 2362670323, 4224994405, + 1303535960, 984961486, 2747007092, 3569037538, + 1256170817, 1037604311, 2765210733, 3554079995, + 1131014506, 879679996, 2909243462, 3663771856, + 1141124467, 855842277, 2852801631, 3708648649, + 1342533948, 654459306, 3188396048, 3373015174, + 1466479909, 544179635, 3110523913, 3462522015, + 1591671054, 702138776, 2966460450, 3352799412, + 1504918807, 783551873, 3082640443, 3233442989, + 3988292384, 2596254646, 62317068, 1957810842, + 3939845945, 2647816111, 81470997, 1943803523, + 3814918930, 2489596804, 225274430, 2053790376, + 3826175755, 2466906013, 167816743, 2097651377, + 4027552580, 2265490386, 503444072, 1762050814, + 4150417245, 2154129355, 426522225, 1852507879, + 4275313526, 2312317920, 282753626, 1742555852, + 4189708143, 2394877945, 397917763, 1622183637, + 3604390888, 2714866558, 953729732, 1340076626, + 3518719985, 2797360999, 1068828381, 1219638859, + 3624741850, 2936675148, 906185462, 1090812512, + 3747672003, 2825379669, 829329135, 1181335161, + 3412177804, 3160834842, 628085408, 1382605366, + 3423369109, 3138078467, 570562233, 1426400815, + 3317316542, 2998733608, 733239954, 1555261956, + 3268935591, 3050360625, 752459403, 1541320221, + 2607071920, 3965973030, 1969922972, 40735498, + 2617837225, 3943577151, 1913087877, 83908371, + 2512341634, 3803740692, 2075208622, 213261112, + 2463272603, 3855990285, 2094854071, 198958881, + 2262029012, 4057260610, 1759359992, 534414190, + 2176718541, 4139329115, 1873836001, 414664567, + 2282248934, 4279200368, 1711684554, 285281116, + 2405801727, 4167216745, 1634467795, 376229701, + 2685067896, 3608007406, 1308918612, 956543938, + 2808555105, 3495958263, 1231636301, 1047427035, + 2932959818, 3654703836, 1088359270, 936918000, + 2847714899, 3736837829, 1202900863, 817233897, + 3183342108, 3401237130, 1404277552, 615818150, + 3134207493, 3453421203, 1423857449, 601450431, + 3009837614, 3294710456, 1567103746, 711928724, + 3020668471, 3272380065, 1510334235, 755167117, + ] + end + end +end diff --git a/spec/ruby/library/zlib/deflate/deflate_spec.rb b/spec/ruby/library/zlib/deflate/deflate_spec.rb new file mode 100644 index 0000000000..e16e6ad0ef --- /dev/null +++ b/spec/ruby/library/zlib/deflate/deflate_spec.rb @@ -0,0 +1,133 @@ +require 'zlib' +require_relative '../../../spec_helper' + +describe "Zlib::Deflate.deflate" do + it "deflates some data" do + data = Array.new(10,0).pack('C*') + + zipped = Zlib::Deflate.deflate data + + zipped.should == [120, 156, 99, 96, 128, 1, 0, 0, 10, 0, 1].pack('C*') + end + + it "deflates lots of data" do + data = "\000" * 32 * 1024 + + zipped = Zlib::Deflate.deflate data + + zipped.should == ([120, 156, 237, 193, 1, 1, 0, 0] + + [0, 128, 144, 254, 175, 238, 8, 10] + + Array.new(31, 0) + + [24, 128, 0, 0, 1]).pack('C*') + end + + it "deflates chunked data" do + random_generator = Random.new(0) + deflated = +'' + + Zlib::Deflate.deflate(random_generator.bytes(20000)) do |chunk| + deflated << chunk + end + + deflated.length.should == 20016 + end +end + +describe "Zlib::Deflate#deflate" do + before :each do + @deflator = Zlib::Deflate.new + end + + it "deflates some data" do + data = "\000" * 10 + + zipped = @deflator.deflate data, Zlib::FINISH + @deflator.finish + + zipped.should == [120, 156, 99, 96, 128, 1, 0, 0, 10, 0, 1].pack('C*') + end + + it "deflates lots of data" do + data = "\000" * 32 * 1024 + + zipped = @deflator.deflate data, Zlib::FINISH + @deflator.finish + + zipped.should == ([120, 156, 237, 193, 1, 1, 0, 0] + + [0, 128, 144, 254, 175, 238, 8, 10] + + Array.new(31, 0) + + [24, 128, 0, 0, 1]).pack('C*') + end + + it "has a binary encoding" do + @deflator.deflate("").encoding.should == Encoding::BINARY + @deflator.finish.encoding.should == Encoding::BINARY + end +end + +describe "Zlib::Deflate#deflate" do + + before :each do + @deflator = Zlib::Deflate.new + @random_generator = Random.new(0) + @original = +'' + @chunks = [] + end + + describe "without break" do + + before do + 2.times do + @input = @random_generator.bytes(20000) + @original << @input + @deflator.deflate(@input) do |chunk| + @chunks << chunk + end + end + end + + it "deflates chunked data" do + @deflator.finish + @chunks.map { |chunk| chunk.length }.should == [16384, 16384] + end + + it "deflates chunked data with final chunk" do + final = @deflator.finish + final.length.should == 7253 + end + + it "deflates chunked data without errors" do + final = @deflator.finish + @chunks << final + @original.should == Zlib.inflate(@chunks.join) + end + + end + + describe "with break" do + before :each do + @input = @random_generator.bytes(20000) + @deflator.deflate(@input) do |chunk| + @chunks << chunk + break + end + end + + it "deflates only first chunk" do + @deflator.finish + @chunks.map { |chunk| chunk.length }.should == [16384] + end + + it "deflates chunked data with final chunk" do + final = @deflator.finish + final.length.should == 3632 + end + + it "deflates chunked data without errors" do + final = @deflator.finish + @chunks << final + @input.should == Zlib.inflate(@chunks.join) + end + + end +end diff --git a/spec/ruby/library/zlib/deflate/params_spec.rb b/spec/ruby/library/zlib/deflate/params_spec.rb new file mode 100644 index 0000000000..0242653528 --- /dev/null +++ b/spec/ruby/library/zlib/deflate/params_spec.rb @@ -0,0 +1,17 @@ +require_relative '../../../spec_helper' +require 'zlib' + +describe "Zlib::Deflate#params" do + it "changes the deflate parameters" do + data = +'abcdefghijklm' + + d = Zlib::Deflate.new Zlib::NO_COMPRESSION, Zlib::MAX_WBITS, + Zlib::DEF_MEM_LEVEL, Zlib::DEFAULT_STRATEGY + + d << data.slice!(0..10) + d.params Zlib::BEST_COMPRESSION, Zlib::DEFAULT_STRATEGY + d << data + + Zlib::Inflate.inflate(d.finish).should == 'abcdefghijklm' + end +end diff --git a/spec/ruby/library/zlib/deflate/set_dictionary_spec.rb b/spec/ruby/library/zlib/deflate/set_dictionary_spec.rb new file mode 100644 index 0000000000..0e461229c7 --- /dev/null +++ b/spec/ruby/library/zlib/deflate/set_dictionary_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../../spec_helper' +require 'zlib' + +describe "Zlib::Deflate#set_dictionary" do + it "sets the dictionary" do + d = Zlib::Deflate.new + d.set_dictionary 'aaaaaaaaaa' + d << 'abcdefghij' + + d.finish.should == [120, 187, 20, 225, 3, 203, 75, 76, + 74, 78, 73, 77, 75, 207, 200, 204, + 2, 0, 21, 134, 3, 248].pack('C*') + end +end diff --git a/spec/ruby/library/zlib/deflate_spec.rb b/spec/ruby/library/zlib/deflate_spec.rb new file mode 100644 index 0000000000..6eeaa164c5 --- /dev/null +++ b/spec/ruby/library/zlib/deflate_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require "zlib" + +describe "Zlib.deflate" do + it "deflates some data" do + Zlib.deflate("1" * 10).should == [120, 156, 51, 52, 132, 1, 0, 10, 145, 1, 235].pack('C*') + end +end diff --git a/spec/ruby/library/zlib/gunzip_spec.rb b/spec/ruby/library/zlib/gunzip_spec.rb new file mode 100644 index 0000000000..2417fed57c --- /dev/null +++ b/spec/ruby/library/zlib/gunzip_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../spec_helper' +require 'zlib' + +describe "Zlib.gunzip" do + before :each do + @data = '12345abcde' + @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77, + 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*') + end + + it "decodes the given gzipped string" do + Zlib.gunzip(@zip).should == @data + end +end diff --git a/spec/ruby/library/zlib/gzip_spec.rb b/spec/ruby/library/zlib/gzip_spec.rb new file mode 100644 index 0000000000..35694264f0 --- /dev/null +++ b/spec/ruby/library/zlib/gzip_spec.rb @@ -0,0 +1,15 @@ +require_relative '../../spec_helper' +require 'zlib' + +describe "Zlib.gzip" do + before :each do + @data = '12345abcde' + @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77, + 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*') + end + + it "gzips the given string" do + # skip gzip header for now + Zlib.gzip(@data)[10..-1].should == @zip[10..-1] + end +end diff --git a/spec/ruby/library/zlib/gzipfile/close_spec.rb b/spec/ruby/library/zlib/gzipfile/close_spec.rb new file mode 100644 index 0000000000..07bafac961 --- /dev/null +++ b/spec/ruby/library/zlib/gzipfile/close_spec.rb @@ -0,0 +1,19 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipFile#close" do + it "finishes the stream and closes the io" do + io = StringIO.new "".b + Zlib::GzipWriter.wrap io do |gzio| + gzio.close + + gzio.should.closed? + + -> { gzio.orig_name }.should.raise(Zlib::GzipFile::Error, 'closed gzip stream') + -> { gzio.comment }.should.raise(Zlib::GzipFile::Error, 'closed gzip stream') + end + + io.string[10..-1].should == ([3] + Array.new(9,0)).pack('C*') + end +end diff --git a/spec/ruby/library/zlib/gzipfile/closed_spec.rb b/spec/ruby/library/zlib/gzipfile/closed_spec.rb new file mode 100644 index 0000000000..726f391b41 --- /dev/null +++ b/spec/ruby/library/zlib/gzipfile/closed_spec.rb @@ -0,0 +1,16 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipFile#closed?" do + it "returns the closed status" do + io = StringIO.new + Zlib::GzipWriter.wrap io do |gzio| + gzio.should_not.closed? + + gzio.close + + gzio.should.closed? + end + end +end diff --git a/spec/ruby/library/zlib/gzipfile/comment_spec.rb b/spec/ruby/library/zlib/gzipfile/comment_spec.rb new file mode 100644 index 0000000000..845224df98 --- /dev/null +++ b/spec/ruby/library/zlib/gzipfile/comment_spec.rb @@ -0,0 +1,25 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipFile#comment" do + before :each do + @io = StringIO.new + end + + it "returns the name" do + Zlib::GzipWriter.wrap @io do |gzio| + gzio.comment = 'name' + + gzio.comment.should == 'name' + end + end + + it "raises an error on a closed stream" do + Zlib::GzipWriter.wrap @io do |gzio| + gzio.close + + -> { gzio.comment }.should.raise(Zlib::GzipFile::Error, 'closed gzip stream') + end + end +end diff --git a/spec/ruby/library/zlib/gzipfile/orig_name_spec.rb b/spec/ruby/library/zlib/gzipfile/orig_name_spec.rb new file mode 100644 index 0000000000..1da375390b --- /dev/null +++ b/spec/ruby/library/zlib/gzipfile/orig_name_spec.rb @@ -0,0 +1,25 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipFile#orig_name" do + before :each do + @io = StringIO.new + end + + it "returns the name" do + Zlib::GzipWriter.wrap @io do |gzio| + gzio.orig_name = 'name' + + gzio.orig_name.should == 'name' + end + end + + it "raises an error on a closed stream" do + Zlib::GzipWriter.wrap @io do |gzio| + gzio.close + + -> { gzio.orig_name }.should.raise(Zlib::GzipFile::Error, 'closed gzip stream') + end + end +end diff --git a/spec/ruby/library/zlib/gzipreader/each_byte_spec.rb b/spec/ruby/library/zlib/gzipreader/each_byte_spec.rb new file mode 100644 index 0000000000..48821dc833 --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/each_byte_spec.rb @@ -0,0 +1,51 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipReader#each_byte" do + + before :each do + @data = '12345abcde' + @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77, + 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*') + + @io = StringIO.new @zip + ScratchPad.clear + end + + it "calls the given block for each byte in the stream, passing the byte as an argument" do + gz = Zlib::GzipReader.new @io + + ScratchPad.record [] + gz.each_byte { |b| ScratchPad << b } + + ScratchPad.recorded.should == [49, 50, 51, 52, 53, 97, 98, 99, 100, 101] + end + + it "returns an enumerator, which yields each byte in the stream, when no block is passed" do + gz = Zlib::GzipReader.new @io + enum = gz.each_byte + + ScratchPad.record [] + while true + begin + ScratchPad << enum.next + rescue StopIteration + break + end + end + + ScratchPad.recorded.should == [49, 50, 51, 52, 53, 97, 98, 99, 100, 101] + end + + it "increments position before calling the block" do + gz = Zlib::GzipReader.new @io + + i = 1 + gz.each_byte do |ignore| + gz.pos.should == i + i += 1 + end + end + +end diff --git a/spec/ruby/library/zlib/gzipreader/each_char_spec.rb b/spec/ruby/library/zlib/gzipreader/each_char_spec.rb new file mode 100644 index 0000000000..de6396da7e --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/each_char_spec.rb @@ -0,0 +1,51 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipReader#each_char" do + + before :each do + @data = '12345abcde' + @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77, + 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*') + + @io = StringIO.new @zip + ScratchPad.clear + end + + it "calls the given block for each char in the stream, passing the char as an argument" do + gz = Zlib::GzipReader.new @io + + ScratchPad.record [] + gz.each_char { |b| ScratchPad << b } + + ScratchPad.recorded.should == ["1", "2", "3", "4", "5", "a", "b", "c", "d", "e"] + end + + it "returns an enumerator, which yields each char in the stream, when no block is passed" do + gz = Zlib::GzipReader.new @io + enum = gz.each_char + + ScratchPad.record [] + while true + begin + ScratchPad << enum.next + rescue StopIteration + break + end + end + + ScratchPad.recorded.should == ["1", "2", "3", "4", "5", "a", "b", "c", "d", "e"] + end + + it "increments position before calling the block" do + gz = Zlib::GzipReader.new @io + + i = 1 + gz.each_char do |ignore| + gz.pos.should == i + i += 1 + end + end + +end diff --git a/spec/ruby/library/zlib/gzipreader/each_line_spec.rb b/spec/ruby/library/zlib/gzipreader/each_line_spec.rb new file mode 100644 index 0000000000..6f17365879 --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/each_line_spec.rb @@ -0,0 +1,6 @@ +require_relative "../../../spec_helper" +require_relative 'shared/each' + +describe "Zlib::GzipReader#each_line" do + it_behaves_like :gzipreader_each, :each_line +end diff --git a/spec/ruby/library/zlib/gzipreader/each_spec.rb b/spec/ruby/library/zlib/gzipreader/each_spec.rb new file mode 100644 index 0000000000..3b98391a87 --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/each_spec.rb @@ -0,0 +1,6 @@ +require_relative "../../../spec_helper" +require_relative 'shared/each' + +describe "Zlib::GzipReader#each" do + it_behaves_like :gzipreader_each, :each +end diff --git a/spec/ruby/library/zlib/gzipreader/eof_spec.rb b/spec/ruby/library/zlib/gzipreader/eof_spec.rb new file mode 100644 index 0000000000..a38e144c72 --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/eof_spec.rb @@ -0,0 +1,54 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipReader#eof?" do + before :each do + @data = '{"a":1234}' + @zip = [31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 171, 86, 74, 84, 178, 50, + 52, 50, 54, 169, 5, 0, 196, 20, 118, 213, 10, 0, 0, 0].pack('C*') + @io = StringIO.new @zip + end + + it "returns true when at EOF" do + gz = Zlib::GzipReader.new @io + gz.eof?.should == false + gz.read + gz.eof?.should == true + end + + it "returns true when at EOF with the exact length of uncompressed data" do + gz = Zlib::GzipReader.new @io + gz.eof?.should == false + gz.read(10) + gz.eof?.should == true + end + + it "returns true when at EOF with a length greater than the size of uncompressed data" do + gz = Zlib::GzipReader.new @io + gz.eof?.should == false + gz.read(11) + gz.eof?.should == true + end + + it "returns false when at EOF when there's data left in the buffer to read" do + gz = Zlib::GzipReader.new @io + gz.read(9) + gz.eof?.should == false + gz.read + gz.eof?.should == true + end + + # This is especially important for JRuby, since eof? there + # is more than just a simple accessor. + it "does not affect the reading data" do + gz = Zlib::GzipReader.new @io + 0.upto(9) do |i| + gz.eof?.should == false + gz.read(1).should == @data[i, 1] + end + gz.eof?.should == true + gz.read.should == "" + gz.eof?.should == true + end +end diff --git a/spec/ruby/library/zlib/gzipreader/getc_spec.rb b/spec/ruby/library/zlib/gzipreader/getc_spec.rb new file mode 100644 index 0000000000..be13592189 --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/getc_spec.rb @@ -0,0 +1,39 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipReader#getc" do + before :each do + @data = '12345abcde' + @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77, + 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*') + @io = StringIO.new @zip + end + + it "returns the next character from the stream" do + gz = Zlib::GzipReader.new @io + gz.pos.should == 0 + + gz.getc.should == '1' + gz.getc.should == '2' + gz.getc.should == '3' + gz.getc.should == '4' + gz.getc.should == '5' + end + + it "increments position" do + gz = Zlib::GzipReader.new @io + (0..@data.size).each do |i| + gz.pos.should == i + gz.getc + end + end + + it "returns nil at the end of the stream" do + gz = Zlib::GzipReader.new @io + gz.read + pos = gz.pos + gz.getc.should == nil + gz.pos.should == pos + end +end diff --git a/spec/ruby/library/zlib/gzipreader/gets_spec.rb b/spec/ruby/library/zlib/gzipreader/gets_spec.rb new file mode 100644 index 0000000000..5d0809f833 --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/gets_spec.rb @@ -0,0 +1,22 @@ +require_relative '../../../spec_helper' +require 'zlib' +require 'stringio' + +describe 'Zlib::GzipReader#gets' do + describe 'with "" separator' do + it 'reads paragraphs skipping newlines' do + # gz contains "\n\n\n\n\n123\n45\n\n\n\n\nabc\nde\n\n\n\n\n" + gz = Zlib::GzipReader.new( + StringIO.new( + [31, 139, 8, 0, 223, 152, 48, 89, 0, 3, 227, 226, 2, 2, 67, 35, + 99, 46, 19, 83, 16, 139, 43, 49, 41, 153, 43, 37, 21, 204, 4, 0, + 32, 119, 45, 184, 27, 0, 0, 0].pack('C*') + ) + ) + + gz.gets('').should == "123\n45\n\n" + gz.gets('').should == "abc\nde\n\n" + gz.eof?.should == true + end + end +end diff --git a/spec/ruby/library/zlib/gzipreader/mtime_spec.rb b/spec/ruby/library/zlib/gzipreader/mtime_spec.rb new file mode 100644 index 0000000000..e8e71fa72e --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/mtime_spec.rb @@ -0,0 +1,11 @@ +require_relative '../../../spec_helper' +require 'zlib' +require 'stringio' + +describe "Zlib::GzipReader#mtime" do + it "returns the timestamp from the Gzip header" do + io = StringIO.new "\x1f\x8b\x08\x00\x44\x33\x22\x11\x00\xff\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00" + gz = Zlib::GzipReader.new(io) + gz.mtime.to_i.should == 0x11223344 + end +end diff --git a/spec/ruby/library/zlib/gzipreader/pos_spec.rb b/spec/ruby/library/zlib/gzipreader/pos_spec.rb new file mode 100644 index 0000000000..8586faec92 --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/pos_spec.rb @@ -0,0 +1,24 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipReader#pos" do + before :each do + @data = '12345abcde' + @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77, + 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*') + @io = StringIO.new @zip + end + + it "returns the position" do + gz = Zlib::GzipReader.new @io + + gz.pos.should == 0 + + gz.read 5 + gz.pos.should == 5 + + gz.read + gz.pos.should == @data.length + end +end diff --git a/spec/ruby/library/zlib/gzipreader/read_spec.rb b/spec/ruby/library/zlib/gzipreader/read_spec.rb new file mode 100644 index 0000000000..b07d433bdd --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/read_spec.rb @@ -0,0 +1,66 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipReader#read" do + before :each do + @data = '12345abcde' + @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77, + 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*') + @io = StringIO.new @zip + end + + it "with no arguments reads the entire content of a gzip file" do + gz = Zlib::GzipReader.new @io + gz.read.should == @data + end + + it "with nil length argument reads the entire content of a gzip file" do + gz = Zlib::GzipReader.new @io + gz.read(nil).should == @data + end + + it "reads the contents up to a certain size" do + gz = Zlib::GzipReader.new @io + gz.read(5).should == @data[0...5] + gz.read(5).should == @data[5...10] + end + + it "does not accept a negative length to read" do + gz = Zlib::GzipReader.new @io + -> { + gz.read(-1) + }.should.raise(ArgumentError) + end + + it "returns an empty string if a 0 length is given" do + gz = Zlib::GzipReader.new @io + gz.read(0).should == "" + end + + it "respects :external_encoding option" do + gz = Zlib::GzipReader.new(@io, external_encoding: 'UTF-8') + gz.read.encoding.should == Encoding::UTF_8 + + @io.rewind + gz = Zlib::GzipReader.new(@io, external_encoding: 'UTF-16LE') + gz.read.encoding.should == Encoding::UTF_16LE + end + + describe "at the end of data" do + it "returns empty string if length parameter is not specified or 0" do + gz = Zlib::GzipReader.new @io + gz.read # read till the end + gz.read(0).should == "" + gz.read().should == "" + gz.read(nil).should == "" + end + + it "returns nil if length parameter is positive" do + gz = Zlib::GzipReader.new @io + gz.read # read till the end + gz.read(1).should == nil + gz.read(2**16).should == nil + end + end +end diff --git a/spec/ruby/library/zlib/gzipreader/readpartial_spec.rb b/spec/ruby/library/zlib/gzipreader/readpartial_spec.rb new file mode 100644 index 0000000000..559ce9f841 --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/readpartial_spec.rb @@ -0,0 +1,17 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipReader#readpartial" do + before :each do + @data = '12345abcde' + @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77, + 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*') + @io = StringIO.new(@zip) + end + + it 'accepts nil buffer' do + gz = Zlib::GzipReader.new(@io) + gz.readpartial(5, nil).should == '12345' + end +end diff --git a/spec/ruby/library/zlib/gzipreader/rewind_spec.rb b/spec/ruby/library/zlib/gzipreader/rewind_spec.rb new file mode 100644 index 0000000000..b31abb6abf --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/rewind_spec.rb @@ -0,0 +1,47 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipReader#rewind" do + before :each do + @data = '12345abcde' + @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77, + 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*') + @io = StringIO.new @zip + ScratchPad.clear + end + + it "resets the position of the stream pointer" do + gz = Zlib::GzipReader.new @io + gz.read + gz.pos.should == @data.length + + gz.rewind + gz.pos.should == 0 + gz.lineno.should == 0 + end + + it "resets the position of the stream pointer to data previously read" do + gz = Zlib::GzipReader.new @io + first_read = gz.read + gz.rewind + first_read.should == gz.read + end + + it "invokes seek method on the associated IO object" do + # first, prepare the mock object: + (obj = mock("io")).should_receive(:get_io).any_number_of_times.and_return(@io) + def obj.read(args); get_io.read(args); end + def obj.seek(pos, whence = 0) + ScratchPad.record :seek + get_io.seek(pos, whence) + end + + gz = Zlib::GzipReader.new(obj) + gz.rewind() + + ScratchPad.recorded.should == :seek + gz.pos.should == 0 + gz.read.should == "12345abcde" + end +end diff --git a/spec/ruby/library/zlib/gzipreader/shared/each.rb b/spec/ruby/library/zlib/gzipreader/shared/each.rb new file mode 100644 index 0000000000..71608e04ab --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/shared/each.rb @@ -0,0 +1,49 @@ +require_relative '../../../../spec_helper' +require 'stringio' +require 'zlib' + +describe :gzipreader_each, shared: true do + before :each do + @data = "firstline\nsecondline\n\nforthline" + @zip = [31, 139, 8, 0, 244, 125, 128, 88, 2, 255, 75, 203, 44, 42, 46, 201, + 201, 204, 75, 229, 42, 78, 77, 206, 207, 75, 1, 51, 185, 210,242, + 139, 74, 50, 64, 76, 0, 180, 54, 61, 111, 31, 0, 0, 0].pack('C*') + + @io = StringIO.new @zip + @gzreader = Zlib::GzipReader.new @io + end + + after :each do + ScratchPad.clear + end + + it "calls the given block for each line in the stream, passing the line as an argument" do + ScratchPad.record [] + @gzreader.send(@method) { |b| ScratchPad << b } + + ScratchPad.recorded.should == ["firstline\n", "secondline\n", "\n", "forthline"] + end + + it "returns an enumerator, which yields each byte in the stream, when no block is passed" do + enum = @gzreader.send(@method) + + ScratchPad.record [] + while true + begin + ScratchPad << enum.next + rescue StopIteration + break + end + end + + ScratchPad.recorded.should == ["firstline\n", "secondline\n", "\n", "forthline"] + end + + it "increments position before calling the block" do + i = 0 + @gzreader.send(@method) do |line| + i += line.length + @gzreader.pos.should == i + end + end +end diff --git a/spec/ruby/library/zlib/gzipreader/ungetbyte_spec.rb b/spec/ruby/library/zlib/gzipreader/ungetbyte_spec.rb new file mode 100644 index 0000000000..53870b9177 --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/ungetbyte_spec.rb @@ -0,0 +1,120 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipReader#ungetbyte" do + before :each do + @data = '12345abcde' + @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77, + 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*') + @io = StringIO.new @zip + end + + describe 'at the start of the stream' do + before :each do + @gz = Zlib::GzipReader.new(@io) + end + + describe 'with an integer' do + it 'prepends the byte to the stream' do + @gz.ungetbyte 0x21 + @gz.read.should == '!12345abcde' + end + + it 'decrements pos' do + @gz.ungetbyte 0x21 + @gz.pos.should == -1 + end + end + + quarantine! do # https://bugs.ruby-lang.org/issues/13675 + describe 'with nil' do + it 'does not prepend anything to the stream' do + @gz.ungetbyte nil + @gz.read.should == '12345abcde' + end + + it 'does not decrement pos' do + @gz.ungetbyte nil + @gz.pos.should == 0 + end + end + end + end + + describe 'in the middle of the stream' do + before :each do + @gz = Zlib::GzipReader.new(@io) + @gz.read 5 + end + + describe 'with an integer' do + it 'inserts the corresponding character into the stream' do + @gz.ungetbyte 0x21 + @gz.read.should == '!abcde' + end + + it 'decrements pos' do + @gz.ungetbyte 0x21 + @gz.pos.should == 4 + end + end + + quarantine! do # https://bugs.ruby-lang.org/issues/13675 + describe 'with nil' do + it 'does not insert anything into the stream' do + @gz.ungetbyte nil + @gz.read.should == 'abcde' + end + + it 'does not decrement pos' do + @gz.ungetbyte nil + @gz.pos.should == 5 + end + end + end + end + + describe 'at the end of the stream' do + before :each do + @gz = Zlib::GzipReader.new(@io) + @gz.read + end + + describe 'with an integer' do + it 'appends the corresponding character to the stream' do + @gz.ungetbyte 0x21 + @gz.read.should == '!' + end + + it 'decrements pos' do + @gz.ungetbyte 0x21 + @gz.pos.should == 9 + end + + it 'makes eof? false' do + @gz.ungetbyte 0x21 + @gz.eof?.should == false + end + end + + quarantine! do # https://bugs.ruby-lang.org/issues/13675 + describe 'with nil' do + it 'does not append anything to the stream' do + @gz.ungetbyte nil + @gz.read.should == '' + end + + it 'does not decrement pos' do + @gz.ungetbyte nil + @gz.pos.should == 10 + end + + it 'does not make eof? false' do + @gz.ungetbyte nil + @gz.eof?.should == true + end + end + end + end +end diff --git a/spec/ruby/library/zlib/gzipreader/ungetc_spec.rb b/spec/ruby/library/zlib/gzipreader/ungetc_spec.rb new file mode 100644 index 0000000000..46dcfde989 --- /dev/null +++ b/spec/ruby/library/zlib/gzipreader/ungetc_spec.rb @@ -0,0 +1,284 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipReader#ungetc" do + before :each do + @data = '12345abcde' + @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77, + 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*') + @io = StringIO.new @zip + end + + describe 'at the start of the stream' do + before :each do + @gz = Zlib::GzipReader.new(@io, external_encoding: Encoding::UTF_8) + end + + describe 'with a single-byte character' do + it 'prepends the character to the stream' do + @gz.ungetc 'x' + @gz.read.should == 'x12345abcde' + end + + it 'decrements pos' do + @gz.ungetc 'x' + @gz.pos.should == -1 + end + end + + describe 'with a multi-byte character' do + it 'prepends the character to the stream' do + @gz.ungetc 'ŷ' + @gz.read.should == 'ŷ12345abcde' + end + + it 'decrements pos' do + @gz.ungetc 'ŷ' + @gz.pos.should == -2 + end + end + + describe 'with a multi-character string' do + it 'prepends the characters to the stream' do + @gz.ungetc 'xŷž' + @gz.read.should == 'xŷž12345abcde' + end + + it 'decrements pos' do + @gz.ungetc 'xŷž' + @gz.pos.should == -5 + end + end + + describe 'with an integer' do + it 'prepends the corresponding character to the stream' do + @gz.ungetc 0x21 + @gz.read.should == '!12345abcde' + end + + it 'decrements pos' do + @gz.ungetc 0x21 + @gz.pos.should == -1 + end + end + + describe 'with an empty string' do + it 'does not prepend anything to the stream' do + @gz.ungetc '' + @gz.read.should == '12345abcde' + end + + it 'does not decrement pos' do + @gz.ungetc '' + @gz.pos.should == 0 + end + end + + quarantine! do # https://bugs.ruby-lang.org/issues/13675 + describe 'with nil' do + it 'does not prepend anything to the stream' do + @gz.ungetc nil + @gz.read.should == '12345abcde' + end + + it 'does not decrement pos' do + @gz.ungetc nil + @gz.pos.should == 0 + end + end + end + end + + describe 'in the middle of the stream' do + before :each do + @gz = Zlib::GzipReader.new(@io, external_encoding: Encoding::UTF_8) + @gz.read 5 + end + + describe 'with a single-byte character' do + it 'inserts the character into the stream' do + @gz.ungetc 'x' + @gz.read.should == 'xabcde' + end + + it 'decrements pos' do + @gz.ungetc 'x' + @gz.pos.should == 4 + end + end + + describe 'with a multi-byte character' do + it 'inserts the character into the stream' do + @gz.ungetc 'ŷ' + @gz.read.should == 'ŷabcde' + end + + it 'decrements pos' do + @gz.ungetc 'ŷ' + @gz.pos.should == 3 + end + end + + describe 'with a multi-character string' do + it 'inserts the characters into the stream' do + @gz.ungetc 'xŷž' + @gz.read.should == 'xŷžabcde' + end + + it 'decrements pos' do + @gz.ungetc 'xŷž' + @gz.pos.should == 0 + end + end + + describe 'with an integer' do + it 'inserts the corresponding character into the stream' do + @gz.ungetc 0x21 + @gz.read.should == '!abcde' + end + + it 'decrements pos' do + @gz.ungetc 0x21 + @gz.pos.should == 4 + end + end + + describe 'with an empty string' do + it 'does not insert anything into the stream' do + @gz.ungetc '' + @gz.read.should == 'abcde' + end + + it 'does not decrement pos' do + @gz.ungetc '' + @gz.pos.should == 5 + end + end + + quarantine! do # https://bugs.ruby-lang.org/issues/13675 + describe 'with nil' do + it 'does not insert anything into the stream' do + @gz.ungetc nil + @gz.read.should == 'abcde' + end + + it 'does not decrement pos' do + @gz.ungetc nil + @gz.pos.should == 5 + end + end + end + end + + describe 'at the end of the stream' do + before :each do + @gz = Zlib::GzipReader.new(@io, external_encoding: Encoding::UTF_8) + @gz.read + end + + describe 'with a single-byte character' do + it 'appends the character to the stream' do + @gz.ungetc 'x' + @gz.read.should == 'x' + end + + it 'decrements pos' do + @gz.ungetc 'x' + @gz.pos.should == 9 + end + + it 'makes eof? false' do + @gz.ungetc 'x' + @gz.eof?.should == false + end + end + + describe 'with a multi-byte character' do + it 'appends the character to the stream' do + @gz.ungetc 'ŷ' + @gz.read.should == 'ŷ' + end + + it 'decrements pos' do + @gz.ungetc 'ŷ' + @gz.pos.should == 8 + end + + it 'makes eof? false' do + @gz.ungetc 'ŷ' + @gz.eof?.should == false + end + end + + describe 'with a multi-character string' do + it 'appends the characters to the stream' do + @gz.ungetc 'xŷž' + @gz.read.should == 'xŷž' + end + + it 'decrements pos' do + @gz.ungetc 'xŷž' + @gz.pos.should == 5 + end + + it 'makes eof? false' do + @gz.ungetc 'xŷž' + @gz.eof?.should == false + end + end + + describe 'with an integer' do + it 'appends the corresponding character to the stream' do + @gz.ungetc 0x21 + @gz.read.should == '!' + end + + it 'decrements pos' do + @gz.ungetc 0x21 + @gz.pos.should == 9 + end + + it 'makes eof? false' do + @gz.ungetc 0x21 + @gz.eof?.should == false + end + end + + describe 'with an empty string' do + it 'does not append anything to the stream' do + @gz.ungetc '' + @gz.read.should == '' + end + + it 'does not decrement pos' do + @gz.ungetc '' + @gz.pos.should == 10 + end + + it 'does not make eof? false' do + @gz.ungetc '' + @gz.eof?.should == true + end + end + + quarantine! do # https://bugs.ruby-lang.org/issues/13675 + describe 'with nil' do + it 'does not append anything to the stream' do + @gz.ungetc nil + @gz.read.should == '' + end + + it 'does not decrement pos' do + @gz.ungetc nil + @gz.pos.should == 10 + end + + it 'does not make eof? false' do + @gz.ungetc nil + @gz.eof?.should == true + end + end + end + end +end diff --git a/spec/ruby/library/zlib/gzipwriter/append_spec.rb b/spec/ruby/library/zlib/gzipwriter/append_spec.rb new file mode 100644 index 0000000000..ef9e3d3a6b --- /dev/null +++ b/spec/ruby/library/zlib/gzipwriter/append_spec.rb @@ -0,0 +1,15 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipWriter#<<" do + before :each do + @io = StringIO.new + end + + it "returns self" do + Zlib::GzipWriter.wrap @io do |gzio| + (gzio << "test").should.equal?(gzio) + end + end +end diff --git a/spec/ruby/library/zlib/gzipwriter/mtime_spec.rb b/spec/ruby/library/zlib/gzipwriter/mtime_spec.rb new file mode 100644 index 0000000000..a70fa68069 --- /dev/null +++ b/spec/ruby/library/zlib/gzipwriter/mtime_spec.rb @@ -0,0 +1,37 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipWriter#mtime=" do + before :each do + @io = StringIO.new + end + + it "sets mtime using Integer" do + Zlib::GzipWriter.wrap @io do |gzio| + gzio.mtime = 1 + + gzio.mtime.should == Time.at(1) + end + + @io.string[4, 4].should == [1,0,0,0].pack('C*') + end + + it "sets mtime using Time" do + Zlib::GzipWriter.wrap @io do |gzio| + gzio.mtime = Time.at 1 + + gzio.mtime.should == Time.at(1) + end + + @io.string[4, 4].should == [1,0,0,0].pack('C*') + end + + it "raises if the header was written" do + Zlib::GzipWriter.wrap @io do |gzio| + gzio.write '' + + -> { gzio.mtime = nil }.should.raise(Zlib::GzipFile::Error, 'header is already written') + end + end +end diff --git a/spec/ruby/library/zlib/gzipwriter/write_spec.rb b/spec/ruby/library/zlib/gzipwriter/write_spec.rb new file mode 100644 index 0000000000..522ae7f2aa --- /dev/null +++ b/spec/ruby/library/zlib/gzipwriter/write_spec.rb @@ -0,0 +1,36 @@ +require_relative '../../../spec_helper' +require 'stringio' +require 'zlib' + +describe "Zlib::GzipWriter#write" do + before :each do + @data = '12345abcde' + @zip = [31, 139, 8, 0, 44, 220, 209, 71, 0, 3, 51, 52, 50, 54, 49, 77, + 76, 74, 78, 73, 5, 0, 157, 5, 0, 36, 10, 0, 0, 0].pack('C*') + @io = StringIO.new "".b + end + + it "writes some compressed data" do + Zlib::GzipWriter.wrap @io do |gzio| + gzio.write @data + end + + # skip gzip header for now + @io.string.unpack('C*')[10..-1].should == @zip.unpack('C*')[10..-1] + end + + it "returns the number of bytes in the input" do + Zlib::GzipWriter.wrap @io do |gzio| + gzio.write(@data).should == @data.size + end + end + + it "handles inputs of 2^23 bytes" do + input = '.'.b * (2 ** 23) + + Zlib::GzipWriter.wrap @io do |gzio| + gzio.write input + end + @io.string.size.should == 8176 + end +end diff --git a/spec/ruby/library/zlib/inflate/append_spec.rb b/spec/ruby/library/zlib/inflate/append_spec.rb new file mode 100644 index 0000000000..a4c791e31e --- /dev/null +++ b/spec/ruby/library/zlib/inflate/append_spec.rb @@ -0,0 +1,60 @@ +require_relative '../../../spec_helper' +require 'zlib' + +describe "Zlib::Inflate#<<" do + before :all do + @foo_deflated = [120, 156, 75, 203, 207, 7, 0, 2, 130, 1, 69].pack('C*') + end + + before :each do + @z = Zlib::Inflate.new + end + + after :each do + @z.close unless @z.closed? + end + + it "appends data to the input stream" do + @z << @foo_deflated + @z.finish.should == 'foo' + end + + it "treats nil argument as the end of compressed data" do + @z = Zlib::Inflate.new + @z << @foo_deflated << nil + @z.finish.should == 'foo' + end + + it "just passes through the data after nil argument" do + @z = Zlib::Inflate.new + @z << @foo_deflated << nil + @z << "-after_nil_data" + @z.finish.should == 'foo-after_nil_data' + end + + it "properly handles data in chunks" do + # add bytes, one by one + @foo_deflated.each_byte { |d| @z << d.chr} + @z.finish.should == "foo" + end + + it "properly handles incomplete data" do + # add bytes, one by one + @foo_deflated[0, 5].each_byte { |d| @z << d.chr} + -> { @z.finish }.should.raise(Zlib::BufError) + end + + it "properly handles excessive data, byte-by-byte" do + # add bytes, one by one + data = @foo_deflated * 2 + data.each_byte { |d| @z << d.chr} + @z.finish.should == "foo" + @foo_deflated + end + + it "properly handles excessive data, in one go" do + # add bytes, one by one + data = @foo_deflated * 2 + @z << data + @z.finish.should == "foo" + @foo_deflated + end +end diff --git a/spec/ruby/library/zlib/inflate/finish_spec.rb b/spec/ruby/library/zlib/inflate/finish_spec.rb new file mode 100644 index 0000000000..b7494e419c --- /dev/null +++ b/spec/ruby/library/zlib/inflate/finish_spec.rb @@ -0,0 +1,29 @@ +require_relative "../../../spec_helper" +require 'zlib' + +describe "Zlib::Inflate#finish" do + + before do + @zeros = Zlib::Deflate.deflate("0" * 100_000) + @inflator = Zlib::Inflate.new + @chunks = [] + + @inflator.inflate(@zeros) do |chunk| + @chunks << chunk + break + end + + @inflator.finish do |chunk| + @chunks << chunk + end + end + + it "inflates chunked data" do + @chunks.map { |chunk| chunk.length }.should == [16384, 16384, 16384, 16384, 16384, 16384, 1696] + end + + it "each chunk should have the same prefix" do + @chunks.all? { |chunk| chunk =~ /\A0+\z/ }.should == true + end + +end diff --git a/spec/ruby/library/zlib/inflate/inflate_spec.rb b/spec/ruby/library/zlib/inflate/inflate_spec.rb new file mode 100644 index 0000000000..92e52363db --- /dev/null +++ b/spec/ruby/library/zlib/inflate/inflate_spec.rb @@ -0,0 +1,159 @@ +require 'zlib' +require_relative '../../../spec_helper' + +describe "Zlib::Inflate#inflate" do + + before :each do + @inflator = Zlib::Inflate.new + end + it "inflates some data" do + data = [120, 156, 99, 96, 128, 1, 0, 0, 10, 0, 1].pack('C*') + unzipped = @inflator.inflate data + @inflator.finish + + unzipped.should == "\000" * 10 + end + + it "inflates lots of data" do + data = [120, 156, 237, 193, 1, 1, 0, 0] + + [0, 128, 144, 254, 175, 238, 8, 10] + + Array.new(31, 0) + + [24, 128, 0, 0, 1] + + unzipped = @inflator.inflate data.pack('C*') + @inflator.finish + + unzipped.should == "\000" * 32 * 1024 + end + + it "works in pass-through mode, once finished" do + data = [120, 156, 99, 96, 128, 1, 0, 0, 10, 0, 1] + @inflator.inflate data.pack('C*') + @inflator.finish # this is a precondition + + out = @inflator.inflate('uncompressed_data') + out << @inflator.finish + out.should == 'uncompressed_data' + + @inflator << ('uncompressed_data') << nil + @inflator.finish.should == 'uncompressed_data' + end + + it "has a binary encoding" do + data = [120, 156, 99, 96, 128, 1, 0, 0, 10, 0, 1].pack('C*') + unzipped = @inflator.inflate data + @inflator.finish.encoding.should == Encoding::BINARY + unzipped.encoding.should == Encoding::BINARY + end + +end + +describe "Zlib::Inflate.inflate" do + + it "inflates some data" do + data = [120, 156, 99, 96, 128, 1, 0, 0, 10, 0, 1] + unzipped = Zlib::Inflate.inflate data.pack('C*') + + unzipped.should == "\000" * 10 + end + + it "inflates lots of data" do + data = [120, 156, 237, 193, 1, 1, 0, 0] + + [0, 128, 144, 254, 175, 238, 8, 10] + + Array.new(31,0) + + [24, 128, 0, 0, 1] + + zipped = Zlib::Inflate.inflate data.pack('C*') + + zipped.should == "\000" * 32 * 1024 + end + + it "properly handles data in chunks" do + data = [120, 156, 75, 203, 207, 7, 0, 2, 130, 1, 69].pack('C*') + z = Zlib::Inflate.new + # add bytes, one by one + result = +"" + data.each_byte { |d| result << z.inflate(d.chr)} + result << z.finish + result.should == "foo" + end + + it "properly handles incomplete data" do + data = [120, 156, 75, 203, 207, 7, 0, 2, 130, 1, 69].pack('C*')[0,5] + z = Zlib::Inflate.new + # add bytes, one by one, but not all + result = +"" + data.each_byte { |d| result << z.inflate(d.chr)} + -> { result << z.finish }.should.raise(Zlib::BufError) + end + + it "properly handles excessive data, byte-by-byte" do + main_data = [120, 156, 75, 203, 207, 7, 0, 2, 130, 1, 69].pack('C*') + data = main_data * 2 + result = +"" + + z = Zlib::Inflate.new + # add bytes, one by one + data.each_byte { |d| result << z.inflate(d.chr)} + result << z.finish + + # the first chunk is inflated to its completion, + # the second chunk is just passed through. + result.should == "foo" + main_data + end + + it "properly handles excessive data, in one go" do + main_data = [120, 156, 75, 203, 207, 7, 0, 2, 130, 1, 69].pack('C*') + data = main_data * 2 + result = +"" + + z = Zlib::Inflate.new + result << z.inflate(data) + result << z.finish + + # the first chunk is inflated to its completion, + # the second chunk is just passed through. + result.should == "foo" + main_data + end +end + +describe "Zlib::Inflate#inflate" do + + before do + @zeros = Zlib::Deflate.deflate("0" * 100_000) + @inflator = Zlib::Inflate.new + @chunks = [] + end + + describe "without break" do + + before do + @inflator.inflate(@zeros) do |chunk| + @chunks << chunk + end + end + + it "inflates chunked data" do + @chunks.map { |chunk| chunk.size }.should == [16384, 16384, 16384, 16384, 16384, 16384, 1696] + end + + it "properly handles chunked data" do + @chunks.all? { |chunk| chunk =~ /\A0+\z/ }.should == true + end + end + + describe "with break" do + + before do + @inflator.inflate(@zeros) do |chunk| + @chunks << chunk + break + end + end + + it "inflates chunked break" do + output = @inflator.inflate nil + (100_000 - @chunks.first.length).should == output.length + end + end +end diff --git a/spec/ruby/library/zlib/inflate/set_dictionary_spec.rb b/spec/ruby/library/zlib/inflate/set_dictionary_spec.rb new file mode 100644 index 0000000000..375ee3c765 --- /dev/null +++ b/spec/ruby/library/zlib/inflate/set_dictionary_spec.rb @@ -0,0 +1,20 @@ +# encoding: binary +require_relative '../../../spec_helper' +require 'zlib' + +describe "Zlib::Inflate#set_dictionary" do + it "sets the inflate dictionary" do + deflated = "x\273\024\341\003\313KLJNIMK\317\310\314\002\000\025\206\003\370" + + i = Zlib::Inflate.new + + begin + i << deflated + flunk 'Zlib::NeedDict not raised' + rescue Zlib::NeedDict + i.set_dictionary 'aaaaaaaaaa' + end + + i.finish.should == 'abcdefghij' + end +end diff --git a/spec/ruby/library/zlib/inflate_spec.rb b/spec/ruby/library/zlib/inflate_spec.rb new file mode 100644 index 0000000000..42c8dc5fbe --- /dev/null +++ b/spec/ruby/library/zlib/inflate_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require "zlib" + +describe "Zlib.inflate" do + it "inflates some data" do + Zlib.inflate([120, 156, 51, 52, 132, 1, 0, 10, 145, 1, 235].pack('C*')).should == "1" * 10 + end +end diff --git a/spec/ruby/library/zlib/zlib_version_spec.rb b/spec/ruby/library/zlib/zlib_version_spec.rb new file mode 100644 index 0000000000..7edc76cdd5 --- /dev/null +++ b/spec/ruby/library/zlib/zlib_version_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require 'zlib' + +describe "Zlib.zlib_version" do + it "returns the version of the libz library" do + Zlib.zlib_version.should.instance_of?(String) + end +end diff --git a/spec/ruby/library/zlib/zstream/adler_spec.rb b/spec/ruby/library/zlib/zstream/adler_spec.rb new file mode 100644 index 0000000000..55ac8ae79e --- /dev/null +++ b/spec/ruby/library/zlib/zstream/adler_spec.rb @@ -0,0 +1,11 @@ +require_relative '../../../spec_helper' +require 'zlib' + +describe "Zlib::ZStream#adler" do + it "generates hash" do + z = Zlib::Deflate.new + z << "foo" + z.finish + z.adler.should == 0x02820145 + end +end diff --git a/spec/ruby/library/zlib/zstream/avail_in_spec.rb b/spec/ruby/library/zlib/zstream/avail_in_spec.rb new file mode 100644 index 0000000000..eddae15830 --- /dev/null +++ b/spec/ruby/library/zlib/zstream/avail_in_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../../spec_helper' +require 'zlib' + +describe "Zlib::ZStream#avail_in" do + it "returns bytes in the input buffer" do + z = Zlib::Deflate.new + z.avail_in.should == 0 + end +end diff --git a/spec/ruby/library/zlib/zstream/avail_out_spec.rb b/spec/ruby/library/zlib/zstream/avail_out_spec.rb new file mode 100644 index 0000000000..2e5a394ec0 --- /dev/null +++ b/spec/ruby/library/zlib/zstream/avail_out_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../../spec_helper' +require 'zlib' + +describe "Zlib::ZStream#avail_out" do + it "returns bytes in the output buffer" do + z = Zlib::Deflate.new + z.avail_out.should == 0 + end +end diff --git a/spec/ruby/library/zlib/zstream/data_type_spec.rb b/spec/ruby/library/zlib/zstream/data_type_spec.rb new file mode 100644 index 0000000000..8be96adf7c --- /dev/null +++ b/spec/ruby/library/zlib/zstream/data_type_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../../spec_helper' +require 'zlib' + +describe "Zlib::ZStream#data_type" do + it "returns the type of the data in the stream" do + z = Zlib::Deflate.new + [Zlib::ASCII, Zlib::BINARY, Zlib::UNKNOWN].include?(z.data_type).should == true + end +end diff --git a/spec/ruby/library/zlib/zstream/flush_next_out_spec.rb b/spec/ruby/library/zlib/zstream/flush_next_out_spec.rb new file mode 100644 index 0000000000..63676a8203 --- /dev/null +++ b/spec/ruby/library/zlib/zstream/flush_next_out_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../../spec_helper' +require 'zlib' + +describe "Zlib::ZStream#flush_next_out" do + + it "flushes the stream and flushes the output buffer" do + zs = Zlib::Inflate.new + zs << [120, 156, 75, 203, 207, 7, 0, 2, 130, 1, 69].pack('C*') + + zs.flush_next_out.should == 'foo' + zs.should.finished? + zs.flush_next_out.should == '' + end +end |
