summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@users.sourceforge.jp>2009-08-25 18:35:26 +0900
committerfrsyuki <frsyuki@users.sourceforge.jp>2009-08-25 18:35:26 +0900
commitd3f9ab7deced474d88bb5b5e85bdb54b312ad86c (patch)
tree20cefea4e031bcb4eab14369ba29b8bd7871aae7 /cpp
parent1ba330c4735082d917099fd8eb5fa1f89626cf93 (diff)
downloadmsgpack-python-d3f9ab7deced474d88bb5b5e85bdb54b312ad86c.tar.gz
add c/test.cpp cpp/test.cpp
Diffstat (limited to 'cpp')
-rw-r--r--cpp/Makefile.am8
-rw-r--r--cpp/test.cpp203
2 files changed, 21 insertions, 190 deletions
diff --git a/cpp/Makefile.am b/cpp/Makefile.am
index 2c11535..af7e6e2 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 dd0b1fd..1c5e6c8 100644
--- a/cpp/test.cpp
+++ b/cpp/test.cpp
@@ -1,201 +1,24 @@
-#include <iostream>
-#include <string>
-#include <msgpack.hpp>
-#include <sstream>
-#include <memory>
+#include "msgpack.hpp"
-using namespace msgpack;
+#include <gtest/gtest.h>
-class checker {
-public:
- template <typename T>
- 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;
-
-#if 0
- { // SimpleValue
- const char d[] = {
- 0x93, 0xc0, 0xc2, 0xc3,
- };
- c.check(d, sizeof(d),
- type::make_tuple(
- type::nil(), false, true
- )
- );
- }
-
- { // 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
- )
- )
- );
- }
-
- { // FixArray
- const char d[] = {
- 0x92,
- 0x90,
- 0x91,
- 0x91, 0xc0,
- };
- std::vector<int> empty;
- c.check(d, sizeof(d),
- type::make_tuple(
- empty,
- type::make_tuple(
- type::make_tuple(
- type::nil()
- )
- )
- )
- );
- }
-
- { // 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
-
-
- static const unsigned TASK_ARRAY = 1000;
- static const unsigned TASK_REPEAT = 10;
- std::vector<std::string> task;
-
- // 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));
- }
- }
+ msgpack::sbuffer sbuf;
+ int v = 0;
- std::stringstream stream;
+ msgpack::pack(sbuf, v);
- // send message
- {
- for(unsigned i=0; i < TASK_REPEAT; ++i) {
- pack(stream, task);
- }
- std::cout << "send " << stream.str().size() << " bytes" << std::endl;
- }
+ msgpack::zone z;
+ msgpack::object obj;
- ssize_t total_bytes = stream.str().size();
- stream.seekg(0);
+ msgpack::unpack_return ret =
+ msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj);
- // reserive message
- {
- unsigned num_msg = 0;
- static const size_t RESERVE_SIZE = 32;//*1024;
+ EXPECT_EQ(ret, msgpack::UNPACK_SUCCESS);
- unpacker pac;
+ obj.convert(&v);
- 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<zone> 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);
}
-