tricu

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

c_abi_test.c (3910B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include "helpers.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     arb_handle_t leaf = checked_arb_leaf(ctx);
     14     arb_handle_t app = checked_arb_app(ctx, leaf, leaf);
     15     arb_handle_t result = checked_arb_reduce(ctx, app, 10000);
     16     arb_handle_t stem = checked_arb_stem(ctx, leaf);
     17 
     18     /* Build expected Stem(Leaf) and compare */
     19     assert(same_value(ctx, result, stem));
     20     printf("PASS: reduce Leaf@Leaf\n");
     21 
     22     /* Test: number codec roundtrip */
     23     arb_handle_t num_tree = checked_arb_of_number(ctx, 42);
     24     uint64_t decoded_num;
     25     if (!(arb_to_number(ctx, num_tree, &decoded_num) == ARB_OK) || 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     arb_handle_t str_tree = checked_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) == ARB_OK) ||
     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     arb_handle_t kernel_root = checked_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=%" PRIu64 ")\n", kernel_root);
     53 
     54     /* Test: tree inspection primitives */
     55     arb_handle_t l = checked_arb_leaf(ctx);
     56     arb_handle_t s = checked_arb_stem(ctx, l);
     57     arb_handle_t f = checked_arb_fork(ctx, s, l);
     58     arb_handle_t a = checked_arb_app(ctx, f, s);
     59 
     60     if (!checked_arb_is_leaf(ctx, l)) {
     61         fprintf(stderr, "FAIL: is_leaf on leaf\n");
     62         arboricx_free(ctx);
     63         return 1;
     64     }
     65     if (checked_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 (!checked_arb_is_stem(ctx, s)) {
     71         fprintf(stderr, "FAIL: is_stem on stem\n");
     72         arboricx_free(ctx);
     73         return 1;
     74     }
     75     if (!checked_arb_is_fork(ctx, f)) {
     76         fprintf(stderr, "FAIL: is_fork on fork\n");
     77         arboricx_free(ctx);
     78         return 1;
     79     }
     80     if (!checked_arb_is_app(ctx, a)) {
     81         fprintf(stderr, "FAIL: is_app on app\n");
     82         arboricx_free(ctx);
     83         return 1;
     84     }
     85 
     86     arb_handle_t child;
     87     if (arb_get_stem_child(ctx, s, &child) != ARB_OK || !same_value(ctx, child, l)) {
     88         fprintf(stderr, "FAIL: get_stem_child\n");
     89         arboricx_free(ctx);
     90         return 1;
     91     }
     92 
     93     arb_handle_t left, right;
     94     if (!(arb_get_fork_children(ctx, f, &left, &right) == ARB_OK) || !same_value(ctx, left, s) || !same_value(ctx, right, l)) {
     95         fprintf(stderr, "FAIL: get_fork_children\n");
     96         arboricx_free(ctx);
     97         return 1;
     98     }
     99 
    100     arb_handle_t func, arg;
    101     if (!(arb_get_app_func_arg(ctx, a, &func, &arg) == ARB_OK) || !same_value(ctx, func, f) || !same_value(ctx, 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     int invalid_out = 42;
    109     if (arb_is_leaf(ctx, 999999, &invalid_out) != ARB_INVALID_HANDLE || invalid_out != 42) {
    110         fprintf(stderr, "FAIL: is_leaf on invalid index should be false\n");
    111         arboricx_free(ctx);
    112         return 1;
    113     }
    114 
    115     printf("PASS: tree inspection primitives\n");
    116 
    117     arboricx_free(ctx);
    118     printf("\nAll C ABI tests passed.\n");
    119     return 0;
    120 }