From 6056f939103624d21092a5e4a4d8ffaf9204c191 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 1 Jun 2010 05:15:36 +0900 Subject: [PATCH] cpp: add cases.mpac test --- cpp/preprocess | 2 ++ cpp/test/Makefile.am | 5 +++++ cpp/test/cases.cc | 36 ++++++++++++++++++++++++++++++++++++ test/cases_gen.rb | 15 +++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 cpp/test/cases.cc diff --git a/cpp/preprocess b/cpp/preprocess index aab89f54..1a5e6a3a 100755 --- a/cpp/preprocess +++ b/cpp/preprocess @@ -25,4 +25,6 @@ cp -f ../msgpack/pack_define.h src/msgpack/ cp -f ../msgpack/pack_template.h src/msgpack/ cp -f ../msgpack/unpack_define.h src/msgpack/ cp -f ../msgpack/unpack_template.h src/msgpack/ +cp -f ../test/cases.mpac test/ +cp -f ../test/cases_compact.mpac test/ diff --git a/cpp/test/Makefile.am b/cpp/test/Makefile.am index 3670d7f1..f9c5f224 100644 --- a/cpp/test/Makefile.am +++ b/cpp/test/Makefile.am @@ -10,6 +10,7 @@ check_PROGRAMS = \ object \ convert \ buffer \ + cases \ msgpackc_test \ msgpack_test @@ -28,7 +29,11 @@ convert_SOURCES = convert.cc buffer_SOURCES = buffer.cc buffer_LDADD = -lz +cases_SOURCES = cases.cc + msgpackc_test_SOURCES = msgpackc_test.cpp msgpack_test_SOURCES = msgpack_test.cpp +EXTRA_DIST = cases.mpac cases_compact.mpac + diff --git a/cpp/test/cases.cc b/cpp/test/cases.cc new file mode 100644 index 00000000..b408876c --- /dev/null +++ b/cpp/test/cases.cc @@ -0,0 +1,36 @@ +#include +#include +#include + +static void feed_file(msgpack::unpacker& pac, const char* path) +{ + std::ifstream fin(path); + while(true) { + pac.reserve_buffer(32*1024); + fin.read(pac.buffer(), pac.buffer_capacity()); + if(fin.bad()) { + throw std::runtime_error("read failed"); + } + pac.buffer_consumed(fin.gcount()); + if(fin.fail()) { + break; + } + } +} + +TEST(cases, format) +{ + msgpack::unpacker pac; + msgpack::unpacker pac_compact; + + feed_file(pac, "cases.mpac"); + feed_file(pac_compact, "cases_compact.mpac"); + + msgpack::unpacked result; + while(pac.next(&result)) { + msgpack::unpacked result_compact; + EXPECT_TRUE( pac_compact.next(&result_compact) ); + EXPECT_EQ(result_compact.get(), result.get()); + } +} + diff --git a/test/cases_gen.rb b/test/cases_gen.rb index 2fb7273a..7efbfe7e 100644 --- a/test/cases_gen.rb +++ b/test/cases_gen.rb @@ -63,14 +63,29 @@ EOF source.gsub!(/\#.+$/,'') bytes = source.strip.split(/\s+/).map {|x| x.to_i(16) }.pack('C*') +objs = [] compact_bytes = "" pac = MessagePack::Unpacker.new pac.feed(bytes) pac.each {|obj| p obj + objs << obj compact_bytes << obj.to_msgpack } +# self check +cpac = MessagePack::Unpacker.new +cpac.feed(compact_bytes) +cpac.each {|cobj| + obj = objs.shift + if obj != cobj + puts "** SELF CHECK FAILED **" + puts "expected: #{obj.inspect}" + puts "actual: #{cobj.inspect}" + exit 1 + end +} + File.open("cases.mpac","w") {|f| f.write(bytes) } File.open("cases_compact.mpac","w") {|f| f.write(compact_bytes) }