Skip to content

Commit

Permalink
Add test for scale.quantile.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Aug 15, 2011
1 parent d6f4b2a commit cdfbca9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 247 deletions.
64 changes: 64 additions & 0 deletions test/scale/quantile-test.js
@@ -0,0 +1,64 @@
require("../env");
require("../../d3");

var vows = require("vows"),
assert = require("assert");

var suite = vows.describe("d3.scale.quantile");

suite.addBatch({
"quantile": {
topic: function() {
return d3.scale.quantile;
},
"has the empty domain by default": function(quantile) {
assert.isEmpty(quantile().domain());
},
"has the empty range by default": function(quantile) {
assert.isEmpty(quantile().range());
},
"uses the R-7 algorithm to compute quantiles": function(quantile) {
var x = d3.scale.quantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]).range([0, 1, 2, 3]);
assert.deepEqual([3, 6, 6.9, 7, 7.1].map(x), [0, 0, 0, 0, 0]);
assert.deepEqual([8, 8.9].map(x), [1, 1]);
assert.deepEqual([9, 9.1, 10, 13].map(x), [2, 2, 2, 2]);
assert.deepEqual([14.9, 15, 15.1, 16, 20].map(x), [3, 3, 3, 3, 3]);
var x = d3.scale.quantile().domain([3, 6, 7, 8, 8, 9, 10, 13, 15, 16, 20]).range([0, 1, 2, 3]);
assert.deepEqual([3, 6, 6.9, 7, 7.1].map(x), [0, 0, 0, 0, 0]);
assert.deepEqual([8, 8.9].map(x), [1, 1]);
assert.deepEqual([9, 9.1, 10, 13].map(x), [2, 2, 2, 2]);
assert.deepEqual([14.9, 15, 15.1, 16, 20].map(x), [3, 3, 3, 3, 3]);
},
"domain values are sorted in ascending order": function(quantile) {
var x = d3.scale.quantile().domain([6, 3, 7, 8, 8, 13, 20, 15, 16, 10]);
assert.deepEqual(x.domain(), [3, 6, 7, 8, 8, 10, 13, 15, 16, 20]);
},
"non-numeric domain values are ignored": function(quantile) {
var x = d3.scale.quantile().domain([6, 3, NaN, undefined, 7, 8, 8, 13, 20, 15, 16, 10, NaN]);
assert.deepEqual(x.domain(), [3, 6, 7, 8, 8, 10, 13, 15, 16, 20]);
},
"quantiles returns the inner thresholds": function(quantile) {
var x = d3.scale.quantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]).range([0, 1, 2, 3]);
assert.deepEqual(x.quantiles(), [7.25, 9, 14.5]);
var x = d3.scale.quantile().domain([3, 6, 7, 8, 8, 9, 10, 13, 15, 16, 20]).range([0, 1, 2, 3]);
assert.deepEqual(x.quantiles(), [7.5, 9, 14]);
},
"range cardinality determines the number of quantiles": function(quantile) {
var x = d3.scale.quantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]);;
assert.deepEqual(x.range([0, 1, 2, 3]).quantiles(), [7.25, 9, 14.5]);
assert.deepEqual(x.range([0, 1]).quantiles(), [9]);
assert.deepEqual(x.range([,,,,,]).quantiles(), [6.8, 8, 11.2, 15.2]);
assert.deepEqual(x.range([,,,,,,]).quantiles(), [6.5, 8, 9, 13, 15.5]);
},
"range values are arbitrary": function(quantile) {
var a = new Object(), b = new Object(), c = new Object();
var x = d3.scale.quantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]).range([a, b, c, a]);
assert.deepEqual([3, 6, 6.9, 7, 7.1].map(x), [a, a, a, a, a]);
assert.deepEqual([8, 8.9].map(x), [b, b]);
assert.deepEqual([9, 9.1, 10, 13].map(x), [c, c, c, c]);
assert.deepEqual([14.9, 15, 15.1, 16, 20].map(x), [a, a, a, a, a]);
}
}
});

suite.export(module);
142 changes: 0 additions & 142 deletions test/scale/test-quantile.js

This file was deleted.

105 changes: 0 additions & 105 deletions test/scale/test-quantile.out

This file was deleted.

0 comments on commit cdfbca9

Please sign in to comment.