summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-05-23 18:34:09 -0300
committerEuAndreh <eu@euandre.org>2024-05-23 18:34:09 -0300
commita5934697be6255a4ed46dafcd9479733313f0af4 (patch)
treea12ee160fd06a89aa79d0b156646688d9ec94401 /tests
parentRename "pindaiba" -> "pindaibabs" (diff)
downloadpindaiba-a5934697be6255a4ed46dafcd9479733313f0af4.tar.gz
pindaiba-a5934697be6255a4ed46dafcd9479733313f0af4.tar.xz
Add some version of vector.c
Diffstat (limited to 'tests')
-rw-r--r--tests/vector.c458
1 files changed, 458 insertions, 0 deletions
diff --git a/tests/vector.c b/tests/vector.c
new file mode 100644
index 0000000..74bffc7
--- /dev/null
+++ b/tests/vector.c
@@ -0,0 +1,458 @@
+#include "../src/vector.c"
+
+#include <assert.h>
+#include <errno.h>
+#include <locale.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "../src/testing.h"
+
+
+static int
+test_vector_new_with(void) {
+ int rc = -1;
+
+ struct Vector *v = NULL;
+
+ test_start("vector_new_with()");
+ {
+ testing("allocating struct Vector with small capacity");
+
+ const size_t capacity = 1U;
+ const size_t max_capacity = 2U;
+ const size_t multiplier = 3U;
+ const size_t size = 4U;
+
+ if (vector_new_with(
+ capacity,
+ max_capacity,
+ multiplier,
+ size,
+ &v
+ )) {
+ logerr("vector_new_with()");
+ goto out;
+ }
+
+ assert(v->capacity == capacity);
+ assert(v->back_count == 0U);
+ assert(v->front_count == 0U);
+ assert(v->max_capacity == max_capacity);
+ assert(v->multiplier == multiplier);
+ assert(v->value_size == size);
+
+ vector_free(v);
+ v = NULL;
+
+ test_ok();
+ }
+ {
+ testing("we can't ask for a capacity beyond max_capacity");
+
+ const size_t max_capacity = 2U;
+
+ assert(v == NULL);
+ assert(vector_new_with(
+ max_capacity + 1,
+ max_capacity,
+ 1U,
+ 1U,
+ &v
+ ) == EOVERFLOW);
+ assert(v == NULL);
+
+ test_ok();
+ }
+
+ rc = 0;
+out:
+ if (v != NULL) {
+ vector_free(v);
+ v = NULL;
+ }
+ return rc;
+}
+
+
+static int
+test_vector_new(void) {
+ int rc = -1;
+
+ struct Vector *v = NULL;
+
+ test_start("vector_new()");
+ {
+ testing("simple allocation of int[]");
+
+ const size_t size = sizeof(int);
+ if (vector_new(size, &v)) {
+ logerr("vector_new()");
+ goto out;
+ }
+
+ assert(v->capacity == VECTOR_DEFAULT_CAPACITY);
+ assert(v->back_count == 0);
+ assert(v->front_count == 0);
+ assert(v->max_capacity == VECTOR_MAX_CAPACITY);
+ assert(v->multiplier == GROWTH_MULTIPLIER);
+ assert(v->value_size == size);
+
+ vector_free(v);
+ v = NULL;
+
+ test_ok();
+ }
+ {
+ testing("simple allocation of custom struct");
+
+ struct Custom {
+ int a;
+ char b;
+ void *c;
+ } custom;
+
+ const size_t size = sizeof(custom);
+ if (vector_new(size, &v)) {
+ logerr("vector_new()");
+ goto out;
+ }
+
+ assert(v->capacity == VECTOR_DEFAULT_CAPACITY);
+ assert(v->back_count == 0);
+ assert(v->front_count == 0);
+ assert(v->max_capacity == VECTOR_MAX_CAPACITY);
+ assert(v->multiplier == GROWTH_MULTIPLIER);
+ assert(v->value_size == size);
+
+ vector_free(v);
+ v = NULL;
+
+ test_ok();
+ }
+
+ rc = 0;
+out:
+ if (v != NULL) {
+ vector_free(v);
+ v = NULL;
+ }
+ return rc;
+}
+
+static int
+test_vector_free(void) {
+ int rc = -1;
+
+ struct Vector *v = NULL;
+
+ test_start("vector_free()");
+ {
+ testing("*v becomes NULL again after vector_free(&v)");
+ assert(v == NULL);
+ if (vector_new(sizeof(char), &v)) {
+ logerr("vector_new()");
+ goto out;
+ }
+
+ assert(v != NULL);
+ vector_free(v);
+ v = NULL;
+
+ test_ok();
+ }
+
+ rc = 0;
+out:
+ return rc;
+}
+
+static int
+test_vector_count(void) {
+ int rc = -1;
+
+ struct Vector *v = NULL;
+
+ test_start("vector_count()");
+ {
+ testing("we get whatever the count is");
+
+ if (vector_new(sizeof(unsigned long), &v)) {
+ logerr("vector_new()");
+ goto out;
+ }
+
+ assert(vector_count(v) == 0);
+ assert(v->back_count == 0);
+ assert(v->front_count == 0);
+
+ if (vector_push_back(v, (void *)123)) {
+ logerr("vector_push_back()");
+ goto out;
+ }
+
+ assert(vector_count(v) == 1);
+ assert(v->back_count == 1);
+ assert(v->front_count == 0);
+
+ vector_free(v);
+ v = NULL;
+
+ test_ok();
+ }
+
+ rc = 0;
+out:
+ if (v != NULL) {
+ vector_free(v);
+ v = NULL;
+ }
+ return rc;
+}
+
+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 size_t values_len = sizeof(values) / sizeof(values[0]);
+
+ return 0;
+ test_start("vector_nth()");
+ {
+ testing("nth with growth");
+
+ if (vector_new(sizeof(int), &v)) {
+ logerr("vector_new_with()");
+ goto out;
+ }
+
+ for (unsigned int i = 0; i < values_len; i++) {
+ if (vector_push_back(v, &values[i])) {
+ logerr("vector_push_back(v, values[%d])", i);
+ goto out;
+ }
+ }
+
+ int ret;
+
+ 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);
+
+ vector_free(v);
+ v = NULL;
+
+ test_ok();
+ }
+ {
+ testing("nth out of bounds errors");
+
+ if (vector_new(sizeof(int), &v)) {
+ logerr("vector_new_with()");
+ goto out;
+ }
+
+ for (unsigned int i = 0; i < values_len; i++) {
+ if (vector_push_back(v, &values[i])) {
+ logerr("vector_push_back(v, values[%d])", i);
+ goto out;
+ }
+ }
+
+ int ret = 222;
+ assert(vector_nth(v, 4U, (void *)&ret) == -1);
+ assert(ret == 222);
+
+ vector_free(v);
+ v = NULL;
+
+ test_ok();
+ }
+
+ rc = 0;
+out:
+ if (v != NULL) {
+ vector_free(v);
+ v = NULL;
+ }
+ 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;
+
+ test_start("vector_push_back()");
+ {
+ testing("a single push back overwrites existing garbage");
+
+ if (vector_new(sizeof(long long), &v)) {
+ logerr("vector_new()");
+ goto out;
+ }
+
+ long long before = 123;
+ long long after = 321;
+ v->values[0] = &before;
+ if (vector_push_back(v, &after)) {
+ logerr("vector_push_back()");
+ goto out;
+ }
+ assert(v->values[0] == &after);
+
+ vector_free(v);
+ v = NULL;
+
+ test_ok();
+ }
+ {
+ testing("push back beyond capacity causes reallocation");
+
+ unsigned long data = 2222U;
+
+ if (vector_new(sizeof(unsigned long), &v)) {
+ logerr("vector_new()");
+ goto out;
+ }
+ assert(v->capacity == VECTOR_DEFAULT_CAPACITY);
+ assert(vector_count(v) == 0);
+
+ for (unsigned int i = 0; i < v->capacity; i++) {
+ if (vector_push_back(v, &data)) {
+ logerr("vector_push_back(): i = %d", i);
+ 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);
+
+ if (vector_push_back(v, &data)) {
+ logerr("vector_push_back()");
+ goto out;
+ }
+ assert(v->capacity == 4);
+ // assert(v->count == 3);
+ assert(v->values[2] == &data);
+
+ assert(vector_push_back(v, &data) == 0);
+ assert(v->capacity == 4);
+ // assert(v->count == 4);
+ assert(v->values[3] == &data);
+
+ vector_free(v);
+ v = NULL;
+
+ test_ok();
+ }
+
+ rc = 0;
+out:
+ if (v != NULL) {
+ vector_free(v);
+ v = NULL;
+ }
+ return rc;
+}
+
+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) {
+ int rc = -1;
+
+ if (test_vector_new_with()) {
+ logerr("test_vector_new_with()");
+ goto out;
+ }
+
+ if (test_vector_new()) {
+ logerr("test_vector_new()");
+ goto out;
+ }
+
+ if (test_vector_free()) {
+ logerr("test_vector_free()");
+ goto out;
+ }
+
+ if (test_vector_count()) {
+ logerr("test_vector_count()");
+ goto out;
+ }
+
+ 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;
+}