summaryrefslogtreecommitdiff
path: root/lichen.test.c
blob: 6329992f5a5e700cc43c94abae7f3eeb4de3583a (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#define LILY_IMPLEMENTATION
#include "lichen.h"
#include "lily-test.h"


LILY_FILE_BEGIN(lichen_suite)

LILY_TEST("xorshift output") {
  struct li_xorshift32_t xor;
  li_xorshift32_seed(&xor, 0);
  CHECK_EQ(xor.generator.state, &(xor.state), "%p");
  CHECK_NEQ(xor.state, 0, "%d");
  uint16_t start = xor.state;
  INFO("%p, %p", &(xor.state), xor.generator.state);
  INFO("%d", xor.generator.rand(xor.generator.state));
  INFO("%d, %d", xor.state, *((uint64_t*)xor.generator.state));
  INFO("%d", xor.generator.rand(xor.generator.state));
  INFO("%d, %d", xor.state, *((uint64_t*)xor.generator.state));
  INFO("%d", xor.generator.rand(xor.generator.state));
  INFO("%d, %d", xor.state, *((uint64_t*)xor.generator.state));
  INFO("%d", xor.generator.rand(xor.generator.state));
  INFO("%d, %d", xor.state, *((uint64_t*)xor.generator.state));
  REQUIRE_NEQ(xor.state, start, "%d");
}
#include LILY_PUSH_TEST()



void do_nothing(void *k) {
  k = k;
}
void free_data(void *m) {
  li_free_llnode(m, do_nothing);
}

LILY_TEST("allocate, copy, and destroy llnode") {
  li_llnode_t *n = li_alloc_llnode();
  REQUIRE_NEQ(n, NULL, "%p");
  CHECK_EQ(n->next, NULL, "%p");
  int k = 0;
  n->tag = 4;
  n->data = &k;
  li_llnode_t *m = li_copy_llnode(n);
  REQUIRE_NEQ(m, NULL, "%p");
  CHECK_EQ(m->tag, 4, "%d");
  CHECK_EQ(m->data, &k, "%p");
  CHECK_EQ(m->next, n->next, "%p");
  n->data = m;
  li_free_llnode(n, free_data);
}
#include LILY_PUSH_TEST()



LILY_TEST("append to list") {
  struct li_ll_t *list = li_alloc_ll();
  REQUIRE_NEQ(list, NULL, "%p");

  int a = 1; int b = 2; int c = 3;

  li_ll_append(list, &a);
  CHECK_EQ(list->count, 1, "%d");
  REQUIRE_NEQ(list->head, NULL, "%p");
  CHECK_EQ(list->head->data, &a, "%p");
  CHECK_EQ(list->head->next, NULL, "%p");

  li_ll_append(list, &b);
  CHECK_EQ(list->count, 2, "%d");
  REQUIRE_NEQ(list->head->next, NULL, "%p");
  CHECK_EQ(list->head->next->data, &b, "%p");
  CHECK_EQ(list->head->next->next, NULL, "%p");

  li_ll_append(list, &c);
  CHECK_EQ(list->count, 3, "%d");
  REQUIRE_NEQ(list->head->next->next, NULL, "%p");
  CHECK_EQ(list->head->next->next->data, &c, "%p");
  CHECK_EQ(list->head->next->next->next, NULL, "%p");

  li_free_ll(list, do_nothing);
}
#include LILY_PUSH_TEST()


LILY_TEST("prepend to list") {
  struct li_ll_t *list = li_alloc_ll();
  REQUIRE_NEQ(list, NULL, "%p");

  int a = 1; int b = 2; int c = 3;

  li_ll_prepend(list, &b);
  CHECK_EQ(list->count, 1, "%d");
  REQUIRE_NEQ(list->head, NULL, "%p");
  CHECK_EQ(list->head->data, &b, "%p");
  CHECK_EQ(list->head->next, NULL, "%p");

  li_ll_prepend(list, &a);
  CHECK_EQ(list->count, 2, "%d");
  REQUIRE_NEQ(list->head->next, NULL, "%p");
  CHECK_EQ(list->head->data, &a, "%p");
  CHECK_EQ(list->head->next->data, &b, "%p");
  CHECK_EQ(list->head->next->next, NULL, "%p");

  li_ll_prepend(list, &c);
  CHECK_EQ(list->count, 3, "%d");
  REQUIRE_NEQ(list->head->next->next, NULL, "%p");
  CHECK_EQ(list->head->data, &c, "%p");
  CHECK_EQ(list->head->next->data, &a, "%p");
  CHECK_EQ(list->head->next->next->data, &b, "%p");
  CHECK_EQ(list->head->next->next->next, NULL, "%p");

  li_free_ll(list, do_nothing);
}
#include LILY_PUSH_TEST()


LILY_TEST("mix prepends and appends") {
  struct li_ll_t *list = li_alloc_ll();
  REQUIRE_NEQ(list, NULL, "%p");

  int a = 1; int b = 2; int c = 3;

  li_ll_prepend(list, &b);
  CHECK_EQ(list->count, 1, "%d");
  REQUIRE_NEQ(list->head, NULL, "%p");
  CHECK_EQ(list->head->data, &b, "%p");
  CHECK_EQ(list->head->next, NULL, "%p");

  li_ll_prepend(list, &a);
  CHECK_EQ(list->count, 2, "%d");
  REQUIRE_NEQ(list->head->next, NULL, "%p");
  CHECK_EQ(list->head->data, &a, "%p");
  CHECK_EQ(list->head->next->data, &b, "%p");
  CHECK_EQ(list->head->next->next, NULL, "%p");

  li_ll_append(list, &c);
  CHECK_EQ(list->count, 3, "%d");
  REQUIRE_NEQ(list->head->next->next, NULL, "%p");
  CHECK_EQ(list->head->data, &a, "%p");
  CHECK_EQ(list->head->next->data, &b, "%p");
  CHECK_EQ(list->head->next->next->data, &c, "%p");
  CHECK_EQ(list->head->next->next->next, NULL, "%p");

  li_free_ll(list, do_nothing);

}
#include LILY_PUSH_TEST()


void * copy_int(void *n) {
  int *copy = malloc(sizeof(int));
  *copy = *((int*)n);
  return copy;
}
LILY_TEST("copy a list") {
  struct li_ll_t *list_a = li_alloc_ll();
  list_a->tag = 8;
  int a = 1; int b = 2; int c = 3;
  li_ll_append(list_a, &a);
  li_ll_append(list_a, &b);
  li_ll_append(list_a, &c);

  struct li_ll_t *list_b;
  li_copy_list(&list_b, list_a, copy_int);
  li_free_ll(list_a, do_nothing);

  REQUIRE_NEQ(list_b, NULL, "%p");
  CHECK_EQ(list_b->tag, 8, "%d");
  CHECK_EQ(list_b->count, 3, "%d");
  CHECK_EQ(*((int*)list_b->head->data), a, "%d");
  CHECK_EQ(*((int*)list_b->head->next->data), b, "%d");
  CHECK_EQ(*((int*)list_b->head->next->next->data), c, "%d");

  li_free_ll(list_b, free);
}
#include LILY_PUSH_TEST()


int compare_data(void *va, void *vb) {
  int a = *((int*)va);
  int b = *((int*)vb);
  return a - b;
}
LILY_TEST("sort a list") {
  li_ll_t *list = li_alloc_ll();
  int arr[] = { 5, 6, 3, 2, 9, 8, 1, 0, 4, 7 };
  for (size_t i=0; i<sizeof(arr)/sizeof(int); i++) {
    li_ll_append(list, arr+i);
  }
  li_sort_list(list, compare_data);
  li_llnode_t *n = list->head;
  for (size_t i=0; i<sizeof(arr)/sizeof(int); i++) {
    INFO("[%d] original: %d / sort: %d", i, arr[i], *((int*)n->data));
    CHECK_EQ((int)i, *((int*)n->data), "%d");
    n = n->next;
  }

  li_free_ll(list, do_nothing);
}
#include LILY_PUSH_TEST()



#define LILY_FILE_END
#include LILY_REGISTER_TESTS()


int main() {
  lily_begin();
  lichen_suite();
  lily_finish();
  return 0;
}