diff options
Diffstat (limited to 'tests/sjs.mjs')
| -rw-r--r-- | tests/sjs.mjs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/sjs.mjs b/tests/sjs.mjs index c8f52dd..9376429 100644 --- a/tests/sjs.mjs +++ b/tests/sjs.mjs @@ -32,6 +32,8 @@ import { reduced, reduceRec, reduce, + reductionsRec, + reductions, mapValues, repeat, runTests, @@ -958,6 +960,72 @@ const test_reduce = t => { }); }; +const test_reductionsRec = t => { + t.start("reductionsRec()"); + + t.testing("return the value if isReduced()", () => { + t.assertEq( + reductionsRec(null, null, reduced("abc"), null, []), + ["abc"], + ); + }); + + t.testing("the bare value if at the end of the collection", () => { + t.assertEq(reductionsRec([], null, null, 0, "ret"), "ret"); + t.assertEq(reductionsRec([1, 2], null, null, 2, "ret"), "ret"); + }); + + t.testing("recurse while applying function", () => { + const expected = [{ + acc: -5, + el: 4, + index: 3, + coll: [1, 2, 3, 4, 5], + ret: -1, + }, { + acc: -1, + el: 5, + index: 4, + coll: [1, 2, 3, 4, 5], + ret: 4, + }]; + const calls = []; + const fn = (acc, el, index, coll) => { + const ret = acc + el; + calls.push({ + acc, + el, + index, + coll, + ret, + }); + return ret; + }; + const coll = [1, 2, 3, 4, 5]; + t.assertEq(reductionsRec(coll, fn, -5, 3, []), [-1, 4]); + t.assertEq(calls, expected); + }); +}; + +const test_reductions = t => { + t.start("reductions()"); + + const add = (a, b) => a + b; + + + t.testing("with and without initial values", () => { + t.assertEq(reductions([1, 1, 1, 1], add), [1, 2, 3, 4]); + t.assertEq(reductions([1, 1, 1], add, 0), [1, 2, 3]); + t.assertEq(reductions([1, 1, 1], add, 1), [2, 3, 4]); + t.assertEq(reductions([1, 2, 3], add), [1, 3, 6]); + }); + + t.testing("the last values corresponds to reduce()", () => { + const arr = [1, 2, 3]; + t.assertEq(last(reductions(arr, add)), reduce(arr, add)); + }); +}; + const test_mapValues = t => { t.start("mapValues()"); @@ -1021,6 +1089,8 @@ runTests([ test_reduced, test_reduceRec, test_reduce, + test_reductionsRec, + test_reductions, test_mapValues, test_repeat, ]); |
