diff options
Diffstat (limited to 'unittests.c')
-rw-r--r-- | unittests.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/unittests.c b/unittests.c index aea00e5..38cbc5d 100644 --- a/unittests.c +++ b/unittests.c @@ -8,6 +8,9 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> #include "dynstr.h" @@ -996,6 +999,43 @@ static int test_fwrite_writes_string(void) } +static int test_write_writes_string(void) +{ + char tempname[] = "unittest.XXXXXX"; + int fd; + int fd2; + size_t num_bytes; + Dynstr *dynstr; + Dynstr *dynstr2; + char buf[1024]; + int read_bytes; + + dynstr = new_from_cstring("life is too short for str* and mem* in apps"); + + fd = mkstemp(tempname); + if (fd == -1) + abort(); + + fd2 = open(tempname, O_WRONLY, 0); + num_bytes = dynstr_write(fd2, dynstr); + close(fd2); + + FAIL_UNLESS_EQUAL(num_bytes, dynstr_len(dynstr)); + + /* FIXME: should compare contents of strings, too */ + read_bytes = read(fd, buf, sizeof(buf)); + if (read_bytes == -1) + abort(); + close(fd); + dynstr2 = dynstr_new_from_memory(buf, read_bytes); + FAIL_UNLESS_EQUAL(read_bytes, dynstr_len(dynstr)); + FAIL_UNLESS_EQUAL(dynstr_cmp(dynstr, dynstr2), 0); + dynstr_free(dynstr2); + + return true; +} + + static void setup(void) { dynstr_init(); @@ -1083,6 +1123,7 @@ static const struct test tests[] = { TEST(test_cmp_returns_negative_if_first_string_comes_first), TEST(test_cmp_returns_positive_if_second_string_comes_first), TEST(test_fwrite_writes_string), + TEST(test_write_writes_string), }; static const int num_tests = sizeof(tests) / sizeof(tests[0]); |