tricu

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

practical-packages.test.mjs (3535B)


      1 import test from 'node:test';
      2 import assert from 'node:assert/strict';
      3 import {readFile} from 'node:fs/promises';
      4 import {Graph} from '../dist/graph.js';
      5 import {decodeBundle,loadBundle,COMPILER_BUNDLE_LIMIT} from '../dist/bundle.js';
      6 import {natural,readNatural,readList,view} from '../dist/codecs.js';
      7 const index=JSON.parse(await readFile(new URL('../packages.json',import.meta.url))).releases;
      8 const bundle=async name=>decodeBundle(await readFile(new URL('../bundles/'+index.find(r=>r.name===name).hash+'.arboricx',import.meta.url)),COMPILER_BUNDLE_LIMIT);
      9 const graph=()=>new Graph({nodes:500000,fuel:20000000,stack:50000},{threshold:100000});
     10 const list=(g,items)=>items.reduceRight((rest,item)=>g.fork(item,rest),g.leaf);
     11 
     12 test('indexed arithmetic and byte readers agree with independent numbers and malformed-byte controls',async()=>{
     13  const g=graph(),math=loadBundle(g,await bundle('tricu-math')),binary=loadBundle(g,await bundle('tricu-binary'));
     14  const number=n=>natural(g,BigInt(n)),call=(library,name,...args)=>g.apply(library.get(name),args);
     15  assert.equal(readNatural(g,call(math,'add',number(123456789),number(11))),123456800n);
     16  for(const [divisor,q,r] of [[7,14,2],[0,0,100]]){
     17   const [quotient,remainder]=g.pair(call(math,'divMod',number(100),number(divisor)));
     18   assert.equal(readNatural(g,quotient),BigInt(q));assert.equal(readNatural(g,remainder),BigInt(r));
     19  }
     20  for(const [input,success,value,rest] of [[[65,66],true,65,[66]],[[256],false,15,[256]]]){
     21   const [flag,payload]=g.pair(call(binary,'readU8',list(g,input.map(number))));
     22   assert.equal(view(g,flag,'bool').value,success);
     23   const [actual,remainder]=g.pair(payload);assert.equal(readNatural(g,actual),BigInt(value));
     24   assert.deepEqual(readList(g,remainder).map(v=>Number(readNatural(g,v))),rest);
     25  }
     26  for(const [input,expected] of [[[],true],[[195,169],true],[[192,175],false],[[237,160,128],false],[[226,130],false]])
     27   assert.equal(view(g,call(binary,'binaryUtf8?',list(g,input.map(number))),'bool').value,expected);
     28 });
     29 
     30 test('published predicate dispatch and list folds preserve first-match and early-stop demand boundaries',async()=>{
     31  const g=graph(),patterns=loadBundle(g,await bundle('tricu-patterns')),traversal=loadBundle(g,await bundle('tricu-list-traversal'));
     32  const math=loadBundle(g,await bundle('tricu-math')),compiler=loadBundle(g,await bundle('tricu-compiler'));
     33  const number=n=>natural(g,BigInt(n)),constant=value=>g.app(compiler.get('base.const'),value);
     34  const predicate=g.app(math.get('gt'),number(5));
     35  const rules=list(g,[list(g,[predicate,g.app(math.get('add'),number(10))]),list(g,[constant(g.stem(g.leaf)),constant(number(99))])]);
     36  for(const [input,expected] of [[3,13],[6,99]])assert.equal(readNatural(g,g.apply(patterns.get('match'),[number(input),rules])),BigInt(expected));
     37  assert.equal(g.toTernary(g.apply(patterns.get('match'),[number(0),g.leaf])),'0');
     38  const input=list(g,[number(1),number(2)]),stop=g.app(traversal.get('listStop'),number(7));
     39  const [state,rest]=g.pair(g.apply(traversal.get('listFoldUntil'),[input,number(0),constant(constant(stop))]));
     40  assert.equal(readNatural(g,state),7n);assert.equal(g.force(rest),g.force(input),'Stop retains the current cell');
     41  const malformed=g.stem(g.leaf),q=g.fromTernary('212110021100'),divergent=g.app(q,q);
     42  const [unchanged,suffix]=g.pair(g.apply(traversal.get('listFoldUntil'),[malformed,number(5),divergent]));
     43  assert.equal(readNatural(g,unchanged),5n);assert.equal(g.force(suffix),malformed,'malformed tails halt without calling a divergent callback');
     44 });