diff options
| author | nahi <nahi@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-01-13 13:09:11 +0000 |
|---|---|---|
| committer | nahi <nahi@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-01-13 13:09:11 +0000 |
| commit | 1a3abb627a47615fc6526dfbcc52f3f7fde5194d (patch) | |
| tree | 5c0bb42a3f275d01b295c9470b15973cfa97935c | |
| parent | a0006c109c518b3bd395d33d458e50808b84e87f (diff) | |
* test/zlib/test_zlib.rb: backport tests for 1.9
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@26317 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
| -rw-r--r-- | ChangeLog | 4 | ||||
| -rw-r--r-- | test/zlib/test_zlib.rb | 258 |
2 files changed, 254 insertions, 8 deletions
@@ -1,3 +1,7 @@ +Wed Jan 13 22:07:47 2010 NAKAMURA, Hiroshi <nahi@ruby-lang.org> + + * test/zlib/test_zlib.rb: backport tests for 1.9 + Wed Jan 13 06:54:44 2010 Nobuyoshi Nakada <nobu@ruby-lang.org> * configure.in: check for if struct timezone is defined. diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index 54706099d9..0b66151a58 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -1,6 +1,6 @@ -require 'test/unit/testsuite' -require 'test/unit/testcase' +require 'test/unit' require 'stringio' +require 'tempfile' begin require 'zlib' @@ -42,16 +42,258 @@ if defined? Zlib r.close } end + + def test_open + t = Tempfile.new("test_zlib_gzip_reader") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } + + assert_raise(ArgumentError) { Zlib::GzipReader.open } + + assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read }) + + f = Zlib::GzipReader.open(t.path) + assert_equal("foo", f.read) + f.close + end + + def test_rewind + t = Tempfile.new("test_zlib_gzip_reader") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } + + f = Zlib::GzipReader.open(t.path) + assert_equal("foo", f.read) + f.rewind + assert_equal("foo", f.read) + f.close + end + + def test_unused + t = Tempfile.new("test_zlib_gzip_reader") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") } + + f = Zlib::GzipReader.open(t.path) + assert_equal("foo", f.read(3)) + f.unused + assert_equal("bar", f.read) + f.unused + f.close + end + + def test_read + t = Tempfile.new("test_zlib_gzip_reader") + t.close + str = "\u3042\u3044\u3046" + Zlib::GzipWriter.open(t.path) {|gz| gz.print(str) } + + f = Zlib::GzipReader.open(t.path) + assert_raise(ArgumentError) { f.read(-1) } + assert_equal(str, f.read) + end + + def test_readpartial + t = Tempfile.new("test_zlib_gzip_reader") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") } + + f = Zlib::GzipReader.open(t.path) + if f.respond_to?(:readpartial) + assert("foo".start_with?(f.readpartial(3))) + + f = Zlib::GzipReader.open(t.path) + s = "" + f.readpartial(3, s) + assert("foo".start_with?(s)) + + assert_raise(ArgumentError) { f.readpartial(-1) } + end + end + + def test_getc + t = Tempfile.new("test_zlib_gzip_reader") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") } + + f = Zlib::GzipReader.open(t.path) + # .chr should not be needed in the future. + # f.getc of 1.9 returns "f" instead of 102. + "foobar".each_char {|c| assert_equal(c, f.getc.chr) } + assert_nil(f.getc) + end + + def test_getbyte + t = Tempfile.new("test_zlib_gzip_reader") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") } + + f = Zlib::GzipReader.open(t.path) + if f.respond_to?(:getbyte) + "foobar".each_byte {|c| assert_equal(c, f.getbyte) } + assert_nil(f.getbyte) + end + end + + def test_readchar + t = Tempfile.new("test_zlib_gzip_reader") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") } + + f = Zlib::GzipReader.open(t.path) + "foobar".each_byte {|c| assert_equal(c, f.readchar.ord) } + assert_raise(EOFError) { f.readchar } + end + + def test_each_byte + t = Tempfile.new("test_zlib_gzip_reader") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") } + + f = Zlib::GzipReader.open(t.path) + a = [] + f.each_byte {|c| a << c } + assert_equal("foobar".each_byte.to_a, a) + end + + def test_gets + t = Tempfile.new("test_zlib_gzip_reader") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") } + + f = Zlib::GzipReader.open(t.path) + assert_equal("foo\n", f.gets) + assert_equal("bar\n", f.gets) + assert_equal("baz\n", f.gets) + assert_nil(f.gets) + f.close + + f = Zlib::GzipReader.open(t.path) + assert_equal("foo\nbar\nbaz\n", f.gets(nil)) + f.close + end + + def test_gets + t = Tempfile.new("test_zlib_gzip_reader") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") } + + f = Zlib::GzipReader.open(t.path) + assert_equal("foo\n", f.readline) + assert_equal("bar\n", f.readline) + assert_equal("baz\n", f.readline) + assert_raise(EOFError) { f.readline } + f.close + end + + def test_each + t = Tempfile.new("test_zlib_gzip_reader") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") } + + f = Zlib::GzipReader.open(t.path) + a = ["foo\n", "bar\n", "baz\n"] + f.each {|l| assert_equal(a.shift, l) } + f.close + end + + def test_readlines + t = Tempfile.new("test_zlib_gzip_reader") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") } + + f = Zlib::GzipReader.open(t.path) + assert_equal(["foo\n", "bar\n", "baz\n"], f.readlines) + f.close + end + + def test_reader_wrap + t = Tempfile.new("test_zlib_gzip_reader") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } + f = open(t.path) + assert_equal("foo", Zlib::GzipReader.wrap(f) {|gz| gz.read }) + assert_raise(IOError) { f.close } + end end class TestZlibGzipWriter < Test::Unit::TestCase def test_invalid_new - # [ruby-dev:23228] - assert_raise(NoMethodError) { Zlib::GzipWriter.new(nil).close } - # [ruby-dev:23344] - assert_raise(NoMethodError) { Zlib::GzipWriter.new(true).close } - assert_raise(NoMethodError) { Zlib::GzipWriter.new(0).close } - assert_raise(NoMethodError) { Zlib::GzipWriter.new(:hoge).close } + assert_raise(NoMethodError, "[ruby-dev:23228]") { Zlib::GzipWriter.new(nil).close } + assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(true).close } + assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(0).close } + assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(:hoge).close } + end + + def test_open + assert_raise(ArgumentError) { Zlib::GzipWriter.open } + + t = Tempfile.new("test_zlib_gzip_writer") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } + assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read }) + + f = Zlib::GzipWriter.open(t.path) + f.print("bar") + f.close + assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read }) + + assert_raise(Zlib::StreamError) { Zlib::GzipWriter.open(t.path, 10000) } + end + + def test_write + t = Tempfile.new("test_zlib_gzip_writer") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } + assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read }) + + o = Object.new + def o.to_s; "bar"; end + Zlib::GzipWriter.open(t.path) {|gz| gz.print(o) } + assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read }) + end + + def test_putc + t = Tempfile.new("test_zlib_gzip_writer") + t.close + Zlib::GzipWriter.open(t.path) {|gz| gz.putc(?x) } + assert_equal("x", Zlib::GzipReader.open(t.path) {|gz| gz.read }) + + # todo: multibyte char + end + + def test_writer_wrap + t = Tempfile.new("test_zlib_gzip_writer") + Zlib::GzipWriter.wrap(t) {|gz| gz.print("foo") } + t.close + assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read }) + end + end + + class TestZlib < Test::Unit::TestCase + def test_version + assert_instance_of(String, Zlib.zlib_version) + assert(Zlib.zlib_version.tainted?) + end + + def test_adler32 + assert_equal(0x00000001, Zlib.adler32) + assert_equal(0x02820145, Zlib.adler32("foo")) + assert_equal(0x02820145, Zlib.adler32("o", Zlib.adler32("fo"))) + assert_equal(0x8a62c964, Zlib.adler32("abc\x01\x02\x03" * 10000)) + end + + def test_crc32 + assert_equal(0x00000000, Zlib.crc32) + assert_equal(0x8c736521, Zlib.crc32("foo")) + assert_equal(0x8c736521, Zlib.crc32("o", Zlib.crc32("fo"))) + assert_equal(0x07f0d68f, Zlib.crc32("abc\x01\x02\x03" * 10000)) + end + + def test_crc_table + t = Zlib.crc_table + assert_instance_of(Array, t) + t.each {|x| assert_kind_of(Integer, x) } end end end |
