~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/zlib/test_zlib.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
233
233
 
234
234
  class TestZlibGzipFile < Test::Unit::TestCase
235
235
    def test_to_io
236
 
      t = Tempfile.new("test_zlib_gzip_file")
 
236
      t = Tempfile.new("test_zlib_gzip_file_to_io")
237
237
      t.close
238
238
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
239
239
 
240
 
      f = Zlib::GzipReader.open(t.path)
241
 
      assert_kind_of(IO, f.to_io)
 
240
      Zlib::GzipReader.open(t.path) do |f|
 
241
        assert_kind_of(IO, f.to_io)
 
242
      end
242
243
    end
243
244
 
244
245
    def test_crc
245
 
      t = Tempfile.new("test_zlib_gzip_file")
 
246
      t = Tempfile.new("test_zlib_gzip_file_crc")
246
247
      t.close
247
248
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
248
249
 
249
 
      f = Zlib::GzipReader.open(t.path)
250
 
      f.read
251
 
      assert_equal(0x8c736521, f.crc)
 
250
      Zlib::GzipReader.open(t.path) do |f|
 
251
        f.read
 
252
        assert_equal(0x8c736521, f.crc)
 
253
      end
252
254
    end
253
255
 
254
256
    def test_mtime
255
257
      tim = Time.now
256
258
 
257
 
      t = Tempfile.new("test_zlib_gzip_file")
 
259
      t = Tempfile.new("test_zlib_gzip_file_mtime")
258
260
      t.close
259
261
      Zlib::GzipWriter.open(t.path) do |gz|
260
262
        gz.mtime = -1
264
266
        assert_raise(Zlib::GzipFile::Error) { gz.mtime = Time.now }
265
267
      end
266
268
 
267
 
      f = Zlib::GzipReader.open(t.path)
268
 
      assert_equal(tim.to_i, f.mtime.to_i)
 
269
      Zlib::GzipReader.open(t.path) do |f|
 
270
        assert_equal(tim.to_i, f.mtime.to_i)
 
271
      end
269
272
    end
270
273
 
271
274
    def test_level
272
 
      t = Tempfile.new("test_zlib_gzip_file")
 
275
      t = Tempfile.new("test_zlib_gzip_file_level")
273
276
      t.close
274
277
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
275
278
 
276
 
      f = Zlib::GzipReader.open(t.path)
277
 
      assert_equal(Zlib::DEFAULT_COMPRESSION, f.level)
 
279
      Zlib::GzipReader.open(t.path) do |f|
 
280
        assert_equal(Zlib::DEFAULT_COMPRESSION, f.level)
 
281
      end
278
282
    end
279
283
 
280
284
    def test_os_code
281
 
      t = Tempfile.new("test_zlib_gzip_file")
 
285
      t = Tempfile.new("test_zlib_gzip_file_os_code")
282
286
      t.close
283
287
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
284
288
 
285
 
      f = Zlib::GzipReader.open(t.path)
286
 
      assert_equal(Zlib::OS_CODE, f.os_code)
 
289
      Zlib::GzipReader.open(t.path) do |f|
 
290
        assert_equal(Zlib::OS_CODE, f.os_code)
 
291
      end
287
292
    end
288
293
 
289
294
    def test_orig_name
290
 
      t = Tempfile.new("test_zlib_gzip_file")
 
295
      t = Tempfile.new("test_zlib_gzip_file_orig_name")
291
296
      t.close
292
297
      Zlib::GzipWriter.open(t.path) do |gz|
293
298
        gz.orig_name = "foobarbazqux\0quux"
296
301
        assert_raise(Zlib::GzipFile::Error) { gz.orig_name = "quux" }
297
302
      end
298
303
 
299
 
      f = Zlib::GzipReader.open(t.path)
300
 
      assert_equal("foobarbazqux", f.orig_name)
 
304
      Zlib::GzipReader.open(t.path) do |f|
 
305
        assert_equal("foobarbazqux", f.orig_name)
 
306
      end
301
307
    end
302
308
 
303
309
    def test_comment
304
 
      t = Tempfile.new("test_zlib_gzip_file")
 
