From 6f24d240e14dfea9d63742f17e5e1a9d789ba833 Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Sat, 17 Aug 2024 13:33:40 +0900 Subject: [PATCH] Supported all msgpack object types. Fixed cmake warning. --- CMakeLists.txt | 5 +++ include/msgpack/object.h | 32 ++++++++++++- src/objectc.c | 97 ++++++++++++++++++++++++++-------------- test/msgpack_c.cpp | 76 +++++++++++++++++++++++++++---- 4 files changed, 167 insertions(+), 43 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d8894b40..c4d890544 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/include/msgpack/object.h b/include/msgpack/object.h index f7d641157..b92b985fb 100644 --- a/include/msgpack/object.h +++ b/include/msgpack/object.h @@ -98,7 +98,37 @@ typedef struct msgpack_object_kv { } msgpack_object_kv; MSGPACK_DLLEXPORT -bool msgpack_object_init(msgpack_object* d, int type, void* data, size_t size); +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 diff --git a/src/objectc.c b/src/objectc.c index 200811770..c33ab2b9b 100644 --- a/src/objectc.c +++ b/src/objectc.c @@ -125,41 +125,70 @@ int msgpack_pack_object(msgpack_packer* pk, msgpack_object d) } } -bool msgpack_object_init(msgpack_object* d, int type, void* data, size_t size) -{ - switch (type) { - case MSGPACK_OBJECT_STR: - { - d->via.str.ptr = (char *)data; - d->via.str.size = size; - break; - } - case MSGPACK_OBJECT_BIN: - { - d->via.bin.ptr = (char *)data; - d->via.bin.size = size; - break; - } - case MSGPACK_OBJECT_ARRAY: - { - d->via.array.ptr = (msgpack_object *)data; - d->via.array.size = size; - break; - } - case MSGPACK_OBJECT_MAP: - { - d->via.map.ptr = (msgpack_object_kv *)data; - d->via.map.size = size; - break; - } - default: - { - // Other types are not supported and need to be initialized manually. - return false; - } +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; } - d->type = type; - return true; +} + +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) diff --git a/test/msgpack_c.cpp b/test/msgpack_c.cpp index 4949a8e78..166c2b482 100644 --- a/test/msgpack_c.cpp +++ b/test/msgpack_c.cpp @@ -1612,10 +1612,61 @@ 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(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(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(&obj, MSGPACK_OBJECT_STR, (void *)buffer, strlen(buffer)); + msgpack_object_init_str(&obj, buffer, (uint32_t)strlen(buffer)); EXPECT_EQ(MSGPACK_OBJECT_STR, obj.type); EXPECT_STREQ(buffer, obj.via.str.ptr); } @@ -1623,20 +1674,29 @@ TEST(MSGPACKC, init_msgpack_obj_string) { TEST(MSGPACKC, init_msgpack_obj_bin) { msgpack_object obj; char buffer[] = "test"; - msgpack_object_init(&obj, MSGPACK_OBJECT_BIN, (void *)buffer, strlen(buffer)); + 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"}; - size_t buffer_size = 4; + uint32_t buffer_size = 4; msgpack_object array[buffer_size]; for(size_t i = 0; i < buffer_size; i++) { - msgpack_object_init(&array[i], MSGPACK_OBJECT_STR, (void *)buffer[i], strlen(buffer[i])); + msgpack_object_init_str(&array[i], buffer[i], (uint32_t)strlen(buffer[i])); } - msgpack_object_init(&obj, MSGPACK_OBJECT_ARRAY, (void *)array, buffer_size); + 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); @@ -1648,10 +1708,10 @@ TEST(MSGPACKC, init_msgpack_obj_map) { char key_str[] = "test_key"; char value_str[] = "test_value"; msgpack_object key,value; - msgpack_object_init(&key, MSGPACK_OBJECT_STR, (void *)key_str, strlen(key_str)); - msgpack_object_init(&value, MSGPACK_OBJECT_STR, (void *)value_str, strlen(value_str)); + 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(&obj, MSGPACK_OBJECT_MAP, (void *)&map, 1); + 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);