]>
git.scottworley.com Git - inverse-tax/blob - tax.test.js
3 var assert
= require('assert');
5 function test(description
, f
) {
9 function rand(limit
= 99) {
10 return Math
.round(Math
.random() * limit
);
13 function make_random_tax_table() {
14 function make_random_table(min_threshold
) {
15 if (rand(4) == 0) return [];
16 const start
= min_threshold
=== undefined ? rand() : min_threshold
;
17 const end
= start
+ 1 + rand();
18 return [[start
, rand()]].concat(make_random_table(end
));
20 return tax_table_from_table(make_random_table());
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]);
31 assert
.strictEqual(sum([]), 0);
32 assert
.strictEqual(sum([7]), 7);
33 assert
.strictEqual(sum([100, 1, 10]), 111);
37 assert
.strictEqual(tax([[10, 100, .01], [100, Infinity
, .1]], 150), 5.9);
40 test("merge tax tables", () => {
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);
47 assert
.ok(near(tax(t1
, income
) + tax(t2
, income
), tax(combined
, income
)));
52 test("invert", () => {
53 assert
.strictEqual(invert([ ])(10), 10);
54 assert
.strictEqual(invert([[ 0, Infinity
, .1]])( 9), 10);
55 assert
.strictEqual(invert([[10, Infinity
, .1]])(19), 20);
56 assert
.strictEqual(invert([[10, Infinity
, .1]])( 9), 9);
57 assert
.strictEqual(invert([[0, 100, .1], [100, Infinity
, .2]])(170), 200);
58 assert
.strictEqual(invert([[0, 100, .1], [100, Infinity
, .2]])(112), 127.5);
59 assert
.strictEqual(invert([[6, 90, 0.75], [90, Infinity
, 0.12]])(27), 90);
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
)));