310
      t = Tempfile.new("test_zlib_gzip_file_comment")
305
311
      t.close
306
312
      Zlib::GzipWriter.open(t.path) do |gz|
307
313
        gz.comment = "foobarbazqux\0quux"
310
316
        assert_raise(Zlib::GzipFile::Error) { gz.comment = "quux" }
311
317
      end
312
318
 
313
 
      f = Zlib::GzipReader.open(t.path)
314
 
      assert_equal("foobarbazqux", f.comment)
 
319
      Zlib::GzipReader.open(t.path) do |f|
 
320
        assert_equal("foobarbazqux", f.comment)
 
321
      end
315
322
    end
316
323
 
317
324
    def test_lineno
318
 
      t = Tempfile.new("test_zlib_gzip_file")
 
325
      t = Tempfile.new("test_zlib_gzip_file_lineno")
319
326
      t.close
320
327
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\nqux\n") }
321
328
 
322
 
      f = Zlib::GzipReader.open(t.path)
323
 
      assert_equal([0, "foo\n"], [f.lineno, f.gets])
324
 
      assert_equal([1, "bar\n"], [f.lineno, f.gets])
325
 
      f.lineno = 1000
326
 
      assert_equal([1000, "baz\n"], [f.lineno, f.gets])
327
 
      assert_equal([1001, "qux\n"], [f.lineno, f.gets])
 
329
      Zlib::GzipReader.open(t.path) do |f|
 
330
        assert_equal([0, "foo\n"], [f.lineno, f.gets])
 
331
        assert_equal([1, "bar\n"], [f.lineno, f.gets])
 
332
        f.lineno = 1000
 
333
        assert_equal([1000, "baz\n"], [f.lineno, f.gets])
 
334
        assert_equal([1001, "qux\n"], [f.lineno, f.gets])
 
335
      end
328
336
    end
329
337
 
330
338
    def test_closed_p
331
 
      t = Tempfile.new("test_zlib_gzip_file")
 
339
      t = Tempfile.new("test_zlib_gzip_file_closed_p")
332
340
      t.close
333
341
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
334
342
 
335
 
      f = Zlib::GzipReader.open(t.path)
336
 
      assert_equal(false, f.closed?)
337
 
      f.read
338
 
      assert_equal(false, f.closed?)
339
 
      f.close
340
 
      assert_equal(true, f.closed?)
 
343
      Zlib::GzipReader.open(t.path) do |f|
 
344
        assert_equal(false, f.closed?)
 
345
        f.read
 
346
        assert_equal(false, f.closed?)
 
347
        f.close
 
348
        assert_equal(true, f.closed?)
 
349
      end
341
350
    end
342
351
 
343
352
    def test_sync
344
 
      t = Tempfile.new("test_zlib_gzip_file")
 
353
      t = Tempfile.new("test_zlib_gzip_file_sync")
345
354
      t.close
346
355
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
347
356
 
348
 
      f = Zlib::GzipReader.open(t.path)
349
 
      f.sync = true
350
 
      assert_equal(true, f.sync)
351
 
      f.read
352
 
      f.sync = false
353
 
      assert_equal(false, f.sync)
354
 
      f.close
 
357
      Zlib::GzipReader.open(t.path) do |f|
 
358
        f.sync = true
 
359
        assert_equal(true, f.sync)
 
360
        f.read
 
361
        f.sync = false
 
362
        assert_equal(false, f.sync)
 
363
      end
355
364
    end
356
365
 
357
366
    def test_pos
358
 
      t = Tempfile.new("test_zlib_gzip_file")
 
367
      t = Tempfile.new("test_zlib_gzip_file_pos")
359
368
      t.close
360
369
      Zlib::GzipWriter.open(t.path) do |gz|
361
370
        gz.print("foo")
365
374
    end
366
375
 
367
376
    def test_path
368
 
      t = Tempfile.new("test_zlib_gzip_file")
 
377
      t = Tempfile.new("test_zlib_gzip_file_path")
369
378
      t.close
370
379
 
371
380
      gz = Zlib::GzipWriter.open(t.path)
374
383
      gz.close
