forked from urbit/vere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noun_tests.c
1789 lines (1487 loc) · 37.9 KB
/
noun_tests.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// @file
#include "noun.h"
#include "ivory.h"
#include "ur/ur.h"
#include "vere.h"
#define TRUE 1
#define FALSE 0
/* _setup(): prepare for tests.
*/
static void
_setup(void)
{
u3m_init(1 << 20);
u3m_pave(c3y);
}
/* _test_u3r_chop: "extract bit slices from atom"
*/
static c3_i
_test_u3r_chop()
{
c3_i ret_i = 1;
c3_w dst_w = 0;
u3_atom src = 0b11011;
// bloq 0
//
{
// read 1 bit from pos=0 (far right)
//
dst_w = 0;
u3r_chop(0, 0, 1, 0, &dst_w, src);
if ( 0x1 != dst_w ) {
fprintf(stderr, "test: u3r_chop: bloq 0, 0\r\n");
ret_i = 0;
}
// read 1 bit from pos=1
//
dst_w = 0;
u3r_chop(0, 1, 1, 0, &dst_w, src);
if ( 0x1 != dst_w ) {
fprintf(stderr, "test: u3r_chop: bloq 0, 1\r\n");
ret_i = 0;
}
// read 1 bit from pos=2
//
dst_w = 0;
u3r_chop(0, 2, 1, 0, &dst_w, src);
if ( 0x0 != dst_w ) {
fprintf(stderr, "test: u3r_chop: bloq 0, 2\r\n");
ret_i = 0;
}
// read 4 x 1 bit bloq from pos=0
//
dst_w = 0;
u3r_chop(0, 0, 4, 0, &dst_w, src);
if ( 0b1011 != dst_w ) {
fprintf(stderr, "test: u3r_chop: bloq 0, 3\r\n");
ret_i = 0;
}
// read 4 x 1 bit bloq from pos=0 into offset 1
//
dst_w = 0;
u3r_chop(0, 0, 4, 1, &dst_w, src);
if ( 0b10110 != dst_w ) {
fprintf(stderr, "test: u3r_chop: bloq 0, 4\r\n");
ret_i = 0;
}
}
// bloq 1
//
{
// read 2 bit from pos=0 (far right)
//
dst_w = 0;
u3r_chop(1, 0, 1, 0, &dst_w, src);
if ( 0b11 != dst_w ) {
fprintf(stderr, "test: u3r_chop: bloq 1, 0\r\n");
ret_i = 0;
}
// read 2 bit from pos=1
//
dst_w = 0;
u3r_chop(1, 1, 1, 0, &dst_w, src);
if ( 0b10 != dst_w ) {
fprintf(stderr, "test: u3r_chop: bloq 1, 1\r\n");
ret_i = 0;
}
// read 2 bit from pos=2 (2 bloq over)
dst_w = 0;
u3r_chop(1, 2, 1, 0, &dst_w, src);
if ( 0b01 != dst_w ) {
fprintf(stderr, "test: u3r_chop: bloq 1, 2\r\n");
ret_i = 0;
}
}
// bloq 3
{
dst_w = 0;
u3r_chop(3, 0, 1, 0, &dst_w, src);
if ( 0b11011 != dst_w ) {
fprintf(stderr, "test: u3r_chop: bloq 3, 0\r\n");
ret_i = 0;
}
}
// read 1,8,16 bit bloqs from an indirect atom
//
{
src = u3i_string("abcdefghij");
// 1 bit pos=0 (far right)
//
dst_w = 0;
u3r_chop(0, 0, 1, 0, &dst_w, src);
if ( 0b1 != dst_w ) {
fprintf(stderr, "test: u3r_chop: indirect 0\r\n");
ret_i = 0;
}
// 8 bits pos=0
//
dst_w = 0;
u3r_chop(0, 0, 8, 0, &dst_w, src);
if ( 0b1100001 != dst_w ) {
fprintf(stderr, "test: u3r_chop: indirect 1\r\n");
ret_i = 0;
}
// 1 byte pos=0
//
dst_w = 0;
u3r_chop(3, 0, 1, 0, &dst_w, src);
if ( 0b1100001 != dst_w ) {
fprintf(stderr, "test: u3r_chop: indirect 2\r\n");
ret_i = 0;
}
// 1 short pos=0
//
dst_w = 0;
u3r_chop(4, 0, 1, 0, &dst_w, src);
if ( 0b0110001001100001 != dst_w ) {
fprintf(stderr, "test: u3r_chop: indirect 3\r\n");
ret_i = 0;
}
u3z(src);
}
// read lots of bits from a direct noun which holds 64 bits of data
// makes sure that we handle top 32 / bottom 32 correctly
{
c3_y inp_y[8] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 };
src = u3i_bytes(8, inp_y);
c3_w dst_w[2] = {0};
u3r_chop(0, 0, 63, 0, dst_w, src);
if ( (0x3020100 != dst_w[0]) || (0x7060504 != dst_w[1]) ) {
fprintf(stderr, "test: u3r_chop: indirect 4\r\n");
ret_i = 0;
}
u3z(src);
}
// as above (read lots of bits from a direct noun which holds 64 bits of data
// makes sure that we handle top 32 / bottom 32 correctly)
// but with a bit more nuance
{
c3_y inp_y[8] = { 0x0, 0x0, 0x0, 0xaa, 0xff, 0x0, 0x0, 0x0 };
src = u3i_bytes(8, (c3_y*)inp_y);
dst_w = 0;
u3r_chop(0, 24, 16, 0, &dst_w, src);
if ( 0b1111111110101010 != dst_w ) {
fprintf(stderr, "test: u3r_chop: indirect 5\r\n");
ret_i = 0;
}
u3z(src);
}
return ret_i;
}
/* _test_chop_slow(): "golden master" for chop tests (formerly u3r_chop())
*/
void
_test_chop_slow(c3_g met_g,
c3_w fum_w,
c3_w wid_w,
c3_w tou_w,
c3_w* dst_w,
c3_w len_w,
c3_w* buf_w)
{
c3_w i_w;
if ( met_g < 5 ) {
c3_w san_w = (1 << met_g);
c3_w mek_w = ((1 << san_w) - 1);
c3_w baf_w = (fum_w << met_g);
c3_w bat_w = (tou_w << met_g);
// XX: efficiency: poor. Iterate by words.
//
for ( i_w = 0; i_w < wid_w; i_w++ ) {
c3_w waf_w = (baf_w >> 5);
c3_g raf_g = (baf_w & 31);
c3_w wat_w = (bat_w >> 5);
c3_g rat_g = (bat_w & 31);
c3_w hop_w;
hop_w = (waf_w >= len_w) ? 0 : buf_w[waf_w];
hop_w = (hop_w >> raf_g) & mek_w;
dst_w[wat_w] ^= (hop_w << rat_g);
baf_w += san_w;
bat_w += san_w;
}
}
else {
c3_g hut_g = (met_g - 5);
c3_w san_w = (1 << hut_g);
c3_w j_w;
for ( i_w = 0; i_w < wid_w; i_w++ ) {
c3_w wuf_w = (fum_w + i_w) << hut_g;
c3_w wut_w = (tou_w + i_w) << hut_g;
for ( j_w = 0; j_w < san_w; j_w++ ) {
dst_w[wut_w + j_w] ^=
((wuf_w + j_w) >= len_w)
? 0
: buf_w[wuf_w + j_w];
}
}
}
}
/* _test_chop_smol(): test permuations of chop from bloq 0-4
*/
static c3_i
_test_chop_smol(c3_c* cap_c, c3_y val_y)
{
c3_i ret_i = 1;
c3_g met_g;
c3_w fum_w, wid_w, tou_w;
c3_w len_w = 34; // (rsh [0 5] (mul 2 (mul 34 (bex 4))))
c3_w src_w[len_w];
c3_w a_w[len_w];
c3_w b_w[len_w];
memset(src_w, val_y, len_w << 2);
for ( met_g = 0; met_g < 5; met_g++ ) {
for ( fum_w = 0; fum_w <= len_w; fum_w++ ) {
for ( wid_w = 0; wid_w <= len_w; wid_w++ ) {
for ( tou_w = 0; tou_w <= len_w; tou_w++ ) {
memset(a_w, 0, len_w << 2);
memset(b_w, 0, len_w << 2);
u3r_chop_words(met_g, fum_w, wid_w, tou_w, a_w, len_w, src_w);
_test_chop_slow(met_g, fum_w, wid_w, tou_w, b_w, len_w, src_w);
if ( 0 != memcmp(a_w, b_w, len_w << 2) ) {
c3_g sif_g = 5 - met_g;
c3_w mas_w = (1 << met_g) - 1;
c3_w out_w = tou_w >> sif_g;
c3_w max_w = out_w + !!(fum_w & mas_w)
+ (wid_w >> sif_g) + !!(wid_w & mas_w);
fprintf(stderr, "%s (0x%x): met_g=%u fum_w=%u wid_w=%u tou_w=%u\r\n",
cap_c, val_y,
met_g, fum_w, wid_w, tou_w);
fprintf(stderr, "%u-%u: ", out_w, max_w - 1);
for ( ; out_w < max_w; out_w++ ) {
fprintf(stderr, "[0x%x 0x%x] ", a_w[out_w], b_w[out_w]);
}
fprintf(stderr, "\r\n");
}
}
}
}
}
return ret_i;
}
/* _test_chop_huge(): test permuations of chop from bloq 5+
*/
static c3_i
_test_chop_huge(c3_c* cap_c, c3_y val_y)
{
c3_i ret_i = 1;
c3_g met_g;
c3_w fum_w, wid_w, tou_w;
c3_w len_w = 192; // (rsh [0 5] (mul 2 (mul 3 (bex 10))))
c3_w src_w[len_w];
c3_w a_w[len_w];
c3_w b_w[len_w];
memset(src_w, val_y, len_w << 2);
for ( met_g = 5; met_g <= 10; met_g++ ) {
for ( fum_w = 0; fum_w <= 3; fum_w++ ) {
for ( wid_w = 0; wid_w <= 2; wid_w++ ) {
for ( tou_w = 0; tou_w <= 1; tou_w++ ) {
memset(a_w, 0, len_w << 2);
memset(b_w, 0, len_w << 2);
u3r_chop_words(met_g, fum_w, wid_w, tou_w, a_w, len_w, src_w);
_test_chop_slow(met_g, fum_w, wid_w, tou_w, b_w, len_w, src_w);
if ( 0 != memcmp(a_w, b_w, len_w << 2) ) {
c3_g sif_g = met_g - 5;
c3_w mas_w = (1 << met_g) - 1;
c3_w out_w = tou_w << sif_g;
c3_w max_w = out_w + !!(fum_w & mas_w)
+ (wid_w << sif_g) + !!(wid_w & mas_w);
fprintf(stderr, "%s (0x%x): met_g=%u fum_w=%u wid_w=%u tou_w=%u\r\n",
cap_c, val_y,
met_g, fum_w, wid_w, tou_w);
fprintf(stderr, "%u-%u: ", out_w, max_w - 1);
for ( ; out_w < max_w; out_w++ ) {
fprintf(stderr, "[0x%x 0x%x] ", a_w[out_w], b_w[out_w]);
}
fprintf(stderr, "\r\n");
}
}
}
}
}
return ret_i;
}
/* _test_u3r_chop(): bit slice XOR
*/
static c3_i
_test_chop()
{
return _test_u3r_chop()
& _test_chop_smol("chop smol zeros", 0x0)
& _test_chop_smol("chop smol ones", 0xff)
& _test_chop_smol("chop smol alt 1", 0xaa)
& _test_chop_smol("chop smol alt 2", 0x55)
& _test_chop_huge("chop huge zeros", 0x0)
& _test_chop_huge("chop huge ones", 0xff)
& _test_chop_huge("chop huge alt 1", 0xaa)
& _test_chop_huge("chop huge alt 2", 0x55);
}
/* _util_rand_string(): dynamically allocated len_w random string
*/
static c3_y*
_util_rand_string(c3_w len_w)
{
c3_c* choice_c =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
c3_w choice_len_w = strlen(choice_c);
c3_y* out_y = c3_malloc(len_w + 1);
c3_w i_w;
for (i_w = 0; i_w < len_w; i_w ++){
out_y[i_w] = choice_c[ (c3_w) rand() % choice_len_w ];
}
out_y[i_w] = 0;
return out_y;
}
/* _test_noun_bits_helper():
*/
static void
_test_noun_bits_helper(u3_noun a, int direct_o,
int indirect_o,
int indirect_atom_o,
int cell_o)
{
#if 0
printf("=========== %u\n", a);
printf(" 31 bit %u\n", a & ( ((c3_w)1) << 31));
printf(" 30 bit %u\n", a & ( ((c3_w)1) << 30));
printf(" dir %x\n", c3y == u3a_is_cat(a));
printf(" ind %x\n", c3y == u3a_is_dog(a));
printf(" i cell %x\n", c3y == u3a_is_cell(a));
printf(" i atom %x\n", c3y == u3a_is_pug(a));
#endif
if ( direct_o != (c3y == u3a_is_cat(a)) ) {
printf("*** _test_noun_bits_one() fail: u3a_is_direct\r\n");
}
if ( indirect_o != (c3y == u3a_is_dog(a)) ) {
printf("*** fail-2 u3a_is_indirect %d\r\n", c3n == u3a_is_cat(a));
}
if ( cell_o != (c3y == u3a_is_cell(a)) ) {
printf("*** fail-4 u3a_is_cell\r\n");
}
if ( indirect_atom_o != (c3y == u3a_is_pug(a)) ) {
printf("*** fail-3 u3a_is_indirect_atom\r\n");
}
}
/* _test_noun_bits_set(): allocate.h level 1a
*/
static void
_test_noun_bits_set()
{
u3_noun a = 1;
// flip indirect bit on
a |= (1 << 31);
if ( c3n == u3a_is_dog(a) ) {
printf("*** fail-5a turn indirect bit on\r\n");
}
if ( c3y == u3a_is_cell(a) ) {
printf("*** fail-5b turn indirect bit on\r\n");
}
if ( c3n == u3a_is_pug(a) ) {
printf("*** fail-5c turn indirect bit on\r\n");
}
// flip all bits off
a = u3a_to_off(a);
if ( c3y == u3a_is_dog(a) ) {
printf("*** fail-5d turn indirect bit off\r\n");
}
if ( c3y == u3a_is_cell(a) ) {
printf("*** fail-5e turn indirect bit on\r\n");
}
if ( c3y == u3a_is_pug(a)) {
printf("*** fail-5f turn indirect bit on\r\n");
}
// flip indirect & cell bit on
a = u3a_to_pom(a);
if ( c3n == u3a_is_dog(a) ) {
printf("*** fail-5g turn indirect bit on\r\n");
}
if ( c3n == u3a_is_cell(a) ) {
printf("*** fail-5h turn indirect bit on\r\n");
}
if ( c3y == u3a_is_pug(a) ) {
printf("*** fail-5i turn indirect bit on\r\n");
}
}
/* _test_noun_bits_read(): allocate.h level 1
*/
static void
_test_noun_bits_read()
{
u3_noun a = (u3_noun)0x1; // direct atom
u3_noun b = u3a_to_pug(0x2); // indirect atom
u3_noun c = u3a_to_pom(0x3); // indirect cell
// direct indirect indirect-atom indirect-cell
//----------------------------------------
_test_noun_bits_helper(a, TRUE, FALSE, FALSE, FALSE);
_test_noun_bits_helper(b, FALSE, TRUE, TRUE, FALSE);
_test_noun_bits_helper(c, FALSE, TRUE, FALSE, TRUE);
}
/* _test_imprison(): test basic data into / out of nouns
** insert and retrieve bytes with u3i_bytes()/u3r_bytes()
*/
static void
_test_imprison()
{
c3_c* input_c = "abcdefghij";
c3_w out_len_w = 300;
c3_y * output_y = c3_malloc(out_len_w);
u3_noun a;
// size 1, direct
a = u3i_bytes(1, (c3_y*)input_c);
memset(output_y, 0, out_len_w);
u3r_bytes(0, 1, output_y, a);
if (0 != memcmp(output_y, "a", 1)) {
printf("*** _test_imprison: fail-1\n");
}
// size 2, direct
a = u3i_bytes(2, (c3_y*)input_c);
memset(output_y, 0, out_len_w);
u3r_bytes(0, 2, output_y, a);
if (0 != memcmp(output_y, "ab", 2)) {
printf("*** _test_imprison: fail-2\n");
}
// size 6, direct (taken from an actual issue)
{
c3_y data_y[] = { 0x1, 0x1f, 0x8e, 0x2d, 0x2c, 0x2f };
a = u3i_bytes(6, data_y);
memset(output_y, 0, out_len_w);
u3r_bytes(0, 6, output_y, a);
int ret;
ret = memcmp(output_y, data_y, 6);
if (0 != ret) {
printf("*** _test_imprison: fail-2.5 %x\n", ret);
printf(" %x %x %x %x %x %x\n", output_y[0],
output_y[1],
output_y[2],
output_y[3],
output_y[4],
output_y[5]);
}
}
// size 8, direct
a = u3i_bytes(8, (c3_y*)input_c);
memset(output_y, 0, out_len_w);
u3r_bytes(0, 8, output_y, a);
if (0 != memcmp(output_y, "abcdefgh", 8)) {
printf("*** _test_imprison: fail-3\n");
}
// size 10, indirect
a = u3i_bytes(10, (c3_y*)input_c);
memset(output_y, 0, out_len_w);
u3r_bytes(0, 10, output_y, a);
if (0 != memcmp(output_y, "abcdefghij", 10)) {
printf("*** _test_imprison: fail-4\n");
}
// size 200, indirect
c3_y * rand_y = _util_rand_string(200);
a = u3i_bytes(200, rand_y);
memset(output_y, 0, out_len_w);
u3r_bytes(0, 200, output_y, a);
if (0 != memcmp(output_y, rand_y, 200)) {
printf("*** _test_imprison: fail-5\n");
}
c3_free(rand_y);
c3_free(output_y);
}
/* _test_cells(): build and inspect cells: u3i_cell(), u3h(), u3t()
*/
static void
_test_cells()
{
// very simple cell
{
u3_noun a = (u3_noun) 0x1;
u3_noun b = (u3_noun) 0x2;
u3_noun c = u3i_cell(a, b);
u3_noun a2 = u3h(c);
if (a2 != a){
printf("*** _test_cells: fail-1\n");
}
u3_noun b2 = u3t(c);
if (b2 != b){
printf("*** _test_cells: fail-2\n");
}
}
// very simple cell with indirect atoms
{
c3_w out_len_w = 200;
c3_y * rand_a = _util_rand_string(out_len_w);
c3_y * rand_b = _util_rand_string(out_len_w);
u3_noun a = u3i_bytes(200, rand_a);
u3_noun b = u3i_bytes(200, rand_b);
u3_noun c = u3i_cell(a, b);
#if 0
printf("a = %x\n", a);
printf("b = %x\n", b);
printf("c = %x\n", c);
printf("a_rand = %s\n", rand_a);
printf("b_rand = %s\n", rand_b);
#endif
u3_noun a2 = u3h(c);
c3_y * output_y = c3_malloc(out_len_w + 1);
memset(output_y, 0, out_len_w + 1);
u3r_bytes(0, out_len_w, output_y, a);
if (0 != memcmp(output_y, rand_a, out_len_w)) {
printf("*** _test_imprison: fail-3\n");
}
u3_noun b2 = u3h(c);
memset(output_y, 0, out_len_w + 1);
u3r_bytes(0, out_len_w, output_y, b);
if (0 != memcmp(output_y, rand_b, out_len_w)) {
printf("*** _test_imprison: fail-4\n");
}
c3_free(output_y);
c3_free(rand_a);
c3_free(rand_b);
}
// medium complicated cell
// q
// / \
// a1 r
// / \
// b2 s
// / \
// c3 d4
{
u3_noun a = (u3_noun) 0x1;
u3_noun b = (u3_noun) 0x2;
u3_noun c = (u3_noun) 0x3;
u3_noun d = (u3_noun) 0x4;
u3_noun s = u3i_cell(c, d);
u3_noun r = u3i_cell(b, s);
u3_noun q = u3i_cell(a, r);
u3_noun a2 = u3h(q);
u3_noun r2 = u3t(q);
if (a2 != a){
printf("*** _test_cells: complicated a\n");
}
u3_noun b2 = u3h(r2);
u3_noun s2 = u3t(r2);
if (b2 != b){
printf("*** _test_cells: complicated b\n");
}
u3_noun c2 = u3h(s2);
u3_noun d2 = u3t(s2);
if (c2 != c){
printf("*** _test_cells: complicated c\n");
}
if (d2 != d){
printf("*** _test_cells: complicated d\n");
}
a2 = 0;
u3r_mean(q, 2, &a2, 0);
if (a2 != a){
printf("*** _test_cells: complicated (via u3r_mean) a\n");
}
}
// trel
{
// q
// / \
// a ?
// / \
// b c
// Produce the triple `[a b c]`.
u3_noun a = (u3_noun) 0x1;
u3_noun b = (u3_noun) 0x2;
u3_noun c = (u3_noun) 0x3;
u3_noun trel = u3i_trel(a, b, c);
u3_noun a2 = u3h(trel);
u3_noun b2 = u3h(u3t(trel));
u3_noun c2 = u3t(u3t(trel));
if (a2 != a){
printf("*** trel: 1 a\n");
}
if (b2 != b){
printf("*** trel: 2 a\n");
}
if (c2 != c){
printf("*** trel: 3 a\n");
}
}
// qual
{
// q
// / \
// a ?
// / \
// b ?
// / \
// c d
//
// Produce the triple `[a b c]`.
u3_noun a = (u3_noun) 0x1;
u3_noun b = (u3_noun) 0x2;
u3_noun c = (u3_noun) 0x3;
u3_noun d = (u3_noun) 0x4;
u3_noun qual = u3i_qual(a, b, c, d);
u3_noun a2 = u3h(qual);
u3_noun b2 = u3h(u3t(qual));
u3_noun c2 = u3h(u3t(u3t(qual)));
u3_noun d2 = u3t(u3t(u3t(qual)));
if (a2 != a){
printf("*** qual: 1 \n");
}
if (b2 != b){
printf("*** qual: 2 \n");
}
if (c2 != c){
printf("*** qual: 3 \n");
}
if (d2 != d){
printf("*** qual: 4 \n");
}
}
}
/* _test_cells_complex(): build cells with more complex methods
*/
static void
_test_cells_complex()
{
// trel
{
// q
// / \
// a ?
// / \
// b c
// Produce the triple `[a b c]`.
u3_noun a = (u3_noun) 0x1;
u3_noun b = (u3_noun) 0x2;
u3_noun c = (u3_noun) 0x3;
u3_noun q = u3i_trel(a, b, c);
u3_noun a2 = 0;
u3_noun b2 = 0;
u3_noun c2 = 0;
u3x_trel(q, &a2, &b2, &c2);
if (a2 != a){
printf("*** _test_cells_complex: trel() 1 a\n");
}
if (b2 != b){
printf("*** _test_cells_complex: trel() 2 a\n");
}
if (c2 != c){
printf("*** _test_cells_complex: trel() 3 a\n");
}
}
// qual
{
// q
// / \
// a ?
// / \
// b z
// / \
// c d
// Produce the qual `[a b c d]`.
u3_noun a = (u3_noun) 0x1;
u3_noun b = (u3_noun) 0x2;
u3_noun c = (u3_noun) 0x3;
u3_noun d = (u3_noun) 0x4;
u3_noun z = u3i_cell(c, d);
u3_noun q = u3i_trel(a, b, z);
u3_noun a2 = 0;
u3_noun b2 = 0;
u3_noun c2 = 0;
u3_noun d2 = 0;
u3x_qual(q, &a2, &b2, &c2, &d2);
if (a2 != a){
printf("*** _test_cells_complex: qual() a\n");
}
if (b2 != b){
printf("*** _test_cells_complex: qual() b\n");
}
if (c2 != c){
printf("*** _test_cells_complex: qual() c\n");
}
if (d2 != d){
printf("*** _test_cells_complex: qual() d\n");
}
}
// quil
{
// q
// / \
// a ?
// / \
// b z
// / \
// c ?
// / \
// d e
// Produce `[a b c d e]`.
u3_noun a = (u3_noun) 0x1;
u3_noun b = (u3_noun) 0x2;
u3_noun c = (u3_noun) 0x3;
u3_noun d = (u3_noun) 0x4;
u3_noun e = (u3_noun) 0x5;
u3_noun z = u3i_trel(c, d, e);
u3_noun q = u3i_trel(a, b, z);
u3_noun a2 = 0;
u3_noun b2 = 0;
u3_noun c2 = 0;
u3_noun d2 = 0;
u3_noun e2 = 0;
u3x_quil(q, &a2, &b2, &c2, &d2, &e2);
if (a2 != a){
printf("*** _test_cells_complex: quil() a\n");
}
if (b2 != b){
printf("*** _test_cells_complex: quil() b\n");
}
if (c2 != c){
printf("*** _test_cells_complex: quil() c\n");
}
if (d2 != d){
printf("*** _test_cells_complex: quil() d\n");
}
if (e2 != e){
printf("*** _test_cells_complex: quil() e\n");
}
}
// hext
{
// q
// / \
// a ?
// / \
// b z
// / \
// c ?
// / \
// d .
// / \
// e f
//
// Produce `[a b c d e f]`.
u3_noun a = (u3_noun) 0x1;
u3_noun b = (u3_noun) 0x2;
u3_noun c = (u3_noun) 0x3;
u3_noun d = (u3_noun) 0x4;
u3_noun e = (u3_noun) 0x5;
u3_noun f = (u3_noun) 0x6;
u3_noun z = u3i_trel(d, e, f);
u3_noun q = u3i_qual(a, b, c, z);
u3_noun a2 = 0;
u3_noun b2 = 0;
u3_noun c2 = 0;
u3_noun d2 = 0;
u3_noun e2 = 0;
u3_noun f2 = 0;
u3x_hext(q, &a2, &b2, &c2, &d2, &e2, &f2);
if (a2 != a){
printf("*** _test_cells_complex: hext() a\n");
}
if (b2 != b){
printf("*** _test_cells_complex: hext() b\n");
}
if (c2 != c){
printf("*** _test_cells_complex: hext() c\n");
}
if (d2 != d){
printf("*** _test_cells_complex: hext() d\n");
}
if (e2 != e){
printf("*** _test_cells_complex: hext() e - e2 = %i\n", e2);
}
if (f2 != f){
printf("*** _test_cells_complex: hext() f - f2 = %i\n", f2);
}
}
}
/* _test_imprison_complex(): more complicated into/out-of nouns
*/
static void
_test_imprison_complex()
{
// vint
{
u3_noun a = 1;
u3_noun b= u3i_vint(a);
if (2 != b){
printf("*** vint 1\n");
}
u3_noun c = u3i_vint(b);
if (3 != c){
printf("*** vint 2\n");
}
// XX disabled, 64-bit
//
#if 0
{
c3_d d = 1ULL << 50;
a = u3i_chubs(1, &d);
b = u3i_vint(a);
if ((a + 1) != b){
printf("*** vint 3\n");
}
}
#endif
}
// bytes
{
c3_y in_y[10] = { 10, 20, 0xff};
u3_noun a = u3i_bytes(3, in_y);
c3_w out_a = u3r_byte(0, a);
if (10 != out_a ){
printf("*** u3r_byte 1\n");
}
c3_w out_b = u3r_byte(1, a);
if (20 != out_b ){
printf("*** u3r_byte 2\n");
}
c3_w out_c = u3r_byte(2, a);
if (0xff != out_c ){
printf("*** u3r_byte 3\n");
}
c3_y out_y[10];
memset(out_y, 0, 10 * sizeof(c3_y));
u3r_bytes(0, 3, out_y, a);
if (10 != out_y[0] ||
20 != out_y[1] ||
0xff != out_y[2] ||
0 != out_y[3]
){
printf("*** u3r_byte 4\n");
}
}
// words
{
c3_w in_w[10] = {10, 20, 0xffffffff};
u3_noun noun = u3i_words(3, in_w);
c3_w out_a = u3r_word(0, noun);