tridactyl/src/mod.test.ts

67 lines
1.9 KiB
TypeScript
Raw Normal View History

2017-10-05 03:42:55 +01:00
/** Some real tests for mod and also an example of how to unit test. */
2017-10-05 02:23:15 +01:00
import { euclid_mod, knuth_mod, my_mod } from "./number.mod"
2017-10-05 02:23:15 +01:00
2017-10-05 03:42:55 +01:00
expect.extend({
/** Expect equal to all values in array */
toBeAll(received, values: Array<any>) {
2017-10-05 03:42:55 +01:00
for (let val of values) {
if (received !== val) {
return {
message: () =>
`expected ${received} === for each v in ${values}`,
pass: false,
2017-10-05 03:42:55 +01:00
}
}
}
return {
message: () => `expected ${received} !== for some v in ${values}`,
pass: true,
2017-10-05 03:42:55 +01:00
}
},
2017-10-05 03:42:55 +01:00
})
2017-10-05 02:23:15 +01:00
2017-10-05 03:42:55 +01:00
/** Manually written tests */
const testcases = [
[-3, 3, 0],
[-2, 3, 1],
[-1, 3, 2],
[0, 3, 0],
[1, 3, 1],
[2, 3, 2],
[3, 3, 0],
[4, 3, 1],
[5, 3, 2],
[6, 3, 0],
]
2017-10-05 02:23:15 +01:00
for (let [a, b, ans] of testcases) {
test(`${a} (mod ${b}) -- .mod`, () => expect(a.mod(b)).toEqual(ans))
test(`${a} (mod ${b}) -- euclid`, () =>
expect(euclid_mod(a, b)).toEqual(ans))
test(`${a} (mod ${b}) -- knuth`, () => expect(knuth_mod(a, b)).toEqual(ans))
test(`${a} (mod ${b}) -- my`, () => expect(my_mod(a, b)).toEqual(ans))
2017-10-05 02:23:15 +01:00
}
2017-10-05 03:42:55 +01:00
/** Test with mixed dividend, positive divisor */
for (let i = 0; i < 100; i++) {
let a = ((Math.random() - 0.5) * 10000) | 0
let b = (Math.random() * 10000) | 0
2017-10-05 03:42:55 +01:00
b = b === 0 ? 17 : b // Don't be 0.
test(`${a} (mod ${b}) -- equivalence check`, () =>
expect(a.mod(b)).toBeAll([
my_mod(a, b),
euclid_mod(a, b),
knuth_mod(a, b),
]))
2017-10-05 02:23:15 +01:00
}
/** Test with a mix of +ve and -ve */
for (let i = 0; i < 100; i++) {
let a = ((Math.random() - 0.5) * 10000) | 0
let b = ((Math.random() - 0.5) * 10000) | 0
2017-10-05 03:42:55 +01:00
b = b === 0 ? 17 : b // Don't be 0.
test(`${a} (mod ${b}) -- equivalence check`, () =>
expect(a.mod(b)).toBeAll([my_mod(a, b), knuth_mod(a, b)]))
2017-10-05 02:23:15 +01:00
}