375
384
      assert_equal(t.path, gz.path)
376
385
 
377
 
      f = Zlib::GzipReader.open(t.path)
378
 
      assert_equal(t.path, f.path)
379
 
      f.close
380
 
      assert_equal(t.path, f.path)
 
386
      Zlib::GzipReader.open(t.path) do |f|
 
387
        assert_equal(t.path, f.path)
 
388
        f.close
 
389
        assert_equal(t.path, f.path)
 
390
      end
381
391
 
382
392
      s = ""
383
393
      sio = StringIO.new(s)
387
397
      gz.close
388
398
 
389
399
      sio = StringIO.new(s)
390
 
      f = Zlib::GzipReader.new(sio)
391
 
      assert_raise(NoMethodError) { f.path }
392
 
      f.close
 
400
      Zlib::GzipReader.new(sio) do |f|
 
401
        assert_raise(NoMethodError) { f.path }
 
402
      end
393
403
    end
394
404
  end
395
405
 
429
439
    end
430
440
 
431
441
    def test_open
432
 
      t = Tempfile.new("test_zlib_gzip_reader")
 
442
      t = Tempfile.new("test_zlib_gzip_reader_open")
433
443
      t.close
 
444
      e = assert_raise(Zlib::GzipFile::Error) {
 
445
        Zlib::GzipReader.open(t.path)
 
446
      }
 
447
      assert_equal("not in gzip format", e.message)
 
448
      assert_nil(e.input)
 
449
      open(t.path, "wb") {|f| f.write("foo")}
 
450
      e = assert_raise(Zlib::GzipFile::Error) {
 
451
        Zlib::GzipReader.open(t.path)
 
452
      }
 
453
      assert_equal("not in gzip format", e.message)
 
454
      assert_equal("foo", e.input)
 
455
      open(t.path, "wb") {|f| f.write("foobarzothoge")}
 
456
      e = assert_raise(Zlib::GzipFile::Error) {
 
457
        Zlib::GzipReader.open(t.path)
 
458
      }
 
459
      assert_equal("not in gzip format", e.message)
 
460
      assert_equal("foobarzothoge", e.input)
 
461
 
434
462
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
435
463
 
436
464
      assert_raise(ArgumentError) { Zlib::GzipReader.open }
438
466
      assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
439
467
 
440
468
      f = Zlib::GzipReader.open(t.path)
441
 
      assert_equal("foo", f.read)
442
 
      f.close
 
469
      begin
 
470
        assert_equal("foo", f.read)
 
471
      ensure
 
472
        f.close
 
473
      end
443
474
    end
444
475
 
445
476
    def test_rewind
446
 
      t = Tempfile.new("test_zlib_gzip_reader")
 
477
      t = Tempfile.new("test_zlib_gzip_reader_rewind")
447
478
      t.close
448
479
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
449
480
 
450
 
      f = Zlib::GzipReader.open(t.path)
451
 
      assert_equal("foo", f.read)
452
 
      f.rewind
453
 
      assert_equal("foo", f.read)
454
 
      f.close
 
481
      Zlib::GzipReader.open(t.path) do |f|
 
482
        assert_equal("foo", f.read)
 
483
        f.rewind
 
484
        assert_equal("foo", f.read)
 
485
      end
455
486
    end
456
487
 
457
488
    def test_unused
458
 
      t = Tempfile.new("test_zlib_gzip_reader")
 
489
      t = Tempfile.new("test_zlib_gzip_reader_unused")
459
490
      t.close
460
491
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
461
492
 
496
527
    end
497
528
 
498
529
    def test_read
499
 
      t = Tempfile.new("test_zlib_gzip_reader")
 
530
      t = Tempfile.new("test_zlib_gzip_reader_read")
500
531
      t.close
501
532
      str = "\u3042\u3044\u3046"
502
533
      Zlib::GzipWriter.open(t.path) {|gz| gz.print(str) }
503
534
 
504
 
      f = Zlib::GzipReader.open(t.path, encoding: "UTF-8")
505
 
      assert_raise(ArgumentError) { f.read(-1) }
506
 
      assert_equal(str, f.read)
 
535
      Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
 
