diff options
Diffstat (limited to 'tests/vector.c')
-rw-r--r-- | tests/vector.c | 237 |
1 files changed, 225 insertions, 12 deletions
diff --git a/tests/vector.c b/tests/vector.c index 1ff270a..443dc81 100644 --- a/tests/vector.c +++ b/tests/vector.c @@ -9,6 +9,12 @@ #include <string.h> #include "../src/testing.h" +#include "slurp.h" + + + +static const char +FNAME[] = __FILE__ ".txt"; @@ -16,9 +22,12 @@ static int test_vector_new_with(void) { int rc = -1; + test_start("vector_new_with()"); + const struct Vector *v = NULL; + FILE *file = NULL; + char *str = NULL; - test_start("vector_new_with()"); { testing("allocating struct Vector with small capacity"); @@ -53,6 +62,13 @@ test_vector_new_with(void) { const size_t max_capacity = 2U; + file = fopen(FNAME, "w"); + if (file == NULL) { + logerr("fopen(FNAME, \"w\")"); + goto out; + } + logerr_set_stream(file); + assert(v == NULL); assert(vector_new_with( max_capacity + 1, @@ -63,16 +79,50 @@ test_vector_new_with(void) { ) == EOVERFLOW); assert(v == NULL); + logerr_set_stream(NULL); + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file)"); + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests()"); + goto out; + } + + const char expected[] = + "tests/../src/vector.c:vector_new_with:56: " + "Already at max capacity (2): " + "Value too large for defined data type\n\n" + ; + + assert(strcmp(str, expected) == 0); + + free(str); + str = NULL; + test_ok(); } rc = 0; out: + logerr_set_stream(NULL); + if (str != NULL) { + free(str); + str = NULL; + } + if (file != NULL) { + if (fclose(file)) { + logerr("fclose(file)"); + rc = -1; + } + } vector_free(&v); return rc; } - static int test_vector_new(void) { int rc = -1; @@ -187,7 +237,6 @@ test_vector_capacity(void) { 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); @@ -344,14 +393,18 @@ static int test_vector_nth(void) { int rc = -1; + test_start("vector_nth()"); + const struct Vector *v = NULL; + FILE *file = NULL; + char *str = 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]); + const size_t values_len = nelem(values); - test_start("vector_nth()"); { testing("nth with growth"); @@ -390,10 +443,40 @@ test_vector_nth(void) { assert(vector_push_back(v, &values[i]) == 0); } + file = fopen(FNAME, "w"); + if (file == NULL) { + logerr("fopen(FNAME, \"w\")"); + goto out; + } + logerr_set_stream(file); + int nth = 222; assert(vector_nth(v, 4U, (void *)&nth) != 0); assert(nth == 222); + logerr_set_stream(NULL); + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file)"); + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests()"); + goto out; + } + + const char expected[] = + "tests/../src/vector.c:vector_nth:165: " + "idx (4) is beyond bounds (3)\n\n" + ; + + assert(strcmp(str, expected) == 0); + + free(str); + str = NULL; + vector_free(&v); test_ok(); @@ -401,6 +484,17 @@ test_vector_nth(void) { rc = 0; out: + logerr_set_stream(NULL); + if (str != NULL) { + free(str); + str = NULL; + } + if (file != NULL) { + if (fclose(file)) { + logerr("fclose(file)"); + rc = -1; + } + } vector_free(&v); return rc; } @@ -411,6 +505,9 @@ test_vector_assign(void) { const struct Vector *v = NULL; + FILE *file = NULL; + char *str = NULL; + test_start("vector_assign()"); { testing("replace existing value"); @@ -420,7 +517,7 @@ test_vector_assign(void) { const int third = 555; const int fourth = 999; const int values[] = { first, second, third }; - const size_t values_len = sizeof(values) / sizeof(values[0]); + const size_t values_len = nelem(values); if (vector_new(sizeof(int), &v)) { logerr("vector_new()"); @@ -432,9 +529,8 @@ test_vector_assign(void) { assert(vector_push_back(v, &values[i]) == 0); } - int value; + int value = 0; assert(vector_assign(v, 1U, &fourth) == 0); - rc = 0; goto out; // FIXME assert(vector_nth(v, 1U, (void *)&value) == 0); assert(value == fourth); @@ -445,11 +541,12 @@ test_vector_assign(void) { { testing("assign new value"); + const int zero = 0; 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]); + const size_t values_len = nelem(values); if (vector_new(sizeof(int), &v)) { logerr("vector_new()"); @@ -458,10 +555,14 @@ test_vector_assign(void) { assert(v->capacity >= values_len); for (unsigned int i = 0; i < values_len; i++) { + assert(vector_push_back(v, &zero) == 0); + } + + for (unsigned int i = 0; i < values_len; i++) { assert(vector_assign(v, i, &values[i]) == 0); } - int value; + int value = 0; assert(vector_nth(v, 0U, (void *)&value) == 0); assert(value == 121); assert(vector_nth(v, 1U, (void *)&value) == 0); @@ -476,13 +577,41 @@ test_vector_assign(void) { { testing("assign out of bounds errors"); + const int n = 222; + 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); + file = fopen(FNAME, "w"); + if (file == NULL) { + logerr("fopen(FNAME, \"w\")"); + goto out; + } + logerr_set_stream(file); + + assert(vector_assign(v, VECTOR_DEFAULT_CAPACITY, &n) != 0); + + logerr_set_stream(NULL); + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file)"); + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests()"); + goto out; + } + + const char expected[] = + "tests/../src/vector.c:vector_assign:185: " + "idx (8) is beyond bounds (0)\n\n" + ; + + assert(strcmp(str, expected) == 0); vector_free(&v); @@ -491,6 +620,17 @@ test_vector_assign(void) { rc = 0; out: + logerr_set_stream(NULL); + if (str != NULL) { + free(str); + str = NULL; + } + if (file != NULL) { + if (fclose(file)) { + logerr("fclose(file)"); + rc = -1; + } + } vector_free(&v); return rc; } @@ -568,6 +708,8 @@ test_vector_pop_back(void) { int rc = -1; const struct Vector *v = NULL; + FILE *file = NULL; + char *str = NULL; test_start("vector_pop_back()"); { @@ -578,10 +720,40 @@ test_vector_pop_back(void) { goto out; } + file = fopen(FNAME, "w"); + if (file == NULL) { + logerr("fopen(FNAME, \"w\")"); + goto out; + } + logerr_set_stream(file); + int val = 222; assert(vector_pop_back(v, (void *)&val) != 0); assert(val == 222); + logerr_set_stream(NULL); + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file)"); + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests()"); + goto out; + } + + const char expected[] = + "tests/../src/vector.c:vector_pop_back:256: " + "pop on an empty vector\n\n" + ; + + assert(strcmp(str, expected) == 0); + + free(str); + str = NULL; + vector_free(&v); test_ok(); @@ -620,8 +792,38 @@ test_vector_pop_back(void) { assert(val == 1234U); } + file = fopen(FNAME, "w"); + if (file == NULL) { + logerr("fopen(FNAME, \"w\")"); + goto out; + } + logerr_set_stream(file); + assert(vector_pop_back(v, (void *)&val) != 0); + logerr_set_stream(NULL); + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file)"); + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests()"); + goto out; + } + + const char expected[] = + "tests/../src/vector.c:vector_pop_back:256: " + "pop on an empty vector\n\n" + ; + + assert(strcmp(str, expected) == 0); + + free(str); + str = NULL; + vector_free(&v); test_ok(); @@ -629,6 +831,17 @@ test_vector_pop_back(void) { rc = 0; out: + logerr_set_stream(NULL); + if (str != NULL) { + free(str); + str = NULL; + } + if (file != NULL) { + if (fclose(file)) { + logerr("fclose(file)"); + rc = -1; + } + } vector_free(&v); return rc; } |