From d4049fe593ae4465e7a258d138c2166571a0f1a7 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 1 Jun 2010 16:35:21 +0900 Subject: ruby: add test/test_cases.rb --- ruby/test/test_cases.rb | 46 ++++++++ ruby/test/test_helper.rb | 4 + ruby/test/test_pack_unpack.rb | 268 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 318 insertions(+) create mode 100644 ruby/test/test_cases.rb create mode 100644 ruby/test/test_pack_unpack.rb (limited to 'ruby/test') diff --git a/ruby/test/test_cases.rb b/ruby/test/test_cases.rb new file mode 100644 index 0000000..bfb752e --- /dev/null +++ b/ruby/test/test_cases.rb @@ -0,0 +1,46 @@ +#!/usr/bin/env ruby +here = File.dirname(__FILE__) +require "#{here}/test_helper" + +begin +require 'json' +rescue LoadError +require 'rubygems' +require 'json' +end + +CASES_PATH = "#{here}/cases.mpac" +CASES_COMPACT_PATH = "#{here}/cases_compact.mpac" +CASES_JSON_PATH = "#{here}/cases.json" + +class MessagePackTestCases < Test::Unit::TestCase + def feed_file(path) + pac = MessagePack::Unpacker.new + pac.feed File.read(path) + pac + end + + def test_compare_compact + pac = feed_file(CASES_PATH) + cpac = feed_file(CASES_COMPACT_PATH) + + objs = []; pac.each {| obj| objs << obj } + cobjs = []; cpac.each {|cobj| cobjs << cobj } + + objs.zip(cobjs).each {|obj, cobj| + assert_equal(obj, cobj) + } + end + + def test_compare_json + pac = feed_file(CASES_PATH) + + objs = []; pac.each {|obj| objs << obj } + jobjs = JSON.load File.read(CASES_JSON_PATH) + + objs.zip(jobjs) {|obj, jobj| + assert_equal(obj, jobj) + } + end +end + diff --git a/ruby/test/test_helper.rb b/ruby/test/test_helper.rb index 6a63489..19226ef 100644 --- a/ruby/test/test_helper.rb +++ b/ruby/test/test_helper.rb @@ -1,3 +1,7 @@ require 'test/unit' +begin +require File.dirname(__FILE__) + '/../msgpack' +rescue LoadError require File.dirname(__FILE__) + '/../lib/msgpack' +end diff --git a/ruby/test/test_pack_unpack.rb b/ruby/test/test_pack_unpack.rb new file mode 100644 index 0000000..e22bab3 --- /dev/null +++ b/ruby/test/test_pack_unpack.rb @@ -0,0 +1,268 @@ +#!/usr/bin/env ruby +require File.dirname(__FILE__)+'/test_helper' + +class MessagePackTestPackUnpack < Test::Unit::TestCase + def self.it(name, &block) + define_method("test_#{name}", &block) + end + + it "nil" do + check 1, nil + end + + it "true" do + check 1, true + end + + it "false" do + check 1, false + end + + it "zero" do + check 1, 0 + end + + it "positive fixnum" do + check 1, 1 + check 1, (1<<6) + check 1, (1<<7)-1 + end + + it "positive int 8" do + check 1, -1 + check 2, (1<<7) + check 2, (1<<8)-1 + end + + it "positive int 16" do + check 3, (1<<8) + check 3, (1<<16)-1 + end + + it "positive int 32" do + check 5, (1<<16) + check 5, (1<<32)-1 + end + + it "positive int 64" do + check 9, (1<<32) + check 9, (1<<64)-1 + end + + it "negative fixnum" do + check 1, -1 + check 1, -((1<<5)-1) + check 1, -(1<<5) + end + + it "negative int 8" do + check 2, -((1<<5)+1) + check 2, -(1<<7) + end + + it "negative int 16" do + check 3, -((1<<7)+1) + check 3, -(1<<15) + end + + it "negative int 32" do + check 5, -((1<<15)+1) + check 5, -(1<<31) + end + + it "negative int 64" do + check 9, -((1<<31)+1) + check 9, -(1<<63) + end + + it "double" do + check 9, 1.0 + check 9, 0.1 + check 9, -0.1 + check 9, -1.0 + end + + it "fixraw" do + check_raw 1, 0 + check_raw 1, (1<<5)-1 + end + + it "raw 16" do + check_raw 3, (1<<5) + check_raw 3, (1<<16)-1 + end + + it "raw 32" do + check_raw 5, (1<<16) + #check_raw 5, (1<<32)-1 # memory error + end + + it "fixarray" do + check_array 1, 0 + check_array 1, (1<<4)-1 + end + + it "array 16" do + check_array 3, (1<<4) + check_array 3, (1<<16)-1 + end + + it "array 32" do + check_array 5, (1<<16) + #check_array 5, (1<<32)-1 # memory error + end + + it "nil" do + match nil, "\xc0" + end + + it "false" do + match false, "\xc2" + end + + it "true" do + match true, "\xc3" + end + + it "0" do + match 0, "\x00" + end + + it "127" do + match 127, "\x7f" + end + + it "128" do + match 128, "\xcc\x80" + end + + it "256" do + match 256, "\xcd\x01\x00" + end + + it "-1" do + match -1, "\xff" + end + + it "-33" do + match -33, "\xd0\xdf" + end + + it "-129" do + match -129, "\xd1\xff\x7f" + end + + it "{1=>1}" do + match ({1=>1}), "\x81\x01\x01" + end + + it "1.0" do + match 1.0, "\xcb\x3f\xf0\x00\x00\x00\x00\x00\x00" + end + + it "[]" do + match [], "\x90" + end + + it "[0, 1, ..., 14]" do + match (0..14).to_a, "\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e" + end + + it "[0, 1, ..., 15]" do + match (0..15).to_a, "\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + end + + it "{}" do + match ({}), "\x80" + end + +## FIXME +# it "{0=>0, 1=>1, ..., 14=>14}" do +# a = (0..14).to_a; +# match Hash[*a.zip(a).flatten], "\x8f\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x04\x04\x0a\x0a" +# end +# +# it "{0=>0, 1=>1, ..., 15=>15}" do +# a = (0..15).to_a; +# match Hash[*a.zip(a).flatten], "\xde\x00\x10\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x0f\x0f\x04\x04\x0a\x0a" +# end + +## FIXME +# it "fixmap" do +# check_map 1, 0 +# check_map 1, (1<<4)-1 +# end +# +# it "map 16" do +# check_map 3, (1<<4) +# check_map 3, (1<<16)-1 +# end +# +# it "map 32" do +# check_map 5, (1<<16) +# #check_map 5, (1<<32)-1 # memory error +# end + + it "gc mark" do + obj = [{["a","b"]=>["c","d"]}, ["e","f"], "d"] + num = 4 + raw = obj.to_msgpack * num + pac = MessagePack::Unpacker.new + parsed = 0 + raw.split(//).each do |b| + pac.feed(b) + pac.each {|o| + GC.start + assert_equal(obj, o) + parsed += 1 + } + GC.start + end + assert_equal(parsed, num) + end + + it "streaming backward compatibility" do + obj = [{["a","b"]=>["c","d"]}, ["e","f"], "d"] + num = 4 + raw = obj.to_msgpack * num + pac = MessagePack::Unpacker.new + buffer = "" + nread = 0 + parsed = 0 + raw.split(//).each do |b| + buffer << b + nread = pac.execute(buffer, nread) + if pac.finished? + o = pac.data + assert_equal(obj, o) + parsed += 1 + pac.reset + buffer.slice!(0, nread) + nread = 0 + next unless buffer.empty? + end + end + assert_equal(parsed, num) + end + + private + def check(len, obj) + v = obj.to_msgpack + assert_equal(v.length, len) + assert_equal(MessagePack.unpack(v), obj) + end + + def check_raw(overhead, num) + check num+overhead, " "*num + end + + def check_array(overhead, num) + check num+overhead, Array.new(num) + end + + def match(obj, buf) + assert_equal(obj.to_msgpack, buf) + assert_equal(MessagePack::unpack(buf), obj) + end +end + -- cgit v1.2.1 From 251090406a642d968e314db3aa1d5240db00598a Mon Sep 17 00:00:00 2001 From: frsyuki Date: Thu, 3 Jun 2010 21:52:01 +0900 Subject: ruby: adds a test case for buffering --- ruby/test/test_pack_unpack.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'ruby/test') diff --git a/ruby/test/test_pack_unpack.rb b/ruby/test/test_pack_unpack.rb index e22bab3..9dff44f 100644 --- a/ruby/test/test_pack_unpack.rb +++ b/ruby/test/test_pack_unpack.rb @@ -203,6 +203,37 @@ class MessagePackTestPackUnpack < Test::Unit::TestCase # #check_map 5, (1<<32)-1 # memory error # end + it "buffer" do + str = "a"*32*1024*4 + raw = str.to_msgpack + pac = MessagePack::Unpacker.new + + len = 0 + parsed = false + + n = 655 + time = raw.size / n + time += 1 unless raw.size % n == 0 + off = 0 + + time.times do + assert(!parsed) + + fe = raw[off, n] + assert(fe.length > 0) + off += fe.length + + pac.feed fe + pac.each {|obj| + assert(!parsed) + assert_equal(obj, str) + parsed = true + } + end + + assert(parsed) + end + it "gc mark" do obj = [{["a","b"]=>["c","d"]}, ["e","f"], "d"] num = 4 -- cgit v1.2.1 From 34a29cd0a50eea4a0e008fe3947c86179d536540 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 29 Jun 2010 14:54:40 +0900 Subject: ruby: fixes SEGV problem caused by GC bug at MessagePack_Unpacker_mark. --- ruby/test/test_helper.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'ruby/test') diff --git a/ruby/test/test_helper.rb b/ruby/test/test_helper.rb index 19226ef..bf9fee8 100644 --- a/ruby/test/test_helper.rb +++ b/ruby/test/test_helper.rb @@ -5,3 +5,4 @@ rescue LoadError require File.dirname(__FILE__) + '/../lib/msgpack' end +GC.stress = true -- cgit v1.2.1 From 123ae024c6d5c217f18a9444c61b292145227278 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 29 Jun 2010 15:12:52 +0900 Subject: ruby: MessagePack::VERSION constant --- ruby/test/test_helper.rb | 2 +- ruby/test/test_pack_unpack.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'ruby/test') diff --git a/ruby/test/test_helper.rb b/ruby/test/test_helper.rb index bf9fee8..80d7806 100644 --- a/ruby/test/test_helper.rb +++ b/ruby/test/test_helper.rb @@ -5,4 +5,4 @@ rescue LoadError require File.dirname(__FILE__) + '/../lib/msgpack' end -GC.stress = true +#GC.stress = true diff --git a/ruby/test/test_pack_unpack.rb b/ruby/test/test_pack_unpack.rb index 9dff44f..25bde81 100644 --- a/ruby/test/test_pack_unpack.rb +++ b/ruby/test/test_pack_unpack.rb @@ -276,6 +276,10 @@ class MessagePackTestPackUnpack < Test::Unit::TestCase assert_equal(parsed, num) end + it "MessagePack::VERSION constant" do + p MessagePack::VERSION + end + private def check(len, obj) v = obj.to_msgpack -- cgit v1.2.1 From b5c78de2ddf82783a6f80a199b68927d1a1747ca Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 31 Aug 2010 06:30:16 +0900 Subject: ruby: converts encodings into UTF-8 on Ruby 1.9 --- ruby/test/test_encoding.rb | 68 ++++++++++++++++++++++++++++++++++++++++++++++ ruby/test/test_helper.rb | 4 ++- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 ruby/test/test_encoding.rb (limited to 'ruby/test') diff --git a/ruby/test/test_encoding.rb b/ruby/test/test_encoding.rb new file mode 100644 index 0000000..2cf0767 --- /dev/null +++ b/ruby/test/test_encoding.rb @@ -0,0 +1,68 @@ +#!/usr/bin/env ruby +require File.dirname(__FILE__)+'/test_helper' + +if RUBY_VERSION < "1.9" + exit +end + +class MessagePackTestEncoding < Test::Unit::TestCase + def self.it(name, &block) + define_method("test_#{name}", &block) + end + + it "US-ASCII" do + check_unpack "abc".force_encoding("US-ASCII") + end + + it "UTF-8 ascii" do + check_unpack "abc".force_encoding("UTF-8") + end + + it "UTF-8 mbstr" do + check_unpack "\xE3\x81\x82".force_encoding("UTF-8") + end + + it "UTF-8 invalid" do + check_unpack "\xD0".force_encoding("UTF-8") + end + + it "ASCII-8BIT" do + check_unpack "\xD0".force_encoding("ASCII-8BIT") + end + + it "EUC-JP" do + x = "\xA4\xA2".force_encoding("EUC-JP") + check_unpack(x) + end + + it "EUC-JP invalid" do + begin + "\xD0".force_encoding("EUC-JP").to_msgpack + assert(false) + rescue Encoding::InvalidByteSequenceError + assert(true) + end + end + + private + def check_unpack(str) + if str.encoding.to_s == "ASCII-8BIT" + should_str = str.dup.force_encoding("UTF-8") + else + should_str = str.encode("UTF-8") + end + + raw = str.to_msgpack + r = MessagePack.unpack(str.to_msgpack) + assert_equal(r.encoding.to_s, "UTF-8") + assert_equal(r, should_str.force_encoding("UTF-8")) + + if str.valid_encoding? + sym = str.to_sym + r = MessagePack.unpack(sym.to_msgpack) + assert_equal(r.encoding.to_s, "UTF-8") + assert_equal(r, should_str.force_encoding("UTF-8")) + end + end +end + diff --git a/ruby/test/test_helper.rb b/ruby/test/test_helper.rb index 80d7806..4def861 100644 --- a/ruby/test/test_helper.rb +++ b/ruby/test/test_helper.rb @@ -5,4 +5,6 @@ rescue LoadError require File.dirname(__FILE__) + '/../lib/msgpack' end -#GC.stress = true +if ENV["GC_STRESS"] + GC.stress = true +end -- cgit v1.2.1 From 09b47cc536ebd951c231fd5e09b4382a25b98020 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 31 Aug 2010 07:00:19 +0900 Subject: ruby: fixes compatibility with ruby-1.8.5 --- ruby/test/test_pack_unpack.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'ruby/test') diff --git a/ruby/test/test_pack_unpack.rb b/ruby/test/test_pack_unpack.rb index 25bde81..545e593 100644 --- a/ruby/test/test_pack_unpack.rb +++ b/ruby/test/test_pack_unpack.rb @@ -153,7 +153,8 @@ class MessagePackTestPackUnpack < Test::Unit::TestCase end it "{1=>1}" do - match ({1=>1}), "\x81\x01\x01" + obj = {1=>1} + match obj, "\x81\x01\x01" end it "1.0" do @@ -165,15 +166,18 @@ class MessagePackTestPackUnpack < Test::Unit::TestCase end it "[0, 1, ..., 14]" do - match (0..14).to_a, "\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e" + obj = (0..14).to_a + match obj, "\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e" end it "[0, 1, ..., 15]" do - match (0..15).to_a, "\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + obj = (0..15).to_a + match obj, "\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" end it "{}" do - match ({}), "\x80" + obj = {} + match obj, "\x80" end ## FIXME -- cgit v1.2.1