bzip2-ruby 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+ require 'mkmf'
3
+ dir_config('bz2')
4
+ have_header('bzlib.h')
5
+
6
+ if have_library("bz2", "BZ2_bzWriteOpen")
7
+ if enable_config("shared", true)
8
+ $static = nil
9
+ end
10
+ create_makefile("bzip2")
11
+ else
12
+ puts "libbz2 not found, maybe try manually specifying --with-bz2-dir to find it?"
13
+ end
@@ -0,0 +1,13 @@
1
+ namespace :extconf do
2
+ desc "Compiles the Ruby extension"
3
+ task :compile
4
+ end
5
+
6
+ task :compile => "extconf:compile"
7
+
8
+ task :test => :compile
9
+
10
+ BIN = "*.{bundle,jar,so,obj,pdb,lib,def,exp}"
11
+ $hoe.clean_globs |= ["ext/**/#{BIN}", "lib/**/#{BIN}", 'ext/**/Makefile']
12
+ $hoe.spec.require_paths = Dir['{lib,ext/*}']
13
+ $hoe.spec.extensions = FileList["ext/**/extconf.rb"].to_a
@@ -0,0 +1,43 @@
1
+ namespace :extconf do
2
+ extension = File.basename(__FILE__, '.rake')
3
+
4
+ ext = "ext/#{extension}"
5
+ ext_so = "#{ext}/#{extension}.#{Config::CONFIG['DLEXT']}"
6
+ ext_files = FileList[
7
+ "#{ext}/*.c",
8
+ "#{ext}/*.h",
9
+ "#{ext}/*.rl",
10
+ "#{ext}/extconf.rb",
11
+ "#{ext}/Makefile",
12
+ # "lib"
13
+ ]
14
+
15
+
16
+ task :compile => extension do
17
+ if Dir.glob("**/#{extension}.{o,so,dll}").length == 0
18
+ STDERR.puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
19
+ STDERR.puts "Gem actually failed to build. Your system is"
20
+ STDERR.puts "NOT configured properly to build #{GEM_NAME}."
21
+ STDERR.puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
22
+ exit(1)
23
+ end
24
+ end
25
+
26
+ desc "Builds just the #{extension} extension"
27
+ task extension.to_sym => ["#{ext}/Makefile", ext_so ]
28
+
29
+ file "#{ext}/Makefile" => ["#{ext}/extconf.rb"] do
30
+ Dir.chdir(ext) do ruby "extconf.rb" end
31
+ end
32
+
33
+ file ext_so => ext_files do
34
+ Dir.chdir(ext) do
35
+ sh(PLATFORM =~ /win32/ ? 'nmake' : 'make') do |ok, res|
36
+ if !ok
37
+ require "fileutils"
38
+ FileUtils.rm Dir.glob('*.{so,o,dll,bundle}')
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,369 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # This is the test from rubicon
4
+
5
+ $LOAD_PATH.unshift(*%w{.. tests})
6
+ require 'bz2'
7
+ require 'runit_'
8
+
9
+ Inh = defined?(RUNIT) ? RUNIT : Test::Unit
10
+
11
+ $file = "_10lines_"
12
+ $data = [
13
+ "00: This is a line\n",
14
+ "01: This is a line\n",
15
+ "02: This is a line\n",
16
+ "03: This is a line\n",
17
+ "04: This is a line\n",
18
+ "05: This is a line\n",
19
+ "06: This is a line\n",
20
+ "07: This is a line\n",
21
+ "08: This is a line\n",
22
+ "09: This is a line\n"
23
+ ]
24
+
25
+ open("|bzip2 > #$file", "w") do |f|
26
+ $data.each { |l| f.puts l }
27
+ end
28
+
29
+ class TestReader < Inh::TestCase
30
+
31
+ SAMPLE = "08: This is a line\n"
32
+
33
+ def test_f_s_foreach
34
+ count = 0
35
+ BZ2::Reader.foreach($file) do |line|
36
+ num = line[0..1].to_i
37
+ assert_equal(count, num)
38
+ count += 1
39
+ end
40
+ assert_equal($data.size, count)
41
+
42
+ count = 0
43
+ BZ2::Reader.foreach($file, nil) do |file|
44
+ file.split(/\n/).each do |line|
45
+ num = line[0..1].to_i
46
+ assert_equal(count, num)
47
+ count += 1
48
+ end
49
+ end
50
+ assert_equal($data.size, count)
51
+
52
+ count = 0
53
+ BZ2::Reader.foreach($file, ' ') do |thing|
54
+ count += 1
55
+ end
56
+ assert_equal(41, count)
57
+ end
58
+
59
+ def test_f_s_readlines
60
+ lines = BZ2::Reader.readlines($file)
61
+ assert_equal($data.size, lines.size)
62
+
63
+ lines = BZ2::Reader.readlines($file, nil)
64
+ assert_equal(1, lines.size)
65
+ assert_equal(SAMPLE.length * $data.size, lines[0].size)
66
+ end
67
+
68
+ def test_f_closed?
69
+ f = BZ2::Reader.open($file)
70
+ assert(!f.closed?)
71
+ f.close
72
+ assert(f.closed?)
73
+ end
74
+
75
+ def test_f_each
76
+ count = 0
77
+ BZ2::Reader.open($file) do |file|
78
+ file.each do |line|
79
+ num = line[0..1].to_i
80
+ assert_equal(count, num)
81
+ count += 1
82
+ end
83
+ assert_equal($data.size, count)
84
+ end
85
+
86
+ count = 0
87
+ BZ2::Reader.open($file) do |file|
88
+ file.each(nil) do |contents|
89
+ contents.split(/\n/).each do |line|
90
+ num = line[0..1].to_i
91
+ assert_equal(count, num)
92
+ count += 1
93
+ end
94
+ end
95
+ end
96
+ assert_equal($data.size, count)
97
+
98
+ count = 0
99
+ BZ2::Reader.open($file) do |file|
100
+ file.each(' ') do |thing|
101
+ count += 1
102
+ end
103
+ end
104
+ assert_equal(41, count)
105
+ end
106
+
107
+ def test_f_each_byte
108
+ count = 0
109
+ data = $data.join
110
+
111
+ BZ2::Reader.open($file) do |file|
112
+ file.each_byte do |b|
113
+ assert_equal(data[count], b)
114
+ count += 1
115
+ end
116
+ end
117
+ assert_equal(SAMPLE.length * $data.size, count)
118
+ end
119
+
120
+ def test_f_each_line
121
+ count = 0
122
+ BZ2::Reader.open($file) do |file|
123
+ file.each_line do |line|
124
+ num = line[0..1].to_i
125
+ assert_equal(count, num)
126
+ count += 1
127
+ end
128
+ assert_equal($data.size, count)
129
+ end
130
+
131
+ count = 0
132
+ BZ2::Reader.open($file) do |file|
133
+ file.each_line(nil) do |contents|
134
+ contents.split(/\n/).each do |line|
135
+ num = line[0..1].to_i
136
+ assert_equal(count, num)
137
+ count += 1
138
+ end
139
+ end
140
+ end
141
+ assert_equal($data.size, count)
142
+
143
+ count = 0
144
+ BZ2::Reader.open($file) do |file|
145
+ file.each_line(' ') do |thing|
146
+ count += 1
147
+ end
148
+ end
149
+ assert_equal(41, count)
150
+ end
151
+
152
+ def test_f_eof
153
+ BZ2::Reader.open($file) do |file|
154
+ $data.size.times do
155
+ assert(!file.eof)
156
+ assert(!file.eof?)
157
+ file.gets
158
+ end
159
+ assert(file.eof)
160
+ assert(file.eof?)
161
+ end
162
+ end
163
+
164
+ def test_f_getc
165
+ count = 0
166
+ data = $data.join
167
+
168
+ BZ2::Reader.open($file) do |file|
169
+ while (ch = file.getc)
170
+ assert_equal(data[count], ch)
171
+ count += 1
172
+ end
173
+ assert_equal(nil, file.getc)
174
+ end
175
+ assert_equal(SAMPLE.length * $data.size, count)
176
+ end
177
+
178
+ def test_f_gets
179
+ count = 0
180
+ BZ2::Reader.open($file) do |file|
181
+ while (line = file.gets)
182
+ num = line[0..1].to_i
183
+ assert_equal(count, num)
184
+ count += 1
185
+ end
186
+ assert_equal(nil, file.gets)
187
+ assert_equal($data.size, count)
188
+ end
189
+
190
+ count = 0
191
+ BZ2::Reader.open($file) do |file|
192
+ while (line = file.gets("line\n"))
193
+ assert_equal($data[count], line)
194
+ num = line[0..1].to_i
195
+ assert_equal(count, num)
196
+ count += 1
197
+ end
198
+ assert_equal(nil, file.gets)
199
+ assert_equal($data.size, count)
200
+ end
201
+
202
+ count = 0
203
+ BZ2::Reader.open($file) do |file|
204
+ while (contents = file.gets(nil))
205
+ contents.split(/\n/).each do |line|
206
+ num = line[0..1].to_i
207
+ assert_equal(count, num)
208
+ count += 1
209
+ end
210
+ end
211
+ end
212
+ assert_equal($data.size, count)
213
+
214
+ count = 0
215
+ BZ2::Reader.open($file) do |file|
216
+ while (thing = file.gets(' '))
217
+ count += 1
218
+ end
219
+ end
220
+ assert_equal(41, count)
221
+ end
222
+
223
+ def test_f_read
224
+ BZ2::Reader.open($file) do |file|
225
+ content = file.read
226
+ assert_equal(SAMPLE.length * $data.size, content.length)
227
+ count = 0
228
+ content.split(/\n/).each do |line|
229
+ num = line[0..1].to_i
230
+ assert_equal(count, num)
231
+ count += 1
232
+ end
233
+ end
234
+
235
+ BZ2::Reader.open($file) do |file|
236
+ assert_equal("00: This is ", file.read(12))
237
+ assert_equal("a line\n01: T", file.read(12))
238
+ end
239
+ end
240
+
241
+ def test_f_readchar
242
+ count = 0
243
+ data = $data.join
244
+ BZ2::Reader.open($file) do |file|
245
+ 190.times do |count|
246
+ ch = file.readchar
247
+ assert_equal(data[count], ch)
248
+ count += 1
249
+ end
250
+ assert_raises(BZ2::EOZError) { file.readchar }
251
+ end
252
+ end
253
+
254
+ def test_f_readline
255
+ count = 0
256
+ BZ2::Reader.open($file) do |file|
257
+ $data.size.times do |count|
258
+ line = file.readline
259
+ num = line[0..1].to_i
260
+ assert_equal(count, num)
261
+ count += 1
262
+ end
263
+ assert_raises(BZ2::EOZError) { file.readline }
264
+ end
265
+
266
+ count = 0
267
+ BZ2::Reader.open($file) do |file|
268
+ contents = file.readline(nil)
269
+ contents.split(/\n/).each do |line|
270
+ num = line[0..1].to_i
271
+ assert_equal(count, num)
272
+ count += 1
273
+ end
274
+ assert_raises(BZ2::EOZError) { file.readline }
275
+ end
276
+ assert_equal($data.size, count)
277
+
278
+ count = 0
279
+ BZ2::Reader.open($file) do |file|
280
+ 41.times do |count|
281
+ thing = file.readline(' ')
282
+ count += 1
283
+ end
284
+ assert_raises(BZ2::EOZError) { file.readline }
285
+ end
286
+ end
287
+
288
+ def test_f_readlines
289
+ BZ2::Reader.open($file) do |file|
290
+ lines = file.readlines
291
+ assert_equal($data.size, lines.size)
292
+ end
293
+
294
+ BZ2::Reader.open($file) do |file|
295
+ lines = file.readlines(nil)
296
+ assert_equal(1, lines.size)
297
+ assert_equal(SAMPLE.length * $data.size, lines[0].size)
298
+ end
299
+ end
300
+
301
+ def test_f_ungetc
302
+ BZ2::Reader.open($file) do |file|
303
+ assert_equal(?0, file.getc)
304
+ assert_equal(?0, file.getc)
305
+ assert_equal(?:, file.getc)
306
+ assert_equal(?\s, file.getc)
307
+ assert_nil(file.ungetc(?:))
308
+ assert_equal(?:, file.getc)
309
+ 1 while file.getc
310
+ assert_nil(file.ungetc(?A))
311
+ assert_equal(?A, file.getc)
312
+ end
313
+ end
314
+
315
+ def test_f_ungets
316
+ count = 0
317
+ BZ2::Reader.open($file) do |file|
318
+ assert_equal($data[count], file.gets)
319
+ assert_equal(count + 1, file.lineno);
320
+ assert_nil(file.ungets($data[count]))
321
+ assert_equal($data[count], file.gets)
322
+ count += 1
323
+ end
324
+ end
325
+
326
+ def test_s_readline
327
+ count = 0
328
+ string = IO.readlines($file, nil)[0]
329
+ file = BZ2::Reader.new(string)
330
+ $data.size.times do |count|
331
+ line = file.readline
332
+ num = line[0..1].to_i
333
+ assert_equal(count, num)
334
+ count += 1
335
+ end
336
+ assert_raises(BZ2::EOZError) { file.readline }
337
+ file.close
338
+
339
+ count = 0
340
+ file = BZ2::Reader.new(string)
341
+ contents = file.readline(nil)
342
+ contents.split(/\n/).each do |line|
343
+ num = line[0..1].to_i
344
+ assert_equal(count, num)
345
+ count += 1
346
+ end
347
+ assert_raises(BZ2::EOZError) { file.readline }
348
+ assert_equal($data.size, count)
349
+ file.close
350
+
351
+ count = 0
352
+ file = BZ2::Reader.new(string)
353
+ 41.times do |count|
354
+ thing = file.readline(' ')
355
+ count += 1
356
+ end
357
+ assert_raises(BZ2::EOZError) { file.readline }
358
+ file.close
359
+ end
360
+
361
+ def test_zzz
362
+ File.unlink($file)
363
+ end
364
+ end
365
+
366
+
367
+ if defined?(RUNIT)
368
+ RUNIT::CUI::TestRunner.run(TestReader.suite)
369
+ end
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'test/unit'
3
+ rescue LoadError
4
+ require 'runit/testcase'
5
+ require 'runit/cui/testrunner'
6
+
7
+ module RUNIT
8
+ module Assert
9
+ def assert_raises(error, message = nil)
10
+ begin
11
+ yield
12
+ rescue error
13
+ assert(true, message)
14
+ rescue
15
+ assert_fail("must fail with #{error} : #{string}")
16
+ else
17
+ assert_fail("*must* fail : #{string}")
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+
25
+ if RUBY_VERSION > "1.7"
26
+ class Array
27
+ alias indices select
28
+ end
29
+ class Hash
30
+ alias indexes select
31
+ end
32
+ end