list-test.c 2.63 KB
Newer Older
1 2 3 4 5
#include <stdio.h>
#include "test.h"
#include "list.h"

#define TEST_ARRAY_SIZE 10
6
INIT_LIST(l, TEST_ARRAY_SIZE);
7 8 9 10 11

void
test_flush(void)
{
	desc("test l_flush()");
12 13 14
	l_flush(l);
	assert(l->head == l->sup);
	assert(l->sup == TEST_ARRAY_SIZE);
15 16 17 18 19 20
}

void
test_empty(void)
{
	desc("test l_empty()");
21
	l_flush(l);
22

23
	assert(l_empty(l));
24

25 26
	l_push(l, 5);
	assert(!l_empty(l));
27

28 29
	l_delete(l, 5);
	assert(l_empty(l));
30 31 32 33 34 35
}

void
test_push(void)
{
	desc("test l_push(), l_first(), l_last()");
36
	l_flush(l);
37

38 39 40
	l_push(l, 2);
	l_push(l, 5);
	l_push(l, 3);
41

42 43
	assert(l_first(l) == 3);
	assert(l_last(l) == 2);
44

45 46 47
	l_delete(l, 5);
	assert(l_first(l) == 3);
	assert(l_last(l) == 2);
48

49 50 51
	l_delete(l, 2);
	assert(l_first(l) == 3);
	assert(l_last(l) == 3);
52

53 54 55 56
	l_push(l, 1);
	l_push(l, 9);
	l_push(l, 9);
	assert(l_first(l) == 9);
57

58 59 60
	l_delete(l, 9);
	assert(l_first(l) == 1);
	assert(l_last(l) == 3);
61

62 63 64
	l_delete(l, 3);
	assert(l_first(l) == 1);
	assert(l_last(l) == 1);
65

66 67
	l_delete(l, 1);
	assert(l_empty(l));
68 69
}

70 71 72 73
void
test_contains(void)
{
	desc("test l_contains()");
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	l_flush(l);

	l_push(l, 3);
	l_push(l, 4);
	l_push(l, 10);
	l_push(l, 1);

	assert(!l_contains(l, 0));
	assert(l_contains(l, 1));
	assert(!l_contains(l, 2));
	assert(l_contains(l, 3));
	assert(l_contains(l, 4));
	assert(!l_contains(l, 5));
	assert(!l_contains(l, 6));
	assert(!l_contains(l, 7));
	assert(!l_contains(l, 8));
	assert(!l_contains(l, 9));
91 92

	for (uint8_t i = 10; i != 0; i++) {
93
		assert(!l_contains(l, i));
94 95
	}

96
	l_flush(l);
97 98 99

	// paranoid test, what if there are 3s everywhere
	for (uint8_t i = 0; i < TEST_ARRAY_SIZE; i++) {
100
		l->array[i] = 3;
101
	}
102 103
	assert(!l_contains(l, 3));
	assert(l_empty(l));
104

105 106 107
	l_push(l, 4);
	assert(!l_contains(l, 3));
	assert(l_contains(l, 4));
108 109
}

110 111 112 113 114
void
test_append(void)
{
	desc("test l_append()");

115 116 117 118 119
	l_flush(l);
	l_push(l, 3);
	l_push(l, 4);
	l_push(l, 9);
	l_append(l, 2);
120

121 122 123 124 125
	assert(l->head == 9);
	assert(l->array[9] == 4);
	assert(l->array[4] == 3);
	assert(l->array[3] == 2);
	assert(l->array[2] == l->sup);
126

127 128
	l_flush(l);
	l_append(l, 5);
129

130 131
	assert(l_last(l) == 5);
	assert(l_first(l) == 5);
132

133 134 135 136 137
	l_flush(l);
	l_push(l, 2);
	l_push(l, 3);
	l_push(l, 4);
	l_append(l, 4);
138

139 140
	assert(l_last(l) == 4);
	assert(l_first(l) == 3);
141 142 143 144 145 146 147
}

void
test_index(void)
{
	desc("test l_index()");

148 149 150 151 152
	l_flush(l);
	l_push(l, 3);
	l_push(l, 4);
	l_push(l, 9);
	l_append(l, 2);
153

154 155 156 157
	assert(l_index(l, 3) == 2);
	assert(l_index(l, 4) == 1);
	assert(l_index(l, 9) == 0);
	assert(l_index(l, 2) == 3);
158

159 160 161
	assert(l_index(l, 5) == l->sup);
	assert(l_index(l, l->sup) == l->sup);
	assert(l_index(l, l->sup + 1) == l->sup);
162 163
}

164 165 166 167 168 169
int
main(int argc, char** argv)
{
	test_flush();
	test_empty();
	test_push();
170
	test_contains();
171 172 173
	test_append();
	test_index();

174 175
	return 0;
}