From dc63cba068f760980e2249e2b932dc49cc1df41c Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 25 Oct 2010 10:13:31 +0100 Subject: Implement dynstr_fwrite. --- dynstr.c | 6 ++++++ unittests.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/dynstr.c b/dynstr.c index 66da740..b3d4cbd 100644 --- a/dynstr.c +++ b/dynstr.c @@ -266,3 +266,9 @@ size_t dynstr_last_string(Dynstr *dynstr, size_t offset, Dynstr *pattern) return result; } + +size_t dynstr_fwrite(FILE *file, Dynstr *dynstr) +{ + return fwrite(dynstr->mem, 1, dynstr->size, file); +} + diff --git a/unittests.c b/unittests.c index 177b954..0a44542 100644 --- a/unittests.c +++ b/unittests.c @@ -1,4 +1,5 @@ #define _POSIX_C_SOURCE 200112L +#define _GNU_SOURCE #include #include @@ -6,6 +7,7 @@ #include #include #include +#include #include "dynstr.h" @@ -874,6 +876,33 @@ static int test_last_string_does_not_find_empty_pattern(void) } +static int test_fwrite_writes_string(void) +{ + FILE *f; + char tempname[] = "unittest.XXXXXX"; + int fd; + size_t num_bytes; + Dynstr *dynstr; + + dynstr = new_from_cstring("life is too short for str* and mem* in apps"); + + fd = mkstemp(tempname); + if (fd == -1) + abort(); + close(fd); + + f = fopen(tempname, "w"); + num_bytes = dynstr_fwrite(f, dynstr); + fclose(f); + + FAIL_UNLESS_EQUAL(num_bytes, dynstr_len(dynstr)); + + /* FIXME: should compare contents of strings, too */ + + return true; +} + + static void setup(void) { dynstr_init(); @@ -954,6 +983,7 @@ static const struct test tests[] = { TEST(test_last_string_does_not_find_pattern_outside_range), TEST(test_last_string_only_finds_pattern_inside_range), TEST(test_last_string_does_not_find_empty_pattern), + TEST(test_fwrite_writes_string), }; static const int num_tests = sizeof(tests) / sizeof(tests[0]); -- cgit v1.2.1