diff --git a/c/Makefile.am b/c/Makefile.am index daa8a762..0a524460 100644 --- a/c/Makefile.am +++ b/c/Makefile.am @@ -18,3 +18,11 @@ nobase_include_HEADERS = \ # -version-info CURRENT:REVISION:AGE libmsgpackc_la_LDFLAGS = -version-info 1:0:0 +check_PROGRAMS = \ + msgpackc_test + +msgpackc_test_SOURCES = test.cpp +msgpackc_test_CXXFLAGS = -I$(top_srcdir) -I$(top_srcdir)/c +msgpackc_test_LDFLAGS = libmsgpackc.la -lgtest_main + +TESTS = $(check_PROGRAMS) diff --git a/c/test.cpp b/c/test.cpp new file mode 100644 index 00000000..b42d931b --- /dev/null +++ b/c/test.cpp @@ -0,0 +1,28 @@ +#include "msgpack.h" + +#include + +TEST(MSGPACKC, simple_buffer) +{ + msgpack_sbuffer sbuf; + msgpack_sbuffer_init(&sbuf); + + msgpack_packer pk; + msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); + + msgpack_pack_int(pk, 1); + + msgpack_zone z; + msgpack_zone_init(&z, 2048); + + msgpack_object obj; + + msgpack_unpack_return ret = + msgpack_unpack(sbuf.data, sbuf.size, NULL, &z, &obj); + + EXPECT_EQ(ret, MSGPACK_UNPACK_SUCCESS); + + EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type); + EXPECT_EQ(1, obj.via.u64); +} + diff --git a/cpp/Makefile.am b/cpp/Makefile.am index 2c115353..af7e6e2b 100644 --- a/cpp/Makefile.am +++ b/cpp/Makefile.am @@ -32,3 +32,11 @@ libmsgpack_la_LIBADD = -L../c -lmsgpackc # -version-info CURRENT:REVISION:AGE libmsgpack_la_LDFLAGS = -version-info 1:0:0 +check_PROGRAMS = \ + msgpack_test + +msgpackc_test_SOURCES = test.cpp +msgpackc_test_CXXFLAGS = -I$(top_srcdir) -I$(top_srcdir)/c -I$(top_srcdir)/cpp +msgpackc_test_LDFLAGS = libmsgpack.la -lgtest_main + +TESTS = $(check_PROGRAMS) diff --git a/cpp/test.cpp b/cpp/test.cpp index dd0b1fde..1c5e6c84 100644 --- a/cpp/test.cpp +++ b/cpp/test.cpp @@ -1,201 +1,24 @@ -#include -#include -#include -#include -#include +#include "msgpack.hpp" -using namespace msgpack; +#include -class checker { -public: - template - void check(const char* d, size_t len, T should) { - try { - std::cout << "----" << std::endl; - - object o; - try { - o = unpack(d, len, m_zone); - } catch (std::runtime_error& e) { - std::cout << o << std::endl; - std::cout << "**" << e.what() << "**" << std::endl; - return; - } - - std::cout << o << std::endl; - - try { - std::stringstream s; - pack(s, should); - std::string str(s.str()); - object ro = unpack(str.data(), str.size(), m_zone); - std::cout << ro << std::endl; - if(ro != o) { throw std::runtime_error("NOT MATCH"); } - } catch (std::runtime_error& e) { - std::cout << "** REUNPACK FAILED **" << std::endl; - std::cout << e.what() << std::endl; - } catch (...) { - std::cout << "** REUNPACK FAILED **" << std::endl; - std::cout << "unknown error" << std::endl; - } - - } catch (...) { m_zone.clear(); throw; } - m_zone.clear(); - } -private: - zone m_zone; -}; - -int main(void) +TEST(MSGPACKC, simple_buffer) { - checker c; + msgpack::sbuffer sbuf; -#if 0 - { // SimpleValue - const char d[] = { - 0x93, 0xc0, 0xc2, 0xc3, - }; - c.check(d, sizeof(d), - type::make_tuple( - type::nil(), false, true - ) - ); - } + int v = 0; - { // Fixnum - const char d[] = { - 0x92, - 0x93, 0x00, 0x40, 0x7f, - 0x93, 0xe0, 0xf0, 0xff, - }; - c.check(d, sizeof(d), - type::make_tuple( - type::make_tuple( - 0, 64, 127 - ), - type::make_tuple( - -32, -16, -1 - ) - ) - ); - } + msgpack::pack(sbuf, v); - { // FixArray - const char d[] = { - 0x92, - 0x90, - 0x91, - 0x91, 0xc0, - }; - std::vector empty; - c.check(d, sizeof(d), - type::make_tuple( - empty, - type::make_tuple( - type::make_tuple( - type::nil() - ) - ) - ) - ); - } + msgpack::zone z; + msgpack::object obj; - { // FixRaw - const char d[] = { - 0x94, - 0xa0, - 0xa1, 'a', - 0xa2, 'b', 'c', - 0xa3, 'd', 'e', 'f', - }; - c.check(d, sizeof(d), - type::make_tuple( - std::string(""), - std::string("a"), - std::string("bc"), - type::raw_ref("def", 3) - ) - ); - } -#endif + msgpack::unpack_return ret = + msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj); + EXPECT_EQ(ret, msgpack::UNPACK_SUCCESS); - static const unsigned TASK_ARRAY = 1000; - static const unsigned TASK_REPEAT = 10; - std::vector task; + obj.convert(&v); - // create task - { - static char traw[64]; - memset(traw, 'a', sizeof(traw)); - - task.resize(TASK_ARRAY); - for(unsigned i=0; i < TASK_ARRAY; ++i) { - task[i] = std::string(traw, sizeof(traw)); - } - } - - - std::stringstream stream; - - // send message - { - for(unsigned i=0; i < TASK_REPEAT; ++i) { - pack(stream, task); - } - std::cout << "send " << stream.str().size() << " bytes" << std::endl; - } - - ssize_t total_bytes = stream.str().size(); - stream.seekg(0); - - // reserive message - { - unsigned num_msg = 0; - static const size_t RESERVE_SIZE = 32;//*1024; - - unpacker pac; - - while(stream.good() && total_bytes > 0) { - - // 1. reserve buffer - pac.reserve_buffer(RESERVE_SIZE); - - // 2. read data to buffer() up to buffer_capacity() bytes - size_t sz = stream.readsome( - pac.buffer(), - pac.buffer_capacity()); - - total_bytes -= sz; - std::cout << "read " << sz << " bytes to capacity " - << pac.buffer_capacity() << " bytes" - << std::endl; - - // 3. specify the number of bytes actually copied - pac.buffer_consumed(sz); - - // 4. repeat execute() until it returns false - while( pac.execute() ) { - // 5.1. take out the parsed object - object o = pac.data(); - - // 5.2 release the zone - std::auto_ptr olife( pac.release_zone() ); - - // 5.3 re-initialize the unpacker */ - pac.reset(); - - // do some with the o and olife - std::cout << "message parsed: " << o << std::endl; - ++num_msg; - } - - } - - std::cout << "stream finished" << std::endl; - std::cout << num_msg << " messages reached" << std::endl; - } - - return 0; + EXPECT_EQ(0, v); } -