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