libfort/tests/test_vector.c

121 lines
3.4 KiB
C
Raw Normal View History

2018-01-01 09:26:34 +01:00
#include "tests.h"
2018-01-17 19:22:57 +01:00
#include "vector.h"
//#include "../src/fort.c"
2018-01-01 09:26:34 +01:00
2018-03-19 21:07:18 +01:00
void test_vector_basic(void)
2018-01-01 09:26:34 +01:00
{
2018-03-09 10:46:40 +01:00
size_t i = 0;
2018-01-01 09:26:34 +01:00
typedef short item_t;
const size_t init_capacity = 10;
vector_t *vector = create_vector(sizeof(item_t), init_capacity);
2018-03-31 12:33:37 +02:00
assert_true(vector != NULL);
assert_true(vector_size(vector) == 0);
assert_true(vector_capacity(vector) == init_capacity);
2018-01-01 09:26:34 +01:00
WHEN("Pushing less items than initial capacity") {
2018-03-09 10:46:40 +01:00
for (i = 0; i < init_capacity; ++i) {
2018-03-29 21:18:49 +02:00
item_t item = (item_t)i;
2018-01-01 09:26:34 +01:00
vector_push(vector, &item);
}
THEN("Then capacity is not changed") {
2018-03-31 12:33:37 +02:00
assert_true(vector_size(vector) == init_capacity);
assert_true(vector_capacity(vector) == init_capacity);
2018-01-01 09:26:34 +01:00
}
}
WHEN("Clearing vector") {
vector_clear(vector);
THEN("Then size == 0") {
2018-03-31 12:33:37 +02:00
assert_true(vector_size(vector) == 0);
2018-01-01 09:26:34 +01:00
}
}
WHEN("Pushing more items than initial capacity") {
2018-03-09 10:46:40 +01:00
for (i = 0; i < 2 * init_capacity; ++i) {
2018-03-29 21:18:49 +02:00
item_t item = (item_t)(2 * i);
2018-01-01 09:26:34 +01:00
vector_push(vector, &item);
}
THEN("Then capacity is increased") {
2018-03-31 12:33:37 +02:00
assert_true(vector_size(vector) == 2 * init_capacity);
assert_true(vector_capacity(vector) > init_capacity);
2018-01-01 09:26:34 +01:00
}
WHEN("Checking indexes of items") {
item_t item = 6;
2018-03-31 12:33:37 +02:00
assert_true(vector_index_of(vector, &item) == 3);
2018-01-01 09:26:34 +01:00
item = 14;
2018-03-31 12:33:37 +02:00
assert_true(vector_index_of(vector, &item) == 7);
2018-01-01 09:26:34 +01:00
item = 25;
2018-03-31 12:33:37 +02:00
assert_true(vector_index_of(vector, &item) == INVALID_VEC_INDEX);
2018-01-01 09:26:34 +01:00
}
WHEN("Checking access to items") {
2018-03-31 12:33:37 +02:00
assert_true(*(item_t *)vector_at(vector, 0) == 0);
assert_true(*(item_t *)vector_at(vector, 10) == 20);
assert_true((item_t *)vector_at(vector, 20) == NULL);
2018-01-01 09:26:34 +01:00
}
WHEN("Erasing items") {
2018-03-31 12:33:37 +02:00
assert_true(vector_erase(vector, 20) != FT_SUCCESS);
2018-01-01 09:26:34 +01:00
2018-03-31 12:33:37 +02:00
assert_true(vector_erase(vector, 0) == FT_SUCCESS);
assert_true(vector_erase(vector, 10) == FT_SUCCESS);
2018-01-01 09:26:34 +01:00
2018-03-31 12:33:37 +02:00
item_t first_item = *(item_t *)vector_at(vector, 0);
assert_true(first_item == 2);
2018-01-01 09:26:34 +01:00
item_t item = 6;
2018-03-31 12:33:37 +02:00
assert_true(vector_index_of(vector, &item) == 2);
2018-01-01 09:26:34 +01:00
item = 26;
2018-03-31 12:33:37 +02:00
assert_true(vector_index_of(vector, &item) == 11);
2018-01-01 09:26:34 +01:00
}
}
WHEN("Clearing vector") {
vector_clear(vector);
THEN("Then size == 0") {
2018-03-31 12:33:37 +02:00
assert_true(vector_size(vector) == 0);
2018-01-01 09:26:34 +01:00
}
}
2018-03-31 12:33:37 +02:00
destroy_vector(vector);
2018-01-01 09:26:34 +01:00
}
2018-04-22 20:42:22 +02:00
void test_vector_stress(void)
{
size_t i = 0;
typedef short item_t;
const size_t init_capacity = 10;
vector_t *vector = create_vector(sizeof(item_t), init_capacity);
assert_true(vector != NULL);
assert_true(vector_size(vector) == 0);
assert_true(vector_capacity(vector) == init_capacity);
WHEN("Pushing a lot of items into vector") {
for (i = 0; i < 1000 * init_capacity; ++i) {
item_t item = (item_t)i;
vector_push(vector, &item);
}
THEN("Then capacity is not changed") {
assert_true(vector_size(vector) == 1000 * init_capacity);
assert_true(vector_capacity(vector) >= 1000 * init_capacity);
}
}
/*
* Destroy without previously called clear
*/
destroy_vector(vector);
}