]> git.scottworley.com Git - inverse-tax/blobdiff - tax.test.js
Broad testing (it works now)
[inverse-tax] / tax.test.js
index 479323bf3b671436756c5e7d022b305b61ff08b6..21b98b62a01955df27e6c9e2931c40ae66e385d9 100644 (file)
@@ -6,6 +6,24 @@ function test(description, f) {
   f();
 }
 
+function near(a, b, epsilon = 1e-6) {
+  return Math.abs(a - b) < epsilon;
+}
+
+function rand(limit = 99) {
+  return Math.round(Math.random() * limit);
+}
+
+function make_random_tax_table() {
+  function make_random_table(min_threshold) {
+    if (rand(2)) return [];
+    const start = min_threshold === undefined ? rand() : min_threshold;
+    const end = start + 1 + rand();
+    return [[start, rand()]].concat(make_random_table(end));
+  }
+  return tax_table_from_table(make_random_table());
+}
+
 test("parse tax table", () => {
   const parsed = parse_tax_table(' 1  2\n10  4\n');
   assert.strictEqual(parsed.length, 2);
@@ -24,28 +42,33 @@ test("tax", () => {
 });
 
 test("merge tax tables", () => {
-  function rand(limit = 100) {
-    return Math.round(Math.random() * limit);
-  }
-  function make_random_tax_table(min_threshold) {
-    if (rand(2)) return [];
-    const start = min_threshold === undefined ? rand() : min_threshold;
-    const end = start + 1 + rand();
-    return [[start, end, rand()]].concat(make_random_tax_table(end));
-  }
   for (var i = 0; i < 1000; i++) {
     const t1 = make_random_tax_table();
     const t2 = make_random_tax_table();
     const combined = merge_tax_tables(t1, t2);
     for (var j = 0; j < 10; j++) {
       const income = rand(250);
-      assert.strictEqual(tax(t1, income) + tax(t2, income), tax(combined, income));
+      assert.ok(near(tax(t1, income) + tax(t2, income), tax(combined, income)));
     }
   }
 });
 
 test("invert", () => {
+  assert.strictEqual(invert([                  ])(10), 10);
   assert.strictEqual(invert([[ 0, Infinity, .1]])( 9), 10);
   assert.strictEqual(invert([[10, Infinity, .1]])(19), 20);
+  assert.strictEqual(invert([[10, Infinity, .1]])( 9),  9);
   assert.strictEqual(invert([[0, 100, .1], [100, Infinity, .2]])(170), 200);
+  assert.strictEqual(invert([[0, 100, .1], [100, Infinity, .2]])(112), 127.5);
+  assert.strictEqual(invert([[6, 90, 0.75], [90, Infinity, 0.12]])(27), 90);
+
+  for (var i = 0; i < 1000; i++) {
+    const t = make_random_tax_table();
+    const inverted_t = invert(t);
+    for (var j = 0; j < 20; j++) {
+      const net = rand(250);
+      const gross = inverted_t(net);
+      assert.ok(near(net, gross - tax(t, gross)));
+    }
+  }
 });