summaryrefslogtreecommitdiff
path: root/lichen.test.c
blob: 622f48069a3db6b316993406778e9f2aa42febc0 (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
#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()



#define LILY_FILE_END
#include LILY_REGISTER_TESTS()


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