Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Athishpranav2003 msgpack object kv #1137

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ else()
ENDIF ()
endif()

IF ((CMAKE_VERSION VERSION_GREATER 3.27) OR
(CMAKE_VERSION VERSION_EQUAL 3.27))
CMAKE_POLICY(SET CMP0145 OLD)
ENDIF ()

OPTION (MSGPACK_BUILD_TESTS "Build msgpack tests." OFF)
OPTION (MSGPACK_GEN_COVERAGE "Enable running gcov to get a test coverage report." OFF)

Expand Down
33 changes: 33 additions & 0 deletions include/msgpack/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,39 @@ typedef struct msgpack_object_kv {
msgpack_object val;
} msgpack_object_kv;

MSGPACK_DLLEXPORT
void msgpack_object_init_nil(msgpack_object* d);

MSGPACK_DLLEXPORT
void msgpack_object_init_boolean(msgpack_object* d, bool v);

MSGPACK_DLLEXPORT
void msgpack_object_init_unsigned_integer(msgpack_object* d, uint64_t v);

MSGPACK_DLLEXPORT
void msgpack_object_init_signed_integer(msgpack_object* d, int64_t v);

MSGPACK_DLLEXPORT
void msgpack_object_init_float32(msgpack_object* d, float v);

MSGPACK_DLLEXPORT
void msgpack_object_init_float64(msgpack_object* d, double v);

MSGPACK_DLLEXPORT
void msgpack_object_init_str(msgpack_object* d, const char* data, uint32_t size);

MSGPACK_DLLEXPORT
void msgpack_object_init_bin(msgpack_object* d, const char* data, uint32_t size);

MSGPACK_DLLEXPORT
void msgpack_object_init_ext(msgpack_object* d, int8_t type, const char* data, uint32_t size);

MSGPACK_DLLEXPORT
void msgpack_object_init_array(msgpack_object* d, msgpack_object* data, uint32_t size);

MSGPACK_DLLEXPORT
void msgpack_object_init_map(msgpack_object* d, msgpack_object_kv* data, uint32_t size);

#if !defined(_KERNEL_MODE)
MSGPACK_DLLEXPORT
void msgpack_object_print(FILE* out, msgpack_object o);
Expand Down
66 changes: 66 additions & 0 deletions src/objectc.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,72 @@ int msgpack_pack_object(msgpack_packer* pk, msgpack_object d)
}
}

void msgpack_object_init_nil(msgpack_object* d) {
d->type = MSGPACK_OBJECT_NIL;
}

void msgpack_object_init_boolean(msgpack_object* d, bool v) {
d->type = MSGPACK_OBJECT_BOOLEAN;
d->via.boolean = v;
}

void msgpack_object_init_unsigned_integer(msgpack_object* d, uint64_t v) {
d->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
d->via.u64 = v;
}

void msgpack_object_init_signed_integer(msgpack_object* d, int64_t v) {
if (v < 0) {
d->type = MSGPACK_OBJECT_NEGATIVE_INTEGER;
d->via.i64 = v;
}
else {
d->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
d->via.u64 = v;
}
}

void msgpack_object_init_float32(msgpack_object* d, float v) {
d->type = MSGPACK_OBJECT_FLOAT32;
d->via.f64 = v;
}

void msgpack_object_init_float64(msgpack_object* d, double v) {
d->type = MSGPACK_OBJECT_FLOAT64;
d->via.f64 = v;
}

void msgpack_object_init_str(msgpack_object* d, const char* data, uint32_t size) {
d->type = MSGPACK_OBJECT_STR;
d->via.str.ptr = data;
d->via.str.size = size;
}

void msgpack_object_init_bin(msgpack_object* d, const char* data, uint32_t size) {
d->type = MSGPACK_OBJECT_BIN;
d->via.bin.ptr = data;
d->via.bin.size = size;
}

void msgpack_object_init_ext(msgpack_object* d, int8_t type, const char* data, uint32_t size) {
d->type = MSGPACK_OBJECT_EXT;
d->via.ext.type = type;
d->via.ext.ptr = data;
d->via.ext.size = size;
}

void msgpack_object_init_array(msgpack_object* d, msgpack_object* data, uint32_t size) {
d->type = MSGPACK_OBJECT_ARRAY;
d->via.array.ptr = data;
d->via.array.size = size;
}

void msgpack_object_init_map(msgpack_object* d, msgpack_object_kv* data, uint32_t size) {
d->type = MSGPACK_OBJECT_MAP;
d->via.map.ptr = data;
d->via.map.size = size;
}

#if !defined(_KERNEL_MODE)

static void msgpack_object_bin_print(FILE* out, const char *ptr, size_t size)
Expand Down
105 changes: 105 additions & 0 deletions test/msgpack_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,111 @@ TEST(MSGPACKC, object_bin_print_buffer_overflow) {
EXPECT_STREQ("\"test\"", buffer);
}

