bzip2-ruby 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +21 -0
- data/README.rdoc +2 -8
- data/Rakefile +1 -1
- data/VERSION.yml +1 -1
- data/bzip2-ruby.gemspec +22 -21
- data/ext/{bz2.c → bzip2.c} +534 -509
- data/ext/extconf.rb +6 -1
- data/lib/bzip2.rb +5 -0
- data/spec/reader_spec.rb +357 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/writer_spec.rb +139 -0
- metadata +13 -10
- data/History.txt +0 -5
- data/test/reader.rb +0 -370
- data/test/runit_.rb +0 -32
- data/test/writer.rb +0 -152
data/ext/extconf.rb
CHANGED
@@ -7,7 +7,12 @@ if have_library("bz2", "BZ2_bzWriteOpen")
|
|
7
7
|
if enable_config("shared", true)
|
8
8
|
$static = nil
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
|
+
if RUBY_VERSION =~ /1.9/
|
12
|
+
$CFLAGS << ' -DRUBY_19_COMPATIBILITY'
|
13
|
+
end
|
14
|
+
|
15
|
+
create_makefile("bzip2_ext")
|
11
16
|
else
|
12
17
|
puts "libbz2 not found, maybe try manually specifying --with-bz2-dir to find it?"
|
13
18
|
end
|
data/lib/bzip2.rb
ADDED
data/spec/reader_spec.rb
ADDED
@@ -0,0 +1,357 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')
|
3
|
+
|
4
|
+
describe "Bzip2::Writer" do
|
5
|
+
before(:all) do
|
6
|
+
@sample = "08: This is a line\n"
|
7
|
+
@file = "_10lines_"
|
8
|
+
@data = [
|
9
|
+
"00: This is a line\n",
|
10
|
+
"01: This is a line\n",
|
11
|
+
"02: This is a line\n",
|
12
|
+
"03: This is a line\n",
|
13
|
+
"04: This is a line\n",
|
14
|
+
"05: This is a line\n",
|
15
|
+
"06: This is a line\n",
|
16
|
+
"07: This is a line\n",
|
17
|
+
"08: This is a line\n",
|
18
|
+
"09: This is a line\n"
|
19
|
+
]
|
20
|
+
|
21
|
+
open("|bzip2 > #{@file}", "w") do |f|
|
22
|
+
@data.each { |l| f.puts l }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
after(:all) do
|
27
|
+
File.unlink(@file)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should test_f_s_foreach" do
|
31
|
+
count = 0
|
32
|
+
Bzip2::Reader.foreach(@file) do |line|
|
33
|
+
num = line[0..1].to_i
|
34
|
+
count.should == num
|
35
|
+
count += 1
|
36
|
+
end
|
37
|
+
@data.size.should == count
|
38
|
+
|
39
|
+
count = 0
|
40
|
+
Bzip2::Reader.foreach(@file, nil) do |file|
|
41
|
+
file.split(/\n/).each do |line|
|
42
|
+
num = line[0..1].to_i
|
43
|
+
count.should == num
|
44
|
+
count += 1
|
45
|
+
end
|
46
|
+
end
|
47
|
+
@data.size.should == count
|
48
|
+
|
49
|
+
count = 0
|
50
|
+
Bzip2::Reader.foreach(@file, ' ') do |thing|
|
51
|
+
count += 1
|
52
|
+
end
|
53
|
+
41.should == count
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should test_f_s_readlines" do
|
57
|
+
lines = Bzip2::Reader.readlines(@file)
|
58
|
+
@data.size.should == lines.size
|
59
|
+
|
60
|
+
lines = Bzip2::Reader.readlines(@file, nil)
|
61
|
+
1.should == lines.size
|
62
|
+
(@sample.length * @data.size).should == lines[0].size
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should test_f_closed?" do
|
66
|
+
f = Bzip2::Reader.open(@file)
|
67
|
+
f.closed?.should be_false
|
68
|
+
f.close
|
69
|
+
f.closed?.should be_true
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should test_f_each" do
|
73
|
+
count = 0
|
74
|
+
Bzip2::Reader.open(@file) do |file|
|
75
|
+
file.each do |line|
|
76
|
+
num = line[0..1].to_i
|
77
|
+
count.should == num
|
78
|
+
count += 1
|
79
|
+
end
|
80
|
+
@data.size.should == count
|
81
|
+
end
|
82
|
+
|
83
|
+
count = 0
|
84
|
+
Bzip2::Reader.open(@file) do |file|
|
85
|
+
file.each(nil) do |contents|
|
86
|
+
contents.split(/\n/).each do |line|
|
87
|
+
num = line[0..1].to_i
|
88
|
+
count.should == num
|
89
|
+
count += 1
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
@data.size.should == count
|
94
|
+
|
95
|
+
count = 0
|
96
|
+
Bzip2::Reader.open(@file) do |file|
|
97
|
+
file.each(' ') do |thing|
|
98
|
+
count += 1
|
99
|
+
end
|
100
|
+
end
|
101
|
+
41.should == count
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should test_f_each_byte" do
|
105
|
+
count = 0
|
106
|
+
data = @data.join
|
107
|
+
|
108
|
+
Bzip2::Reader.open(@file) do |file|
|
109
|
+
file.each_byte do |b|
|
110
|
+
data.getbyte(count).should == b
|
111
|
+
count += 1
|
112
|
+
end
|
113
|
+
end
|
114
|
+
(@sample.length * @data.size).should == count
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should test_f_each_line" do
|
118
|
+
count = 0
|
119
|
+
Bzip2::Reader.open(@file) do |file|
|
120
|
+
file.each_line do |line|
|
121
|
+
num = line[0..1].to_i
|
122
|
+
count.should == num
|
123
|
+
count += 1
|
124
|
+
end
|
125
|
+
@data.size.should == count
|
126
|
+
end
|
127
|
+
|
128
|
+
count = 0
|
129
|
+
Bzip2::Reader.open(@file) do |file|
|
130
|
+
file.each_line(nil) do |contents|
|
131
|
+
contents.split(/\n/).each do |line|
|
132
|
+
num = line[0..1].to_i
|
133
|
+
count.should == num
|
134
|
+
count += 1
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
@data.size.should == count
|
139
|
+
|
140
|
+
count = 0
|
141
|
+
Bzip2::Reader.open(@file) do |file|
|
142
|
+
file.each_line(' ') do |thing|
|
143
|
+
count += 1
|
144
|
+
end
|
145
|
+
end
|
146
|
+
41.should == count
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should test_f_eof" do
|
150
|
+
Bzip2::Reader.open(@file) do |file|
|
151
|
+
@data.size.times do
|
152
|
+
(!file.eof).should be_true
|
153
|
+
(!file.eof?).should be_true
|
154
|
+
file.gets
|
155
|
+
end
|
156
|
+
file.eof.should be_true
|
157
|
+
file.eof?.should be_true
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should test_f_getc" do
|
162
|
+
count = 0
|
163
|
+
data = @data.join
|
164
|
+
|
165
|
+
Bzip2::Reader.open(@file) do |file|
|
166
|
+
while (ch = file.getc)
|
167
|
+
data.getbyte(count).should == ch
|
168
|
+
count += 1
|
169
|
+
end
|
170
|
+
file.getc.should be_nil
|
171
|
+
end
|
172
|
+
(@sample.length * @data.size).should == count
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should test_f_gets" do
|
176
|
+
count = 0
|
177
|
+
Bzip2::Reader.open(@file) do |file|
|
178
|
+
while (line = file.gets)
|
179
|
+
num = line[0..1].to_i
|
180
|
+
count.should == num
|
181
|
+
count += 1
|
182
|
+
end
|
183
|
+
file.gets.should be_nil
|
184
|
+
@data.size.should == count
|
185
|
+
end
|
186
|
+
|
187
|
+
count = 0
|
188
|
+
Bzip2::Reader.open(@file) do |file|
|
189
|
+
while (line = file.gets("line\n"))
|
190
|
+
@data[count].should == line
|
191
|
+
num = line[0..1].to_i
|
192
|
+
count.should == num
|
193
|
+
count += 1
|
194
|
+
end
|
195
|
+
file.gets.should be_nil
|
196
|
+
@data.size.should == count
|
197
|
+
end
|
198
|
+
|
199
|
+
count = 0
|
200
|
+
Bzip2::Reader.open(@file) do |file|
|
201
|
+
while (contents = file.gets(nil))
|
202
|
+
contents.split(/\n/).each do |line|
|
203
|
+
num = line[0..1].to_i
|
204
|
+
count.should == num
|
205
|
+
count += 1
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
@data.size.should == count
|
210
|
+
|
211
|
+
count = 0
|
212
|
+
Bzip2::Reader.open(@file) do |file|
|
213
|
+
while (thing = file.gets(' '))
|
214
|
+
count += 1
|
215
|
+
end
|
216
|
+
end
|
217
|
+
41.should == count
|
218
|
+
end
|
219
|
+
|
220
|
+
it "should test_f_read" do
|
221
|
+
Bzip2::Reader.open(@file) do |file|
|
222
|
+
content = file.read
|
223
|
+
(@sample.length * @data.size).should == content.length
|
224
|
+
count = 0
|
225
|
+
content.split(/\n/).each do |line|
|
226
|
+
num = line[0..1].to_i
|
227
|
+
count.should == num
|
228
|
+
count += 1
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
Bzip2::Reader.open(@file) do |file|
|
233
|
+
"00: This is ".should == file.read(12)
|
234
|
+
"a line\n01: T".should == file.read(12)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
it "should test_f_readchar" do
|
239
|
+
count = 0
|
240
|
+
data = @data.join
|
241
|
+
Bzip2::Reader.open(@file) do |file|
|
242
|
+
190.times do |count|
|
243
|
+
ch = file.readchar
|
244
|
+
data.getbyte(count).should == ch
|
245
|
+
count += 1
|
246
|
+
end
|
247
|
+
lambda { file.readchar }.should raise_error(Bzip2::EOZError)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
it "should test_f_readline" do
|
252
|
+
count = 0
|
253
|
+
Bzip2::Reader.open(@file) do |file|
|
254
|
+
@data.size.times do |count|
|
255
|
+
line = file.readline
|
256
|
+
num = line[0..1].to_i
|
257
|
+
count.should == num
|
258
|
+
count += 1
|
259
|
+
end
|
260
|
+
lambda { file.readline }.should raise_error(Bzip2::EOZError)
|
261
|
+
end
|
262
|
+
|
263
|
+
count = 0
|
264
|
+
Bzip2::Reader.open(@file) do |file|
|
265
|
+
contents = file.readline(nil)
|
266
|
+
contents.split(/\n/).each do |line|
|
267
|
+
num = line[0..1].to_i
|
268
|
+
count.should == num
|
269
|
+
count += 1
|
270
|
+
end
|
271
|
+
lambda { file.readline }.should raise_error(Bzip2::EOZError)
|
272
|
+
end
|
273
|
+
@data.size.should == count
|
274
|
+
|
275
|
+
count = 0
|
276
|
+
Bzip2::Reader.open(@file) do |file|
|
277
|
+
41.times do |count|
|
278
|
+
thing = file.readline(' ')
|
279
|
+
count += 1
|
280
|
+
end
|
281
|
+
lambda { file.readline }.should raise_error(Bzip2::EOZError)
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
it "should test_f_readlines" do
|
286
|
+
Bzip2::Reader.open(@file) do |file|
|
287
|
+
lines = file.readlines
|
288
|
+
@data.size.should == lines.size
|
289
|
+
end
|
290
|
+
|
291
|
+
Bzip2::Reader.open(@file) do |file|
|
292
|
+
lines = file.readlines(nil)
|
293
|
+
1.should == lines.size
|
294
|
+
(@sample.length * @data.size).should == lines[0].size
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
# it "should test_f_ungetc" do
|
299
|
+
# Bzip2::Reader.open(@file) do |file|
|
300
|
+
# ?0.getbyte(0).should == file.getc
|
301
|
+
# ?0.getbyte(0).should == file.getc
|
302
|
+
# ?:.getbyte(0).should == file.getc
|
303
|
+
# ?\s.getbyte(0).should == file.getc
|
304
|
+
# file.ungetc(?:.to_i).should be_nil
|
305
|
+
# ?:.getbyte(0).should == file.getc
|
306
|
+
# 1 while file.getc
|
307
|
+
# file.ungetc(?A).should be_nil
|
308
|
+
# ?A.should == file.getc
|
309
|
+
# end
|
310
|
+
# end
|
311
|
+
|
312
|
+
it "should test_f_ungets" do
|
313
|
+
count = 0
|
314
|
+
Bzip2::Reader.open(@file) do |file|
|
315
|
+
@data[count].should == file.gets
|
316
|
+
(count + 1).should == file.lineno
|
317
|
+
file.ungets(@data[count]).should be_nil
|
318
|
+
@data[count].should == file.gets
|
319
|
+
count += 1
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
it "should test_s_readline" do
|
324
|
+
count = 0
|
325
|
+
string = IO.readlines(@file, nil)[0]
|
326
|
+
file = Bzip2::Reader.new(string)
|
327
|
+
@data.size.times do |count|
|
328
|
+
line = file.readline
|
329
|
+
num = line[0..1].to_i
|
330
|
+
count.should == num
|
331
|
+
count += 1
|
332
|
+
end
|
333
|
+
lambda { file.readline }.should raise_error(Bzip2::EOZError)
|
334
|
+
file.close
|
335
|
+
|
336
|
+
count = 0
|
337
|
+
file = Bzip2::Reader.new(string)
|
338
|
+
contents = file.readline(nil)
|
339
|
+
contents.split(/\n/).each do |line|
|
340
|
+
num = line[0..1].to_i
|
341
|
+
count.should == num
|
342
|
+
count += 1
|
343
|
+
end
|
344
|
+
lambda { file.readline }.should raise_error(Bzip2::EOZError)
|
345
|
+
@data.size.should == count
|
346
|
+
file.close
|
347
|
+
|
348
|
+
count = 0
|
349
|
+
file = Bzip2::Reader.new(string)
|
350
|
+
41.times do |count|
|
351
|
+
thing = file.readline(' ')
|
352
|
+
count += 1
|
353
|
+
end
|
354
|
+
lambda { file.readline }.should raise_error(Bzip2::EOZError)
|
355
|
+
file.close
|
356
|
+
end
|
357
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/writer_spec.rb
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')
|
3
|
+
|
4
|
+
describe "Bzip2::Writer" do
|
5
|
+
class Dummy
|
6
|
+
def to_s
|
7
|
+
"dummy"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
before(:all) do
|
12
|
+
@file = "_10lines_"
|
13
|
+
end
|
14
|
+
|
15
|
+
after(:all) do
|
16
|
+
File.unlink(@file)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_f_LSHIFT # '<<'
|
20
|
+
Bzip2::Writer.open($file, "w") do |file|
|
21
|
+
file << 1 << "\n" << Dummy.new << "\n" << "cat\n"
|
22
|
+
end
|
23
|
+
expected = [ "1\n", "dummy\n", "cat\n"]
|
24
|
+
Bzip2::Reader.foreach($file) do |line|
|
25
|
+
expected.shift.should == line
|
26
|
+
end
|
27
|
+
[].should == expected
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_f_error
|
31
|
+
io = File.new($file, "w")
|
32
|
+
bz2 = Bzip2::Writer.new(io)
|
33
|
+
bz2 << 1 << "\n" << Dummy.new << "\n" << "cat\n"
|
34
|
+
bz = Bzip2::Reader.new($file)
|
35
|
+
lambda { bz.gets }.should raise_error(Bzip2::EOZError)
|
36
|
+
bz = Bzip2::Reader.open($file)
|
37
|
+
lambda { bz.gets }.should raise_error(Bzip2::EOZError)
|
38
|
+
io.close
|
39
|
+
lambda { Bzip2::Reader.new(io) }.should raise_error(IOError)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_f_gets_para
|
43
|
+
Bzip2::Writer.open($file) do |file|
|
44
|
+
file.print "foo\n"*4096, "\n"*4096, "bar"*4096, "\n"*4096, "zot\n"*1024
|
45
|
+
end
|
46
|
+
Bzip2::Reader.open($file) do |file|
|
47
|
+
("foo\n"*4096+"\n").should == file.gets("")
|
48
|
+
("bar"*4096+"\n\n").should == file.gets("")
|
49
|
+
("zot\n"*1024).should == file.gets("")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_f_print
|
54
|
+
Bzip2::Writer.open($file) do |file|
|
55
|
+
file.print "hello"
|
56
|
+
file.print 1,2
|
57
|
+
$_ = "wombat\n"
|
58
|
+
file.print
|
59
|
+
$\ = ":"
|
60
|
+
$, = ","
|
61
|
+
file.print 3, 4
|
62
|
+
file.print 5, 6
|
63
|
+
$\ = nil
|
64
|
+
file.print "\n"
|
65
|
+
$, = nil
|
66
|
+
end
|
67
|
+
|
68
|
+
Bzip2::Reader.open($file) do |file|
|
69
|
+
content = file.gets(nil)
|
70
|
+
"hello12wombat\n3,4:5,6:\n".should == content
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_f_putc
|
75
|
+
Bzip2::Writer.open($file, "wb") do |file|
|
76
|
+
file.putc "A"
|
77
|
+
0.upto(255) { |ch| file.putc ch }
|
78
|
+
end
|
79
|
+
|
80
|
+
Bzip2::Reader.open($file, "rb") do |file|
|
81
|
+
?A.should == file.getc
|
82
|
+
0.upto(255) { |ch| ch.should == file.getc }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_f_puts
|
87
|
+
Bzip2::Writer.open($file, "w") do |file|
|
88
|
+
file.puts "line 1", "line 2"
|
89
|
+
file.puts [ Dummy.new, 4 ]
|
90
|
+
end
|
91
|
+
|
92
|
+
Bzip2::Reader.open($file) do |file|
|
93
|
+
"line 1\n".should == file.gets
|
94
|
+
"line 2\n".should == file.gets
|
95
|
+
"dummy\n".should == file.gets
|
96
|
+
"4\n".should == file.gets
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_f_write
|
101
|
+
Bzip2::Writer.open($file, "w") do |file|
|
102
|
+
10.should == file.write('*' * 10)
|
103
|
+
5.should == file.write('!' * 5)
|
104
|
+
0.should == file.write('')
|
105
|
+
1.should == file.write(1)
|
106
|
+
3.should == file.write(2.30000)
|
107
|
+
1.should == file.write("\n")
|
108
|
+
end
|
109
|
+
|
110
|
+
Bzip2::Reader.open($file) do |file|
|
111
|
+
"**********!!!!!12.3\n".should == file.gets
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_s_string
|
116
|
+
file = Bzip2::Writer.new
|
117
|
+
10.should == file.write('*' * 10)
|
118
|
+
5.should == file.write('!' * 5)
|
119
|
+
0.should == file.write('')
|
120
|
+
1.should == file.write(1)
|
121
|
+
3.should == file.write(2.30000)
|
122
|
+
1.should == file.write("\n")
|
123
|
+
line = Bzip2::bunzip2(file.flush)
|
124
|
+
"**********!!!!!12.3\n".should == line
|
125
|
+
|
126
|
+
line = Bzip2::bunzip2(Bzip2::bzip2("**********!!!!!12.3\n"))
|
127
|
+
"**********!!!!!12.3\n".should == line
|
128
|
+
|
129
|
+
test = "foo\n"*4096 + "\n"*4096 + "bar"*4096 + "\n"*4096 + "zot\n"*1024
|
130
|
+
line = Bzip2::bunzip2(Bzip2::bzip2(test))
|
131
|
+
test.should == line
|
132
|
+
|
133
|
+
Bzip2::bzip2("aaaaaaaaa".taint).tainted?.should be_true
|
134
|
+
Bzip2::bunzip2(Bzip2::bzip2("aaaaaaaaa".taint)).tainted?.should be_true
|
135
|
+
(!Bzip2::bzip2("aaaaaaaaa").tainted?).should be_false
|
136
|
+
(!Bzip2::bunzip2(Bzip2::bzip2("aaaaaaaaa")).tainted?).should be_false
|
137
|
+
|
138
|
+
end
|
139
|
+
end
|