Commit 1ff5f504 authored by Mike Bostock's avatar Mike Bostock
Browse files

Refactor geom tests for minimal loading.

parent 480bfb6c
var vows = require("vows"),
d3 = require("../../"),
load = require("../load"),
assert = require("../env-assert");
......@@ -6,89 +7,89 @@ var suite = vows.describe("d3.geom.polygon");
suite.addBatch({
"polygon": {
topic: load("geom/polygon", "arrays/range"),
topic: load("geom/polygon").expression("d3.geom.polygon"),
"closed counterclockwise unit square": {
topic: function(d3) {
return d3.geom.polygon([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]);
topic: function(polygon) {
return polygon([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]);
},
"has area 1": function(polygon) {
assert.equal(polygon.area(), 1);
"has area 1": function(p) {
assert.equal(p.area(), 1);
},
"has centroid ⟨.5,.5⟩": function(polygon) {
assert.deepEqual(polygon.centroid(), [.5, .5]);
"has centroid ⟨.5,.5⟩": function(p) {
assert.deepEqual(p.centroid(), [.5, .5]);
}
},
"closed clockwise unit square": {
topic: function(d3) {
return d3.geom.polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]);
topic: function(polygon) {
return polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]);
},
"has area 1": function(polygon) {
assert.equal(polygon.area(), -1);
"has area 1": function(p) {
assert.equal(p.area(), -1);
},
"has centroid ⟨.5,.5⟩": function(polygon) {
assert.deepEqual(polygon.centroid(), [.5, .5]);
"has centroid ⟨.5,.5⟩": function(p) {
assert.deepEqual(p.centroid(), [.5, .5]);
}
},
"closed clockwise triangle": {
topic: function(d3) {
return d3.geom.polygon([[1, 1], [3, 2], [2, 3], [1, 1]]);
topic: function(polygon) {
return polygon([[1, 1], [3, 2], [2, 3], [1, 1]]);
},
"has area 1.5": function(polygon) {
assert.equal(polygon.area(), -1.5);
"has area 1.5": function(p) {
assert.equal(p.area(), -1.5);
},
"has centroid ⟨2,2⟩": function(polygon) {
var centroid = polygon.centroid();
"has centroid ⟨2,2⟩": function(p) {
var centroid = p.centroid();
assert.inDelta(centroid[0], 2, 1e-6);
assert.inDelta(centroid[1], 2, 1e-6);
}
},
"open counterclockwise unit square": {
topic: function(d3) {
return d3.geom.polygon([[0, 0], [0, 1], [1, 1], [1, 0]]);
topic: function(polygon) {
return polygon([[0, 0], [0, 1], [1, 1], [1, 0]]);
},
"has area 1": function(polygon) {
assert.equal(polygon.area(), 1);
"has area 1": function(p) {
assert.equal(p.area(), 1);
},
"has centroid ⟨.5,.5⟩": function(polygon) {
assert.deepEqual(polygon.centroid(), [.5, .5]);
"has centroid ⟨.5,.5⟩": function(p) {
assert.deepEqual(p.centroid(), [.5, .5]);
}
},
"open clockwise unit square": {
topic: function(d3) {
return d3.geom.polygon([[0, 0], [1, 0], [1, 1], [0, 1]]);
topic: function(polygon) {
return polygon([[0, 0], [1, 0], [1, 1], [0, 1]]);
},
"has area 1": function(polygon) {
assert.equal(polygon.area(), -1);
"has area 1": function(p) {
assert.equal(p.area(), -1);
},
"has centroid ⟨.5,.5⟩": function(polygon) {
assert.deepEqual(polygon.centroid(), [.5, .5]);
"has centroid ⟨.5,.5⟩": function(p) {
assert.deepEqual(p.centroid(), [.5, .5]);
}
},
"open clockwise triangle": {
topic: function(d3) {
return d3.geom.polygon([[1, 1], [3, 2], [2, 3]]);
topic: function(polygon) {
return polygon([[1, 1], [3, 2], [2, 3]]);
},
"has area 1.5": function(polygon) {
assert.equal(polygon.area(), -1.5);
"has area 1.5": function(p) {
assert.equal(p.area(), -1.5);
},
"has centroid ⟨2,2⟩": function(polygon) {
var centroid = polygon.centroid();
"has centroid ⟨2,2⟩": function(p) {
var centroid = p.centroid();
assert.inDelta(centroid[0], 2, 1e-6);
assert.inDelta(centroid[1], 2, 1e-6);
}
},
"large square": {
topic: function(d3) {
topic: function(polygon) {
var r = 1e8,
d = d3.range(0, r, r / 1e4);
return d3.geom.polygon(
return polygon(
d.map(function(y) { return [0, y]; }).concat(
d.map(function(x) { return [x, r]; })).concat(
d.map(function(y) { return [r, y]; }).reverse()).concat(
d.map(function(x) { return [x, 0]; }).reverse()));
},
"has area 1e16 - 5e7": function(polygon) {
assert.equal(polygon.area(), 1e16 - 5e7);
"has area 1e16 - 5e7": function(p) {
assert.equal(p.area(), 1e16 - 5e7);
}
}
}
......
......@@ -6,9 +6,9 @@ var suite = vows.describe("d3.geom.quadtree");
suite.addBatch({
"quadtree": {
topic: load("geom/quadtree"),
"can create an empty quadtree": function(d3) {
var q = d3.geom.quadtree([], 8, 10, 56, 47),
topic: load("geom/quadtree").expression("d3.geom.quadtree"),
"can create an empty quadtree": function(quadtree) {
var q = quadtree([], 8, 10, 56, 47),
n = 0;
q.visit(function(node, x1, y1, x2, y2) {
assert.isNull(node.point);
......@@ -21,12 +21,12 @@ suite.addBatch({
});
assert.strictEqual(n, 1, "number of visits");
},
"squarifies the input dimensions": function(d3) {
"squarifies the input dimensions": function(quadtree) {
var ox1 = 8,
oy1 = 10,
ox2 = 56,
oy2 = 47,
q = d3.geom.quadtree([], ox1, oy1, ox2, oy2),
q = quadtree([], ox1, oy1, ox2, oy2),
n = 0;
q.visit(function(node, x1, y1, x2, y2) {
assert.strictEqual(x1, ox1);
......@@ -37,10 +37,10 @@ suite.addBatch({
});
assert.strictEqual(n, 1, "number of visits");
},
"with three arguments, x1 and y1 are 0,0": function(d3) {
"with three arguments, x1 and y1 are 0,0": function(quadtree) {
var dx = 56,
dy = 47,
q = d3.geom.quadtree([], dx, dy),
q = quadtree([], dx, dy),
n = 0;
q.visit(function(node, x1, y1, x2, y2) {
assert.strictEqual(x1, 0);
......@@ -52,11 +52,11 @@ suite.addBatch({
assert.strictEqual(n, 1, "number of visits");
},
"visit": {
"uses pre-order traversal": function(d3) {
"uses pre-order traversal": function(quadtree) {
var a = {x: 100, y: 100},
b = {x: 200, y: 200},
c = {x: 300, y: 300},
q = d3.geom.quadtree([a, b, c], 960, 500),
q = quadtree([a, b, c], 960, 500),
expected = [
{point: null, x1: 0, y1: 0, x2: 960, y2: 960},
{point: null, x1: 0, y1: 0, x2: 480, y2: 480},
......@@ -71,11 +71,11 @@ suite.addBatch({
});
assert.isEmpty(expected);
},
"does not recurse if the callback returns truthy": function(d3) {
"does not recurse if the callback returns truthy": function(quadtree) {
var a = {x: 100, y: 100},
b = {x: 700, y: 700},
c = {x: 800, y: 800},
q = d3.geom.quadtree([a, b, c], 960, 500),
q = quadtree([a, b, c], 960, 500),
n = 0;
q.visit(function(node, x1, y1, x2, y2) {
++n;
......
......@@ -6,38 +6,38 @@ var suite = vows.describe("d3.geom.voronoi");
suite.addBatch({
"voronoi": {
topic: load("geom/voronoi"),
topic: load("geom/voronoi").expression("d3.geom.voronoi"),
"with zero points": {
"returns the empty array": function(d3) {
assert.deepEqual(d3.geom.voronoi([]), []);
"returns the empty array": function(voronoi) {
assert.deepEqual(voronoi([]), []);
}
},
"with one point": {
"returns the semi-infinite bounding box": function(d3) {
assert.deepEqual(d3.geom.voronoi([[50, 50]], 100, 100), [[[-1000000,-1000000],[-1000000,1000000],[1000000,1000000],[1000000,-1000000]]]);
"returns the semi-infinite bounding box": function(voronoi) {
assert.deepEqual(voronoi([[50, 50]], 100, 100), [[[-1000000,-1000000],[-1000000,1000000],[1000000,1000000],[1000000,-1000000]]]);
}
},
"with two points": {
"separated by a line at 90° (vertical)": function(d3) {
assert.deepEqual(d3.geom.voronoi([[50, 25], [50, 75]], 100, 100), [[[-1000000,50],[1000000,50],[-1000000,-1000000],[1000000,-1000000]],[[-1000000,50],[1000000,50],[-1000000,1000000],[1000000,1000000]]]);
assert.deepEqual(d3.geom.voronoi([[50, 75], [50, 25]], 100, 100), [[[-1000000,50],[1000000,50],[-1000000,1000000],[1000000,1000000]],[[-1000000,50],[1000000,50],[-1000000,-1000000],[1000000,-1000000]]]);
"separated by a line at 90° (vertical)": function(voronoi) {
assert.deepEqual(voronoi([[50, 25], [50, 75]], 100, 100), [[[-1000000,50],[1000000,50],[-1000000,-1000000],[1000000,-1000000]],[[-1000000,50],[1000000,50],[-1000000,1000000],[1000000,1000000]]]);
assert.deepEqual(voronoi([[50, 75], [50, 25]], 100, 100), [[[-1000000,50],[1000000,50],[-1000000,1000000],[1000000,1000000]],[[-1000000,50],[1000000,50],[-1000000,-1000000],[1000000,-1000000]]]);
},
"separated by a line at 0° (horizontal)": function(d3) {
assert.deepEqual(d3.geom.voronoi([[25, 50], [75, 50]], 100, 100), [[[50,1000000],[50,-1000000],[-1000000,-1000000],[-1000000,1000000]],[[50,-1000000],[50,1000000],[1000000,-1000000],[1000000,1000000]]]);
assert.deepEqual(d3.geom.voronoi([[75, 50], [25, 50]], 100, 100), [[[50,-1000000],[50,1000000],[1000000,-1000000],[1000000,1000000]],[[50,1000000],[50,-1000000],[-1000000,-1000000],[-1000000,1000000]]]);
"separated by a line at 0° (horizontal)": function(voronoi) {
assert.deepEqual(voronoi([[25, 50], [75, 50]], 100, 100), [[[50,1000000],[50,-1000000],[-1000000,-1000000],[-1000000,1000000]],[[50,-1000000],[50,1000000],[1000000,-1000000],[1000000,1000000]]]);
assert.deepEqual(voronoi([[75, 50], [25, 50]], 100, 100), [[[50,-1000000],[50,1000000],[1000000,-1000000],[1000000,1000000]],[[50,1000000],[50,-1000000],[-1000000,-1000000],[-1000000,1000000]]]);
},
"separated by a line at 45° (diagonal)": function(d3) {
assert.deepEqual(d3.geom.voronoi([[25, 25], [75, 75]], 100, 100), [[[-999900,1000000],[1000100,-1000000],[-1000000,-1000000]],[[-999900,1000000],[1000100,-1000000],[1000000,1000000]]]);
assert.deepEqual(d3.geom.voronoi([[75, 25], [25, 75]], 100, 100), [[[-1000000,-1000000],[1000000,1000000],[1000000,-1000000]],[[-1000000,-1000000],[1000000,1000000],[-1000000,1000000]]]);
"separated by a line at 45° (diagonal)": function(voronoi) {
assert.deepEqual(voronoi([[25, 25], [75, 75]], 100, 100), [[[-999900,1000000],[1000100,-1000000],[-1000000,-1000000]],[[-999900,1000000],[1000100,-1000000],[1000000,1000000]]]);
assert.deepEqual(voronoi([[75, 25], [25, 75]], 100, 100), [[[-1000000,-1000000],[1000000,1000000],[1000000,-1000000]],[[-1000000,-1000000],[1000000,1000000],[-1000000,1000000]]]);
},
"separated by an arbitrary diagonal": function(d3) {
assert.deepEqual(d3.geom.voronoi([[25, 25], [50, 75]], 100, 100), [[[-1000000,500068.75],[1000000,-499931.25],[-1000000,-1000000],[1000000,-1000000]],[[-1000000,500068.75],[1000000,-499931.25],[-1000000,1000000],[1000000,1000000]]]);
assert.deepEqual(d3.geom.voronoi([[25, 25], [75, 50]], 100, 100), [[[-499931.25,1000000],[500068.75,-1000000],[-1000000,1000000],[1000000,1000000]], [[-499931.25,1000000],[500068.75,-1000000],[-1000000,-1000000],[1000000,-1000000]]]);
"separated by an arbitrary diagonal": function(voronoi) {
assert.deepEqual(voronoi([[25, 25], [50, 75]], 100, 100), [[[-1000000,500068.75],[1000000,-499931.25],[-1000000,-1000000],[1000000,-1000000]],[[-1000000,500068.75],[1000000,-499931.25],[-1000000,1000000],[1000000,1000000]]]);
assert.deepEqual(voronoi([[25, 25], [75, 50]], 100, 100), [[[-499931.25,1000000],[500068.75,-1000000],[-1000000,1000000],[1000000,1000000]], [[-499931.25,1000000],[500068.75,-1000000],[-1000000,-1000000],[1000000,-1000000]]]);
}
},
"with three points": {
"collinear": function(d3) {
assert.deepEqual(d3.geom.voronoi([[25, 25], [50, 50], [75, 75]], 100, 100), [[[-999925,1000000],[1000075,-1000000],[-1000000,-1000000]],[[-999925,1000000],[-999875,1000000],[1000125,-1000000],[1000075,-1000000]],[[-999875,1000000],[1000125,-1000000],[1000000,1000000]]]);
"collinear": function(voronoi) {
assert.deepEqual(voronoi([[25, 25], [50, 50], [75, 75]], 100, 100), [[[-999925,1000000],[1000075,-1000000],[-1000000,-1000000]],[[-999925,1000000],[-999875,1000000],[1000125,-1000000],[1000075,-1000000]],[[-999875,1000000],[1000125,-1000000],[1000000,1000000]]]);
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment