diff options
author | Edward Thomson <ethomson@microsoft.com> | 2015-02-11 07:55:07 +0300 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2015-02-13 06:54:47 +0300 |
commit | 4aa664ae3953d99c2ae4cd769f02818bc122eebc (patch) | |
tree | 7316e3d7febcbcde19dd4844af9503f4c6d57d69 /tests/buf | |
parent | 3603cb0978b7ef21ff9cd63693ebd6d27bc2bc53 (diff) |
git_buf_grow_by: increase buf asize incrementally
Introduce `git_buf_grow_by` to incrementally increase the size of a
`git_buf`, performing an overflow calculation on the growth.
Diffstat (limited to 'tests/buf')
-rw-r--r-- | tests/buf/basic.c | 20 | ||||
-rw-r--r-- | tests/buf/oom.c | 10 |
2 files changed, 30 insertions, 0 deletions
diff --git a/tests/buf/basic.c b/tests/buf/basic.c index d558757a9..dcc0916d2 100644 --- a/tests/buf/basic.c +++ b/tests/buf/basic.c @@ -15,6 +15,26 @@ void test_buf_basic__resize(void) git_buf_free(&buf1); } +void test_buf_basic__resize_incremental(void) +{ + git_buf buf1 = GIT_BUF_INIT; + + /* Presently, asking for 6 bytes will round up to 8. */ + cl_git_pass(git_buf_puts(&buf1, "Hello")); + cl_assert_equal_i(5, buf1.size); + cl_assert_equal_i(8, buf1.asize); + + /* Ensure an additional byte does not realloc. */ + cl_git_pass(git_buf_grow_by(&buf1, 1)); + cl_assert_equal_i(5, buf1.size); + cl_assert_equal_i(8, buf1.asize); + + /* But requesting many does. */ + cl_git_pass(git_buf_grow_by(&buf1, 16)); + cl_assert_equal_i(5, buf1.size); + cl_assert(buf1.asize > 8); +} + void test_buf_basic__printf(void) { git_buf buf2 = GIT_BUF_INIT; diff --git a/tests/buf/oom.c b/tests/buf/oom.c index 709439aa5..b9fd29cbb 100644 --- a/tests/buf/oom.c +++ b/tests/buf/oom.c @@ -29,3 +29,13 @@ void test_buf_oom__grow(void) git_buf_free(&buf); } + +void test_buf_oom__grow_by(void) +{ + git_buf buf = GIT_BUF_INIT; + + buf.size = SIZE_MAX-10; + + cl_assert(git_buf_grow_by(&buf, 50) == -1); + cl_assert(git_buf_oom(&buf)); +} |