MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add shift and brackets test

+200
+114
examples/spec/tco_brackets.js
··· 1 + import { test, summary } from './helpers.js'; 2 + 3 + console.log('TCO Bracket Edge Cases\n'); 4 + 5 + function indexCallResult(n) { 6 + if (n <= 0) return [42]; 7 + return indexCallResult(n - 1); 8 + } 9 + test('f() then index (small)', indexCallResult(5)[0], 42); 10 + 11 + function indexByCall(n) { 12 + if (n <= 0) return [99]; 13 + return indexByCall(n - 1); 14 + } 15 + function zeroFn() { 16 + return 0; 17 + } 18 + test('f()[g()] (small)', indexByCall(3)[zeroFn()], 99); 19 + 20 + function returnBracketAccess(arr) { 21 + return arr[0]; 22 + } 23 + test('bare bracket access', returnBracketAccess([7]), 7); 24 + 25 + function indexArrByCall(arr) { 26 + return arr[zeroFn()]; 27 + } 28 + test('arr[f()] access', indexArrByCall([55]), 55); 29 + 30 + const methods = { 31 + greet() { 32 + return 'hello'; 33 + }, 34 + farewell() { 35 + return 'bye'; 36 + } 37 + }; 38 + function computedCall(key) { 39 + return methods[key](); 40 + } 41 + test('obj[key]() simple', computedCall('greet'), 'hello'); 42 + test('obj[key]() simple 2', computedCall('farewell'), 'bye'); 43 + 44 + const ops = { 45 + dec(n) { 46 + return recurseViaComputed(n - 1); 47 + } 48 + }; 49 + function recurseViaComputed(n) { 50 + if (n <= 0) return 'done'; 51 + return ops['dec'](n); 52 + } 53 + test('obj[key]() deep recursion', recurseViaComputed(100000), 'done'); 54 + 55 + function callWithBracketArg(arr) { 56 + return identity(arr[0]); 57 + } 58 + function identity(x) { 59 + return x; 60 + } 61 + test('f(a[0]) bracket in arg', callWithBracketArg([33]), 33); 62 + 63 + function callWithBracketBinopArg(arr) { 64 + return identity(arr[0] + arr[1]); 65 + } 66 + test('f(a[0]+a[1]) binop inside args', callWithBracketBinopArg([10, 20]), 30); 67 + 68 + function recurseWithBracketArg(arr, i) { 69 + if (i >= arr.length) return 0; 70 + return recurseWithBracketArg(arr, i + 1); 71 + } 72 + test('deep with bracket arg', recurseWithBracketArg(new Array(100000), 0), 0); 73 + 74 + const handlers = {}; 75 + for (let i = 0; i < 10; i++) { 76 + handlers['h' + i] = function (n) { 77 + if (n <= 0) return 'handled'; 78 + return handlers['h' + i](n - 1); 79 + }; 80 + } 81 + test('obj[key+expr]() result', handlers['h0'](50), 'handled'); 82 + 83 + const dispatch = { 84 + action_run(n) { 85 + if (n <= 0) return 'ran'; 86 + return dispatch['action' + '_' + 'run'](n - 1); 87 + } 88 + }; 89 + test('obj[a+b]() deep (may fail without bracket fix)', dispatch['action_run'](100000), 'ran'); 90 + 91 + const table = []; 92 + for (let i = 0; i < 20; i++) { 93 + table[i] = function () { 94 + return i; 95 + }; 96 + } 97 + function callFromTable(a) { 98 + return table[a * 2 + 1](); 99 + } 100 + test('table[a*2+1]() computed', callFromTable(3), 7); 101 + 102 + function ternaryBracket(arr, flag) { 103 + return flag ? identity(arr[0]) : identity(arr[1]); 104 + } 105 + test('ternary with bracket args true', ternaryBracket([10, 20], true), 10); 106 + test('ternary with bracket args false', ternaryBracket([10, 20], false), 20); 107 + 108 + function ternaryComputedVsPlain(flag, n) { 109 + if (n <= 0) return 'end'; 110 + return flag ? ops['dec'](n) : ternaryComputedVsPlain(flag, n - 1); 111 + } 112 + test('ternary computed vs plain', ternaryComputedVsPlain(false, 100000), 'end'); 113 + 114 + summary();
+86
examples/spec/tco_shift.js
··· 1 + import { test, summary } from './helpers.js'; 2 + 3 + console.log('TCO Shift/Comparison Operator Tests\n'); 4 + 5 + function identity(x) { 6 + return x; 7 + } 8 + 9 + function shiftLeft(n) { 10 + if (n <= 0) return identity(1) << 3; 11 + return shiftLeft(n - 1); 12 + } 13 + test('f() << N result', shiftLeft(5), 8); 14 + 15 + function shiftRight(n) { 16 + if (n <= 0) return identity(64) >> 2; 17 + return shiftRight(n - 1); 18 + } 19 + test('f() >> N result', shiftRight(5), 16); 20 + 21 + function shiftRightUnsigned(n) { 22 + if (n <= 0) return identity(-1) >>> 24; 23 + return shiftRightUnsigned(n - 1); 24 + } 25 + test('f() >>> N result', shiftRightUnsigned(5), 255); 26 + 27 + function lessThan(n) { 28 + if (n <= 0) return identity(3) < 5; 29 + return lessThan(n - 1); 30 + } 31 + test('f() < N result', lessThan(3), true); 32 + 33 + function greaterThan(n) { 34 + if (n <= 0) return identity(10) > 5; 35 + return greaterThan(n - 1); 36 + } 37 + test('f() > N result', greaterThan(3), true); 38 + 39 + function lessEq(n) { 40 + if (n <= 0) return identity(5) <= 5; 41 + return lessEq(n - 1); 42 + } 43 + test('f() <= N result', lessEq(3), true); 44 + 45 + function greaterEq(n) { 46 + if (n <= 0) return identity(5) >= 10; 47 + return greaterEq(n - 1); 48 + } 49 + test('f() >= N result', greaterEq(3), false); 50 + 51 + function deepShiftLeft(n) { 52 + if (n <= 0) return identity(1) << 3; 53 + return deepShiftLeft(n - 1); 54 + } 55 + test('deep f()<<N value correct', deepShiftLeft(500), 8); 56 + 57 + function deepShiftRight(n) { 58 + if (n <= 0) return identity(64) >> 2; 59 + return deepShiftRight(n - 1); 60 + } 61 + test('deep f()>>N value correct', deepShiftRight(500), 16); 62 + 63 + function deepUnsignedShift(n) { 64 + if (n <= 0) return identity(-1) >>> 24; 65 + return deepUnsignedShift(n - 1); 66 + } 67 + test('deep f()>>>N value correct', deepUnsignedShift(500), 255); 68 + 69 + function recurseShiftArg(n) { 70 + if (n <= 0) return 'done'; 71 + return recurseShiftArg((n - 1) >> 0); 72 + } 73 + test('shift inside arg (tail-eligible)', recurseShiftArg(100000), 'done'); 74 + 75 + function ternaryShift(n) { 76 + return n <= 0 ? identity(1) << 4 : ternaryShift(n - 1); 77 + } 78 + test('ternary with shift in then-branch', ternaryShift(5), 16); 79 + 80 + function ternaryBothShift(flag) { 81 + return flag ? identity(1) << 2 : identity(1) >> 1; 82 + } 83 + test('ternary both shift true', ternaryBothShift(true), 4); 84 + test('ternary both shift false', ternaryBothShift(false), 0); 85 + 86 + summary();