Commit 1f5c380d authored by Brice Videau's avatar Brice Videau

Added new test infrastructure.

parent 8c78da1b
......@@ -3,7 +3,14 @@ AM_COLOR_TESTS = yes
AM_CFLAGS = -I$(top_srcdir)/src -Wall -Werror -pedantic
AM_LDFLAGS = ../src/libexcit.la
UNIT_TESTS = excit
LIBHSOURCES = excit_test.h
LIBCSOURCES = excit_test.c
excit_range_SOURCES = $(LIBHSOURCES) $(LIBCSOURCES) excit_range.c
excit_product_SOURCES = $(LIBHSOURCES) $(LIBCSOURCES) excit_product.c
excit_repeat_SOURCES = $(LIBHSOURCES) $(LIBCSOURCES) excit_repeat.c
UNIT_TESTS = excit_range excit_product excit_repeat
# all tests
check_PROGRAMS = $(UNIT_TESTS)
......
This diff is collapsed.
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "excit.h"
#include "excit_test.h"
excit_t create_test_range(ssize_t start, ssize_t stop, ssize_t step)
{
excit_t it;
it = excit_alloc_test(EXCIT_RANGE);
assert(excit_range_init(it, start, stop, step) == ES);
return it;
}
void test_alloc_init_prod(int count, excit_t *its)
{
excit_t it;
ssize_t dim, expected_dim, c, size, expected_size;
it = excit_alloc_test(EXCIT_PRODUCT);
assert(excit_dimension(it, &dim) == ES);
assert(dim == 0);
assert(excit_product_count(it, &c) == ES);
assert(c == 0);
assert(excit_size(it, &size) == ES);
assert(size == 0);
expected_dim = 0;
expected_size = 1;
for (int i = 0; i < count; i++) {
assert(excit_dimension(its[i], &dim) == ES);
expected_dim += dim;
assert(excit_size(its[i], &size) == ES);
expected_size *= size;
assert(excit_product_add_copy(it, its[i]) == ES);
assert(excit_product_count(it, &c) == ES);
assert(c == i+1);
assert(excit_dimension(it, &dim) == ES);
assert(expected_dim == dim);
assert(excit_size(it, &size) == ES);
assert(expected_size == size);
}
excit_free(it);
}
excit_t create_test_product(int count, excit_t *its)
{
excit_t it;
it = excit_alloc_test(EXCIT_PRODUCT);
for (int i = 0; i < count; i++) {
assert(excit_product_add_copy(it, its[i]) == ES);
}
return it;
}
void test_next_product_helper(int count, excit_t it, excit_t *its,
ssize_t *indexes1, ssize_t *indexes2,
ssize_t *cindexes2)
{
if (count == 0) {
assert(excit_next(it, indexes1) == ES);
assert(memcmp(indexes1, indexes2, (intptr_t)cindexes2 - (intptr_t)indexes2) == 0);
} else {
excit_t cit;
ssize_t dim;
cit = excit_dup(*its);
assert(cit != NULL);
assert( excit_dimension(cit, &dim) == ES);
while(excit_next(cit, cindexes2) == ES) {
test_next_product_helper(count - 1, it, its + 1,
indexes1, indexes2,
cindexes2 + dim);
}
}
}
void test_next_product(int count, excit_t *its)
{
excit_t it;
ssize_t *indexes1, *indexes2;
ssize_t dim;
it = create_test_product(count, its);
assert(excit_dimension(it, &dim) == ES);
indexes1 = (ssize_t *)malloc(dim * sizeof(ssize_t));
indexes2 = (ssize_t *)malloc(dim * sizeof(ssize_t));
test_next_product_helper(count, it, its, indexes1, indexes2, indexes2);
assert(excit_next(it, NULL) == EXCIT_STOPIT);
free(indexes1);
free(indexes2);
excit_free(it);
}
void test_product_iterator(int count, excit_t *its)
{
test_alloc_init_prod(count, its);
test_next_product(count, its);
int i = 0;
while (synthetic_tests[i]) {
excit_t it = create_test_product(count, its);
synthetic_tests[i](it);
excit_free(it);
i++;
}
}
void test_product_split_dim(void)
{
excit_t it;
excit_t its[3];
excit_t new_its[3];
ssize_t indexes[3];
its[0] = create_test_range(0, 3, 1);
its[1] = create_test_range(1, -1, -1);
its[2] = create_test_range(-5, 5, 1);
it = create_test_product(3, its);
//first
assert(excit_product_split_dim(it, 0, 2, new_its) == ES);
for (int i = 0; i <= 1; i++) {
for (int j = 1; j >= -1; j--) {
for (int k = -5; k <= 5; k++) {
assert(excit_next(new_its[0], indexes) == ES);
assert(indexes[0] == i);
assert(indexes[1] == j);
assert(indexes[2] == k);
}
}
}
assert(excit_next(new_its[0], indexes) == EXCIT_STOPIT);
for (int i = 2; i <= 3; i++) {
for (int j = 1; j >= -1; j--) {
for (int k = -5; k <= 5; k++) {
assert(excit_next(new_its[1], indexes) == ES);
assert(indexes[0] == i);
assert(indexes[1] == j);
assert(indexes[2] == k);
}
}
}
assert(excit_next(new_its[1], indexes) == EXCIT_STOPIT);
excit_free(new_its[0]);
excit_free(new_its[1]);
//second
assert(excit_product_split_dim(it, 1, 2, new_its) == ES);
for (int i = 0; i <= 3; i++) {
for (int j = 1; j >= 0; j--) {
for (int k = -5; k <= 5; k++) {
assert(excit_next(new_its[0], indexes) == ES);
assert(indexes[0] == i);
assert(indexes[1] == j);
assert(indexes[2] == k);
}
}
}
assert(excit_next(new_its[0], indexes) == EXCIT_STOPIT);
for (int i = 0; i <= 3; i++) {
for (int j = -1; j >= -1; j--) {
for (int k = -5; k <= 5; k++) {
assert(excit_next(new_its[1], indexes) == ES);
assert(indexes[0] == i);
assert(indexes[1] == j);
assert(indexes[2] == k);
}
}
}
assert(excit_next(new_its[1], indexes) == EXCIT_STOPIT);
excit_free(new_its[0]);
excit_free(new_its[1]);
//third
assert(excit_product_split_dim(it, 2, 2, new_its) == ES);
for (int i = 0; i <= 3; i++) {
for (int j = 1; j >= -1; j--) {
for (int k = -5; k <= 0; k++) {
assert(excit_next(new_its[0], indexes) == ES);
assert(indexes[0] == i);
assert(indexes[1] == j);
assert(indexes[2] == k);
}
}
}
assert(excit_next(new_its[0], indexes) == EXCIT_STOPIT);
for (int i = 0; i <= 3; i++) {
for (int j = 1; j >= -1; j--) {
for (int k = 1; k <= 5; k++) {
assert(excit_next(new_its[1], indexes) == ES);
assert(indexes[0] == i);
assert(indexes[1] == j);
assert(indexes[2] == k);
}
}
}
assert(excit_next(new_its[1], indexes) == EXCIT_STOPIT);
excit_free(new_its[0]);
excit_free(new_its[1]);
excit_free(its[0]);
excit_free(its[1]);
excit_free(its[2]);
excit_free(it);
}
int main(int argc, char *argv[])
{
excit_t its[4];
its[0] = create_test_range(0, 3, 1);
test_product_iterator(1, its);
its[1] = create_test_range(1, -1, -1);
test_product_iterator(2, its);
its[2] = create_test_range(-5, 5, 1);
test_product_iterator(3, its);
its[3] = create_test_product(3, its);
test_product_iterator(4, its);
for (int i = 0; i < 4; i++)
excit_free(its[i]);
test_product_split_dim();
}
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "excit.h"
#include "excit_test.h"
void test_alloc_init_range(ssize_t start, ssize_t stop, ssize_t step) {
excit_t it;
ssize_t dim;
it = excit_alloc_test(EXCIT_RANGE);
assert(excit_dimension(it, &dim) == ES);
assert(dim == 0);
assert(excit_range_init(it, start, stop, step) == ES);
assert(excit_dimension(it, &dim) == ES);
assert(dim == 1);
excit_free(it);
}
excit_t create_test_range(ssize_t start, ssize_t stop, ssize_t step) {
excit_t it;
it = excit_alloc_test(EXCIT_RANGE);
assert(excit_range_init(it, start, stop, step) == ES);
return it;
}
void test_next_range(ssize_t start, ssize_t stop, ssize_t step)
{
excit_t it;
ssize_t indexes[1];
it = create_test_range(start, stop, step);
assert(step != 0);
if (step < 0)
for (int i = start; i >= stop; i += step) {
assert(excit_next(it, indexes) == ES);
assert(indexes[0] == i);
}
else
for (int i = start; i <= stop; i += step) {
assert(excit_next(it, indexes) == ES);
assert(indexes[0] == i);
}
assert(excit_next(it, indexes) == EXCIT_STOPIT);
excit_free(it);
}
void test_range_iterator(ssize_t start, ssize_t stop, ssize_t step)
{
test_alloc_init_range(start, stop, step);
test_next_range(start, stop, step);
int i = 0;
while (synthetic_tests[i]) {
excit_t it = create_test_range(start, stop, step);
synthetic_tests[i](it);
excit_free(it);
i++;
}
}
int main(int argc, char *argv[])
{
test_range_iterator(0, 3, 1);
test_range_iterator(0, 6, 2);
test_range_iterator(-15, 14, 2);
test_range_iterator(3, 0, -1);
test_range_iterator(6, 0, -2);
test_range_iterator(15, -14, -2);
return 0;
}
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "excit.h"
#include "excit_test.h"
excit_t create_test_range(ssize_t start, ssize_t stop, ssize_t step)
{
excit_t it;
it = excit_alloc_test(EXCIT_RANGE);
assert(excit_range_init(it, start, stop, step) == ES);
return it;
}
void test_alloc_init_repeat(int repeat, excit_t sit)
{
excit_t it;
ssize_t dim, expected_dim, size, expected_size;
it = excit_alloc_test(EXCIT_REPEAT);
assert(excit_dimension(it, &dim) == ES);
assert(dim == 0);
assert(excit_repeat_init(it, excit_dup(sit), repeat) == ES);
assert(excit_dimension(it, &dim) == ES);
assert(excit_dimension(sit, &expected_dim) == ES);
assert(dim == expected_dim);
assert(excit_size(sit, &expected_size) == ES);
expected_size *= repeat;
assert(excit_size(it, &size) == ES);
assert(size == expected_size);
excit_free(it);
}
excit_t create_test_repeat(int repeat, excit_t sit)
{
excit_t it;
it = excit_alloc_test(EXCIT_REPEAT);
assert(excit_repeat_init(it, excit_dup(sit), repeat) == ES);
return it;
}
void test_next_repeat(int repeat, excit_t sit)
{
excit_t it, new_sit;
ssize_t *indexes1, *indexes2;
ssize_t dim;
it = create_test_repeat(repeat, sit);
new_sit = excit_dup(sit);
assert(excit_dimension(it, &dim) == ES);
indexes1 = (ssize_t *)malloc(dim * sizeof(ssize_t));
indexes2 = (ssize_t *)malloc(dim * sizeof(ssize_t));
while (excit_next(new_sit, indexes1) == ES)
for (int i = 0; i < repeat; i++) {
assert(excit_next(it, indexes2) == ES);
assert(memcmp(indexes1, indexes2, dim * sizeof(ssize_t)) == 0);
}
assert(excit_next(it, indexes2) == EXCIT_STOPIT);
free(indexes1);
free(indexes2);
excit_free(it);
excit_free(new_sit);
}
void test_repeat_iterator(int repeat, excit_t sit)
{
test_alloc_init_repeat(repeat, sit);
test_next_repeat(repeat, sit);
int i = 0;
while (synthetic_tests[i]) {
excit_t it = create_test_repeat(repeat, sit);
synthetic_tests[i](it);
excit_free(it);
i++;
}
}
int main(int argc, char *argv[])
{
excit_t it;
it = create_test_range(0, 3, 1);
test_repeat_iterator(3, it);
return 0;
}
#include "excit_test.h"
#include "excit.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
excit_t excit_alloc_test(enum excit_type_e type) {
excit_t it;
enum excit_type_e newtype;
it = excit_alloc(type);
assert(it != NULL);
assert(excit_type(it, &newtype) == ES);
assert(type == newtype);
return it;
}
static inline excit_t excit_dup_test(const excit_t it) {
excit_t newit = excit_dup(it);
assert(newit != NULL);
return newit;
}
static inline void excit_dimension_test(excit_t it, ssize_t *dim) {
assert( excit_dimension(it, dim) == ES );
}
static inline void deplete_iterator(excit_t it) {
while(excit_next(it, NULL) == ES)
;
}
static void test_iterator_result_equal(excit_t it1, excit_t it2) {
ssize_t dim1, dim2;
excit_dimension_test(it1, &dim1);
excit_dimension_test(it2, &dim2);
assert(dim1 == dim2);
ssize_t *indexes1, *indexes2;
ssize_t buff_dim = dim1 * sizeof(ssize_t);
indexes1 = (ssize_t *)malloc(buff_dim);
indexes2 = (ssize_t *)malloc(buff_dim);
while (excit_next(it1, indexes1) == ES) {
assert(excit_next(it2, indexes2) == ES);
assert(memcmp(indexes1, indexes2, buff_dim) == 0);
}
assert( excit_next(it2, indexes2) == EXCIT_STOPIT );
free(indexes1);
free(indexes2);
}
void test_dup(excit_t it1) {
excit_t it2, it3;
it2 = excit_dup_test(it1);
it3 = excit_dup_test(it1);
test_iterator_result_equal(it1, it2);
excit_free(it2);
/* Check that the state is correctly copied */
assert(excit_next(it3, NULL) == ES);
it2 = excit_dup(it3);
test_iterator_result_equal(it3, it2);
excit_free(it2);
excit_free(it3);
}
void test_rewind(excit_t it1) {
excit_t it2, it3;
it2 = excit_dup_test(it1);
it3 = excit_dup_test(it1);
assert(excit_next(it1, NULL) == ES);
assert(excit_rewind(it1) == ES);
test_iterator_result_equal(it1, it2);
deplete_iterator(it1);
assert(excit_rewind(it1) == ES);
test_iterator_result_equal(it1, it3);
excit_free(it2);
excit_free(it3);
}
void test_peek(excit_t it1) {
excit_t it2;
it2 = excit_dup_test(it1);
ssize_t dim1;
excit_dimension_test(it1, &dim1);
ssize_t *indexes1, *indexes2;
ssize_t buff_dim = dim1 * sizeof(ssize_t);
indexes1 = (ssize_t *)malloc(buff_dim);
indexes2 = (ssize_t *)malloc(buff_dim);
while (excit_next(it1, indexes1) == ES) {
assert(excit_peek(it2, indexes2) == ES);
assert(memcmp(indexes1, indexes2, buff_dim) == 0);
assert(excit_next(it2, indexes2) == ES);
assert(memcmp(indexes1, indexes2, buff_dim) == 0);
}
assert(excit_peek(it2, indexes2) == EXCIT_STOPIT);
assert(excit_next(it2, indexes2) == EXCIT_STOPIT);
free(indexes1);
free(indexes2);
excit_free(it2);
}
void test_size(excit_t it) {
ssize_t size;
ssize_t count = 0;
int err;
err = excit_size(it, &size);
if (err == -EXCIT_ENOTSUP)
return;
assert(err == ES);
while (excit_next(it, NULL) == ES) {
count++;
}
assert( size == count);
}
void test_cyclic_next(excit_t it1) {
excit_t it2, it3;
int looped = 0;
it2 = excit_dup_test(it1);
it3 = excit_dup_test(it1);
ssize_t dim1;
excit_dimension_test(it1, &dim1);
ssize_t *indexes1, *indexes2;
ssize_t buff_dim = dim1 * sizeof(ssize_t);
indexes1 = (ssize_t *)malloc(buff_dim);
indexes2 = (ssize_t *)malloc(buff_dim);
while (excit_next(it1, indexes1) == ES) {
assert(looped == 0);
assert(excit_cyclic_next(it2, indexes2, &looped) == ES);
assert(memcmp(indexes1, indexes2, buff_dim) == 0);
}
assert(looped == 1);
test_iterator_result_equal(it2, it3);
free(indexes1);
free(indexes2);
excit_free(it2);
excit_free(it3);
}
void test_skip(excit_t it1) {
excit_t it2;
it2 = excit_dup_test(it1);
while (excit_next(it1, NULL) == ES) {
assert( excit_skip(it2) == ES);
}
assert( excit_skip(it2) == EXCIT_STOPIT );
excit_free(it2);
}
void test_pos(excit_t it) {
ssize_t rank, expected_rank;
if (excit_pos(it, &rank) == -EXCIT_ENOTSUP)
return;
expected_rank = 0;
while (excit_peek(it, NULL) == ES) {
assert(excit_pos(it, &rank) == ES);
assert(expected_rank == rank);
assert(excit_next(it, NULL) == ES);
expected_rank++;
}
assert(excit_pos(it, &rank) == EXCIT_STOPIT);
}
void test_nth(excit_t it1) {
excit_t it2;
ssize_t rank = -1;
if (excit_nth(it1, 0, NULL) == -EXCIT_ENOTSUP)
return;
it2 = excit_dup_test(it1);
ssize_t dim1;
excit_dimension_test(it1, &dim1);
ssize_t *indexes1, *indexes2;
ssize_t buff_dim = dim1 * sizeof(ssize_t);
indexes1 = (ssize_t *)malloc(buff_dim);
indexes2 = (ssize_t *)malloc(buff_dim);
assert(excit_nth(it2, rank, indexes2) == -EXCIT_EDOM);
rank++;
while (excit_next(it1, indexes1) == ES) {
assert(excit_nth(it2, rank, indexes2) == ES);
assert(memcmp(indexes1, indexes2, buff_dim) == 0);
rank++;
}
assert(excit_nth(it2, rank, indexes2) == -EXCIT_EDOM);
free(indexes1);
free(indexes2);
excit_free(it2);
}
void test_rank(excit_t it1) {
excit_t it2;
ssize_t rank, expected_rank;
it2 = excit_dup_test(it1);
ssize_t dim1;
excit_dimension_test(it1, &dim1);
ssize_t *indexes1;
ssize_t buff_dim = dim1 * sizeof(ssize_t);
indexes1 = (ssize_t *)malloc(buff_dim);
assert(excit_peek(it1, indexes1) == ES);
if (excit_rank(it2, indexes1, &rank) == -EXCIT_ENOTSUP)
goto error;
expected_rank = 0;
while(excit_next(it1, indexes1) == ES) {
assert(excit_rank(it2, indexes1, &rank) == ES);
assert(expected_rank == rank);
expected_rank++;
}
for(int i = 0; i < dim1; i++) {
indexes1[i] = 0xDEADBEEFDEADBEEF;
}
assert(excit_rank(it2, indexes1, &rank) == -EXCIT_EINVAL);
error:
free(indexes1);
excit_free(it2);
}
void test_split(excit_t it) {
int num_split = 3;
excit_t its[num_split];
ssize_t dim;
assert(excit_split(it, 0, NULL) == -EXCIT_EDOM);
assert(excit_split(it, -1, NULL) == -EXCIT_EDOM);
assert(excit_split(it, 0xDEADBEEF, NULL) == -EXCIT_EDOM);
excit_dimension_test(it, &dim);
ssize_t *indexes1, *indexes2;
ssize_t buff_dim = dim * sizeof(ssize_t);
indexes1 = (ssize_t *)malloc(buff_dim);
indexes2 = (ssize_t *)malloc(buff_dim);
assert(excit_split(it, num_split, its) == ES);
fprintf(stderr, "-----------------\n");
for(int i = 0; i < num_split; i++) {
while(excit_next(its[i], indexes2) == ES) {
assert(excit_next(it, indexes1) == ES);
fprintf(stderr, "%ld %ld\n", indexes1[0], indexes2[0]);
assert(memcmp(indexes1, indexes2, buff_dim) == 0);
}
excit_free(its[i]);
}
assert(excit_next(it, indexes1) == EXCIT_STOPIT);
free(indexes1);