536
        assert_raise(ArgumentError) { f.read(-1) }
 
537
        assert_equal(str, f.read)
 
538
      end
507
539
    end
508
540
 
509
541
    def test_readpartial
510
 
      t = Tempfile.new("test_zlib_gzip_reader")
 
542
      t = Tempfile.new("test_zlib_gzip_reader_readpartial")
511
543
      t.close
512
544
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
513
545
 
514
 
      f = Zlib::GzipReader.open(t.path)
515
 
      assert("foo".start_with?(f.readpartial(3)))
516
 
 
517
 
      f = Zlib::GzipReader.open(t.path)
518
 
      s = ""
519
 
      f.readpartial(3, s)
520
 
      assert("foo".start_with?(s))
521
 
 
522
 
      assert_raise(ArgumentError) { f.readpartial(-1) }
 
546
      Zlib::GzipReader.open(t.path) do |f|
 
547
        assert("foo".start_with?(f.readpartial(3)))
 
548
      end
 
549
 
 
550
      Zlib::GzipReader.open(t.path) do |f|
 
551
        s = ""
 
552
        f.readpartial(3, s)
 
553
        assert("foo".start_with?(s))
 
554
 
 
555
        assert_raise(ArgumentError) { f.readpartial(-1) }
 
556
      end
523
557
    end
524
558
 
525
559
    def test_getc
526
 
      t = Tempfile.new("test_zlib_gzip_reader")
 
560
      t = Tempfile.new("test_zlib_gzip_reader_getc")
527
561
      t.close
528
562
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
529
563
 
530
 
      f = Zlib::GzipReader.open(t.path)
531
 
      "foobar".each_char {|c| assert_equal(c, f.getc) }
532
 
      assert_nil(f.getc)
 
564
      Zlib::GzipReader.open(t.path) do |f|
 
565
        "foobar".each_char {|c| assert_equal(c, f.getc) }
 
566
        assert_nil(f.getc)
 
567
      end
533
568
    end
534
569
 
535
570
    def test_getbyte
536
 
      t = Tempfile.new("test_zlib_gzip_reader")
 
571
      t = Tempfile.new("test_zlib_gzip_reader_getbyte")
537
572
      t.close
538
573
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
539
574
 
540
 
      f = Zlib::GzipReader.open(t.path)
541
 
      "foobar".each_byte {|c| assert_equal(c, f.getbyte) }
542
 
      assert_nil(f.getbyte)
 
575
      Zlib::GzipReader.open(t.path) do |f|
 
576
        "foobar".each_byte {|c| assert_equal(c, f.getbyte) }
 
577
        assert_nil(f.getbyte)
 
578
      end
543
579
    end
544
580
 
545
581
    def test_readchar
546
 
      t = Tempfile.new("test_zlib_gzip_reader")
 
582
      t = Tempfile.new("test_zlib_gzip_reader_readchar")
547
583
      t.close
548
584
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
549
585
 
550
 
      f = Zlib::GzipReader.open(t.path)
551
 
      "foobar".each_byte {|c| assert_equal(c, f.readchar.ord) }
552
 
      assert_raise(EOFError) { f.readchar }
 
586
      Zlib::GzipReader.open(t.path) do |f|
 
587
        "foobar".each_byte {|c| assert_equal(c, f.readchar.ord) }
 
588
        assert_raise(EOFError) { f.readchar }
 
589
      end
553
590
    end
554
591
 
555
592
    def test_each_byte
556
 
      t = Tempfile.new("test_zlib_gzip_reader")
 
593
      t = Tempfile.new("test_zlib_gzip_reader_each_byte")
557
594
      t.close
558
595
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
559
596
 
560
 
      f = Zlib::GzipReader.open(t.path)
561
 
      a = []
562
 
      f.each_byte {|c| a << c }
563
 
      assert_equal("foobar".each_byte.to_a, a)
 
597
      Zlib::GzipReader.open(t.path) do |f|
 
598
        a = []
 
599
        f.each_byte {|c| a << c }
 
600
        assert_equal("foobar".each_byte.to_a, a)
 
601
      end
 
602
    end
 
