|
| 1 | +require("../env"); |
| 2 | + |
| 3 | +var vows = require("vows"), |
| 4 | + assert = require("assert"); |
| 5 | + |
| 6 | +var suite = vows.describe("d3.geom.quadtree"); |
| 7 | + |
| 8 | +suite.addBatch({ |
| 9 | + "can create an empty quadtree": function() { |
| 10 | + var q = d3.geom.quadtree([], 8, 10, 56, 47), |
| 11 | + n = 0; |
| 12 | + q.visit(function(node, x1, y1, x2, y2) { |
| 13 | + assert.isNull(node.point); |
| 14 | + assert.isUndefined(node[0]); |
| 15 | + assert.isUndefined(node[1]); |
| 16 | + assert.isUndefined(node[2]); |
| 17 | + assert.isUndefined(node[3]); |
| 18 | + assert.isTrue(node.leaf); |
| 19 | + ++n; |
| 20 | + }); |
| 21 | + assert.strictEqual(n, 1, "number of visits"); |
| 22 | + }, |
| 23 | + "squarifies the input dimensions": function() { |
| 24 | + var ox1 = 8, |
| 25 | + oy1 = 10, |
| 26 | + ox2 = 56, |
| 27 | + oy2 = 47, |
| 28 | + q = d3.geom.quadtree([], ox1, oy1, ox2, oy2), |
| 29 | + n = 0; |
| 30 | + q.visit(function(node, x1, y1, x2, y2) { |
| 31 | + assert.strictEqual(x1, ox1); |
| 32 | + assert.strictEqual(y1, oy1); |
| 33 | + assert.strictEqual(x2, Math.max(ox2 - ox1, oy2 - oy1) + x1); |
| 34 | + assert.strictEqual(y2, Math.max(ox2 - ox1, oy2 - oy1) + y1); |
| 35 | + ++n; |
| 36 | + }); |
| 37 | + assert.strictEqual(n, 1, "number of visits"); |
| 38 | + }, |
| 39 | + "with three arguments, x1 and y1 are 0,0": function() { |
| 40 | + var dx = 56, |
| 41 | + dy = 47, |
| 42 | + q = d3.geom.quadtree([], dx, dy), |
| 43 | + n = 0; |
| 44 | + q.visit(function(node, x1, y1, x2, y2) { |
| 45 | + assert.strictEqual(x1, 0); |
| 46 | + assert.strictEqual(y1, 0); |
| 47 | + assert.strictEqual(x2, Math.max(dx, dy)); |
| 48 | + assert.strictEqual(y2, Math.max(dx, dy)); |
| 49 | + ++n; |
| 50 | + }); |
| 51 | + assert.strictEqual(n, 1, "number of visits"); |
| 52 | + }, |
| 53 | + "visit": { |
| 54 | + "uses pre-order traversal": function() { |
| 55 | + var a = {x: 100, y: 100}, |
| 56 | + b = {x: 200, y: 200}, |
| 57 | + c = {x: 300, y: 300}, |
| 58 | + q = d3.geom.quadtree([a, b, c], 960, 500), |
| 59 | + expected = [ |
| 60 | + {point: null, x1: 0, y1: 0, x2: 960, y2: 960}, |
| 61 | + {point: null, x1: 0, y1: 0, x2: 480, y2: 480}, |
| 62 | + {point: null, x1: 0, y1: 0, x2: 240, y2: 240}, |
| 63 | + {point: a, x1: 0, y1: 0, x2: 120, y2: 120}, |
| 64 | + {point: b, x1: 120, y1: 120, x2: 240, y2: 240}, |
| 65 | + {point: c, x1: 240, y1: 240, x2: 480, y2: 480} |
| 66 | + ]; |
| 67 | + q.visit(function(node, x1, y1, x2, y2) { |
| 68 | + assert.deepEqual({point: node.point, x1: x1, y1: y1, x2: x2, y2: y2}, expected.shift()); |
| 69 | + assert.equal(!!node.point, node.leaf); |
| 70 | + }); |
| 71 | + assert.isEmpty(expected); |
| 72 | + }, |
| 73 | + "does not recurse if the callback returns truthy": function() { |
| 74 | + var a = {x: 100, y: 100}, |
| 75 | + b = {x: 700, y: 700}, |
| 76 | + c = {x: 800, y: 800}, |
| 77 | + q = d3.geom.quadtree([a, b, c], 960, 500), |
| 78 | + n = 0; |
| 79 | + q.visit(function(node, x1, y1, x2, y2) { |
| 80 | + ++n; |
| 81 | + return x1 > 0; |
| 82 | + }); |
| 83 | + assert.equal(n, 3); |
| 84 | + } |
| 85 | + } |
| 86 | +}); |
| 87 | + |
| 88 | +suite.export(module); |
0 commit comments