summaryrefslogtreecommitdiff
path: root/src/util.test.c
blob: c2f846265528eb6e3f28acf9b7ca73277458c330 (plain)
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
#include <stdlib.h>

#include <kalmia.h>
#include "util.h"
#include "test/test.h"

void next_array_element();

void suite_util()
{
	lily_run_test(next_array_element);
}

void next_array_element()
{
	int *arr = NULL;
	size_t count = 0;
	size_t max = 0;

	int *next = kai_next_array_element((void**) &arr, &count, &max, sizeof(int));
	lily_assert_not_null(arr);
	lily_assert_not_null(next);

	lily_assert_ptr_equal(arr+0, next);
	lily_assert_int_equal(count, 1);
	lily_assert_int_equal(max, 1);
	*next = 2;

	next = kai_next_array_element((void**) &arr, &count, &max, sizeof(int));

	lily_assert_not_null(next);
	lily_assert_ptr_equal(arr+1, next);
	lily_assert_int_equal(count, 2);
	lily_assert_int_equal(max, 2);
	*next = 1;

	next = kai_next_array_element((void**) &arr, &count, &max, sizeof(int));

	lily_assert_not_null(next);
	lily_assert_ptr_equal(arr+2, next);
	lily_assert_int_equal(count, 3);
	lily_assert_int_equal(max, 4);
	*next = 3;

	next = kai_next_array_element((void**) &arr, &count, &max, sizeof(int));

	lily_assert_not_null(next);
	lily_assert_ptr_equal(arr+3, next);
	lily_assert_int_equal(count, 4);
	lily_assert_int_equal(max, 4);
	*next = 4;

	int expected[] = { 2, 1, 3, 4 };
	lily_assert_memory_equal(expected, arr, 4 * sizeof(int));

	free(arr);
}