native_bundle_append_test.c (2746B)
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 printf("ctx=%p\n", (void*)ctx); 23 24 size_t bundle_len; 25 uint8_t *bundle = read_file("../../test/fixtures/append.arboricx", &bundle_len); 26 if (!bundle) { printf("bundle not found\n"); return 1; } 27 printf("bundle size=%zu\n", bundle_len); 28 29 clock_t t0 = clock(); 30 uint32_t term = arb_load_bundle(ctx, bundle, bundle_len, "append"); 31 clock_t t1 = clock(); 32 printf("load_bundle took %.3f ms, term=%u\n", (double)(t1 - t0) * 1000.0 / CLOCKS_PER_SEC, term); 33 if (term == 0) { 34 printf("load_bundle failed\n"); 35 return 1; 36 } 37 38 uint32_t arg1 = arb_of_string(ctx, "Hello, "); 39 uint32_t arg2 = arb_of_string(ctx, "world!"); 40 printf("arg1=%u arg2=%u\n", arg1, arg2); 41 42 uint32_t app0 = arb_app(ctx, term, arg1); 43 uint32_t app1 = arb_app(ctx, app0, arg2); 44 printf("app1=%u\n", app1); 45 46 printf("reducing...\n"); 47 clock_t t2 = clock(); 48 uint32_t result = arb_reduce(ctx, app1, 1000000000ULL); 49 clock_t t3 = clock(); 50 printf("reduce took %.3f ms, result=%u\n", (double)(t3 - t2) * 1000.0 / CLOCKS_PER_SEC, result); 51 52 /* Try decoding as a plain string first (direct call, no kernel wrapper) */ 53 uint8_t *str_ptr; 54 size_t str_len; 55 if (arb_to_string(ctx, result, &str_ptr, &str_len)) { 56 printf("RESULT: %.*s\n", (int)str_len, str_ptr); 57 arboricx_free_buf(ctx, str_ptr, str_len); 58 } else { 59 printf("to_string failed, trying unwrap_result...\n"); 60 int ok; 61 uint32_t value, rest; 62 if (!arb_unwrap_result(ctx, result, &ok, &value, &rest)) { 63 printf("unwrap_result also failed\n"); 64 return 1; 65 } 66 printf("unwrap_result: ok=%d value=%u\n", ok, value); 67 uint64_t htag; 68 uint32_t payload; 69 if (!arb_unwrap_host_value(ctx, value, &htag, &payload)) { 70 printf("unwrap_host_value failed\n"); 71 return 1; 72 } 73 printf("htag=%lu payload=%u\n", htag, payload); 74 if (arb_to_string(ctx, payload, &str_ptr, &str_len)) { 75 printf("RESULT: %.*s\n", (int)str_len, str_ptr); 76 arboricx_free_buf(ctx, str_ptr, str_len); 77 } 78 } 79 80 free(bundle); 81 arboricx_free(ctx); 82 printf("done\n"); 83 return 0; 84 }