Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### v0.2.1

* New floating type append functions
* Updated header include guard macro name

### v0.2.0 (2022-02-06)
Expand Down
10 changes: 10 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
3 changes: 3 additions & 0 deletions include/stringbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

18 changes: 18 additions & 0 deletions src/stringbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_append_double.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "stringbuffer.h"
#include "test.h"
#include <stdlib.h>


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);
}

27 changes: 27 additions & 0 deletions tests/test_append_float.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "stringbuffer.h"
#include "test.h"
#include <stdlib.h>


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);
}

27 changes: 27 additions & 0 deletions tests/test_append_long_double.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "stringbuffer.h"
#include "test.h"
#include <stdlib.h>


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);
}