TEST(MSGPACKC, init_msgpack_obj_nil) {
msgpack_object obj;
msgpack_object_init_nil(&obj);
EXPECT_EQ(MSGPACK_OBJECT_NIL, obj.type);
}

TEST(MSGPACKC, init_msgpack_obj_boolean) {
msgpack_object obj;
msgpack_object_init_boolean(&obj, true);
EXPECT_EQ(MSGPACK_OBJECT_BOOLEAN, obj.type);
EXPECT_EQ(true, obj.via.boolean);
}

TEST(MSGPACKC, init_msgpack_obj_unsigned_integer) {
msgpack_object obj;
msgpack_object_init_unsigned_integer(&obj, 123);
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type);
EXPECT_EQ(static_cast<uint64_t>(123), obj.via.u64);
}

TEST(MSGPACKC, init_msgpack_obj_signed_integer1) {
msgpack_object obj;
msgpack_object_init_signed_integer(&obj, -123);
EXPECT_EQ(MSGPACK_OBJECT_NEGATIVE_INTEGER, obj.type);
EXPECT_EQ(-123, obj.via.i64);
}

TEST(MSGPACKC, init_msgpack_obj_signed_integer2) {
msgpack_object obj;
msgpack_object_init_signed_integer(&obj, 123);
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type);
EXPECT_EQ(static_cast<uint64_t>(123), obj.via.u64);
}

TEST(MSGPACKC, init_msgpack_obj_float32) {
msgpack_object obj;
float val = 1.23f;
msgpack_object_init_float32(&obj, val);
EXPECT_EQ(MSGPACK_OBJECT_FLOAT32, obj.type);
EXPECT_TRUE(fabs(obj.via.f64 - val) <= kEPS);
}

TEST(MSGPACKC, init_msgpack_obj_float64) {
msgpack_object obj;
double val = 1.23;
msgpack_object_init_float64(&obj, val);
EXPECT_EQ(MSGPACK_OBJECT_FLOAT64, obj.type);
EXPECT_TRUE(fabs(obj.via.f64 - val) <= kEPS);
}


TEST(MSGPACKC, init_msgpack_obj_string) {
msgpack_object obj;
char buffer[] = "test";
msgpack_object_init_str(&obj, buffer, (uint32_t)strlen(buffer));
EXPECT_EQ(MSGPACK_OBJECT_STR, obj.type);
EXPECT_STREQ(buffer, obj.via.str.ptr);
}

TEST(MSGPACKC, init_msgpack_obj_bin) {
msgpack_object obj;
char buffer[] = "test";
msgpack_object_init_bin(&obj, buffer, (uint32_t)strlen(buffer));
EXPECT_EQ(MSGPACK_OBJECT_BIN, obj.type);
EXPECT_STREQ(buffer, obj.via.bin.ptr);
}

TEST(MSGPACKC, init_msgpack_obj_ext) {
msgpack_object obj;
char buffer[] = "test";
msgpack_object_init_ext(&obj, 1, buffer, (uint32_t)strlen(buffer));
EXPECT_EQ(MSGPACK_OBJECT_EXT, obj.type);
EXPECT_EQ(1, obj.via.ext.type);
EXPECT_STREQ(buffer, obj.via.ext.ptr);
}

TEST(MSGPACKC, init_msgpack_obj_array) {
msgpack_object obj;
char buffer[][7] = {"test_1", "test_2", "test_3", "test_4"};
uint32_t buffer_size = 4;
msgpack_object array[buffer_size];
for(size_t i = 0; i < buffer_size; i++) {
msgpack_object_init_str(&array[i], buffer[i], (uint32_t)strlen(buffer[i]));
}
msgpack_object_init_array(&obj, array, buffer_size);
EXPECT_EQ(MSGPACK_OBJECT_ARRAY, obj.type);
for(size_t i = 0; i < buffer_size; i++) {
EXPECT_STREQ(buffer[i], obj.via.array.ptr[i].via.str.ptr);
}
}

TEST(MSGPACKC, init_msgpack_obj_map) {
msgpack_object obj;
char key_str[] = "test_key";
char value_str[] = "test_value";
msgpack_object key,value;
msgpack_object_init_str(&key, key_str, (uint32_t)strlen(key_str));
msgpack_object_init_str(&value, value_str, (uint32_t)strlen(value_str));
msgpack_object_kv map = { key, value };
msgpack_object_init_map(&obj, &map, 1);
EXPECT_EQ(MSGPACK_OBJECT_MAP, obj.type);
EXPECT_STREQ(key_str, obj.via.map.ptr->key.via.str.ptr);
EXPECT_STREQ(value_str, obj.via.map.ptr->val.via.str.ptr);
}

/* test for vrefbuffer */
#define GEN_TEST_VREFBUFFER_PREPARE(...) \
msgpack_vrefbuffer vbuf; \
Expand Down
Loading