603
 
 
604
    def test_gets
 
605
      t = Tempfile.new("test_zlib_gzip_reader_gets")
 
606
      t.close
 
607
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
 
608
 
 
609
      Zlib::GzipReader.open(t.path) do |f|
 
610
        assert_equal("foo\n", f.gets)
 
611
        assert_equal("bar\n", f.gets)
 
612
        assert_equal("baz\n", f.gets)
 
613
        assert_nil(f.gets)
 
614
      end
 
615
 
 
616
      Zlib::GzipReader.open(t.path) do |f|
 
617
        assert_equal("foo\nbar\nbaz\n", f.gets(nil))
 
618
      end
 
619
 
 
620
      Zlib::GzipReader.open(t.path) do |f|
 
621
        assert_equal("foo\n", f.gets(10))
 
622
        assert_equal("ba", f.gets(2))
 
623
        assert_equal("r\nb", f.gets(nil, 3))
 
624
        assert_equal("az\n", f.gets(nil, 10))
 
625
        assert_nil(f.gets)
 
626
      end
564
627
    end
565
628
 
566
629
    def test_gets2
567
 
      t = Tempfile.new("test_zlib_gzip_reader")
 
630
      t = Tempfile.new("test_zlib_gzip_reader_gets2")
568
631
      t.close
569
 
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
570
 
 
571
 
      f = Zlib::GzipReader.open(t.path)
572
 
      assert_equal("foo\n", f.gets)
573
 
      assert_equal("bar\n", f.gets)
574
 
      assert_equal("baz\n", f.gets)
575
 
      assert_nil(f.gets)
576
 
      f.close
577
 
 
578
 
      f = Zlib::GzipReader.open(t.path)
579
 
      assert_equal("foo\nbar\nbaz\n", f.gets(nil))
580
 
      f.close
 
632
      ustrs = %W"\u{3042 3044 3046}\n \u{304b 304d 304f}\n \u{3055 3057 3059}\n"
 
633
      Zlib::GzipWriter.open(t.path) {|gz| gz.print(*ustrs) }
 
634
 
 
635
      Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
 
636
        assert_equal(ustrs[0], f.gets)
 
637
        assert_equal(ustrs[1], f.gets)
 
638
        assert_equal(ustrs[2], f.gets)
 
639
        assert_nil(f.gets)
 
640
      end
 
641
 
 
642
      Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
 
643
        assert_equal(ustrs.join(''), f.gets(nil))
 
644
      end
 
645
 
 
646
      Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
 
647
        assert_equal(ustrs[0], f.gets(20))
 
648
        assert_equal(ustrs[1][0,2], f.gets(5))
 
649
        assert_equal(ustrs[1][2..-1]+ustrs[2][0,1], f.gets(nil, 5))
 
650
        assert_equal(ustrs[2][1..-1], f.gets(nil, 20))
 
651
        assert_nil(f.gets)
 
652
      end
581
653
    end
582
654
 
583
 
    def test_gets
584
 
      t = Tempfile.new("test_zlib_gzip_reader")
 
655
    def test_readline
 
656
      t = Tempfile.new("test_zlib_gzip_reader_readline")
585
657
      t.close
586
658
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
587
659
 
588
 
      f = Zlib::GzipReader.open(t.path)
589
 
      assert_equal("foo\n", f.readline)
590
 
      assert_equal("bar\n", f.readline)
591
 
      assert_equal("baz\n", f.readline)
592
 
      assert_raise(EOFError) { f.readline }
593
 
      f.close
 
660
      Zlib::GzipReader.open(t.path) do |f|
 
661
        assert_equal("foo\n", f.readline)
 
662
        assert_equal("bar\n", f.readline)
 
663
        assert_equal("baz\n", f.readline)
 
664
        assert_raise(EOFError) { f.readline }
 
665
      end
594
666
    end
595
667
 
596
668
    def test_each
597
 
      t = Tempfile.new("test_zlib_gzip_reader")
 
669
      t = Tempfile.new("test_zlib_gzip_reader_each")
598
670
      t.close
599
671
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
600
672
 
601
 
      f = Zlib::GzipReader.open(t.path)
