-
Notifications
You must be signed in to change notification settings - Fork 224
/
log_average.test.js
63 lines (56 loc) · 1.7 KB
/
log_average.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
/* eslint no-shadow: 0 */
const test = require("tap").test;
const ss = require("../");
test("log average", function (t) {
t.test("cannot calculate for empty lists", function (t) {
t.throws(function () {
ss.logAverage([]);
});
t.end();
});
t.test("cannot calculate for lists with negative numbers", function (t) {
t.throws(function () {
ss.logAverage([-1]);
});
t.end();
});
t.test("does not overflow for large products", function (t) {
const value = 1000;
const array = [];
for (let i = 0; i < 100; i++) {
array.push(value);
}
if (!isFinite(ss.logAverage(array))) {
t.fail("log average failed for large product");
}
t.end();
});
t.test("does not underflow for small products", function (t) {
const value = 0.001;
const array = [];
for (let i = 0; i < 100; i++) {
array.push(value);
}
if (ss.logAverage(array) === 0) {
t.fail("log average failed for small product");
}
t.end();
});
t.test("agrees with geometricMean", function (t) {
const arr = [];
for (let i = 0; i < 10; i++) {
arr.push(Math.exp(Math.random()));
}
if (Math.abs(ss.logAverage(arr) - ss.geometricMean(arr)) > ss.epsilon) {
t.fail("log average and geometric mean are not equal");
}
t.end();
});
t.test("equals zero if array contains zero", function (t) {
if (ss.logAverage([0, 1, 2]) !== 0) {
t.fail("log average of array containing zero is not zero");
}
t.end();
});
t.end();
});