excit_range.c 2.02 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*******************************************************************************
 * Copyright 2019 UChicago Argonne, LLC.
 * (c.f. AUTHORS, LICENSE)
 *
 * This file is part of the EXCIT project.
 * For more info, see https://xgitlab.cels.anl.gov/argo/excit
 *
 * SPDX-License-Identifier: BSD-3-Clause
*******************************************************************************/
Brice Videau's avatar
Brice Videau committed
10 11 12 13 14 15
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "excit.h"
#include "excit_test.h"

Brice Videau's avatar
Brice Videau committed
16 17
void test_alloc_init_range(ssize_t start, ssize_t stop, ssize_t step)
{
Brice Videau's avatar
Brice Videau committed
18 19 20 21 22 23 24 25 26 27 28 29 30
	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);
}

Brice Videau's avatar
Brice Videau committed
31 32
excit_t create_test_range(ssize_t start, ssize_t stop, ssize_t step)
{
Brice Videau's avatar
Brice Videau committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	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);
Brice Videau's avatar
Brice Videau committed
53
	} else
Brice Videau's avatar
Brice Videau committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
		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;
Brice Videau's avatar
Brice Videau committed
69

Brice Videau's avatar
Brice Videau committed
70 71 72
	while (synthetic_tests[i]) {
		excit_t it = create_test_range(start, stop, step);

Brice Videau's avatar
Brice Videau committed
73
		synthetic_tests[i] (it);
Brice Videau's avatar
Brice Videau committed
74 75 76 77 78
		excit_free(it);
		i++;
	}
}

Brice Videau's avatar
Brice Videau committed
79
int main()
Brice Videau's avatar
Brice Videau committed
80
{
Brice Videau's avatar
Brice Videau committed
81
	test_range_iterator(4, 12, 3);
Brice Videau's avatar
Brice Videau committed
82 83 84 85 86 87 88 89
	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;
}