602
 
      a = ["foo\n", "bar\n", "baz\n"]
603
 
      f.each {|l| assert_equal(a.shift, l) }
604
 
      f.close
 
673
      Zlib::GzipReader.open(t.path) do |f|
 
674
        a = ["foo\n", "bar\n", "baz\n"]
 
675
        f.each {|l| assert_equal(a.shift, l) }
 
676
      end
605
677
    end
606
678
 
607
679
    def test_readlines
608
 
      t = Tempfile.new("test_zlib_gzip_reader")
 
680
      t = Tempfile.new("test_zlib_gzip_reader_readlines")
609
681
      t.close
610
682
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
611
683
 
612
 
      f = Zlib::GzipReader.open(t.path)
613
 
      assert_equal(["foo\n", "bar\n", "baz\n"], f.readlines)
614
 
      f.close
 
684
      Zlib::GzipReader.open(t.path) do |f|
 
685
        assert_equal(["foo\n", "bar\n", "baz\n"], f.readlines)
 
686
      end
615
687
    end
616
688
 
617
689
    def test_reader_wrap
618
 
      t = Tempfile.new("test_zlib_gzip_reader")
 
690
      t = Tempfile.new("test_zlib_gzip_reader_wrap")
619
691
      t.close
620
692
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
621
693
      f = open(t.path)
622
694
      assert_equal("foo", Zlib::GzipReader.wrap(f) {|gz| gz.read })
623
695
      assert_raise(IOError) { f.close }
624
696
    end
 
697
 
 
698
    def test_corrupted_header
 
699
      gz = Zlib::GzipWriter.new(StringIO.new(s = ""))
 
700
      gz.orig_name = "X"
 
701
      gz.comment = "Y"
 
702
      gz.print("foo")
 
703
      gz.finish
 
704
      # 14: magic(2) + method(1) + flag(1) + mtime(4) + exflag(1) + os(1) + orig_name(2) + comment(2)
 
705
      1.upto(14) do |idx|
 
706
        assert_raise(Zlib::GzipFile::Error, idx) do
 
707
          Zlib::GzipReader.new(StringIO.new(s[0, idx])).read
 
708
        end
 
709
      end
 
710
    end
625
711
  end
626
712
 
627
713
  class TestZlibGzipWriter < Test::Unit::TestCase
635
721
    def test_open
636
722
      assert_raise(ArgumentError) { Zlib::GzipWriter.open }
637
723
 
638
 
      t = Tempfile.new("test_zlib_gzip_writer")
 
724
      t = Tempfile.new("test_zlib_gzip_writer_open")
639
725
      t.close
640
726
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
641
727
      assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
642
728
 
643
729
      f = Zlib::GzipWriter.open(t.path)
644
 
      f.print("bar")
645
 
      f.close
 
730
      begin
 
731
        f.print("bar")
 
732
      ensure
 
733
        f.close
 
734
      end
646
735
      assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
647
736
 
648
737
      assert_raise(Zlib::StreamError) { Zlib::GzipWriter.open(t.path, 10000) }
649
738
    end
650
739
 
651
740
    def test_write
652
 
      t = Tempfile.new("test_zlib_gzip_writer")
 
741
      t = Tempfile.new("test_zlib_gzip_writer_write")
653
742
      t.close
654
743
      Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
655
744
      assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
661
750
    end
662
751
 
663
752
    def test_putc
664
 
      t = Tempfile.new("test_zlib_gzip_writer")
 
753
      t = Tempfile.new("test_zlib_gzip_writer_putc")
665
754
      t.close
666
755
      Zlib::GzipWriter.open(t.path) {|gz| gz.putc(?x) }
667
756
      assert_equal("x", Zlib::GzipReader.open(t.path) {|gz| gz.read })
670
759
    end
671
760
 
672
761
    def test_writer_wrap
673
 
      t = Tempfile.new("test_zlib_gzip_writer")
 
762
      t = Tempfile.new("test_zlib_gzip_writer_wrap")
674
763
      Zlib::GzipWriter.wrap(t) {|gz| gz.print("foo") }
675
764
      t.close
676
765
      assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })