]>
git.scottworley.com Git - inverse-tax/blob - tax.test.js
48f2e0dda7437785ccf317e5094575b6b42b1d95
3 var assert
= require('assert');
5 function test(description
, f
) {
9 function rand(limit
= 100) {
10 return Math
.round(Math
.random() * limit
);
13 function make_random_tax_table(min_threshold
) {
14 if (rand(2)) return [];
15 const start
= min_threshold
=== undefined ? rand() : min_threshold
;
16 const end
= start
+ 1 + rand();
17 return [[start
, end
, rand()]].concat(make_random_tax_table(end
));
20 test("parse tax table", () => {
21 const parsed
= parse_tax_table(' 1 2\n10 4\n');
22 assert
.strictEqual(parsed
.length
, 2);
23 assert
.deepStrictEqual(parsed
[0], [1, 10, .02]);
24 assert
.deepStrictEqual(parsed
[1], [10, Infinity
, .04]);
28 assert
.strictEqual(sum([]), 0);
29 assert
.strictEqual(sum([7]), 7);
30 assert
.strictEqual(sum([100, 1, 10]), 111);
34 assert
.strictEqual(tax([[10, 100, .01], [100, Infinity
, .1]], 150), 5.9);
37 test("merge tax tables", () => {
38 for (var i
= 0; i
< 1000; i
++) {
39 const t1
= make_random_tax_table();
40 const t2
= make_random_tax_table();
41 const combined
= merge_tax_tables(t1
, t2
);
42 for (var j
= 0; j
< 10; j
++) {
43 const income
= rand(250);
44 assert
.strictEqual(tax(t1
, income
) + tax(t2
, income
), tax(combined
, income
));
49 test("invert", () => {
50 assert
.strictEqual(invert([ ])(10), 10);
51 assert
.strictEqual(invert([[ 0, Infinity
, .1]])( 9), 10);
52 assert
.strictEqual(invert([[10, Infinity
, .1]])(19), 20);
53 assert
.strictEqual(invert([[0, 100, .1], [100, Infinity
, .2]])(170), 200);