-
Notifications
You must be signed in to change notification settings - Fork 0
/
luka_test.js
93 lines (84 loc) · 3.03 KB
/
luka_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {
assert,
assertStrictEquals,
} from "https://deno.land/std@0.190.0/testing/asserts.ts";
import op from "./luka.js";
Deno.test("negation", function () {
const { neg } = op;
// double negation: --x = x
assertStrictEquals(neg(neg(7)), 7, "double negation");
// In JavaScript, if x is 0, -x returns -0.
assertStrictEquals(neg(0), 0, "negative zero");
});
Deno.test("addition", function () {
const { add } = op;
// commutative: x + y = y + x
assertStrictEquals(add(7, 11), add(11, 7), "commutative");
// associative: (x + y) + z = x + (y + z)
assertStrictEquals(add(1, add(2, 3)), add(add(1, 2), 3), "associative");
// distributive: x * (y + z) = (x * y) + (x * z)
assertStrictEquals(2 * add(3, 4), add(2 * 3, 2 * 4), "distributive");
// identity: x + 0 = x
assertStrictEquals(add(7, 0), 7, "identity");
});
Deno.test("subtraction", function () {
const { sub } = op;
// distributive: x * (y - z) = (x * y) - (x * z)
assertStrictEquals(2 * sub(11, 7), sub(2 * 11, 2 * 7), "distributive");
// identity: x - 0 = x
assertStrictEquals(sub(7, 0), 7, "identity");
});
Deno.test("multiplication", function () {
const { mul } = op;
// commutative: x * y = y * x
assertStrictEquals(mul(2, 7), mul(7, 2), "commutative");
// associative: (x * y) * z = x * (y * z)
assertStrictEquals(mul(mul(3, 4), 5), mul(3, mul(4, 5)), "associative");
// distributive: x * (y + z) = (x * y) + (x * z)
assertStrictEquals(mul(2, 7 + 11), mul(2, 7) + mul(2, 11), "distributive");
// identity: x * 1 = x
assertStrictEquals(mul(7, 1), 7, "identity");
// JavaScript will return -0 if we multiply 0 by a negative number.
assertStrictEquals(mul(-1, 0), -0, "negative zero");
});
Deno.test("division", function () {
const { div } = op;
// identity: x / 1 = x
assertStrictEquals(div(7, 1), 7, "identity");
// divide by self: x / x = 1
assertStrictEquals(div(7, 7), 1, "divide by self");
});
Deno.test("exponent", function () {
const { exp } = op;
// right associative
assertStrictEquals(exp(2, exp(3, 4)), 2 ** 3 ** 4, "right associative");
});
Deno.test("remainder", function () {
const { rem } = op;
// positive dividend
assertStrictEquals(rem(11, -5), 1, "positive dividend");
// negative dividend
assertStrictEquals(rem(-11, 5), -1, "negative dividend");
});
Deno.test("foldable", function () {
const { add, sub, mul, div } = op;
// add fold
assertStrictEquals(add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 55, "add fold");
// subtract fold
assertStrictEquals(
sub(55, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
0,
"subtract fold",
);
// multiply fold
assertStrictEquals(mul(10, 10, 10), 1000, "multiply fold");
// divide fold
assertStrictEquals(div(1000, 10, 10), 10, "divide fold");
});
Deno.test("floating point imprecision", function () {
const { add, sub } = op;
const x = 0.1;
const y = 0.3;
const z = 0.4;
assert(add(x, y) === z && !(sub(z, y) === x), "non-reflexive");
});