diff options
-rw-r--r-- | src/vector.c | 23 | ||||
-rw-r--r-- | src/vector.h | 3 | ||||
-rw-r--r-- | tests/vector.c | 103 |
3 files changed, 126 insertions, 3 deletions
diff --git a/src/vector.c b/src/vector.c index 143e6a1..72b41e2 100644 --- a/src/vector.c +++ b/src/vector.c @@ -18,6 +18,7 @@ #include "vector.h" + struct Vector { const void **values; size_t capacity; @@ -27,6 +28,7 @@ struct Vector { const size_t value_size; }; + static const size_t VECTOR_MAX_CAPACITY = SIZE_MAX; @@ -37,6 +39,7 @@ static const size_t GROWTH_MULTIPLIER = 2; + int vector_new_with( const size_t capacity, @@ -157,6 +160,26 @@ out: } int +vector_assign( + const struct Vector *const v, + const size_t idx, + const void *const value +) { + int rc = -1; + + const size_t count = vector_count(v); + if (idx >= count) { + logerr(_(MSG_ERR_VECTOR_OUT_OF_BOUNDS), idx, count); + goto out; + } + + memcpy(&v->values[idx], value, v->value_size); + rc = 0; +out: + return rc; +} + +int vector_push_back(const struct Vector *const v, const void *const value) { int rc = -1; diff --git a/src/vector.h b/src/vector.h index fc2f81a..11f998b 100644 --- a/src/vector.h +++ b/src/vector.h @@ -25,6 +25,9 @@ int vector_nth(const struct Vector *const v, const size_t idx, const void **const out); int +vector_assign(const struct Vector *const v, const size_t idx, const void *const value); + +int vector_push_back(const struct Vector *const v, const void *const value); int diff --git a/tests/vector.c b/tests/vector.c index f27715b..c93e793 100644 --- a/tests/vector.c +++ b/tests/vector.c @@ -11,6 +11,7 @@ #include "../src/testing.h" + static int test_vector_new_with(void) { int rc = -1; @@ -345,7 +346,7 @@ test_vector_nth(void) { testing("nth with growth"); if (vector_new(sizeof(int), &v)) { - logerr("vector_new_with()"); + logerr("vector_new()"); goto out; } @@ -355,7 +356,6 @@ test_vector_nth(void) { } int nth; - assert(vector_nth(v, 0U, (void *)&nth) == 0); assert(nth == 123); assert(vector_nth(v, 1U, (void *)&nth) == 0); @@ -371,7 +371,7 @@ test_vector_nth(void) { testing("nth out of bounds errors"); if (vector_new(sizeof(int), &v)) { - logerr("vector_new_with()"); + logerr("vector_new()"); goto out; } @@ -398,6 +398,98 @@ out: } static int +test_vector_assign(void) { + int rc = -1; + + const struct Vector *v = NULL; + + test_start("vector_assign()"); + { + testing("replace existing value"); + + const int first = 123; + const int second = 321; + const int third = 555; + const int fourth = 999; + const int values[] = { first, second, third }; + const size_t values_len = sizeof(values) / sizeof(values[0]); + + if (vector_new(sizeof(int), &v)) { + logerr("vector_new()"); + goto out; + } + + assert(v->capacity >= values_len); + for (unsigned int i = 0; i < values_len; i++) { + assert(vector_push_back(v, &values[i]) == 0); + } + + int value; + assert(vector_assign(v, 1U, &fourth) == 0); + rc = 0; goto out; // FIXME + assert(vector_nth(v, 1U, (void *)&value) == 0); + assert(value == fourth); + + vector_free(&v); + + test_ok(); + } + { + testing("assign new value"); + + const int first = 121; + const int second = 122; + const int third = 123; + const int values[] = { first, second, third }; + const size_t values_len = sizeof(values) / sizeof(values[0]); + + if (vector_new(sizeof(int), &v)) { + logerr("vector_new()"); + goto out; + } + + assert(v->capacity >= values_len); + for (unsigned int i = 0; i < values_len; i++) { + assert(vector_assign(v, i, &values[i]) == 0); + } + + int value; + assert(vector_nth(v, 0U, (void *)&value) == 0); + assert(value == 121); + assert(vector_nth(v, 1U, (void *)&value) == 0); + assert(value == 122); + assert(vector_nth(v, 2U, (void *)&value) == 0); + assert(value == 123); + + vector_free(&v); + + test_ok(); + } + { + testing("assign out of bounds errors"); + + if (vector_new(sizeof(short), &v)) { + logerr("vector_new()"); + goto out; + } + + const int new_value = 222; + assert(vector_assign(v, VECTOR_DEFAULT_CAPACITY, &new_value) != 0); + + vector_free(&v); + + test_ok(); + } + + rc = 0; +out: + if (v != NULL) { + vector_free(&v); + } + return rc; +} + +static int test_vector_push_back(void) { int rc = -1; @@ -638,6 +730,11 @@ main(void) { goto out; } + if (test_vector_assign()) { + logerr("test_vector_assign()"); + goto out; + } + if (test_vector_push_back()) { logerr("test_vector_push_back()"); goto out; |