Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed May 23, 2015
1 parent ae58d1e commit cc7ae88
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/simple_statistics.js
Expand Up @@ -8,13 +8,16 @@
(function() {
var ss = {};

/* istanbul ignore else */
if (typeof module !== 'undefined') {
// Assign the `ss` object to exports, so that you can require
// it in [node.js](http://nodejs.org/)
module.exports = ss;
} else {
// Otherwise, in a browser, we assign `ss` to the window object,
// so you can simply refer to it as `ss`.
// Coverage testing will always skip this line, so we exclude
// it from istanbul's vision.
this.ss = ss;
}

Expand Down Expand Up @@ -1453,6 +1456,8 @@
// of Javascript.
function mixin(array) {
var support = !!(Object.defineProperty && Object.defineProperties);
// Coverage testing will never test this error.
/* istanbul ignore next */
if (!support) throw new Error('without defineProperty, simple-statistics cannot be mixed in');

// only methods which work on basic arrays in a single step
Expand Down
4 changes: 4 additions & 0 deletions test/chi_squared_goodness_of_fit.test.js
Expand Up @@ -19,5 +19,9 @@ test('chi_squared_goodness_of_fit', function(t) {
t.equal(true, ss.chi_squared_goodness_of_fit(data_10_19, ss.poisson_distribution, 0.10));
t.end();
});
test('can tolerate gaps in distribution', function(t) {
t.equal(true, ss.chi_squared_goodness_of_fit([0, 2, 3, 7, 7, 7, 7, 7, 7, 9, 10], ss.poisson_distribution, 0.10));
t.end();
});
t.end();
});
12 changes: 7 additions & 5 deletions test/cumulative.js
Expand Up @@ -4,20 +4,22 @@ var ss = require('../');
test('cumulative_std_normal_probability', function(t) {
// https://en.wikipedia.org/wiki/Standard_normal_table#Examples_of_use
test('wikipedia test example works', function(t) {
for (var i = 0; i < ss.standard_normal_table.length; i++) {
t.equal(ss.cumulative_std_normal_probability(0.4), 0.6554);
}
t.equal(ss.cumulative_std_normal_probability(0.4), 0.6554);
t.end();
});
test('nondecreasing', function(t) {
for (var i = 0; i < ss.standard_normal_table.length; i++) {
t.equal(ss.cumulative_std_normal_probability(i / 100) >= ss.cumulative_std_normal_probability((i - 1) / 100), true);
if (!ss.cumulative_std_normal_probability(i / 100) >= ss.cumulative_std_normal_probability((i - 1) / 100)) {
t.fail('non-decreasing failure on ' + i);
}
}
t.end();
});
test('matches error_function', function(t) {
for (var i = 0; i < ss.standard_normal_table.length; i++) {
t.equal(Math.abs(ss.cumulative_std_normal_probability(i / 100) - (.5 + .5 * ss.error_function(i / 100 / Math.sqrt(2)))) < ss.epsilon, true);
if (!(Math.abs(ss.cumulative_std_normal_probability(i / 100) - (.5 + .5 * ss.error_function(i / 100 / Math.sqrt(2)))) < ss.epsilon)) {
t.fail('error-fn failure on ' + i);
}
}
t.end();
});
Expand Down
7 changes: 7 additions & 0 deletions test/shuffle.test.js
Expand Up @@ -22,3 +22,10 @@ test('shuffle_in_place', function(t) {
t.deepEqual(input, [6, 1, 5, 2, 4, 3], 'changes original array');
t.end();
});

test('shuffle_in_place truly random', function(t) {
var input = [1, 2, 3, 4, 5, 6];
t.deepEqual(ss.shuffle_in_place([]), []);
t.deepEqual(ss.shuffle_in_place(input).sort(), [1, 2, 3, 4, 5, 6]);
t.end();
});

0 comments on commit cc7ae88

Please sign in to comment.