From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp46.i.mail.ru (smtp46.i.mail.ru [94.100.177.106]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 12B4D469710 for ; Tue, 12 May 2020 02:45:57 +0300 (MSK) From: Vladislav Shpilevoy Date: Tue, 12 May 2020 01:45:49 +0200 Message-Id: <449685df941ca9a14e102738da413e737e559aea.1589240704.git.v.shpilevoy@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH 2/5] decimal: provide MP_DECIMAL extension serializer List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org, gorcunov@gmail.com, sergepetrenko@tarantool.org, korablev@tarantool.org Msgpuck functions mp_snprint() and mp_fprint() now support customizable MP_EXT serializer. This patch adds such for MP_DECIMAL. It is not activated yet, but will be later, when other extensions also get their serializers. Part of #4719 --- src/lib/core/mp_decimal.c | 18 +++++++++++ src/lib/core/mp_decimal.h | 27 +++++++++++++++++ test/unit/decimal.c | 63 ++++++++++++++++++++++++++++++++++++++- test/unit/decimal.result | 11 ++++++- 4 files changed, 117 insertions(+), 2 deletions(-) diff --git a/src/lib/core/mp_decimal.c b/src/lib/core/mp_decimal.c index 985e75291..ffc2c5773 100644 --- a/src/lib/core/mp_decimal.c +++ b/src/lib/core/mp_decimal.c @@ -70,3 +70,21 @@ mp_encode_decimal(char *data, const decimal_t *dec) data = decimal_pack(data, dec); return data; } + +int +mp_snprint_decimal(char *buf, int size, const char **data, uint32_t len) +{ + decimal_t d; + if (decimal_unpack(data, len, &d) == NULL) + return -1; + return snprintf(buf, size, "%s", decimal_to_string(&d)); +} + +int +mp_fprint_decimal(FILE *file, const char **data, uint32_t len) +{ + decimal_t d; + if (decimal_unpack(data, len, &d) == NULL) + return -1; + return fprintf(file, "%s", decimal_to_string(&d)); +} diff --git a/src/lib/core/mp_decimal.h b/src/lib/core/mp_decimal.h index 778529068..b8a327632 100644 --- a/src/lib/core/mp_decimal.h +++ b/src/lib/core/mp_decimal.h @@ -63,6 +63,33 @@ mp_decode_decimal(const char **data, decimal_t *dec); char * mp_encode_decimal(char *data, const decimal_t *dec); +/** + * Print decimal's string representation into a given buffer. + * @param buf Target buffer to write string to. + * @param size Buffer size. + * @param data MessagePack encoded decimal, without MP_EXT header. + * @param len Length of @a data. If not all data is used, it is + * an error. + * @retval <0 Error. Couldn't decode decimal. + * @retval >=0 How many bytes were written, or would have been + * written, if there was enough buffer space. + */ +int +mp_snprint_decimal(char *buf, int size, const char **data, uint32_t len); + +/** + * Print decimal's string representation into a stream. + * @param file Target stream to write string to. + * @param data MessagePack encoded decimal, without MP_EXT header. + * @param len Length of @a data. If not all data is used, it is + * an error. + * @retval <0 Error. Couldn't decode decimal, or couldn't write to + * the stream. + * @retval >=0 How many bytes were written. + */ +int +mp_fprint_decimal(FILE *file, const char **data, uint32_t len); + #if defined(__cplusplus) } /* extern "C" */ #endif /* defined(__cplusplus) */ diff --git a/test/unit/decimal.c b/test/unit/decimal.c index 254114b5f..179723d02 100644 --- a/test/unit/decimal.c +++ b/test/unit/decimal.c @@ -254,10 +254,70 @@ test_to_int(void) return check_plan(); } +static int +mp_fprint_ext_test(FILE *file, const char **data, int depth) +{ + (void)depth; + int8_t type; + uint32_t len = mp_decode_extl(data, &type); + if (type != MP_DECIMAL) + return fprintf(file, "undefined"); + return mp_fprint_decimal(file, data, len); +} + +static int +mp_snprint_ext_test(char *buf, int size, const char **data, int depth) +{ + (void)depth; + int8_t type; + uint32_t len = mp_decode_extl(data, &type); + if (type != MP_DECIMAL) + return snprintf(buf, size, "undefined"); + return mp_snprint_decimal(buf, size, data, len); +} + +static void +test_mp_print(void) +{ + plan(5); + header(); + + mp_snprint_ext = mp_snprint_ext_test; + mp_fprint_ext = mp_fprint_ext_test; + + char buffer[1024]; + char str[1024]; + const char *expected = "1.234"; + const int len = strlen(expected); + decimal_t d; + decimal_from_string(&d, expected); + mp_encode_decimal(buffer, &d); + int rc = mp_snprint(NULL, 0, buffer); + is(rc, len, "correct mp_snprint size with empty buffer"); + rc = mp_snprint(str, sizeof(str), buffer); + is(rc, len, "correct mp_snprint size"); + is(strcmp(str, expected), 0, "correct mp_snprint result"); + + FILE *f = tmpfile(); + rc = mp_fprint(f, buffer); + is(rc, len, "correct mp_fprint size"); + rewind(f); + rc = fread(str, 1, sizeof(str), f); + str[rc] = 0; + is(strcmp(str, expected), 0, "correct mp_fprint result"); + fclose(f); + + mp_snprint_ext = mp_snprint_ext_default; + mp_fprint_ext = mp_fprint_ext_default; + + footer(); + check_plan(); +} + int main(void) { - plan(281); + plan(282); dectest(314, 271, uint64, uint64_t); dectest(65535, 23456, uint64, uint64_t); @@ -322,6 +382,7 @@ main(void) test_pack_unpack(); test_mp_decimal(); + test_mp_print(); return check_plan(); } diff --git a/test/unit/decimal.result b/test/unit/decimal.result index e8432fb36..f11396df3 100644 --- a/test/unit/decimal.result +++ b/test/unit/decimal.result @@ -1,4 +1,4 @@ -1..281 +1..282 ok 1 - decimal(314) ok 2 - decimal(271) ok 3 - decimal(314) + decimal(271) @@ -698,3 +698,12 @@ ok 280 - subtests ok 197 - decimal_unpack() after mp_decode_extl() value ok 198 - decimal_unpack() after mp_decode_extl() len ok 281 - subtests + 1..5 + *** test_mp_print *** + ok 1 - correct mp_snprint size with empty buffer + ok 2 - correct mp_snprint size + ok 3 - correct mp_snprint result + ok 4 - correct mp_fprint size + ok 5 - correct mp_fprint result + *** test_mp_print: done *** +ok 282 - subtests -- 2.21.1 (Apple Git-122.3)