]>
Commit | Line | Data |
---|---|---|
0993d859 SW |
1 | "use strict"; |
2 | ||
3 | var assert = require('assert'); | |
4 | ||
5 | function test(description, f) { | |
6 | f(); | |
7 | } | |
8 | ||
82b7a498 | 9 | function rand(limit = 99) { |
9737900e SW |
10 | return Math.round(Math.random() * limit); |
11 | } | |
12 | ||
a659a4f0 SW |
13 | function make_random_tax_table() { |
14 | function make_random_table(min_threshold) { | |
9c149d52 | 15 | if (rand(4) == 0) return []; |
a659a4f0 SW |
16 | const start = min_threshold === undefined ? rand() : min_threshold; |
17 | const end = start + 1 + rand(); | |
18 | return [[start, rand()]].concat(make_random_table(end)); | |
19 | } | |
20 | return tax_table_from_table(make_random_table()); | |
9737900e SW |
21 | } |
22 | ||
0993d859 SW |
23 | test("parse tax table", () => { |
24 | const parsed = parse_tax_table(' 1 2\n10 4\n'); | |
25 | assert.strictEqual(parsed.length, 2); | |
26 | assert.deepStrictEqual(parsed[0], [1, 10, .02]); | |
27 | assert.deepStrictEqual(parsed[1], [10, Infinity, .04]); | |
28 | }); | |
29 | ||
30 | test("sum", () => { | |
31 | assert.strictEqual(sum([]), 0); | |
32 | assert.strictEqual(sum([7]), 7); | |
33 | assert.strictEqual(sum([100, 1, 10]), 111); | |
34 | }); | |
35 | ||
36 | test("tax", () => { | |
37 | assert.strictEqual(tax([[10, 100, .01], [100, Infinity, .1]], 150), 5.9); | |
0993d859 | 38 | }); |
26460571 SW |
39 | |
40 | test("merge tax tables", () => { | |
26460571 SW |
41 | for (var i = 0; i < 1000; i++) { |
42 | const t1 = make_random_tax_table(); | |
43 | const t2 = make_random_tax_table(); | |
44 | const combined = merge_tax_tables(t1, t2); | |
45 | for (var j = 0; j < 10; j++) { | |
46 | const income = rand(250); | |
a659a4f0 | 47 | assert.ok(near(tax(t1, income) + tax(t2, income), tax(combined, income))); |
26460571 SW |
48 | } |
49 | } | |
50 | }); | |
53d6ad33 SW |
51 | |
52 | test("invert", () => { | |
1c6e4069 | 53 | assert.strictEqual(invert([ ])(10), 10); |
53d6ad33 SW |
54 | assert.strictEqual(invert([[ 0, Infinity, .1]])( 9), 10); |
55 | assert.strictEqual(invert([[10, Infinity, .1]])(19), 20); | |
49188cbe | 56 | assert.strictEqual(invert([[10, Infinity, .1]])( 9), 9); |
53d6ad33 | 57 | assert.strictEqual(invert([[0, 100, .1], [100, Infinity, .2]])(170), 200); |
b442070b | 58 | assert.strictEqual(invert([[0, 100, .1], [100, Infinity, .2]])(112), 127.5); |
68251a4c | 59 | assert.strictEqual(invert([[6, 90, 0.75], [90, Infinity, 0.12]])(27), 90); |
a3217513 SW |
60 | |
61 | for (var i = 0; i < 1000; i++) { | |
62 | const t = make_random_tax_table(); | |
63 | const inverted_t = invert(t); | |
64 | for (var j = 0; j < 20; j++) { | |
65 | const net = rand(250); | |
66 | const gross = inverted_t(net); | |
67 | assert.ok(near(net, gross - tax(t, gross))); | |
68 | } | |
69 | } | |
53d6ad33 | 70 | }); |