summaryrefslogtreecommitdiff
path: root/spec/ruby/library/zlib/deflate/deflate_spec.rb
blob: 67bcf22d498c04a28599d2815be914b5ba31a065 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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(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
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