mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-23 01:08:20 +01:00
cpp: update tests
This commit is contained in:
parent
684bca203a
commit
eabcf15790
@ -6,7 +6,9 @@ AM_LDFLAGS = ../src/libmsgpack.la -lgtest_main
|
||||
check_PROGRAMS = \
|
||||
zone \
|
||||
pack_unpack \
|
||||
pack_unpack_c \
|
||||
streaming \
|
||||
streaming_c \
|
||||
object \
|
||||
convert \
|
||||
buffer \
|
||||
@ -20,8 +22,12 @@ zone_SOURCES = zone.cc
|
||||
|
||||
pack_unpack_SOURCES = pack_unpack.cc
|
||||
|
||||
pack_unpack_c_SOURCES = pack_unpack_c.cc
|
||||
|
||||
streaming_SOURCES = streaming.cc
|
||||
|
||||
streaming_c_SOURCES = streaming_c.cc
|
||||
|
||||
object_SOURCES = object.cc
|
||||
|
||||
convert_SOURCES = convert.cc
|
||||
|
70
cpp/test/pack_unpack_c.cc
Normal file
70
cpp/test/pack_unpack_c.cc
Normal file
@ -0,0 +1,70 @@
|
||||
#include <msgpack.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <stdio.h>
|
||||
|
||||
TEST(pack, num)
|
||||
{
|
||||
msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
|
||||
msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);
|
||||
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 1));
|
||||
|
||||
msgpack_sbuffer_free(sbuf);
|
||||
msgpack_packer_free(pk);
|
||||
}
|
||||
|
||||
|
||||
TEST(pack, array)
|
||||
{
|
||||
msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
|
||||
msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);
|
||||
|
||||
EXPECT_EQ(0, msgpack_pack_array(pk, 3));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 1));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 2));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 3));
|
||||
|
||||
msgpack_sbuffer_free(sbuf);
|
||||
msgpack_packer_free(pk);
|
||||
}
|
||||
|
||||
|
||||
TEST(unpack, sequence)
|
||||
{
|
||||
msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
|
||||
msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);
|
||||
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 1));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 2));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 3));
|
||||
|
||||
msgpack_packer_free(pk);
|
||||
|
||||
bool success;
|
||||
size_t offset = 0;
|
||||
|
||||
msgpack_unpacked msg;
|
||||
msgpack_unpacked_init(&msg);
|
||||
|
||||
success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
|
||||
EXPECT_TRUE(success);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
|
||||
EXPECT_EQ(1, msg.data.via.u64);
|
||||
|
||||
success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
|
||||
EXPECT_TRUE(success);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
|
||||
EXPECT_EQ(2, msg.data.via.u64);
|
||||
|
||||
success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
|
||||
EXPECT_TRUE(success);
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
|
||||
EXPECT_EQ(3, msg.data.via.u64);
|
||||
|
||||
success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
|
||||
EXPECT_FALSE(success);
|
||||
|
||||
msgpack_sbuffer_free(sbuf);
|
||||
msgpack_unpacked_destroy(&msg);
|
||||
}
|
||||
|
@ -2,28 +2,33 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
TEST(streaming, basic)
|
||||
{
|
||||
std::ostringstream stream;
|
||||
msgpack::packer<std::ostream> pk(&stream);
|
||||
msgpack::sbuffer buffer;
|
||||
|
||||
msgpack::packer<msgpack::sbuffer> pk(&buffer);
|
||||
pk.pack(1);
|
||||
pk.pack(2);
|
||||
pk.pack(3);
|
||||
|
||||
std::istringstream input(stream.str());
|
||||
const char* input = buffer.data();
|
||||
const char* const eof = input + buffer.size();
|
||||
|
||||
msgpack::unpacker pac;
|
||||
msgpack::unpacked result;
|
||||
|
||||
int count = 0;
|
||||
while(count < 3) {
|
||||
pac.reserve_buffer(32*1024);
|
||||
|
||||
size_t len = input.readsome(pac.buffer(), pac.buffer_capacity());
|
||||
// read buffer into pac.buffer() upto
|
||||
// pac.buffer_capacity() bytes.
|
||||
size_t len = 1;
|
||||
memcpy(pac.buffer(), input, len);
|
||||
input += len;
|
||||
|
||||
pac.buffer_consumed(len);
|
||||
|
||||
msgpack::unpacked result;
|
||||
while(pac.next(&result)) {
|
||||
msgpack::object obj = result.get();
|
||||
switch(count++) {
|
||||
@ -38,6 +43,8 @@ TEST(streaming, basic)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_TRUE(input < eof);
|
||||
}
|
||||
}
|
||||
|
||||
|
57
cpp/test/streaming_c.cc
Normal file
57
cpp/test/streaming_c.cc
Normal file
@ -0,0 +1,57 @@
|
||||
#include <msgpack.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <stdio.h>
|
||||
|
||||
TEST(streaming, basic)
|
||||
{
|
||||
msgpack_sbuffer* buffer = msgpack_sbuffer_new();
|
||||
|
||||
msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 1));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 2));
|
||||
EXPECT_EQ(0, msgpack_pack_int(pk, 3));
|
||||
msgpack_packer_free(pk);
|
||||
|
||||
const char* input = buffer->data;
|
||||
const char* const eof = input + buffer->size;
|
||||
|
||||
msgpack_unpacker pac;
|
||||
msgpack_unpacker_init(&pac, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
|
||||
|
||||
msgpack_unpacked result;
|
||||
msgpack_unpacked_init(&result);
|
||||
|
||||
int count = 0;
|
||||
while(count < 3) {
|
||||
msgpack_unpacker_reserve_buffer(&pac, 32*1024);
|
||||
|
||||
/* read buffer into msgpack_unapcker_buffer(&pac) upto
|
||||
* msgpack_unpacker_buffer_capacity(&pac) bytes. */
|
||||
size_t len = 1;
|
||||
memcpy(msgpack_unpacker_buffer(&pac), input, len);
|
||||
input += len;
|
||||
|
||||
msgpack_unpacker_buffer_consumed(&pac, len);
|
||||
|
||||
while(msgpack_unpacker_next(&pac, &result)) {
|
||||
msgpack_object obj = result.data;
|
||||
switch(count++) {
|
||||
case 0:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type);
|
||||
EXPECT_EQ(1, result.data.via.u64);
|
||||
break;
|
||||
case 1:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type);
|
||||
EXPECT_EQ(2, result.data.via.u64);
|
||||
break;
|
||||
case 2:
|
||||
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type);
|
||||
EXPECT_EQ(3, result.data.via.u64);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_TRUE(input < eof);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user