From e0818eeb79ad06c79364fc672e4b43a040c7a916 Mon Sep 17 00:00:00 2001 From: sagie gur ari Date: Fri, 3 Feb 2023 14:04:45 +0000 Subject: [PATCH] New floating type append functions --- CHANGELOG.md | 1 + build.sh | 10 ++++++++++ include/stringbuffer.h | 3 +++ src/stringbuffer.c | 18 ++++++++++++++++++ tests/test_append_double.c | 27 +++++++++++++++++++++++++++ tests/test_append_float.c | 27 +++++++++++++++++++++++++++ tests/test_append_long_double.c | 27 +++++++++++++++++++++++++++ 7 files changed, 113 insertions(+) create mode 100644 tests/test_append_double.c create mode 100644 tests/test_append_float.c create mode 100644 tests/test_append_long_double.c diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b50fe5..b817963 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### v0.2.1 +* New floating type append functions * Updated header include guard macro name ### v0.2.0 (2022-02-06) diff --git a/build.sh b/build.sh index 509fdaf..ac0b0f7 100755 --- a/build.sh +++ b/build.sh @@ -9,11 +9,21 @@ mkdir ./target cd ./target +set -e + cmake .. && make && ctest -C Release --output-on-failure if [ "$1" = "--memcheck" ]; then # memory check ctest -T memcheck || true +elif [ "$1" = "--valgrind" ]; then + # memory check + for testfile in ./bin/test_* + do + echo "------------------------------------------------" + echo "Testing ${testfile}" + valgrind --leak-check=yes --undef-value-errors=no --error-exitcode=1 "${testfile}" + done fi cd - diff --git a/include/stringbuffer.h b/include/stringbuffer.h index 1f5df4a..99b06a6 100644 --- a/include/stringbuffer.h +++ b/include/stringbuffer.h @@ -40,6 +40,9 @@ bool stringbuffer_append_unsigned_short(struct StringBuffer *, unsigned short); bool stringbuffer_append_unsigned_int(struct StringBuffer *, unsigned int); bool stringbuffer_append_unsigned_long(struct StringBuffer *, unsigned long); bool stringbuffer_append_unsigned_long_long(struct StringBuffer *, unsigned long long); +bool stringbuffer_append_float(struct StringBuffer *, float); +bool stringbuffer_append_double(struct StringBuffer *, double); +bool stringbuffer_append_long_double(struct StringBuffer *, long double); #endif diff --git a/src/stringbuffer.c b/src/stringbuffer.c index 6f78671..844baa8 100644 --- a/src/stringbuffer.c +++ b/src/stringbuffer.c @@ -357,6 +357,24 @@ bool stringbuffer_append_unsigned_long_long(struct StringBuffer *buffer, unsigne } +bool stringbuffer_append_float(struct StringBuffer *buffer, float value) +{ + return(_stringbuffer_add_numeric_type(buffer, "%f", value)); +} + + +bool stringbuffer_append_double(struct StringBuffer *buffer, double value) +{ + return(_stringbuffer_add_numeric_type(buffer, "%f", value)); +} + + +bool stringbuffer_append_long_double(struct StringBuffer *buffer, long double value) +{ + return(_stringbuffer_add_numeric_type(buffer, "%Lf", value)); +} + + static bool _stringbuffer_clear(struct StringBuffer *buffer) { if (buffer == NULL) diff --git a/tests/test_append_double.c b/tests/test_append_double.c new file mode 100644 index 0000000..9644118 --- /dev/null +++ b/tests/test_append_double.c @@ -0,0 +1,27 @@ +#include "stringbuffer.h" +#include "test.h" +#include + + +void test_impl() +{ + struct StringBuffer *buffer = stringbuffer_new_with_options(100, true); + + assert_true(stringbuffer_append_double(buffer, (double)1.5)); + + assert_num_equal(stringbuffer_get_initial_size(buffer), 100); + assert_num_equal(stringbuffer_get_max_size(buffer), 100); + + char *content = stringbuffer_to_string(buffer); + assert_string_equal(content, "1.500000"); + + stringbuffer_release(buffer); + free(content); +} + + +int main() +{ + test_run(test_impl); +} + diff --git a/tests/test_append_float.c b/tests/test_append_float.c new file mode 100644 index 0000000..1c9af9a --- /dev/null +++ b/tests/test_append_float.c @@ -0,0 +1,27 @@ +#include "stringbuffer.h" +#include "test.h" +#include + + +void test_impl() +{ + struct StringBuffer *buffer = stringbuffer_new_with_options(100, true); + + assert_true(stringbuffer_append_float(buffer, 1.5f)); + + assert_num_equal(stringbuffer_get_initial_size(buffer), 100); + assert_num_equal(stringbuffer_get_max_size(buffer), 100); + + char *content = stringbuffer_to_string(buffer); + assert_string_equal(content, "1.500000"); + + stringbuffer_release(buffer); + free(content); +} + + +int main() +{ + test_run(test_impl); +} + diff --git a/tests/test_append_long_double.c b/tests/test_append_long_double.c new file mode 100644 index 0000000..48abcc9 --- /dev/null +++ b/tests/test_append_long_double.c @@ -0,0 +1,27 @@ +#include "stringbuffer.h" +#include "test.h" +#include + + +void test_impl() +{ + struct StringBuffer *buffer = stringbuffer_new_with_options(100, true); + + assert_true(stringbuffer_append_long_double(buffer, 1.5L)); + + assert_num_equal(stringbuffer_get_initial_size(buffer), 100); + assert_num_equal(stringbuffer_get_max_size(buffer), 100); + + char *content = stringbuffer_to_string(buffer); + assert_string_equal(content, "1.500000"); + + stringbuffer_release(buffer); + free(content); +} + + +int main() +{ + test_run(test_impl); +} +