diff options
author | Lars Wirzenius <liw@liw.fi> | 2010-10-25 10:13:31 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2010-10-25 10:13:31 +0100 |
commit | dc63cba068f760980e2249e2b932dc49cc1df41c (patch) | |
tree | 096c5b44cf414e78fa4eafdcb4905eb4a4447d91 | |
parent | 4367eab8f75c452f559039c84db464571fef3e41 (diff) | |
download | dynstr-dc63cba068f760980e2249e2b932dc49cc1df41c.tar.gz |
Implement dynstr_fwrite.
-rw-r--r-- | dynstr.c | 6 | ||||
-rw-r--r-- | unittests.c | 30 |
2 files changed, 36 insertions, 0 deletions
@@ -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 <setjmp.h> #include <signal.h> @@ -6,6 +7,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #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]); |