summaryrefslogtreecommitdiff
path: root/spec/ruby/library/zlib/gzipreader/ungetc_spec.rb
blob: 34f2a1a2ca7f5526546212b41be175c087516d34 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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 be_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 be_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 be_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 be_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 be_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 be_true
        end
      end
    end
  end
end