tricu

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

io_run_test.c (3103B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include "helpers.h"
      4 
      5 static arb_handle_t make_pure(arb_ctx_t* ctx, arb_handle_t val) {
      6     arb_handle_t zero = checked_arb_of_number(ctx, 0);
      7     return checked_arb_fork(ctx, zero, val);
      8 }
      9 
     10 static arb_handle_t make_io_sentinel(arb_ctx_t* ctx, arb_handle_t action) {
     11     arb_handle_t sentinel = checked_arb_of_string(ctx, "tricuIO");
     12     arb_handle_t version = checked_arb_of_number(ctx, 1);
     13     arb_handle_t version_action = checked_arb_fork(ctx, version, action);
     14     return checked_arb_fork(ctx, sentinel, version_action);
     15 }
     16 
     17 int main(void) {
     18     arb_ctx_t* ctx = arboricx_init();
     19     if (!ctx) {
     20         fprintf(stderr, "Failed to initialize Arboricx context\n");
     21         return 1;
     22     }
     23 
     24     /* Version-specific v2 execution; output handles remain untouched on errors. */
     25     {
     26         arb_handle_t payload = checked_arb_of_number(ctx, 42);
     27         arb_handle_t action = make_pure(ctx, payload);
     28         arb_handle_t v1 = make_io_sentinel(ctx, action);
     29         arb_handle_t v2 = checked_arb_fork(ctx, checked_arb_of_string(ctx, "tricuIO"),
     30             checked_arb_fork(ctx, checked_arb_of_number(ctx, 2), action));
     31         arb_handle_t out = 123;
     32         uint64_t value = 0;
     33         if (arb_run_io_v2(ctx,v2,&out) != ARB_OK || arb_to_number(ctx,out,&value) != ARB_OK || value != 42) return 1;
     34         if (arb_release(ctx,out) != ARB_OK) return 1;
     35         out = 123;
     36         if (arb_run_io_v2(ctx,v1,&out) != ARB_IO_ERROR || out != 123) return 1;
     37         if (arb_run_io_v2(ctx,0,&out) != ARB_INVALID_HANDLE || out != 123) return 1;
     38         arb_handle_t malformed = checked_arb_fork(ctx, checked_arb_of_string(ctx, "notIO"),
     39             checked_arb_fork(ctx, checked_arb_of_number(ctx, 2), action));
     40         if (arb_run_io_v2(ctx,malformed,&out) != ARB_IO_ERROR || out != 123) return 1;
     41         for (uint64_t tag = 0; tag <= 100; tag++) {
     42             arb_handle_t retired = make_io_sentinel(ctx,
     43                 checked_arb_fork(ctx, checked_arb_of_number(ctx, tag), payload));
     44             if (arb_run_io_v2(ctx,retired,&out) != ARB_IO_ERROR || out != 123) return 1;
     45         }
     46         /* Non-normal terminal payloads must not become successful Results. */
     47         arb_handle_t inert = checked_arb_app(ctx, checked_arb_leaf(ctx), checked_arb_leaf(ctx));
     48         for (uint64_t tag = 2; tag <= 9; tag += 7) {
     49             arb_handle_t terminal = checked_arb_fork(ctx, checked_arb_of_string(ctx, "tricuIO"),
     50                 checked_arb_fork(ctx, checked_arb_of_number(ctx, 2),
     51                     checked_arb_fork(ctx, checked_arb_of_number(ctx, tag), inert)));
     52             if (arb_run_io_v2(ctx,terminal,&out) != ARB_IO_ERROR || out != 123) return 1;
     53         }
     54         /* A failed run does not poison subsequent valid runs in this context. */
     55         if (arb_run_io_v2(ctx,v2,&out) != ARB_OK || arb_to_number(ctx,out,&value) != ARB_OK || value != 42) return 1;
     56         if (arb_release(ctx,out) != ARB_OK) return 1;
     57         printf("PASS: explicit v2 ABI and no fallback\n");
     58     }
     59 
     60     arboricx_free(ctx);
     61     printf("\nAll IO run tests passed.\n");
     62     return 0;
     63 }