penguin/narou-android

Simple 縦書き reader for narou

tests/motion.test.cjs

raw · 986 bytes

const assert = require('node:assert/strict');
const Motion = require('../app/src/main/assets/reader/motion.js');
const c={acceleration:900,speed:120,deadzone:.08,brake:.15,neutral:0,reverse:false};
function run(hz,x,cfg=c,seconds=2){const m=new Motion();let d=0;for(let i=0;i<hz*seconds;i++)d+=m.step(x,1/hz,cfg);return {m,d};}
assert.equal(run(60,.04).d,0);
assert.equal(run(60,.54,{...c,neutral:.5}).d,0);
assert(run(60,.5).d>100);
assert.equal(run(60,.5).m.velocity,120);
assert.equal(run(60,-.5).m.velocity,-120);
assert.equal(run(60,.5,{...c,reverse:true}).d,-run(60,.5).d);
assert(Math.abs(run(60,.5).d-run(120,.5).d)<.1,'refresh-rate independence');
const {m}=run(60,.5);for(let i=0;i<120;i++)m.step(0,1/60,c);assert.equal(m.velocity,0);
m.step(.8,.02,c);assert(m.velocity>0);assert.equal(m.step(.8,2,c),0);assert.equal(m.velocity,0);
assert.equal(m.step(NaN,.02,c),0);
console.log('PASS: dead zone, calibration, acceleration, cap, reverse, braking, refresh rate, stale frame');