tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

io_protocol_test.c (7874B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include "arboricx.h"
      4 
      5 int main(void) {
      6     arb_ctx_t* ctx = arboricx_init();
      7     if (!ctx) {
      8         fprintf(stderr, "Failed to initialize Arboricx context\n");
      9         return 1;
     10     }
     11 
     12     /* Test: construct and verify pure action = Fork 0 Leaf */
     13     uint32_t leaf = arb_leaf(ctx);
     14     uint32_t zero = arb_of_number(ctx, 0);
     15     uint32_t pure_action = arb_fork(ctx, zero, leaf);
     16 
     17     if (!arb_is_fork(ctx, pure_action)) {
     18         fprintf(stderr, "FAIL: pure action should be fork\n");
     19         arboricx_free(ctx);
     20         return 1;
     21     }
     22 
     23     uint32_t tag, payload;
     24     if (!arb_get_fork_children(ctx, pure_action, &tag, &payload) ||
     25         tag != zero || payload != leaf) {
     26         fprintf(stderr, "FAIL: pure action children mismatch\n");
     27         arboricx_free(ctx);
     28         return 1;
     29     }
     30 
     31     uint64_t tag_num;
     32     if (!arb_to_number(ctx, tag, &tag_num) || tag_num != 0) {
     33         fprintf(stderr, "FAIL: pure action tag should be 0\n");
     34         arboricx_free(ctx);
     35         return 1;
     36     }
     37     printf("PASS: pure action shape\n");
     38 
     39     /* Test: construct and verify bind action = Fork 1 (Fork left k) */
     40     uint32_t one = arb_of_number(ctx, 1);
     41     uint32_t left = arb_fork(ctx, zero, leaf);  /* pure Leaf */
     42     uint32_t k = arb_fork(ctx, leaf, leaf);     /* identity as Fork Leaf Leaf */
     43     uint32_t bind_pair = arb_fork(ctx, left, k);
     44     uint32_t bind_action = arb_fork(ctx, one, bind_pair);
     45 
     46     if (!arb_get_fork_children(ctx, bind_action, &tag, &payload) ||
     47         !arb_to_number(ctx, tag, &tag_num) || tag_num != 1) {
     48         fprintf(stderr, "FAIL: bind action tag should be 1\n");
     49         arboricx_free(ctx);
     50         return 1;
     51     }
     52 
     53     uint32_t bind_left, bind_k;
     54     if (!arb_get_fork_children(ctx, payload, &bind_left, &bind_k) ||
     55         bind_left != left || bind_k != k) {
     56         fprintf(stderr, "FAIL: bind payload should be Fork left k\n");
     57         arboricx_free(ctx);
     58         return 1;
     59     }
     60     printf("PASS: bind action shape\n");
     61 
     62     /* Test: construct and verify IO sentinel = Fork "tricuIO" (Fork 1 action) */
     63     uint32_t sentinel_str = arb_of_string(ctx, "tricuIO");
     64     uint32_t version = arb_of_number(ctx, 1);
     65     uint32_t version_action_pair = arb_fork(ctx, version, pure_action);
     66     uint32_t io_sentinel = arb_fork(ctx, sentinel_str, version_action_pair);
     67 
     68     if (!arb_is_fork(ctx, io_sentinel)) {
     69         fprintf(stderr, "FAIL: IO sentinel should be fork\n");
     70         arboricx_free(ctx);
     71         return 1;
     72     }
     73 
     74     uint32_t sent_left, sent_right;
     75     if (!arb_get_fork_children(ctx, io_sentinel, &sent_left, &sent_right)) {
     76         fprintf(stderr, "FAIL: get_fork_children on IO sentinel\n");
     77         arboricx_free(ctx);
     78         return 1;
     79     }
     80 
     81     /* Verify sentinel string */
     82     uint8_t* decoded_sentinel;
     83     size_t decoded_len;
     84     if (!arb_to_string(ctx, sent_left, &decoded_sentinel, &decoded_len) ||
     85         decoded_len != 7 || memcmp(decoded_sentinel, "tricuIO", 7) != 0) {
     86         fprintf(stderr, "FAIL: IO sentinel string mismatch\n");
     87         arboricx_free(ctx);
     88         return 1;
     89     }
     90     arboricx_free_buf(ctx, decoded_sentinel, decoded_len);
     91 
     92     /* Verify version = 1 and action = pure */
     93     uint32_t ver, act;
     94     if (!arb_get_fork_children(ctx, sent_right, &ver, &act) ||
     95         !arb_to_number(ctx, ver, &tag_num) || tag_num != 1 ||
     96         act != pure_action) {
     97         fprintf(stderr, "FAIL: IO sentinel version/action mismatch\n");
     98         arboricx_free(ctx);
     99         return 1;
    100     }
    101     printf("PASS: IO sentinel shape\n");
    102 
    103     /* Test: putStr action = Fork 10 string */
    104     uint32_t ten = arb_of_number(ctx, 10);
    105     uint32_t msg = arb_of_string(ctx, "hello");
    106     uint32_t putStr_action = arb_fork(ctx, ten, msg);
    107 
    108     if (!arb_get_fork_children(ctx, putStr_action, &tag, &payload) ||
    109         !arb_to_number(ctx, tag, &tag_num) || tag_num != 10) {
    110         fprintf(stderr, "FAIL: putStr tag should be 10\n");
    111         arboricx_free(ctx);
    112         return 1;
    113     }
    114     printf("PASS: putStr action shape\n");
    115 
    116     /* Test: getLine action = Fork 11 Leaf */
    117     uint32_t eleven = arb_of_number(ctx, 11);
    118     uint32_t getLine_action = arb_fork(ctx, eleven, leaf);
    119 
    120     if (!arb_get_fork_children(ctx, getLine_action, &tag, &payload) ||
    121         !arb_to_number(ctx, tag, &tag_num) || tag_num != 11 ||
    122         payload != leaf) {
    123         fprintf(stderr, "FAIL: getLine tag should be 11 with Leaf payload\n");
    124         arboricx_free(ctx);
    125         return 1;
    126     }
    127     printf("PASS: getLine action shape\n");
    128 
    129     /* Test: readFile action = Fork 20 path */
    130     uint32_t twenty = arb_of_number(ctx, 20);
    131     uint32_t path = arb_of_string(ctx, "/tmp/test.txt");
    132     uint32_t readFile_action = arb_fork(ctx, twenty, path);
    133 
    134     if (!arb_get_fork_children(ctx, readFile_action, &tag, &payload) ||
    135         !arb_to_number(ctx, tag, &tag_num) || tag_num != 20) {
    136         fprintf(stderr, "FAIL: readFile tag should be 20\n");
    137         arboricx_free(ctx);
    138         return 1;
    139     }
    140     printf("PASS: readFile action shape\n");
    141 
    142     /* Test: writeFile action = Fork 21 (Fork path contents) */
    143     uint32_t twenty_one = arb_of_number(ctx, 21);
    144     uint32_t contents = arb_of_string(ctx, "data");
    145     uint32_t write_pair = arb_fork(ctx, path, contents);
    146     uint32_t writeFile_action = arb_fork(ctx, twenty_one, write_pair);
    147 
    148     if (!arb_get_fork_children(ctx, writeFile_action, &tag, &payload) ||
    149         !arb_to_number(ctx, tag, &tag_num) || tag_num != 21) {
    150         fprintf(stderr, "FAIL: writeFile tag should be 21\n");
    151         arboricx_free(ctx);
    152         return 1;
    153     }
    154 
    155     uint32_t wf_path, wf_contents;
    156     if (!arb_get_fork_children(ctx, payload, &wf_path, &wf_contents) ||
    157         wf_path != path || wf_contents != contents) {
    158         fprintf(stderr, "FAIL: writeFile payload should be Fork path contents\n");
    159         arboricx_free(ctx);
    160         return 1;
    161     }
    162     printf("PASS: writeFile action shape\n");
    163 
    164     /* Test: ok result = Fork (Stem Leaf) (Fork val Leaf) */
    165     uint32_t stem_leaf = arb_stem(ctx, leaf);
    166     uint32_t val_pair = arb_fork(ctx, msg, leaf);
    167     uint32_t ok_result = arb_fork(ctx, stem_leaf, val_pair);
    168 
    169     if (!arb_is_fork(ctx, ok_result)) {
    170         fprintf(stderr, "FAIL: ok result should be fork\n");
    171         arboricx_free(ctx);
    172         return 1;
    173     }
    174 
    175     uint32_t ok_tag, ok_rest;
    176     if (!arb_get_fork_children(ctx, ok_result, &ok_tag, &ok_rest) ||
    177         !arb_is_stem(ctx, ok_tag)) {
    178         fprintf(stderr, "FAIL: ok result left should be stem\n");
    179         arboricx_free(ctx);
    180         return 1;
    181     }
    182 
    183     uint32_t ok_val, ok_leaf;
    184     if (!arb_get_fork_children(ctx, ok_rest, &ok_val, &ok_leaf) ||
    185         ok_val != msg || ok_leaf != leaf) {
    186         fprintf(stderr, "FAIL: ok result right should be Fork val Leaf\n");
    187         arboricx_free(ctx);
    188         return 1;
    189     }
    190     printf("PASS: ok result shape\n");
    191 
    192     /* Test: err result = Fork Leaf (Fork code Leaf) */
    193     uint32_t err_code = arb_of_number(ctx, 42);
    194     uint32_t err_pair = arb_fork(ctx, err_code, leaf);
    195     uint32_t err_result = arb_fork(ctx, leaf, err_pair);
    196 
    197     if (!arb_is_fork(ctx, err_result)) {
    198         fprintf(stderr, "FAIL: err result should be fork\n");
    199         arboricx_free(ctx);
    200         return 1;
    201     }
    202 
    203     uint32_t err_tag, err_rest;
    204     if (!arb_get_fork_children(ctx, err_result, &err_tag, &err_rest) ||
    205         !arb_is_leaf(ctx, err_tag)) {
    206         fprintf(stderr, "FAIL: err result left should be leaf\n");
    207         arboricx_free(ctx);
    208         return 1;
    209     }
    210 
    211     uint32_t err_c, err_l;
    212     if (!arb_get_fork_children(ctx, err_rest, &err_c, &err_l) ||
    213         err_c != err_code || err_l != leaf) {
    214         fprintf(stderr, "FAIL: err result right should be Fork code Leaf\n");
    215         arboricx_free(ctx);
    216         return 1;
    217     }
    218     printf("PASS: err result shape\n");
    219 
    220     arboricx_free(ctx);
    221     printf("\nAll IO protocol tests passed.\n");
    222     return 0;
    223 }