tricu

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

native_bundle_id_test.c (1740B)


      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 main() {
     20     arb_ctx_t *ctx = arboricx_init();
     21     if (!ctx) { printf("init failed\n"); return 1; }
     22 
     23     size_t bundle_len;
     24     uint8_t *bundle = read_file("../../test/fixtures/id.arboricx", &bundle_len);
     25     if (!bundle) { printf("bundle not found\n"); return 1; }
     26     printf("bundle size=%zu\n", bundle_len);
     27 
     28     clock_t t0 = clock();
     29     uint32_t term = arb_load_bundle(ctx, bundle, bundle_len, "id");
     30     clock_t t1 = clock();
     31     printf("load_bundle took %.3f ms, term=%u\n", (double)(t1 - t0) * 1000.0 / CLOCKS_PER_SEC, term);
     32     if (term == 0) {
     33         printf("load_bundle failed\n");
     34         return 1;
     35     }
     36 
     37     uint32_t arg1 = arb_of_string(ctx, "hello");
     38     uint32_t app0 = arb_app(ctx, term, arg1);
     39 
     40     printf("reducing...\n");
     41     clock_t t2 = clock();
     42     uint32_t result = arb_reduce(ctx, app0, 1000000000ULL);
     43     clock_t t3 = clock();
     44     printf("reduce took %.3f ms, result=%u\n", (double)(t3 - t2) * 1000.0 / CLOCKS_PER_SEC, result);
     45 
     46     uint8_t *str_ptr;
     47     size_t str_len;
     48     if (arb_to_string(ctx, result, &str_ptr, &str_len)) {
     49         printf("RESULT: %.*s\n", (int)str_len, str_ptr);
     50         arboricx_free_buf(ctx, str_ptr, str_len);
     51     } else {
     52         printf("to_string failed\n");
     53         return 1;
     54     }
     55 
     56     free(bundle);
     57     arboricx_free(ctx);
     58     printf("done\n");
     59     return 0;
     60 }