#include <check.h>
+#include <float.h>
#include <stdlib.h>
#include "common.h"
}
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;
}
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;
+}