-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-cell.c
148 lines (118 loc) · 2.47 KB
/
test-cell.c
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
#include <unit.h>
#include <cell.c>
static void test_cell_alloc();
static void test_cell_link();
static void test_cell_traverse();
static void test_tape_buffer();
static void test_tape_free();
static void test_tape_read();
struct unit_test tests[] = {
{.msg = "should allocate cells",
.fun = unit_list(test_cell_alloc),},
{.msg = "should link cells",
.fun = unit_list(test_cell_link),},
{.msg = "should traverse cells",
.fun = unit_list(test_cell_traverse),},
{.msg = "should free tapes",
.fun = unit_list(test_tape_free),},
{.msg = "should convert buffers to tapes",
.fun = unit_list(test_tape_buffer),},
{.msg = "should read tapes into buffers",
.fun = unit_list(test_tape_read),},
};
#include <unit.t>
#define make_tape(...) _make_tape((cell **[]){__VA_ARGS__, 0});
void
_make_tape(cell ***cells)
{
size_t i;
for (i=0; cells[i]; ++i) {
ok(*cells[i] = cell_from_bit(0));
if (i) try(link_cells(*cells[i-1],*cells[i]));
}
}
void
test_cell_alloc()
{
cell *p;
ok(p = cell_from_bit(0));
ok(*p == 0);
try(free(p));
ok(p = cell_from_bit(1));
ok(*p == 1);
try(free(p));
}
void
test_cell_link()
{
cell *a, *b, *c;
make_tape(&a, &b, &c);
ok(*a == (cell)b);
ok(*b^(cell)c == (cell)a);
ok(*b^(cell)a == (cell)c);
ok(*c == (cell)b);
try(free(a));
try(free(b));
try(free(c));
}
void
test_cell_traverse()
{
cell *a, *b, *c;
make_tape(&a, &b, &c);
ok(b == get_next_cell(a, 0));
ok(c == get_next_cell(b, a));
ok(0 == get_next_cell(c, b));
ok(0 == get_next_cell(a, b));
ok(a == get_next_cell(b, c));
ok(a == walk_tape(c, 0));
ok(c == walk_tape(a, 0));
try(free(a));
try(free(b));
try(free(c));
}
void
test_tape_buffer()
{
struct walker walker[1];
cell *current;
cell *tape;
uint8_t b;
char ch;
int i;
ch = 1;
again:
ok(tape = tape_from_buffer(&ch, 1));
try(walker_begin(walker, tape, 0));
for (i=0; i<8; ++i) {
b = bit_at_index(ch, i);
current = walker->current;
okf(b == (*current & 1),
"expected %hhu in cell %d, got %lu",
b, i, *current & 1);
try(walker_step(walker));
}
ok(walker->next == 0x0);
free_tape(tape, 0);
ch <<= 1;
if (ch) goto again;
}
void
test_tape_free()
{
cell *a, *b, *c;
make_tape(&a, &b, &c);
try(free_tape(a, 0));
}
void
test_tape_read()
{
char string[6]="hello";
char buffer[6]={0};
cell *tape;
ok(tape = tape_from_buffer(string, strlen(string)));
try(copy_tape_into_buffer(buffer, strlen(string), tape));
okf(!strcmp(buffer, string),
"expected \"%s\", got \"%s\"",
string, buffer);
}