]> git.scottworley.com Git - inverse-tax/blame - tax.test.js
Test setup helpers to higher scope
[inverse-tax] / tax.test.js
CommitLineData
0993d859
SW
1"use strict";
2
3var assert = require('assert');
4
5function test(description, f) {
6 f();
7}
8
9737900e
SW
9function rand(limit = 100) {
10 return Math.round(Math.random() * limit);
11}
12
13function 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));
18}
19
0993d859
SW
20test("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]);
25});
26
27test("sum", () => {
28 assert.strictEqual(sum([]), 0);
29 assert.strictEqual(sum([7]), 7);
30 assert.strictEqual(sum([100, 1, 10]), 111);
31});
32
33test("tax", () => {
34 assert.strictEqual(tax([[10, 100, .01], [100, Infinity, .1]], 150), 5.9);
0993d859 35});
26460571
SW
36
37test("merge tax tables", () => {
26460571
SW
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));
45 }
46 }
47});
53d6ad33
SW
48
49test("invert", () => {
50 assert.strictEqual(invert([[ 0, Infinity, .1]])( 9), 10);
51 assert.strictEqual(invert([[10, Infinity, .1]])(19), 20);
52 assert.strictEqual(invert([[0, 100, .1], [100, Infinity, .2]])(170), 200);
53});