diff options
author | Lars Wirzenius <liw@liw.fi> | 2010-10-22 21:51:26 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2010-10-22 21:51:26 +0100 |
commit | 92274449dd371313252bd36c99b85aef29b8d30a (patch) | |
tree | 79a6b39212186a16f6e51239e6235bd1b59be9d6 | |
parent | 1eae44b8ba5e2683d1d15eec8ae340f6951ff242 (diff) | |
download | dynstr-92274449dd371313252bd36c99b85aef29b8d30a.tar.gz |
Implement dynstr_byte_at.
-rw-r--r-- | dynstr.c | 10 | ||||
-rw-r--r-- | unittests.c | 24 |
2 files changed, 33 insertions, 1 deletions
@@ -9,7 +9,7 @@ struct Dynstr { - const char *mem; + const unsigned char *mem; size_t size; bool dynamic; }; @@ -197,3 +197,11 @@ Dynstr *dynstr_cat(Dynstr *dynstr, ...) return result; } + +int dynstr_byte_at(Dynstr *dynstr, size_t offset) +{ + if (offset >= dynstr->size) + return -1; + return dynstr->mem[offset]; +} + diff --git a/unittests.c b/unittests.c index e6caa0a..fcd740b 100644 --- a/unittests.c +++ b/unittests.c @@ -518,6 +518,28 @@ static int test_cat_returns_NULL_for_second_malloc_failure(void) } +static int test_byteat_reports_correct_character(void) +{ + Dynstr *dynstr; + + dynstr = dynstr_new_from_cstring("abc"); + FAIL_UNLESS_EQUAL(dynstr_byte_at(dynstr, 1), 'b'); + dynstr_free(dynstr); + return true; +} + + +static int test_byteat_reports_error_for_too_large_offset(void) +{ + Dynstr *dynstr; + + dynstr = dynstr_new_from_cstring("abc"); + FAIL_UNLESS_EQUAL(dynstr_byte_at(dynstr, 1024), -1); + dynstr_free(dynstr); + return true; +} + + static void setup(void) { dynstr_init(); @@ -570,6 +592,8 @@ static const struct test tests[] = { TEST(test_cats_ok), TEST(test_cat_returns_NULL_for_first_malloc_failure), TEST(test_cat_returns_NULL_for_second_malloc_failure), + TEST(test_byteat_reports_correct_character), + TEST(test_byteat_reports_error_for_too_large_offset), }; static const int num_tests = sizeof(tests) / sizeof(tests[0]); |