summaryrefslogtreecommitdiff
path: root/unittests.c
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-10-25 10:37:32 +0100
committerLars Wirzenius <liw@liw.fi>2010-10-25 10:37:32 +0100
commited77916173766327248d84453564f1ae05b05535 (patch)
treeadb0e4c58330deae014897fca913fd51b869833b /unittests.c
parent20da8374e9da7009da7fe7cfbf81387f6cc94dc6 (diff)
downloaddynstr-ed77916173766327248d84453564f1ae05b05535.tar.gz
Implement dynstr_write.
Changed the function signature since write(2) is so different from fwrite(3), and there's no point in trying to shoehorn the two into the same mold.
Diffstat (limited to 'unittests.c')
-rw-r--r--unittests.c41
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]);