summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-10-20 21:14:16 +0100
committerLars Wirzenius <liw@liw.fi>2010-10-20 21:14:16 +0100
commit70840f2aa13d91de3254c9358a27df974e059831 (patch)
treefeb27a713fe9be99043f8af1053f1253ff48f77b
parent228bdfa2eea2ba323d47c326dbcf2d53c02c7771 (diff)
downloaddynstr-70840f2aa13d91de3254c9358a27df974e059831.tar.gz
Implement dynstr_create_from_memory.
-rw-r--r--dynstr.c5
-rw-r--r--unittests.c18
2 files changed, 23 insertions, 0 deletions
diff --git a/dynstr.c b/dynstr.c
index f593df7..73426a3 100644
--- a/dynstr.c
+++ b/dynstr.c
@@ -92,6 +92,11 @@ Dynstr *dynstr_new_from_cstring(const char *cstring)
return new(cstring, strlen(cstring), true);
}
+Dynstr *dynstr_new_from_memory(const void *mem, size_t size)
+{
+ return new(mem, size, true);
+}
+
size_t dynstr_len(Dynstr *dynstr)
{
return dynstr->size;
diff --git a/unittests.c b/unittests.c
index 0e0285c..c97d2f7 100644
--- a/unittests.c
+++ b/unittests.c
@@ -118,6 +118,23 @@ static int test_creates_from_cstring(void)
}
+static int test_creates_from_memory(void)
+{
+ const char bytes[] = "asdfasdfafdasdfasdfqw4tb";
+ char newbytes[sizeof(bytes)];
+ Dynstr *dynstr;
+ size_t size;
+
+ dynstr = dynstr_new_from_memory(bytes, sizeof(bytes));
+ FAIL_UNLESS_EQUAL(dynstr_len(dynstr), sizeof(bytes));
+
+ size = dynstr_memcpy(newbytes, dynstr, 0, sizeof(bytes));
+ FAIL_UNLESS_EQUAL(size, sizeof(bytes));
+ FAIL_UNLESS_EQUAL(memcmp(bytes, newbytes, sizeof(bytes)), 0);
+ return true;
+}
+
+
struct test {
const char *name;
int (*test)(void);
@@ -133,6 +150,7 @@ static const struct test tests[] = {
TEST(test_abort_handler_calls_abort),
TEST(test_empty_string_is_empty),
TEST(test_creates_from_cstring),
+ TEST(test_creates_from_memory),
};
static const int num_tests = sizeof(tests) / sizeof(tests[0]);