reduce.test.js (2584B)
1 import { readFileSync } from 'node:fs'; 2 import { strictEqual, ok } from 'node:assert'; 3 import { describe, it } from 'node:test'; 4 import { 5 findLib, 6 init, 7 free, 8 leaf, 9 stem, 10 fork, 11 app, 12 reduce, 13 toBool, 14 toString, 15 toNumber, 16 loadBundleDefault, 17 ofString, 18 ofNumber, 19 } from '../src/lib.js'; 20 21 const libPath = findLib(); 22 23 describe('tree construction', () => { 24 it('leaf returns a positive index', () => { 25 const ctx = init(libPath); 26 try { 27 const idx = leaf(ctx); 28 ok(idx > 0); 29 } finally { 30 free(ctx); 31 } 32 }); 33 34 it('stem wraps a child', () => { 35 const ctx = init(libPath); 36 try { 37 const l = leaf(ctx); 38 const s = stem(ctx, l); 39 ok(s > 0); 40 ok(s !== l); 41 } finally { 42 free(ctx); 43 } 44 }); 45 46 it('fork combines left and right', () => { 47 const ctx = init(libPath); 48 try { 49 const a = leaf(ctx); 50 const b = leaf(ctx); 51 const f = fork(ctx, a, b); 52 ok(f > 0); 53 ok(f !== a && f !== b); 54 } finally { 55 free(ctx); 56 } 57 }); 58 }); 59 60 describe('reduction — booleans', () => { 61 it('true.arboricx reduces to boolean true', () => { 62 const ctx = init(libPath); 63 try { 64 const bundle = readFileSync('../../test/fixtures/true.arboricx'); 65 const root = loadBundleDefault(ctx, bundle); 66 const result = reduce(ctx, root, 1_000_000n); 67 strictEqual(toBool(ctx, result), true); 68 } finally { 69 free(ctx); 70 } 71 }); 72 73 it('false.arboricx reduces to boolean false', () => { 74 const ctx = init(libPath); 75 try { 76 const bundle = readFileSync('../../test/fixtures/false.arboricx'); 77 const root = loadBundleDefault(ctx, bundle); 78 const result = reduce(ctx, root, 1_000_000n); 79 strictEqual(toBool(ctx, result), false); 80 } finally { 81 free(ctx); 82 } 83 }); 84 }); 85 86 describe('reduction — id', () => { 87 it('id applied to string returns the string', () => { 88 const ctx = init(libPath); 89 try { 90 const bundle = readFileSync('../../test/fixtures/id.arboricx'); 91 const idRoot = loadBundleDefault(ctx, bundle); 92 const arg = ofString(ctx, 'hello'); 93 const applied = app(ctx, idRoot, arg); 94 const result = reduce(ctx, applied, 1_000_000n); 95 strictEqual(toString(ctx, result), 'hello'); 96 } finally { 97 free(ctx); 98 } 99 }); 100 }); 101 102 describe('reduction — numbers', () => { 103 it('ofNumber round-trips through toNumber', () => { 104 const ctx = init(libPath); 105 try { 106 const num = ofNumber(ctx, 42); 107 strictEqual(toNumber(ctx, num), 42); 108 } finally { 109 free(ctx); 110 } 111 }); 112 }); 113