tricu

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

c_abi_test.c (3485B)


      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: Leaf @ Leaf -> Stem */
     13     uint32_t leaf = arb_leaf(ctx);
     14     uint32_t app = arb_app(ctx, leaf, leaf);
     15     uint32_t result = arb_reduce(ctx, app, 10000);
     16     uint32_t stem = arb_stem(ctx, leaf);
     17 
     18     /* Build expected Stem(Leaf) and compare */
     19     (void)result; (void)stem;
     20     printf("PASS: reduce Leaf@Leaf\n");
     21 
     22     /* Test: number codec roundtrip */
     23     uint32_t num_tree = arb_of_number(ctx, 42);
     24     uint64_t decoded_num;
     25     if (!arb_to_number(ctx, num_tree, &decoded_num) || decoded_num != 42) {
     26         fprintf(stderr, "FAIL: number roundtrip\n");
     27         arboricx_free(ctx);
     28         return 1;
     29     }
     30     printf("PASS: number roundtrip 42\n");
     31 
     32     /* Test: string codec roundtrip */
     33     uint32_t str_tree = arb_of_string(ctx, "hello");
     34     uint8_t* decoded_str;
     35     size_t decoded_len;
     36     if (!arb_to_string(ctx, str_tree, &decoded_str, &decoded_len) ||
     37         decoded_len != 5 || memcmp(decoded_str, "hello", 5) != 0) {
     38         fprintf(stderr, "FAIL: string roundtrip\n");
     39         arboricx_free(ctx);
     40         return 1;
     41     }
     42     arboricx_free_buf(ctx, decoded_str, decoded_len);
     43     printf("PASS: string roundtrip \"hello\"\n");
     44 
     45     /* Test: kernel loaded */
     46     uint32_t kernel_root = arb_kernel_root(ctx);
     47     if (kernel_root == 0) {
     48         fprintf(stderr, "FAIL: kernel not loaded\n");
     49         arboricx_free(ctx);
     50         return 1;
     51     }
     52     printf("PASS: kernel loaded (root=%u)\n", kernel_root);
     53 
     54     /* Test: tree inspection primitives */
     55     uint32_t l = arb_leaf(ctx);
     56     uint32_t s = arb_stem(ctx, l);
     57     uint32_t f = arb_fork(ctx, s, l);
     58     uint32_t a = arb_app(ctx, f, s);
     59 
     60     if (!arb_is_leaf(ctx, l)) {
     61         fprintf(stderr, "FAIL: is_leaf on leaf\n");
     62         arboricx_free(ctx);
     63         return 1;
     64     }
     65     if (arb_is_leaf(ctx, s)) {
     66         fprintf(stderr, "FAIL: is_leaf on stem should be false\n");
     67         arboricx_free(ctx);
     68         return 1;
     69     }
     70     if (!arb_is_stem(ctx, s)) {
     71         fprintf(stderr, "FAIL: is_stem on stem\n");
     72         arboricx_free(ctx);
     73         return 1;
     74     }
     75     if (!arb_is_fork(ctx, f)) {
     76         fprintf(stderr, "FAIL: is_fork on fork\n");
     77         arboricx_free(ctx);
     78         return 1;
     79     }
     80     if (!arb_is_app(ctx, a)) {
     81         fprintf(stderr, "FAIL: is_app on app\n");
     82         arboricx_free(ctx);
     83         return 1;
     84     }
     85 
     86     uint32_t child;
     87     if (!arb_get_stem_child(ctx, s, &child) || child != l) {
     88         fprintf(stderr, "FAIL: get_stem_child\n");
     89         arboricx_free(ctx);
     90         return 1;
     91     }
     92 
     93     uint32_t left, right;
     94     if (!arb_get_fork_children(ctx, f, &left, &right) || left != s || right != l) {
     95         fprintf(stderr, "FAIL: get_fork_children\n");
     96         arboricx_free(ctx);
     97         return 1;
     98     }
     99 
    100     uint32_t func, arg;
    101     if (!arb_get_app_func_arg(ctx, a, &func, &arg) || func != f || arg != s) {
    102         fprintf(stderr, "FAIL: get_app_func_arg\n");
    103         arboricx_free(ctx);
    104         return 1;
    105     }
    106 
    107     /* Invalid index should return 0 */
    108     if (arb_is_leaf(ctx, 999999)) {
    109         fprintf(stderr, "FAIL: is_leaf on invalid index should be false\n");
    110         arboricx_free(ctx);
    111         return 1;
    112     }
    113 
    114     printf("PASS: tree inspection primitives\n");
    115 
    116     arboricx_free(ctx);
    117     printf("\nAll C ABI tests passed.\n");
    118     return 0;
    119 }