diff options
author | EuAndreh <eu@euandre.org> | 2024-06-02 21:03:24 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-06-02 21:03:24 -0300 |
commit | 537a8851a7be615abfd9f1e2bb8c26699e8086b2 (patch) | |
tree | 76d8ae04b275a1d581d0f008a130a338d3ee98bc | |
parent | src/hash.c: Add thread safe initialization of SipHash seed (diff) | |
download | pindaiba-537a8851a7be615abfd9f1e2bb8c26699e8086b2.tar.gz pindaiba-537a8851a7be615abfd9f1e2bb8c26699e8086b2.tar.xz |
src/vector.h: Add vector_capacity()
Diffstat (limited to '')
-rw-r--r-- | src/vector.c | 5 | ||||
-rw-r--r-- | src/vector.h | 3 | ||||
-rw-r--r-- | tests/vector.c | 52 |
3 files changed, 60 insertions, 0 deletions
diff --git a/src/vector.c b/src/vector.c index e5f5949..143e6a1 100644 --- a/src/vector.c +++ b/src/vector.c @@ -116,6 +116,11 @@ vector_free(const struct Vector **const v) { } size_t +vector_capacity(const struct Vector *const v) { + return v->capacity; +} + +size_t vector_count(const struct Vector *const v) { return v->count; } diff --git a/src/vector.h b/src/vector.h index 441ac14..fc2f81a 100644 --- a/src/vector.h +++ b/src/vector.h @@ -16,6 +16,9 @@ void vector_free(const struct Vector **const v); size_t +vector_capacity(const struct Vector *const v); + +size_t vector_count(const struct Vector *const v); int diff --git a/tests/vector.c b/tests/vector.c index 11568fe..f27715b 100644 --- a/tests/vector.c +++ b/tests/vector.c @@ -166,6 +166,53 @@ out: } static int +test_vector_capacity(void) { + int rc = -1; + + const struct Vector *v = NULL; + + test_start("vector_capacity()"); + { + testing("we get whatever the capacity is"); + + if (vector_new(sizeof(long), &v)) { + logerr("vector_new()"); + goto out; + } + + assert(vector_capacity(v) == VECTOR_DEFAULT_CAPACITY); + assert(v->capacity == VECTOR_DEFAULT_CAPACITY); + + printf("\n"); + const long value = 123; + for (size_t i = 0U; i < VECTOR_DEFAULT_CAPACITY; i++) { + assert(vector_push_back(v, &value) == 0); + } + const size_t new_capacity = next_capacity(v); + + if (vector_push_back(v, &value)) { + logerr("vector_push_back()"); + goto out; + } + + assert(vector_capacity(v) == new_capacity); + assert(v->capacity == new_capacity); + assert(vector_capacity(v) != VECTOR_DEFAULT_CAPACITY); + + vector_free(&v); + + test_ok(); + } + + rc = 0; +out: + if (v != NULL) { + vector_free(&v); + } + return rc; +} + +static int test_vector_count(void) { int rc = -1; @@ -573,6 +620,11 @@ main(void) { goto out; } + if (test_vector_capacity()) { + logerr("test_vector_capacity()"); + goto out; + } + if (test_vector_count()) { logerr("test_vector_count()"); goto out; |