From d9b467098a8cc5c4521717d58c2561f92222b333 Mon Sep 17 00:00:00 2001 From: UENISHI Kota Date: Mon, 31 May 2010 23:56:06 +0900 Subject: [PATCH 1/3] erlang: added more cross-language tests. better type specification. --- erlang/msgpack.erl | 80 ++++++++++++++++++++++-------------- erlang/testcase_generator.rb | 61 +++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 30 deletions(-) create mode 100644 erlang/testcase_generator.rb diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl index 90ddb769..f23ec09e 100644 --- a/erlang/msgpack.erl +++ b/erlang/msgpack.erl @@ -18,6 +18,11 @@ -module(msgpack). -author('kuenishi+msgpack@gmail.com'). +%% tuples, atoms are not supported. lists, integers, double, and so on. +%% see http://msgpack.sourceforge.jp/spec for +%% supported formats. APIs are almost compatible +%% for C API (http://msgpack.sourceforge.jp/c:doc) +%% except buffering functions (both copying and zero-copying). -export([pack/1, unpack/1, unpack_all/1, test/0]). -include_lib("eunit/include/eunit.hrl"). @@ -27,20 +32,8 @@ % erl> S = . % erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ). -%% tuples, atoms are not supported. lists, integers, double, and so on. -%% see http://msgpack.sourceforge.jp/spec for -%% supported formats. APIs are almost compatible -%% for C API (http://msgpack.sourceforge.jp/c:doc) -%% except buffering functions (both copying and zero-copying). --export([ - pack_nil/0, - pack_bool/1, - pack_float/1, - pack_double/1, - pack_raw/1, - pack_array/1, - pack_map/1, - pack_object/1 ]). + +-type reason() :: enomem. % positive fixnum pack_uint_(N) when is_integer( N ) , N < 128 -> @@ -85,9 +78,9 @@ pack_bool(true)-> << 16#C3:8 >>; pack_bool(false)-> << 16#C2:8 >>. % float : erlang's float is always IEEE 754 64bit format. -pack_float(F) when is_float(F)-> +%pack_float(F) when is_float(F)-> % << 16#CA:8, F:32/big-float-unit:1 >>. - pack_double(F). +% pack_double(F). % double pack_double(F) when is_float(F)-> << 16#CB:8, F:64/big-float-unit:1 >>. @@ -177,7 +170,9 @@ pack(Obj)-> % if failed in decoding and not end, get more data % and feed more Bin into this function. % TODO: error case for imcomplete format when short for any type formats. --spec unpack( binary() )-> {term(), binary()}. +-spec unpack( binary() )-> {term(), binary()} | {more, non_neg_integer()} | {error, reason()}. +unpack(Bin) when not is_binary(Bin)-> + {error, badard}; unpack(Bin) when bit_size(Bin) >= 8 -> << Flag:8/unsigned-integer, Payload/binary >> = Bin, case Flag of @@ -310,7 +305,9 @@ unpack(Bin) when bit_size(Bin) >= 8 -> _Other -> erlang:display(_Other), {error, no_code_matches} - end. + end; +unpack(_)-> % when bit_size(Bin) < 8 -> + {more, 8}. unpack_all(Data)-> case unpack(Data) of @@ -322,31 +319,54 @@ unpack_all(Data)-> -ifdef(EUNIT). -test()-> - Tests = [0, 1, 2, 123, 512, 1230, 678908, 16#FFFFFFFFFF, - -1, -23, -512, -1230, -567898, -16#FFFFFFFFFF, - 123.123, -234.4355, 1.0e-34, 1.0e64, - [23, 234, 0.23], - "hogehoge", "243546rf7g68h798j", - <<"hoasfdafdas][">>, - [0,42,"sum", [1,2]], [1,42, nil, [3]] - ], +test_data()-> + [0, 1, 2, 123, 512, 1230, 678908, 16#FFFFFFFFFF, + -1, -23, -512, -1230, -567898, -16#FFFFFFFFFF, + 123.123, -234.4355, 1.0e-34, 1.0e64, + [23, 234, 0.23], + "hogehoge", "243546rf7g68h798j", + <<"hoasfdafdas][">>, + [0,42,"sum", [1,2]], [1,42, nil, [3]] + ]. + +basic_test()-> + Tests = test_data(), Passed = test_(Tests), - Passed = length(Tests), + Passed = length(Tests). + +port_test()-> + Tests = test_data(), {[Tests],<<>>} = msgpack:unpack(msgpack:pack([Tests])), Port = open_port({spawn, "ruby ../crosslang.rb"}, [binary]), true = port_command(Port, msgpack:pack(Tests) ), - %Port ! {self, {command, msgpack:pack(Tests)}}, ... not owner + %Port ! {self, {command, msgpack:pack(Tests)}}, ... not owner receive {Port, {data, Data}}-> {Tests, <<>>}=msgpack:unpack(Data) after 1024-> ?assert(false) end, port_close(Port). +unknown_test()-> + Tests = [0, 1, 2, 123, 512, 1230, 678908, + -1, -23, -512, -1230, -567898, +% "hogehoge", "243546rf7g68h798j", + 123.123 %-234.4355, 1.0e-34, 1.0e64, +% [23, 234, 0.23] +% [0,42,"sum", [1,2]], [1,42, nil, [3]] + ], + Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary]), + %Port ! {self, {command, msgpack:pack(Tests)}}, ... not owner + receive + {Port, {data, Data}}-> + Tests=msgpack:unpack_all(Data) +% io:format("~p~n", [Tests]) + after 1024-> ?assert(false) end, + port_close(Port). + test_([]) -> 0; test_([S|Rest])-> Pack = msgpack:pack(S), % io:format("testing: ~p => ~p~n", [S, Pack]), {S, <<>>} = msgpack:unpack( Pack ), - test_(Rest) + 1. + 1+test_(Rest). -endif. diff --git a/erlang/testcase_generator.rb b/erlang/testcase_generator.rb new file mode 100644 index 00000000..a173790a --- /dev/null +++ b/erlang/testcase_generator.rb @@ -0,0 +1,61 @@ +begin +require 'rubygems' +rescue LoadError +end +require 'msgpack' + +def usage + puts < (default: stdout) + +EOF + exit 1 +end + +code = 1 +outio = $stdout + +if ARGV.length > 2 + usage +end + +if fname = ARGV[0] + unless fname == "-" + begin + outio = File.open(fname, "w") + rescue + puts "can't open output file: #{$!}" + exit 1 + end + end +end + +objs = [0, 1, 2, 123, 512, 1230, 678908, + -1, -23, -512, -1230, -567898, +# "hogehoge", "243546rf7g68h798j", + 123.123, #-234.4355, 1.0e-34, 1.0e64, +# [23, 234, 0.23] +# [0,42,"sum", [1,2]], [1,42, nil, [3]] + ] +begin + objs.each do |obj| + outio.write MessagePack.pack(obj) + outio.flush + end +rescue EOFError + code=0 +rescue + $stderr.puts $! + code=1 +end + +outio.close +exit code + From 49f3872d047624b1995b8c60edec8bad35429fd3 Mon Sep 17 00:00:00 2001 From: UENISHI Kota Date: Tue, 1 Jun 2010 00:31:12 +0900 Subject: [PATCH 2/3] erlang: temporary documentation and .gitignore --- erlang/.gitignore | 3 ++- erlang/README.md | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 erlang/README.md diff --git a/erlang/.gitignore b/erlang/.gitignore index 911d6e38..7d6d3247 100644 --- a/erlang/.gitignore +++ b/erlang/.gitignore @@ -1,3 +1,4 @@ MANIFEST *.beam - +.omakedb* +*.omc diff --git a/erlang/README.md b/erlang/README.md new file mode 100644 index 00000000..50d446d6 --- /dev/null +++ b/erlang/README.md @@ -0,0 +1,19 @@ +MessagePack for Erlang +====================== +Binary-based efficient object serialization library. + +## Status + +still in development. + +TODOs: + - decide string specification. + +## Installation + +## Example + +## License + + + - From 7cd41aeb7280e5752deb5b60a140b627bd50d61e Mon Sep 17 00:00:00 2001 From: UENISHI Kota Date: Thu, 3 Jun 2010 00:17:17 +0900 Subject: [PATCH 3/3] erlang: tracing crosslang.rb moving to ../test --- erlang/msgpack.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl index f23ec09e..ca9769e6 100644 --- a/erlang/msgpack.erl +++ b/erlang/msgpack.erl @@ -337,7 +337,7 @@ basic_test()-> port_test()-> Tests = test_data(), {[Tests],<<>>} = msgpack:unpack(msgpack:pack([Tests])), - Port = open_port({spawn, "ruby ../crosslang.rb"}, [binary]), + Port = open_port({spawn, "ruby ../test/crosslang.rb"}, [binary]), true = port_command(Port, msgpack:pack(Tests) ), %Port ! {self, {command, msgpack:pack(Tests)}}, ... not owner receive