summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-05-24 13:06:19 -0300
committerEuAndreh <eu@euandre.org>2024-05-24 13:06:19 -0300
commit423d46bdae07ceee0af94e5cc12f335afabbcbb8 (patch)
tree864e0bd291379d3341750ff0954e758f95549428 /tests
parentsrc/math.c: Add mul_size() (diff)
downloadpindaiba-423d46bdae07ceee0af94e5cc12f335afabbcbb8.tar.gz
pindaiba-423d46bdae07ceee0af94e5cc12f335afabbcbb8.tar.xz
src/vector.{c,h}: Improve code and tests
Diffstat (limited to 'tests')
-rw-r--r--tests/vector.c140
1 files changed, 56 insertions, 84 deletions
diff --git a/tests/vector.c b/tests/vector.c
index 74bffc7..8f4a20c 100644
--- a/tests/vector.c
+++ b/tests/vector.c
@@ -15,7 +15,7 @@ static int
test_vector_new_with(void) {
int rc = -1;
- struct Vector *v = NULL;
+ const struct Vector *v = NULL;
test_start("vector_new_with()");
{
@@ -38,8 +38,7 @@ test_vector_new_with(void) {
}
assert(v->capacity == capacity);
- assert(v->back_count == 0U);
- assert(v->front_count == 0U);
+ assert(v->count == 0U);
assert(v->max_capacity == max_capacity);
assert(v->multiplier == multiplier);
assert(v->value_size == size);
@@ -81,7 +80,7 @@ static int
test_vector_new(void) {
int rc = -1;
- struct Vector *v = NULL;
+ const struct Vector *v = NULL;
test_start("vector_new()");
{
@@ -94,8 +93,7 @@ test_vector_new(void) {
}
assert(v->capacity == VECTOR_DEFAULT_CAPACITY);
- assert(v->back_count == 0);
- assert(v->front_count == 0);
+ assert(v->count == 0);
assert(v->max_capacity == VECTOR_MAX_CAPACITY);
assert(v->multiplier == GROWTH_MULTIPLIER);
assert(v->value_size == size);
@@ -121,8 +119,7 @@ test_vector_new(void) {
}
assert(v->capacity == VECTOR_DEFAULT_CAPACITY);
- assert(v->back_count == 0);
- assert(v->front_count == 0);
+ assert(v->count == 0);
assert(v->max_capacity == VECTOR_MAX_CAPACITY);
assert(v->multiplier == GROWTH_MULTIPLIER);
assert(v->value_size == size);
@@ -146,7 +143,7 @@ static int
test_vector_free(void) {
int rc = -1;
- struct Vector *v = NULL;
+ const struct Vector *v = NULL;
test_start("vector_free()");
{
@@ -173,7 +170,7 @@ static int
test_vector_count(void) {
int rc = -1;
- struct Vector *v = NULL;
+ const struct Vector *v = NULL;
test_start("vector_count()");
{
@@ -185,17 +182,16 @@ test_vector_count(void) {
}
assert(vector_count(v) == 0);
- assert(v->back_count == 0);
- assert(v->front_count == 0);
+ assert(v->count == 0);
- if (vector_push_back(v, (void *)123)) {
+ const unsigned long value = 123;
+ if (vector_push_back(v, &value)) {
logerr("vector_push_back()");
goto out;
}
assert(vector_count(v) == 1);
- assert(v->back_count == 1);
- assert(v->front_count == 0);
+ assert(v->count == 1);
vector_free(v);
v = NULL;
@@ -212,19 +208,29 @@ out:
return rc;
}
+static void
+test_next_capacity(void) {
+ test_start("next_capacity()");
+ // FIXME: write tests
+}
+
+static void
+test_next_size(void) {
+ test_start("next_size()");
+ // FIXME: write tests
+}
+
static int
test_vector_nth(void) {
int rc = -1;
- struct Vector *v = NULL;
- int first = 123;
- int second = 321;
- int third = 555;
- // const int values[] = { first, second, third };
- int values[] = { first, second, third };
+ const struct Vector *v = NULL;
+ const int first = 123;
+ const int second = 321;
+ const int third = 555;
+ const int values[] = { first, second, third };
const size_t values_len = sizeof(values) / sizeof(values[0]);
- return 0;
test_start("vector_nth()");
{
testing("nth with growth");
@@ -241,14 +247,14 @@ test_vector_nth(void) {
}
}
- int ret;
+ int nth;
- assert(vector_nth(v, 0U, (void *)&ret) == 0);
- assert(ret == 123);
- assert(vector_nth(v, 1U, (void *)&ret) == 0);
- assert(ret == 321);
- assert(vector_nth(v, 2U, (void *)&ret) == 0);
- assert(ret == 555);
+ assert(vector_nth(v, 0U, (void *)&nth) == 0);
+ assert(nth == 123);
+ assert(vector_nth(v, 1U, (void *)&nth) == 0);
+ assert(nth == 321);
+ assert(vector_nth(v, 2U, (void *)&nth) == 0);
+ assert(nth == 555);
vector_free(v);
v = NULL;
@@ -270,9 +276,9 @@ test_vector_nth(void) {
}
}
- int ret = 222;
- assert(vector_nth(v, 4U, (void *)&ret) == -1);
- assert(ret == 222);
+ int nth = 222;
+ assert(vector_nth(v, 4U, (void *)&nth) != 0);
+ assert(nth == 222);
vector_free(v);
v = NULL;
@@ -289,16 +295,11 @@ out:
return rc;
}
-static void
-test_next_capacity(void) {
- test_start("next_capacity()");
-}
-
static int
test_vector_push_back(void) {
int rc = -1;
- struct Vector *v = NULL;
+ const struct Vector *v = NULL;
test_start("vector_push_back()");
{
@@ -309,14 +310,14 @@ test_vector_push_back(void) {
goto out;
}
- long long before = 123;
- long long after = 321;
- v->values[0] = &before;
+ const long long before = 123;
+ const long long after = 321;
+ v->values[0] = (void *)before;
if (vector_push_back(v, &after)) {
logerr("vector_push_back()");
goto out;
}
- assert(v->values[0] == &after);
+ assert(v->values[0] == (void *)after);
vector_free(v);
v = NULL;
@@ -326,7 +327,7 @@ test_vector_push_back(void) {
{
testing("push back beyond capacity causes reallocation");
- unsigned long data = 2222U;
+ const unsigned long data = 2222U;
if (vector_new(sizeof(unsigned long), &v)) {
logerr("vector_new()");
@@ -341,30 +342,23 @@ test_vector_push_back(void) {
goto out;
}
}
-
- assert(v->capacity == vector_count(v));
assert(v->values[0] == (void *)data);
- if (vector_push_back(v, &data)) {
- logerr("vector_push_vack()");
- goto out;
- }
- assert(v->capacity == (v->capacity * v->multiplier));
- assert(vector_count(v) == v->capacity);
- assert(v->values[1] == &data);
+ const size_t capacity = v->capacity;
+ assert(capacity == vector_count(v));
+ // we grow the vector here
if (vector_push_back(v, &data)) {
- logerr("vector_push_back()");
+ logerr("vector_push_vack()");
goto out;
}
- assert(v->capacity == 4);
- // assert(v->count == 3);
- assert(v->values[2] == &data);
+ const size_t new_capacity = capacity * v->multiplier;
+ assert(v->capacity == new_capacity);
+ assert(vector_count(v) == capacity + 1U);
assert(vector_push_back(v, &data) == 0);
- assert(v->capacity == 4);
- // assert(v->count == 4);
- assert(v->values[3] == &data);
+ assert(v->capacity == new_capacity);
+ assert(v->count == capacity + 2U);
vector_free(v);
v = NULL;
@@ -382,23 +376,11 @@ out:
}
static int
-test_vector_push_front(void) {
- test_start("vector_push_front()");
- return 0;
-}
-
-static int
test_vector_pop_back(void) {
test_start("vector_pop_back()");
return 0;
}
-static int
-test_vector_pop_front(void) {
- test_start("vector_pop_front()");
- return 0;
-}
-
int
main(void) {
@@ -424,34 +406,24 @@ main(void) {
goto out;
}
+ test_next_capacity();
+ test_next_size();
+
if (test_vector_nth()) {
logerr("test_vector_nth()");
goto out;
}
- return 0;
- test_next_capacity();
-
if (test_vector_push_back()) {
logerr("test_vector_push_back()");
goto out;
}
- if (test_vector_push_front()) {
- logerr("test_vector_push_front()");
- goto out;
- }
-
if (test_vector_pop_back()) {
logerr("test_vector_pop_back()");
goto out;
}
- if (test_vector_pop_front()) {
- logerr("test_vector_pop_front()");
- goto out;
- }
-
rc = 0;
out:
return !!rc;