GitLab maintenance scheduled for Today, 2020-02-27, from 17:00 to 18:00 CT - Services will be unavailable during this time.

excit_repeat.c 3.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#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);
}

Brice Videau's avatar
Brice Videau committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
void test_repeat_split(int repeat, excit_t sit)
{
	excit_t new_sits[3], new_its[3];
	excit_t it = create_test_repeat(repeat, sit);
	ssize_t *indexes1, *indexes2;
	ssize_t dim;

	assert(excit_dimension(it, &dim) == ES);
	indexes1 = (ssize_t *)malloc(dim * sizeof(ssize_t));
	indexes2 = (ssize_t *)malloc(dim * sizeof(ssize_t));

	assert( excit_split(sit, 3, new_sits) == ES );
	assert( excit_repeat_split(it, 3, new_its) == ES );
	for (int i = 0; i < 3; i++) {
		while (excit_next(new_sits[i], indexes1) == ES) {
			for (int j = 0; j < repeat; j++) {
				assert(excit_next(new_its[i], indexes2) == ES);
				assert(memcmp(indexes1, indexes2, dim * sizeof(ssize_t)) == 0);
			}
		}
		assert(excit_next(new_its[i], indexes2) == EXCIT_STOPIT);
		excit_free(new_sits[i]);
		excit_free(new_its[i]);
	}

	free(indexes1);
	free(indexes2);

	excit_free(it);
}

105 106 107 108 109 110
void test_repeat_iterator(int repeat, excit_t sit)
{
	test_alloc_init_repeat(repeat, sit);

	test_next_repeat(repeat, sit);

Brice Videau's avatar
Brice Videau committed
111 112
	test_repeat_split(repeat, sit);

113 114 115 116 117 118 119 120 121 122 123 124
	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[])
{
Brice Videau's avatar
Brice Videau committed
125 126 127 128 129 130 131 132 133 134 135
	excit_t it1, it2, it3;

	it1 = create_test_range(0, 3, 1);
	test_repeat_iterator(3, it1);
	it2 = create_test_range(-15, 14, 2);
	test_repeat_iterator(3, it2);

	it3 = excit_alloc_test(EXCIT_PRODUCT);
	assert(excit_product_add_copy(it3, it1) == ES);
	assert(excit_product_add_copy(it3, it2) == ES);
	test_repeat_iterator(4, it3);
136

Brice Videau's avatar
Brice Videau committed
137 138 139
	excit_free(it1);
	excit_free(it2);
	excit_free(it3);
140 141
	return 0;
}