-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmean-test.js
47 lines (44 loc) · 1.64 KB
/
mean-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
var vows = require("vows"),
load = require("../load"),
assert = require("../assert"),
OneTimeNumber = require("./one-time-number");
var suite = vows.describe("d3.mean");
suite.addBatch({
"mean": {
topic: load("arrays/mean").expression("d3.mean"),
"returns the mean value for numbers": function(mean) {
assert.equal(mean([1]), 1);
assert.equal(mean([5, 1, 2, 3, 4]), 3);
assert.equal(mean([20, 3]), 11.5);
assert.equal(mean([3, 20]), 11.5);
},
"ignores null, undefined and NaN": function(mean) {
assert.equal(mean([NaN, 1, 2, 3, 4, 5]), 3);
assert.equal(mean([1, 2, 3, 4, 5, NaN]), 3);
assert.equal(mean([10, null, 3, undefined, 5, NaN]), 6);
},
"returns undefined for empty array": function(mean) {
assert.isUndefined(mean([]));
assert.isUndefined(mean([null]));
assert.isUndefined(mean([undefined]));
assert.isUndefined(mean([NaN]));
assert.isUndefined(mean([NaN, NaN]));
},
"applies the optional accessor function": function(mean) {
assert.equal(mean([[1, 2, 3, 4, 5], [2, 4, 6, 8, 10]], function(d) { return mean(d); }), 4.5);
assert.equal(mean([1, 2, 3, 4, 5], function(d, i) { return i; }), 2);
},
"coerces values to numbers": function(mean) {
assert.equal(mean(["1"]), 1);
assert.equal(mean(["5", "1", "2", "3", "4"]), 3);
assert.equal(mean(["20", "3"]), 11.5);
assert.equal(mean(["3", "20"]), 11.5);
},
"coerces values exactly once": function(mean) {
var array = [1, new OneTimeNumber(3)];
assert.equal(mean(array), 2);
assert.equal(mean(array), 1);
}
}
});
suite.export(module);