tricu

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

native_bundle_bools_test.c (1620B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <time.h>
      5 #include "../include/arboricx.h"
      6 
      7 static uint8_t *read_file(const char *path, size_t *out_len) {
      8     FILE *f = fopen(path, "rb");
      9     if (!f) return NULL;
     10     fseek(f, 0, SEEK_END);
     11     *out_len = ftell(f);
     12     fseek(f, 0, SEEK_SET);
     13     uint8_t *buf = malloc(*out_len);
     14     fread(buf, 1, *out_len, f);
     15     fclose(f);
     16     return buf;
     17 }
     18 
     19 int test_bundle(arb_ctx_t *ctx, const char *path, const char *name, int expect_val) {
     20     size_t bundle_len;
     21     uint8_t *bundle = read_file(path, &bundle_len);
     22     if (!bundle) { printf("bundle not found: %s\n", path); return 1; }
     23 
     24     uint32_t term = arb_load_bundle(ctx, bundle, bundle_len, name);
     25     if (term == 0) {
     26         printf("load_bundle failed for %s\n", path);
     27         free(bundle);
     28         return 1;
     29     }
     30 
     31     uint32_t result = arb_reduce(ctx, term, 1000000000ULL);
     32 
     33     int b;
     34     if (!arb_to_bool(ctx, result, &b)) {
     35         printf("to_bool failed for %s\n", path);
     36         free(bundle);
     37         return 1;
     38     }
     39     printf("%s result bool=%d (expected %d)\n", path, b, expect_val);
     40     if (b != expect_val) {
     41         printf("MISMATCH!\n");
     42         free(bundle);
     43         return 1;
     44     }
     45 
     46     free(bundle);
     47     return 0;
     48 }
     49 
     50 int main() {
     51     arb_ctx_t *ctx = arboricx_init();
     52     if (!ctx) { printf("init failed\n"); return 1; }
     53 
     54     if (test_bundle(ctx, "../../test/fixtures/true.arboricx", "true", 1) != 0) return 1;
     55     if (test_bundle(ctx, "../../test/fixtures/false.arboricx", "false", 0) != 0) return 1;
     56 
     57     arboricx_free(ctx);
     58     printf("All bool tests passed.\n");
     59     return 0;
     60 }