libfort/tests/wb_tests/test_vector.c

180 lines
5.6 KiB
C
Raw Permalink 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"
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;
2019-08-27 14:04:54 +02:00
f_vector_t *vector = create_vector(sizeof(item_t), init_capacity);
2018-01-01 09:26:34 +01:00
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
}
2018-09-01 18:42:57 +02:00
#ifdef FT_TEST_BUILD
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
}
2018-09-01 18:42:57 +02:00
#endif
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
}
2018-09-01 18:42:57 +02:00
#ifdef FT_TEST_BUILD
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
}
2018-09-01 18:42:57 +02:00
#endif
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("Testing insert method vector") {
vector_clear(vector);
size_t capacity = 10 * init_capacity;
for (i = 0; i < capacity; ++i) {
item_t item = (item_t)i;
vector_insert(vector, &item, 0);
}
assert_true(vector_size(vector) == capacity);
for (i = 0; i < capacity; ++i) {
assert_true(*(item_t *)vector_at(vector, i) == (item_t)(capacity - i) - 1);
}
item_t item_666 = 666;
vector_insert(vector, &item_666, 5 * capacity - 1);
assert_true(vector_size(vector) == 5 * capacity);
assert_true(*(item_t *)vector_at(vector, 5 * capacity - 1) == item_666);
item_t item_777 = 777;
vector_insert(vector, &item_777, 10);
assert_true(vector_size(vector) == 5 * capacity + 1);
assert_true(*(item_t *)vector_at(vector, 10) == item_777);
}
2018-05-12 11:45:42 +02:00
WHEN("Moving from another vector") {
vector_clear(vector);
for (i = 0; i < 10; ++i) {
item_t item = (item_t)i;
vector_push(vector, &item);
}
2019-08-27 14:04:54 +02:00
f_vector_t *mv_vector = create_vector(sizeof(item_t), 5);
2018-05-12 11:45:42 +02:00
assert_true(mv_vector != NULL);
for (i = 0; i < 5; ++i) {
item_t item = (item_t)i * 2;
assert_true(vector_push(mv_vector, &item) == FT_SUCCESS);
}
assert_true(vector_swap(vector, mv_vector, 2) == FT_SUCCESS);
destroy_vector(mv_vector);
assert_true(vector_size(vector) == 10);
assert_true(*(item_t *)vector_at(vector, 1) == 1); /* original value */
assert_true(*(item_t *)vector_at(vector, 2) == 0); /* inserted value */
assert_true(*(item_t *)vector_at(vector, 4) == 4); /* inserted value */
assert_true(*(item_t *)vector_at(vector, 9) == 9); /* original value */
mv_vector = create_vector(sizeof(item_t), 5);
assert_true(mv_vector != NULL);
for (i = 0; i < 5; ++i) {
item_t item = (item_t)i * 2;
assert_true(vector_push(mv_vector, &item) == FT_SUCCESS);
}
assert_true(vector_swap(vector, mv_vector, 10) == FT_SUCCESS);
destroy_vector(mv_vector);
assert_true(vector_size(vector) == 15);
}
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;
2019-08-27 14:04:54 +02:00
f_vector_t *vector = create_vector(sizeof(item_t), init_capacity);
2018-04-22 20:42:22 +02:00
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);
}