-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestspace.c
executable file
·359 lines (305 loc) · 9.55 KB
/
testspace.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <stdlib.h> // free & co
#include <stdio.h> // sprintf & co
#include <assert.h>
#include <string.h>
#include "log.h"
#include "entrelacs/entrelacs.h"
#include "entrelacs/entrelacsm.h"
#include "mem.h" // geoalloc
#define test_title(T) fprintf(stderr, T "\n")
static struct s_buffer {
int size;
int max;
char* buffer;
} buffer = {0, 0, NULL};
Arrow _printArrow(Arrow a) {
if (xl_isRooted(a)) {
int size = buffer.size;
geoalloc(&buffer.buffer, &buffer.max, &buffer.size, sizeof (char), size + 1);
sprintf(buffer.buffer + size - 1, "_");
}
enum e_xlType t = xl_typeOf(a);
if (t == XL_ATOM) {
int l;
char* s = xl_memOf(a, &l);
int size = buffer.size;
geoalloc(&buffer.buffer, &buffer.max, &buffer.size, sizeof (char), size + l + 2);
sprintf(buffer.buffer + size - 1, "\"%s\"", s);
free(s);
} else if (t == XL_PAIR) {
int size;
size = buffer.size;
geoalloc(&buffer.buffer, &buffer.max, &buffer.size, sizeof (char), size + 1);
sprintf(buffer.buffer + size - 1, "(");
_printArrow(xl_tailOf(a));
size = buffer.size;
geoalloc(&buffer.buffer, &buffer.max, &buffer.size, sizeof (char), size + 2);
sprintf(buffer.buffer + size - 1, ", ");
_printArrow(xl_headOf(a));
size = buffer.size;
geoalloc(&buffer.buffer, &buffer.max, &buffer.size, sizeof (char), size + 1);
sprintf(buffer.buffer + size - 1, ")");
}
return a;
}
Arrow printArrow(Arrow a, Arrow ctx) {
geoalloc(&buffer.buffer, &buffer.max, &buffer.size, sizeof (char), 1);
buffer.buffer[0] = '\0';
_printArrow(a);
fprintf(stderr, "%s\n", buffer.buffer);
return 0;
}
int basic() {
// assimilate arrows
test_title("assimilate arrows");
DEFATOM(hello); // Arrow hello = xl_atom("hello");
DEFATOM(world);
DEFATOM(small12345);
DEFATOM(more_bigger_string_11111111111111111111);
DEFA(hello, world); // Arrow _hello_world = xl_pair(hello, world);
// check regular pairs
test_title("check regular pairs");
assert(typeOf(_hello_world) == XL_PAIR);
assert(tail(_hello_world) == hello);
assert(head(_hello_world) == world);
test_title("Test Adam special case");
assert(A(EVE, EVE) != EVE);
// Check atoms
test_title("check atoms");
assert(typeOf(hello) == XL_ATOM && typeOf(world) == XL_ATOM
&& typeOf(more_bigger_string_11111111111111111111) == XL_ATOM
&& typeOf(small12345) == XL_ATOM);
char *s1 = str(hello);
assert(!strcmp("hello", s1));
free(s1);
char *s2 = str(world);
assert(!strcmp("world", s2));
free(s2);
char *s3 = str(more_bigger_string_11111111111111111111);
assert(!strcmp("more_bigger_string_11111111111111111111", s3));
free(s3);
char *s4 = str(small12345);
assert(!strcmp("small12345", s4));
free(s4);
// check rooting
test_title("check rooting");
root(_hello_world);
root(more_bigger_string_11111111111111111111);
assert(isRooted(_hello_world));
assert(isRooted(more_bigger_string_11111111111111111111));
{ // check very big string (blob)
test_title("check very big string (blob)");
char* bigStr = "11111111112222222222233333333333334444444444445555555555566666666666677777777777788888888888888999999999999999";
Arrow bigAtom = atom(bigStr);
char* bigStrBack = strOf(bigAtom);
assert(0 == strcmp(bigStrBack, bigStr));
assert(xl_isAtom(bigAtom));
free(bigStrBack);
{ // digest computation
test_title("check digest computation");
char* digest = digestOf(bigAtom, NULL);
assert(digest);
fprintf(stderr, "digest: %s\n", digest);
{ // check digest
test_title("check digest-based arrow retrieval");
Arrow byDigest = digestMaybe(digest);
assert(byDigest == bigAtom);
}
free(digest);
}
}
// check GC
test_title("check GC");
DEFATOM(loose);
DEFA(hello, loose);
commit();
assert(typeOf(loose) == XL_UNDEF);
assert(typeOf(_hello_loose) == XL_UNDEF);
// check rooting persistency
test_title("check rooting persistency");
assert(isRooted(_hello_world));
// check deduplication
test_title("check deduplication");
Arrow original = _hello_world;
Arrow original_big_string = more_bigger_string_11111111111111111111;
{
DEFATOM(more_bigger_string_11111111111111111111);
assert(original_big_string == more_bigger_string_11111111111111111111);
DEFATOM(hello);
DEFATOM(world);
DEFA(hello, world);
assert(original == _hello_world);
}
// check uri assimilation
test_title("check URI assimilation");
{
Arrow uri = uri("/hello+world");
assert(uri == _hello_world);
}
// check natom/atom equivalency
test_title("check natom/atom equivalency");
{
Arrow helloB = atomn(5, "hello");
Arrow worldB = atomn(5, "world");
DEFA(helloB, worldB);
assert(original == _helloB_worldB);
}
// check natom dedup
test_title("check natom dedup");
Arrow fooB = atom("headOf");
Arrow barB = atom("tailOf");
DEFA(fooB, barB);
Arrow originalB = _fooB_barB;
{
Arrow fooB = atomn(6, "headOf");
Arrow barB = atomn(6, "tailOf");
DEFA(fooB, barB);
assert(originalB == _fooB_barB);
}
// check pair connection
test_title("pair connection");
DEFATOM(dude);
DEFA(hello, dude);
root(_hello_dude);
childrenOfCB(hello, printArrow, Eve());
// check unrooting
test_title("check unrooting");
unroot(_hello_world);
assert(!isRooted(_hello_world));
// check GC after unrooting
test_title("check GC after unrooting");
commit();
assert(XL_UNDEF == typeOf(_hello_world));
assert(XL_UNDEF == typeOf(world));
assert(XL_ATOM == typeOf(hello));
// check pair disconnection
test_title("check pair disconnection");
childrenOfCB(hello, printArrow, Eve());
unroot(_hello_dude);
commit();
return 0;
}
int stress() {
char buffer[50];
Arrow atoms[1000];
Arrow pairs[500];
Arrow big;
// deduplication stress
test_title("deduplication stress");
{
for (int i = 0; i < 200; i++) {
snprintf(buffer, 50, "This is the tag #%d", i);
atoms[i] = atom(buffer);
if (i % 2) {
int j = (i - 1) / 2;
pairs[j] = A(atoms[i - 1], atoms[i]);
printArrow(tailOf(pairs[j]), Eve());
printArrow(headOf(pairs[j]), Eve());
printArrow(pairs[j], Eve());
}
}
for (int i = 0; i < 200; i++) {
snprintf(buffer, 50, "This is the tag #%d", i);
Arrow tagi = atom(buffer);
assert(atoms[i] == tagi);
if (i % 2) {
int j = (i - 1) / 2;
Arrow pairj = A(atoms[i - 1], atoms[i]);
assert(pairs[j] == pairj);
}
}
}
// rooting stress (also preserve this material from GC for further tests)
test_title("rooting stress");
{
for (int i = 0; i < 200; i++) {
root(atoms[i]);
if (i % 2) {
int j = (i - 1) / 2;
root(pairs[j]);
}
}
commit();
}
DEFATOM(connectMe);
// connection stress
test_title("connection stress");
{
root(connectMe);
for (int i = 0; i < 200; i++) {
Arrow child = A(connectMe, atoms[i]);
root(child);
}
for (int j = 0; j < 100; j++) {
printArrow(pairs[j], Eve());
Arrow child = A(connectMe, pairs[j]);
root(child);
}
childrenOfCB(connectMe, printArrow, Eve());
}
// disconnection stress
test_title("disconnection stress");
{
for (int i = 0; i < 200; i++) {
Arrow child = A(connectMe, atoms[i]);
unroot(child);
}
for (int j = 0; j < 100; j++) {
Arrow child = A(connectMe, pairs[j]);
unroot(child);
}
childrenOfCB(connectMe, printArrow, Eve());
}
// connecting stress (big depth)
test_title("connecting stress (big depth)");
{
Arrow loose = atom("save me!");
big = loose;
for (int i = 0; i < 2; i++) {
if (i % 2)
big = A(big, atoms[i]);
else
big = A(atoms[i], big);
}
for (int j = 0; j < 100; j++) {
if (j % 2)
big = A(big, pairs[j]);
else
big = A(pairs[j], big);
}
root(big);
commit();
assert(typeOf(loose) == XL_ATOM);
printArrow(big, Eve());
}
// disconnecting stress (big depth)
test_title("disconnecting stress (big depth)");
{
unroot(big);
commit();
assert(isEve(atomMaybe("save me!")));
// unrooting stress
test_title("unrooting stress");
for (int i = 0; i < 200; i++) {
unroot(atoms[i]);
if (i % 2) {
int j = (i - 1) / 2;
unroot(pairs[j]);
}
}
unroot(connectMe);
commit();
}
return 0;
}
int space_unitTest();
int main(int argc, char* argv[]) {
log_init(NULL, "space=debug");
xl_init();
xl_begin();
space_unitTest();
basic();
stress();
xl_over();
return 0;
}