Commit fe0d6f3e authored by Mike Bostock's avatar Mike Bostock
Browse files

Refactor array tests using smash.

parent 5c4282f5
import "min";
d3.zip = function() {
if (!(n = arguments.length)) return [];
for (var i = -1, m = d3.min(arguments, d3_zipLength), zips = new Array(m); ++i < m;) {
......
......@@ -33,7 +33,7 @@ console.log(JSON.stringify({
"jsdom": "~0.5.2"
},
"devDependencies": {
"smash": "~0.0.5",
"smash": "~0.0.7",
"uglify-js": "2.2.x",
"vows": "0.7.x"
},
......
require("../env");
var vows = require("vows"),
load = require("../load"),
assert = require("../env-assert");
var suite = vows.describe("d3.ascending");
suite.addBatch({
"numbers": {
"returns a negative number if a < b": function() {
assert.isTrue(d3.ascending(0, 1) < 0);
},
"returns a positive number if a > b": function() {
assert.isTrue(d3.ascending(1, 0) > 0);
},
"returns zero if a == b": function() {
assert.equal(d3.ascending(0, 0), 0);
},
"returns NaN if a or b is undefined": function() {
assert.isNaN(d3.ascending(0, undefined));
assert.isNaN(d3.ascending(undefined, 0));
assert.isNaN(d3.ascending(undefined, undefined));
},
"returns NaN if a or b is NaN": function() {
assert.isNaN(d3.ascending(0, NaN));
assert.isNaN(d3.ascending(NaN, 0));
assert.isNaN(d3.ascending(NaN, NaN));
}
}
});
suite.addBatch({
"strings": {
"returns a negative number if a < b": function() {
assert.isTrue(d3.ascending("a", "b") < 0);
},
"returns a positive number if a > b": function() {
assert.isTrue(d3.ascending("b", "a") > 0);
"d3.ascending": {
topic: load("arrays/ascending"),
"numbers": {
"returns a negative number if a < b": function(d3) {
assert.isTrue(d3.ascending(0, 1) < 0);
},
"returns a positive number if a > b": function(d3) {
assert.isTrue(d3.ascending(1, 0) > 0);
},
"returns zero if a == b": function(d3) {
assert.equal(d3.ascending(0, 0), 0);
},
"returns NaN if a or b is undefined": function(d3) {
assert.isNaN(d3.ascending(0, undefined));
assert.isNaN(d3.ascending(undefined, 0));
assert.isNaN(d3.ascending(undefined, undefined));
},
"returns NaN if a or b is NaN": function(d3) {
assert.isNaN(d3.ascending(0, NaN));
assert.isNaN(d3.ascending(NaN, 0));
assert.isNaN(d3.ascending(NaN, NaN));
}
},
"returns zero if a == b": function() {
assert.equal(d3.ascending("a", "a"), 0);
"strings": {
"returns a negative number if a < b": function(d3) {
assert.isTrue(d3.ascending("a", "b") < 0);
},
"returns a positive number if a > b": function(d3) {
assert.isTrue(d3.ascending("b", "a") > 0);
},
"returns zero if a == b": function(d3) {
assert.equal(d3.ascending("a", "a"), 0);
}
}
}
});
......
require("../env");
var vows = require("vows"),
load = require("../load"),
assert = require("../env-assert");
var suite = vows.describe("d3.bisect");
......@@ -9,49 +8,47 @@ var i30 = 1 << 30;
suite.addBatch({
"bisectLeft": {
topic: function() {
return d3.bisectLeft;
},
"finds the index of an exact match": function(bisect) {
topic: load("arrays/bisect"),
"finds the index of an exact match": function(d3) {
var array = [1, 2, 3];
assert.equal(bisect(array, 1), 0);
assert.equal(bisect(array, 2), 1);
assert.equal(bisect(array, 3), 2);
assert.equal(d3.bisectLeft(array, 1), 0);
assert.equal(d3.bisectLeft(array, 2), 1);
assert.equal(d3.bisectLeft(array, 3), 2);
},
"finds the index of the first match": function(bisect) {
"finds the index of the first match": function(d3) {
var array = [1, 2, 2, 3];
assert.equal(bisect(array, 1), 0);
assert.equal(bisect(array, 2), 1);
assert.equal(bisect(array, 3), 3);
assert.equal(d3.bisectLeft(array, 1), 0);
assert.equal(d3.bisectLeft(array, 2), 1);
assert.equal(d3.bisectLeft(array, 3), 3);
},
"finds the insertion point of a non-exact match": function(bisect) {
"finds the insertion point of a non-exact match": function(d3) {
var array = [1, 2, 3];
assert.equal(bisect(array, 0.5), 0);
assert.equal(bisect(array, 1.5), 1);
assert.equal(bisect(array, 2.5), 2);
assert.equal(bisect(array, 3.5), 3);
assert.equal(d3.bisectLeft(array, 0.5), 0);
assert.equal(d3.bisectLeft(array, 1.5), 1);
assert.equal(d3.bisectLeft(array, 2.5), 2);
assert.equal(d3.bisectLeft(array, 3.5), 3);
},
"observes the optional lower bound": function(bisect) {
"observes the optional lower bound": function(d3) {
var array = [1, 2, 3, 4, 5];
assert.equal(bisect(array, 0, 2), 2);
assert.equal(bisect(array, 1, 2), 2);
assert.equal(bisect(array, 2, 2), 2);
assert.equal(bisect(array, 3, 2), 2);
assert.equal(bisect(array, 4, 2), 3);
assert.equal(bisect(array, 5, 2), 4);
assert.equal(bisect(array, 6, 2), 5);
assert.equal(d3.bisectLeft(array, 0, 2), 2);
assert.equal(d3.bisectLeft(array, 1, 2), 2);
assert.equal(d3.bisectLeft(array, 2, 2), 2);
assert.equal(d3.bisectLeft(array, 3, 2), 2);
assert.equal(d3.bisectLeft(array, 4, 2), 3);
assert.equal(d3.bisectLeft(array, 5, 2), 4);
assert.equal(d3.bisectLeft(array, 6, 2), 5);
},
"observes the optional bounds": function(bisect) {
"observes the optional bounds": function(d3) {
var array = [1, 2, 3, 4, 5];
assert.equal(bisect(array, 0, 2, 3), 2);
assert.equal(bisect(array, 1, 2, 3), 2);
assert.equal(bisect(array, 2, 2, 3), 2);
assert.equal(bisect(array, 3, 2, 3), 2);
assert.equal(bisect(array, 4, 2, 3), 3);
assert.equal(bisect(array, 5, 2, 3), 3);
assert.equal(bisect(array, 6, 2, 3), 3);
assert.equal(d3.bisectLeft(array, 0, 2, 3), 2);
assert.equal(d3.bisectLeft(array, 1, 2, 3), 2);
assert.equal(d3.bisectLeft(array, 2, 2, 3), 2);
assert.equal(d3.bisectLeft(array, 3, 2, 3), 2);
assert.equal(d3.bisectLeft(array, 4, 2, 3), 3);
assert.equal(d3.bisectLeft(array, 5, 2, 3), 3);
assert.equal(d3.bisectLeft(array, 6, 2, 3), 3);
},
"large arrays": function(bisect) {
"large arrays": function(d3) {
var array = [],
i = i30;
array[i++] = 1;
......@@ -59,62 +56,57 @@ suite.addBatch({
array[i++] = 3;
array[i++] = 4;
array[i++] = 5;
assert.equal(bisect(array, 0, i - 5, i), i - 5);
assert.equal(bisect(array, 1, i - 5, i), i - 5);
assert.equal(bisect(array, 2, i - 5, i), i - 4);
assert.equal(bisect(array, 3, i - 5, i), i - 3);
assert.equal(bisect(array, 4, i - 5, i), i - 2);
assert.equal(bisect(array, 5, i - 5, i), i - 1);
assert.equal(bisect(array, 6, i - 5, i), i - 0);
assert.equal(d3.bisectLeft(array, 0, i - 5, i), i - 5);
assert.equal(d3.bisectLeft(array, 1, i - 5, i), i - 5);
assert.equal(d3.bisectLeft(array, 2, i - 5, i), i - 4);
assert.equal(d3.bisectLeft(array, 3, i - 5, i), i - 3);
assert.equal(d3.bisectLeft(array, 4, i - 5, i), i - 2);
assert.equal(d3.bisectLeft(array, 5, i - 5, i), i - 1);
assert.equal(d3.bisectLeft(array, 6, i - 5, i), i - 0);
}
}
});
suite.addBatch({
},
"bisectRight": {
topic: function() {
return d3.bisectRight;
},
"finds the index after an exact match": function(bisect) {
topic: load("arrays/bisect"),
"finds the index after an exact match": function(d3) {
var array = [1, 2, 3];
assert.equal(bisect(array, 1), 1);
assert.equal(bisect(array, 2), 2);
assert.equal(bisect(array, 3), 3);
assert.equal(d3.bisectRight(array, 1), 1);
assert.equal(d3.bisectRight(array, 2), 2);
assert.equal(d3.bisectRight(array, 3), 3);
},
"finds the index after the last match": function(bisect) {
"finds the index after the last match": function(d3) {
var array = [1, 2, 2, 3];
assert.equal(bisect(array, 1), 1);
assert.equal(bisect(array, 2), 3);
assert.equal(bisect(array, 3), 4);
assert.equal(d3.bisectRight(array, 1), 1);
assert.equal(d3.bisectRight(array, 2), 3);
assert.equal(d3.bisectRight(array, 3), 4);
},
"finds the insertion point of a non-exact match": function(bisect) {
"finds the insertion point of a non-exact match": function(d3) {
var array = [1, 2, 3];
assert.equal(bisect(array, 0.5), 0);
assert.equal(bisect(array, 1.5), 1);
assert.equal(bisect(array, 2.5), 2);
assert.equal(bisect(array, 3.5), 3);
assert.equal(d3.bisectRight(array, 0.5), 0);
assert.equal(d3.bisectRight(array, 1.5), 1);
assert.equal(d3.bisectRight(array, 2.5), 2);
assert.equal(d3.bisectRight(array, 3.5), 3);
},
"observes the optional lower bound": function(bisect) {
"observes the optional lower bound": function(d3) {
var array = [1, 2, 3, 4, 5];
assert.equal(bisect(array, 0, 2), 2);
assert.equal(bisect(array, 1, 2), 2);
assert.equal(bisect(array, 2, 2), 2);
assert.equal(bisect(array, 3, 2), 3);
assert.equal(bisect(array, 4, 2), 4);
assert.equal(bisect(array, 5, 2), 5);
assert.equal(bisect(array, 6, 2), 5);
assert.equal(d3.bisectRight(array, 0, 2), 2);
assert.equal(d3.bisectRight(array, 1, 2), 2);
assert.equal(d3.bisectRight(array, 2, 2), 2);
assert.equal(d3.bisectRight(array, 3, 2), 3);
assert.equal(d3.bisectRight(array, 4, 2), 4);
assert.equal(d3.bisectRight(array, 5, 2), 5);
assert.equal(d3.bisectRight(array, 6, 2), 5);
},
"observes the optional bounds": function(bisect) {
"observes the optional bounds": function(d3) {
var array = [1, 2, 3, 4, 5];
assert.equal(bisect(array, 0, 2, 3), 2);
assert.equal(bisect(array, 1, 2, 3), 2);
assert.equal(bisect(array, 2, 2, 3), 2);
assert.equal(bisect(array, 3, 2, 3), 3);
assert.equal(bisect(array, 4, 2, 3), 3);
assert.equal(bisect(array, 5, 2, 3), 3);
assert.equal(bisect(array, 6, 2, 3), 3);
assert.equal(d3.bisectRight(array, 0, 2, 3), 2);
assert.equal(d3.bisectRight(array, 1, 2, 3), 2);
assert.equal(d3.bisectRight(array, 2, 2, 3), 2);
assert.equal(d3.bisectRight(array, 3, 2, 3), 3);
assert.equal(d3.bisectRight(array, 4, 2, 3), 3);
assert.equal(d3.bisectRight(array, 5, 2, 3), 3);
assert.equal(d3.bisectRight(array, 6, 2, 3), 3);
},
"large arrays": function(bisect) {
"large arrays": function(d3) {
var array = [],
i = i30;
array[i++] = 1;
......@@ -122,25 +114,20 @@ suite.addBatch({
array[i++] = 3;
array[i++] = 4;
array[i++] = 5;
assert.equal(bisect(array, 0, i - 5, i), i - 5);
assert.equal(bisect(array, 1, i - 5, i), i - 4);
assert.equal(bisect(array, 2, i - 5, i), i - 3);
assert.equal(bisect(array, 3, i - 5, i), i - 2);
assert.equal(bisect(array, 4, i - 5, i), i - 1);
assert.equal(bisect(array, 5, i - 5, i), i - 0);
assert.equal(bisect(array, 6, i - 5, i), i - 0);
assert.equal(d3.bisectRight(array, 0, i - 5, i), i - 5);
assert.equal(d3.bisectRight(array, 1, i - 5, i), i - 4);
assert.equal(d3.bisectRight(array, 2, i - 5, i), i - 3);
assert.equal(d3.bisectRight(array, 3, i - 5, i), i - 2);
assert.equal(d3.bisectRight(array, 4, i - 5, i), i - 1);
assert.equal(d3.bisectRight(array, 5, i - 5, i), i - 0);
assert.equal(d3.bisectRight(array, 6, i - 5, i), i - 0);
}
}
});
suite.addBatch({
},
"bisector(key)": {
topic: function() {
return d3.bisector(function(d) { return d.key; });
},
topic: load("arrays/bisect"),
"left": {
topic: function(bisector) {
return bisector.left;
topic: function(d3) {
return d3.bisector(function(d) { return d.key; }).left;
},
"finds the index of an exact match": function(bisect) {
var array = [{key: 1}, {key: 2}, {key: 3}];
......@@ -199,8 +186,8 @@ suite.addBatch({
}
},
"right": {
topic: function(bisector) {
return bisector.right;
topic: function(d3) {
return d3.bisector(function(d) { return d.key; }).right;
},
"finds the index after an exact match": function(bisect) {
var array = [{key: 1}, {key: 2}, {key: 3}];
......
require("../env");
var vows = require("vows"),
load = require("../load"),
assert = require("../env-assert");
var suite = vows.describe("d3.descending");
suite.addBatch({
"numbers": {
"returns a negative number if a > b": function() {
assert.isTrue(d3.descending(1, 0) < 0);
},
"returns a positive number if a < b": function() {
assert.isTrue(d3.descending(0, 1) > 0);
},
"returns zero if a == b": function() {
assert.equal(d3.descending(0, 0), 0);
},
"returns NaN if a or b is undefined": function() {
assert.isNaN(d3.descending(0, undefined));
assert.isNaN(d3.descending(undefined, 0));
assert.isNaN(d3.descending(undefined, undefined));
},
"returns NaN if a or b is NaN": function() {
assert.isNaN(d3.descending(0, NaN));
assert.isNaN(d3.descending(NaN, 0));
assert.isNaN(d3.descending(NaN, NaN));
}
}
});
suite.addBatch({
"strings": {
"returns a negative number if a > b": function() {
assert.isTrue(d3.descending("b", "a") < 0);
},
"returns a positive number if a < b": function() {
assert.isTrue(d3.descending("a", "b") > 0);
"descending": {
topic: load("arrays/descending"),
"numbers": {
"returns a negative number if a > b": function(d3) {
assert.isTrue(d3.descending(1, 0) < 0);
},
"returns a positive number if a < b": function(d3) {
assert.isTrue(d3.descending(0, 1) > 0);
},
"returns zero if a == b": function(d3) {
assert.equal(d3.descending(0, 0), 0);
},
"returns NaN if a or b is undefined": function(d3) {
assert.isNaN(d3.descending(0, undefined));
assert.isNaN(d3.descending(undefined, 0));
assert.isNaN(d3.descending(undefined, undefined));
},
"returns NaN if a or b is NaN": function(d3) {
assert.isNaN(d3.descending(0, NaN));
assert.isNaN(d3.descending(NaN, 0));
assert.isNaN(d3.descending(NaN, NaN));
}
},
"returns zero if a == b": function() {
assert.equal(d3.descending("a", "a"), 0);
"strings": {
"returns a negative number if a > b": function(d3) {
assert.isTrue(d3.descending("b", "a") < 0);
},
"returns a positive number if a < b": function(d3) {
assert.isTrue(d3.descending("a", "b") > 0);
},
"returns zero if a == b": function(d3) {
assert.equal(d3.descending("a", "a"), 0);
}
}
}
});
......
require("../env");
var vows = require("vows"),
load = require("../load"),
assert = require("../env-assert");
var suite = vows.describe("d3.entries");
suite.addBatch({
"entries": {
topic: function() {
return d3.entries;
},
"enumerates every entry": function(entries) {
assert.deepEqual(entries({a: 1, b: 2}), [
topic: load("arrays/entries"),
"enumerates every entry": function(d3) {
assert.deepEqual(d3.entries({a: 1, b: 2}), [
{key: "a", value: 1},
{key: "b", value: 2}
]);
},
"includes entries defined on prototypes": function(entries) {
"includes entries defined on prototypes": function(d3) {
function abc() {
this.a = 1;
this.b = 2;
}
abc.prototype.c = 3;
assert.deepEqual(entries(new abc()), [
assert.deepEqual(d3.entries(new abc()), [
{key: "a", value: 1},
{key: "b", value: 2},
{key: "c", value: 3}
]);
},
"includes null or undefined values": function(entries) {
var v = entries({a: undefined, b: null, c: NaN});
"includes null or undefined values": function(d3) {
var v = d3.entries({a: undefined, b: null, c: NaN});
assert.equal(v.length, 3);
assert.deepEqual(v[0], {key: "a", value: undefined});
assert.deepEqual(v[1], {key: "b", value: null});
......
require("../env");
var vows = require("vows"),
load = require("../load"),
assert = require("../env-assert");
var suite = vows.describe("d3.extent");
suite.addBatch({
"extent": {
topic: function() {
return d3.extent;
},
"returns the numeric extent for numbers": function(extent) {
assert.deepEqual(extent([1]), [1, 1]);
assert.deepEqual(extent([5, 1, 2, 3, 4]), [1, 5]);
assert.deepEqual(extent([20, 3]), [3, 20]);
assert.deepEqual(extent([3, 20]), [3, 20]);
topic: load("arrays/extent"),
"returns the numeric extent for numbers": function(d3) {
assert.deepEqual(d3.extent([1]), [1, 1]);
assert.deepEqual(d3.extent([5, 1, 2, 3, 4]), [1, 5]);
assert.deepEqual(d3.extent([20, 3]), [3, 20]);
assert.deepEqual(d3.extent([3, 20]), [3, 20]);
},
"returns the lexicographic extent for strings": function(extent) {
assert.deepEqual(extent(["c", "a", "b"]), ["a", "c"]);
assert.deepEqual(extent(["20", "3"]), ["20", "3"]);
assert.deepEqual(extent(["3", "20"]), ["20", "3"]);
"returns the lexicographic extent for strings": function(d3) {
assert.deepEqual(d3.extent(["c", "a", "b"]), ["a", "c"]);
assert.deepEqual(d3.extent(["20", "3"]), ["20", "3"]);
assert.deepEqual(d3.extent(["3", "20"]), ["20", "3"]);
},
"ignores null, undefined and NaN": function(extent) {
assert.deepEqual(extent([NaN, 1, 2, 3, 4, 5]), [1, 5]);
assert.deepEqual(extent([1, 2, 3, 4, 5, NaN]), [1, 5]);
assert.deepEqual(extent([10, null, 3, undefined, 5, NaN]), [3, 10]);
assert.deepEqual(extent([-1, null, -3, undefined, -5, NaN]), [-5, -1]);
"ignores null, undefined and NaN": function(d3) {
assert.deepEqual(d3.extent([NaN, 1, 2, 3, 4, 5]), [1, 5]);
assert.deepEqual(d3.extent([1, 2, 3, 4, 5, NaN]), [1, 5]);
assert.deepEqual(d3.extent([10, null, 3, undefined, 5, NaN]), [3, 10]);
assert.deepEqual(d3.extent([-1, null, -3, undefined, -5, NaN]), [-5, -1]);
},
"compares heterogenous types as numbers": function(extent) {
assert.deepEqual(extent([20, "3"]), ["3", 20]);
assert.deepEqual(extent(["20", 3]), [3, "20"]);
assert.deepEqual(extent([3, "20"]), [3, "20"]);
assert.deepEqual(extent(["3", 20]), ["3", 20]);
"compares heterogenous types as numbers": function(d3) {
assert.deepEqual(d3.extent([20, "3"]), ["3", 20]);
assert.deepEqual(d3.extent(["20", 3]), [3, "20"]);
assert.deepEqual(d3.extent([3, "20"]), [3, "20"]);
assert.deepEqual(d3.extent(["3", 20]), ["3", 20]);
},
"returns undefined for empty array": function(extent) {
assert.deepEqual(extent([]), [undefined, undefined]);
assert.deepEqual(extent([null]), [undefined, undefined]);
assert.deepEqual(extent([undefined]), [undefined, undefined]);
assert.deepEqual(extent([NaN]), [undefined, undefined]);
assert.deepEqual(extent([NaN, NaN]), [undefined, undefined]);
"returns undefined for empty array": function(d3) {
assert.deepEqual(d3.extent([]), [undefined, undefined]);
assert.deepEqual(d3.extent([null]), [undefined, undefined]);
assert.deepEqual(d3.extent([undefined]), [undefined, undefined]);
assert.deepEqual(d3.extent([NaN]), [undefined, undefined]);
assert.deepEqual(d3.extent([NaN, NaN]), [undefined, undefined]);
},
"applies the optional accessor function exactly once": function(extent) {
"applies the optional accessor function exactly once": function(d3) {
var i = 10;
assert.deepEqual(d3.extent([0,1,2,3], function() { return ++i; }), [11, 14]);
}
......
require("../env");
var vows = require("vows"),
load = require("../load"),
assert = require("../env-assert");
var suite = vows.describe("d3.keys");
suite.addBatch({
"keys": {
topic: function() {
return d3.keys;
},
"enumerates every defined key": function(keys) {
assert.deepEqual(keys({a: 1, b: 1}), ["a", "b"]);
topic: load("arrays/keys"),
"enumerates every defined key": function(d3) {
assert.deepEqual(d3.keys({a: 1, b: 1}), ["a", "b"]);
},
"includes keys defined on prototypes": function(keys) {
"includes keys defined on prototypes": function(d3) {
function abc() {
this.a = 1;
this.b = 2;
}
abc.prototype.c = 3;
assert.deepEqual(keys(new abc()), ["a", "b", "c"]);
<