summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-10-22 14:22:30 +0100
committerLars Wirzenius <liw@liw.fi>2010-10-22 14:22:30 +0100
commit8dda24d8c82332f03cd280d8a8df5f16c48ad63c (patch)
tree460d01473640b3f3be02449e2167b01cc2112d59
parent1870ec38019d65d39c1b0aabfdf91b240f24e3be (diff)
downloaddynstr-8dda24d8c82332f03cd280d8a8df5f16c48ad63c.tar.gz
Implement dynstr_substr.
-rw-r--r--dynstr.c12
-rw-r--r--unittests.c117
2 files changed, 129 insertions, 0 deletions
diff --git a/dynstr.c b/dynstr.c
index ad03f7a..1d15a1c 100644
--- a/dynstr.c
+++ b/dynstr.c
@@ -141,3 +141,15 @@ size_t dynstr_memcpy(void *mem, Dynstr *dynstr, size_t offset, size_t size)
return size;
}
+Dynstr *dynstr_substr(Dynstr *dynstr, size_t offset, size_t size)
+{
+ if (offset >= dynstr->size) {
+ offset = 0;
+ size = 0;
+ } else if (size > dynstr->size - offset) {
+ size = dynstr->size - offset;
+ }
+ return new(dynstr->mem + offset, size, true);
+}
+
+
diff --git a/unittests.c b/unittests.c
index 6d0d2c9..a17686c 100644
--- a/unittests.c
+++ b/unittests.c
@@ -325,6 +325,116 @@ static int test_memcpy_copies_substring_ok(void)
}
+static int test_copies_empty_substring_ok(void)
+{
+ Dynstr *dynstr;
+ Dynstr *sub;
+ size_t size;
+ char buf[1024];
+
+ dynstr = dynstr_new_from_cstring("abcdef");
+ sub = dynstr_substr(dynstr, 1, 0);
+ size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub));
+ FAIL_UNLESS_EQUAL(size, 0);
+ return true;
+}
+
+
+static int test_copies_single_byte_substring_ok(void)
+{
+ Dynstr *dynstr;
+ Dynstr *sub;
+ size_t size;
+ char buf[1024];
+
+ dynstr = dynstr_new_from_cstring("abcdef");
+ sub = dynstr_substr(dynstr, 1, 1);
+ size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub));
+ FAIL_UNLESS_EQUAL(size, 1);
+ FAIL_UNLESS_EQUAL(memcmp(buf, "b", 1), 0);
+ return true;
+}
+
+
+static int test_copies_middle_substring_ok(void)
+{
+ Dynstr *dynstr;
+ Dynstr *sub;
+ size_t size;
+ char buf[1024];
+
+ dynstr = dynstr_new_from_cstring("abcdef");
+ sub = dynstr_substr(dynstr, 1, 4);
+ size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub));
+ FAIL_UNLESS_EQUAL(size, 4);
+ FAIL_UNLESS_EQUAL(memcmp(buf, "bcde", 4), 0);
+ return true;
+}
+
+
+static int test_copies_initial_substring_ok(void)
+{
+ Dynstr *dynstr;
+ Dynstr *sub;
+ size_t size;
+ char buf[1024];
+
+ dynstr = dynstr_new_from_cstring("abcdef");
+ sub = dynstr_substr(dynstr, 0, 5);
+ size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub));
+ FAIL_UNLESS_EQUAL(size, 5);
+ FAIL_UNLESS_EQUAL(memcmp(buf, "abcde", 5), 0);
+ return true;
+}
+
+
+static int test_copies_final_substring_ok(void)
+{
+ Dynstr *dynstr;
+ Dynstr *sub;
+ size_t size;
+ char buf[1024];
+
+ dynstr = dynstr_new_from_cstring("abcdef");
+ sub = dynstr_substr(dynstr, 1, 5);
+ size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub));
+ FAIL_UNLESS_EQUAL(size, 5);
+ FAIL_UNLESS_EQUAL(memcmp(buf, "bcdef", 5), 0);
+ return true;
+}
+
+
+static int test_copies_empty_substring_when_offset_too_big(void)
+{
+ Dynstr *dynstr;
+ Dynstr *sub;
+ size_t size;
+ char buf[1024];
+
+ dynstr = dynstr_new_from_cstring("abcdef");
+ sub = dynstr_substr(dynstr, 100, 1);
+ size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub));
+ FAIL_UNLESS_EQUAL(size, 0);
+ return true;
+}
+
+
+static int test_copies_partial_substring_when_length_too_big(void)
+{
+ Dynstr *dynstr;
+ Dynstr *sub;
+ size_t size;
+ char buf[1024];
+
+ dynstr = dynstr_new_from_cstring("abcdef");
+ sub = dynstr_substr(dynstr, 1, 6);
+ size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub));
+ FAIL_UNLESS_EQUAL(size, 5);
+ FAIL_UNLESS_EQUAL(memcmp(buf, "bcdef", 5), 0);
+ return true;
+}
+
+
static void setup(void)
{
dynstr_init();
@@ -367,6 +477,13 @@ static const struct test tests[] = {
TEST(test_memcpy_truncates_if_copying_too_much),
TEST(test_memcpy_copies_whole_string_ok),
TEST(test_memcpy_copies_substring_ok),
+ TEST(test_copies_empty_substring_ok),
+ TEST(test_copies_single_byte_substring_ok),
+ TEST(test_copies_middle_substring_ok),
+ TEST(test_copies_initial_substring_ok),
+ TEST(test_copies_final_substring_ok),
+ TEST(test_copies_empty_substring_when_offset_too_big),
+ TEST(test_copies_partial_substring_when_length_too_big),
};
static const int num_tests = sizeof(tests) / sizeof(tests[0]);