61 lines
1.7 KiB
C
61 lines
1.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include "../include/arboricx.h"
|
|
|
|
static uint8_t *read_file(const char *path, size_t *out_len) {
|
|
FILE *f = fopen(path, "rb");
|
|
if (!f) return NULL;
|
|
fseek(f, 0, SEEK_END);
|
|
*out_len = ftell(f);
|
|
fseek(f, 0, SEEK_SET);
|
|
uint8_t *buf = malloc(*out_len);
|
|
fread(buf, 1, *out_len, f);
|
|
fclose(f);
|
|
return buf;
|
|
}
|
|
|
|
int main() {
|
|
arb_ctx_t *ctx = arboricx_init();
|
|
if (!ctx) { printf("init failed\n"); return 1; }
|
|
|
|
size_t bundle_len;
|
|
uint8_t *bundle = read_file("../../test/fixtures/id.arboricx", &bundle_len);
|
|
if (!bundle) { printf("bundle not found\n"); return 1; }
|
|
printf("bundle size=%zu\n", bundle_len);
|
|
|
|
clock_t t0 = clock();
|
|
uint32_t term = arb_load_bundle(ctx, bundle, bundle_len, "root");
|
|
clock_t t1 = clock();
|
|
printf("load_bundle took %.3f ms, term=%u\n", (double)(t1 - t0) * 1000.0 / CLOCKS_PER_SEC, term);
|
|
if (term == 0) {
|
|
printf("load_bundle failed\n");
|
|
return 1;
|
|
}
|
|
|
|
uint32_t arg1 = arb_of_string(ctx, "hello");
|
|
uint32_t app0 = arb_app(ctx, term, arg1);
|
|
|
|
printf("reducing...\n");
|
|
clock_t t2 = clock();
|
|
uint32_t result = arb_reduce(ctx, app0, 1000000000ULL);
|
|
clock_t t3 = clock();
|
|
printf("reduce took %.3f ms, result=%u\n", (double)(t3 - t2) * 1000.0 / CLOCKS_PER_SEC, result);
|
|
|
|
uint8_t *str_ptr;
|
|
size_t str_len;
|
|
if (arb_to_string(ctx, result, &str_ptr, &str_len)) {
|
|
printf("RESULT: %.*s\n", (int)str_len, str_ptr);
|
|
arboricx_free_buf(ctx, str_ptr, str_len);
|
|
} else {
|
|
printf("to_string failed\n");
|
|
return 1;
|
|
}
|
|
|
|
free(bundle);
|
|
arboricx_free(ctx);
|
|
printf("done\n");
|
|
return 0;
|
|
}
|