From b4a3d8884769126e6aee69aaff5ea35476eb5d7a Mon Sep 17 00:00:00 2001 From: Ahmet Artu Yildirim Date: Wed, 21 Dec 2022 22:47:07 -0800 Subject: [PATCH] Add floating-point getters and setters --- src/check_progbuf.c | 94 ++++++++++++++++++++++++++++++++++++++--- src/progbuf.c | 100 ++++++++++++++++++++++++++++++++++++++++++++ src/progbuf.h | 6 +++ 3 files changed, 195 insertions(+), 5 deletions(-) diff --git a/src/check_progbuf.c b/src/check_progbuf.c index 822df87..b02dcfa 100644 --- a/src/check_progbuf.c +++ b/src/check_progbuf.c @@ -1,4 +1,5 @@ #include +#include #include #include "common.h" @@ -414,38 +415,121 @@ START_TEST (test_progbuf_load_from_buffer) } END_TEST +START_TEST (test_progbuf_write_read_float) +{ + int ret; + + progbuf_h buf = progbuf_alloc (1); + + ck_assert (buf); + + float val = FLT_MAX; + float p_val = val; + + ret = progbuf_set_float (buf, p_val); + ck_assert (ret == PROGBUF_SUCCESS); + + size_t size; + ret = progbuf_buffer_size (buf, &size); + ck_assert (ret == PROGBUF_SUCCESS); + ck_assert (size == 6); + + progbuf_it_h iter = progbuf_iter_alloc (buf); + + ck_assert (iter); + + ret = progbuf_get_float (iter, &p_val); + + ck_assert (ret == PROGBUF_SUCCESS); + ck_assert (val == p_val); + + struct progbuf_it_s *iter_internal = iter; + + ck_assert (iter_internal->read_pos == 6); + + ck_assert (ret == PROGBUF_SUCCESS); + + ret = progbuf_iter_free (iter); + ck_assert (ret == PROGBUF_SUCCESS); + + ret = progbuf_free (buf); + ck_assert (ret == PROGBUF_SUCCESS); +} + +START_TEST (test_progbuf_write_read_double) +{ + int ret; + + progbuf_h buf = progbuf_alloc (1); + + ck_assert (buf); + + double val = DBL_MAX; + double p_val = val; + + ret = progbuf_set_double (buf, p_val); + ck_assert (ret == PROGBUF_SUCCESS); + + size_t size; + ret = progbuf_buffer_size (buf, &size); + ck_assert (ret == PROGBUF_SUCCESS); + ck_assert (size == 10); + + progbuf_it_h iter = progbuf_iter_alloc (buf); + + ck_assert (iter); + + ret = progbuf_get_double (iter, &p_val); + + ck_assert (ret == PROGBUF_SUCCESS); + ck_assert (val == p_val); + + struct progbuf_it_s *iter_internal = iter; + + ck_assert (iter_internal->read_pos == 10); + + ck_assert (ret == PROGBUF_SUCCESS); + + ret = progbuf_iter_free (iter); + ck_assert (ret == PROGBUF_SUCCESS); + + ret = progbuf_free (buf); + ck_assert (ret == PROGBUF_SUCCESS); +} + static Suite * progbuf_suite (void) { Suite *s; - TCase *tc_basic, *tc_long, *tc_longlong, *tc_load; + TCase *tc_basic, *tc_long, *tc_longlong, *tc_load, *tc_float; s = suite_create ("progbuf test suite"); tc_basic = tcase_create ("basic"); - tcase_add_test (tc_basic, test_progbuf_alloc_version); tc_long = tcase_create ("encode_long"); - tcase_add_test (tc_long, test_progbuf_write_read_positive_long); tcase_add_test (tc_long, test_progbuf_write_read_negative_long); tcase_add_test (tc_long, test_progbuf_write_read_ulong); tc_longlong = tcase_create ("encode_longlong"); - tcase_add_test (tc_longlong, test_progbuf_write_read_positive_longlong); tcase_add_test (tc_longlong, test_progbuf_write_read_negative_longlong); tcase_add_test (tc_longlong, test_progbuf_write_read_ulonglong); tc_load = tcase_create ("load_from_buffer"); - tcase_add_test (tc_load, test_progbuf_load_from_buffer); + tc_float = tcase_create ("encode_floating_point"); + tcase_add_test (tc_float, test_progbuf_write_read_float); + tcase_add_test (tc_float, test_progbuf_write_read_double); + suite_add_tcase (s, tc_basic); suite_add_tcase (s, tc_long); suite_add_tcase (s, tc_longlong); suite_add_tcase (s, tc_load); + suite_add_tcase (s, tc_float); return s; } diff --git a/src/progbuf.c b/src/progbuf.c index 1128820..56f17ec 100644 --- a/src/progbuf.c +++ b/src/progbuf.c @@ -399,3 +399,103 @@ progbuf_iter_reset (progbuf_it_h iter) return PROGBUF_SUCCESS; } + +int +progbuf_set_float (progbuf_h buf, float value) +{ + int ret; + + if (!buf) + return PROGBUF_ERROR_NULL_PARAM; + + if (!buf->buffer) + return PROGBUF_ERROR_NOT_OWNING; + + ret = check_buffer_and_expand (buf, 5); + if (ret != 0) + return ret; + + buf->buffer[buf->size] = PROGBUF_TYPE_FLOAT32; + buf->size++; + + memcpy (buf->buffer + buf->size, &value, 4); + buf->size += 4; + + return PROGBUF_SUCCESS; +} + +int +progbuf_get_float (progbuf_it_h iter, float *value) +{ + if (!iter || !value) + return PROGBUF_ERROR_NULL_PARAM; + + if (!iter->buf->buffer) + return PROGBUF_ERROR_NOT_OWNING; + + if (iter->read_pos >= iter->buf->size) + return PROGBUF_ERROR_END_OF_ITER; + + char val_type = iter->buf->buffer[iter->read_pos]; + + if ((val_type & PROGBUF_TYPE_FLOAT32) != PROGBUF_TYPE_FLOAT32) + return PROGBUF_ERROR_UNEXPECTED_TYPE; + + iter->read_pos++; + + memcpy (value, iter->buf->buffer + iter->read_pos, 4); + + iter->read_pos += 4; + + return PROGBUF_SUCCESS; +} + +int +progbuf_set_double (progbuf_h buf, double value) +{ + int ret; + + if (!buf) + return PROGBUF_ERROR_NULL_PARAM; + + if (!buf->buffer) + return PROGBUF_ERROR_NOT_OWNING; + + ret = check_buffer_and_expand (buf, 9); + if (ret != 0) + return ret; + + buf->buffer[buf->size] = PROGBUF_TYPE_FLOAT64; + buf->size++; + + memcpy (buf->buffer + buf->size, &value, 8); + buf->size += 8; + + return PROGBUF_SUCCESS; +} + +int +progbuf_get_double (progbuf_it_h iter, double *value) +{ + if (!iter || !value) + return PROGBUF_ERROR_NULL_PARAM; + + if (!iter->buf->buffer) + return PROGBUF_ERROR_NOT_OWNING; + + if (iter->read_pos >= iter->buf->size) + return PROGBUF_ERROR_END_OF_ITER; + + char val_type = iter->buf->buffer[iter->read_pos]; + + if ((val_type & PROGBUF_TYPE_FLOAT64) != PROGBUF_TYPE_FLOAT64) + return PROGBUF_ERROR_UNEXPECTED_TYPE; + + iter->read_pos++; + + memcpy (value, iter->buf->buffer + iter->read_pos, 8); + + iter->read_pos += 8; + + return PROGBUF_SUCCESS; +} diff --git a/src/progbuf.h b/src/progbuf.h index 1e54c71..0cff17c 100644 --- a/src/progbuf.h +++ b/src/progbuf.h @@ -39,6 +39,12 @@ int progbuf_get_longlong (progbuf_it_h iter, long long *value); int progbuf_set_ulonglong (progbuf_h buf, unsigned long long value); int progbuf_get_ulonglong (progbuf_it_h iter, unsigned long long *value); +int progbuf_set_float (progbuf_h buf, float value); +int progbuf_get_float (progbuf_it_h iter, float *value); + +int progbuf_set_double (progbuf_h buf, double value); +int progbuf_get_double (progbuf_it_h iter, double *value); + progbuf_it_h progbuf_iter_alloc (progbuf_h buf); int progbuf_iter_free (progbuf_it_h iter); int progbuf_iter_reset (progbuf_it_h iter); -- 2.20.1