Skip to content

Commit e0818ee

Browse files
committed
New floating type append functions
1 parent 93a357f commit e0818ee

File tree

7 files changed

+113
-0
lines changed

7 files changed

+113
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### v0.2.1
44

5+
* New floating type append functions
56
* Updated header include guard macro name
67

78
### v0.2.0 (2022-02-06)

build.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@ mkdir ./target
99

1010
cd ./target
1111

12+
set -e
13+
1214
cmake .. && make && ctest -C Release --output-on-failure
1315

1416
if [ "$1" = "--memcheck" ]; then
1517
# memory check
1618
ctest -T memcheck || true
19+
elif [ "$1" = "--valgrind" ]; then
20+
# memory check
21+
for testfile in ./bin/test_*
22+
do
23+
echo "------------------------------------------------"
24+
echo "Testing ${testfile}"
25+
valgrind --leak-check=yes --undef-value-errors=no --error-exitcode=1 "${testfile}"
26+
done
1727
fi
1828

1929
cd -

include/stringbuffer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ bool stringbuffer_append_unsigned_short(struct StringBuffer *, unsigned short);
4040
bool stringbuffer_append_unsigned_int(struct StringBuffer *, unsigned int);
4141
bool stringbuffer_append_unsigned_long(struct StringBuffer *, unsigned long);
4242
bool stringbuffer_append_unsigned_long_long(struct StringBuffer *, unsigned long long);
43+
bool stringbuffer_append_float(struct StringBuffer *, float);
44+
bool stringbuffer_append_double(struct StringBuffer *, double);
45+
bool stringbuffer_append_long_double(struct StringBuffer *, long double);
4346

4447
#endif
4548

src/stringbuffer.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,24 @@ bool stringbuffer_append_unsigned_long_long(struct StringBuffer *buffer, unsigne
357357
}
358358

359359

360+
bool stringbuffer_append_float(struct StringBuffer *buffer, float value)
361+
{
362+
return(_stringbuffer_add_numeric_type(buffer, "%f", value));
363+
}
364+
365+
366+
bool stringbuffer_append_double(struct StringBuffer *buffer, double value)
367+
{
368+
return(_stringbuffer_add_numeric_type(buffer, "%f", value));
369+
}
370+
371+
372+
bool stringbuffer_append_long_double(struct StringBuffer *buffer, long double value)
373+
{
374+
return(_stringbuffer_add_numeric_type(buffer, "%Lf", value));
375+
}
376+
377+
360378
static bool _stringbuffer_clear(struct StringBuffer *buffer)
361379
{
362380
if (buffer == NULL)

tests/test_append_double.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "stringbuffer.h"
2+
#include "test.h"
3+
#include <stdlib.h>
4+
5+
6+
void test_impl()
7+
{
8+
struct StringBuffer *buffer = stringbuffer_new_with_options(100, true);
9+
10+
assert_true(stringbuffer_append_double(buffer, (double)1.5));
11+
12+
assert_num_equal(stringbuffer_get_initial_size(buffer), 100);
13+
assert_num_equal(stringbuffer_get_max_size(buffer), 100);
14+
15+
char *content = stringbuffer_to_string(buffer);
16+
assert_string_equal(content, "1.500000");
17+
18+
stringbuffer_release(buffer);
19+
free(content);
20+
}
21+
22+
23+
int main()
24+
{
25+
test_run(test_impl);
26+
}
27+

tests/test_append_float.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "stringbuffer.h"
2+
#include "test.h"
3+
#include <stdlib.h>
4+
5+
6+
void test_impl()
7+
{
8+
struct StringBuffer *buffer = stringbuffer_new_with_options(100, true);
9+
10+
assert_true(stringbuffer_append_float(buffer, 1.5f));
11+
12+
assert_num_equal(stringbuffer_get_initial_size(buffer), 100);
13+
assert_num_equal(stringbuffer_get_max_size(buffer), 100);
14+
15+
char *content = stringbuffer_to_string(buffer);
16+
assert_string_equal(content, "1.500000");
17+
18+
stringbuffer_release(buffer);
19+
free(content);
20+
}
21+
22+
23+
int main()
24+
{
25+
test_run(test_impl);
26+
}
27+

tests/test_append_long_double.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "stringbuffer.h"
2+
#include "test.h"
3+
#include <stdlib.h>
4+
5+
6+
void test_impl()
7+
{
8+
struct StringBuffer *buffer = stringbuffer_new_with_options(100, true);
9+
10+
assert_true(stringbuffer_append_long_double(buffer, 1.5L));
11+
12+
assert_num_equal(stringbuffer_get_initial_size(buffer), 100);
13+
assert_num_equal(stringbuffer_get_max_size(buffer), 100);
14+
15+
char *content = stringbuffer_to_string(buffer);
16+
assert_string_equal(content, "1.500000");
17+
18+
stringbuffer_release(buffer);
19+
free(content);
20+
}
21+
22+
23+
int main()
24+
{
25+
test_run(test_impl);
26+
}
27+

0 commit comments

Comments
 (0)