forked from d3/d3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpack-test.js
More file actions
129 lines (125 loc) · 5.07 KB
/
Copy pathpack-test.js
File metadata and controls
129 lines (125 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var vows = require("vows"),
load = require("../load"),
assert = require("../assert");
var suite = vows.describe("d3.layout.pack");
suite.addBatch({
"pack": {
topic: load("layout/pack").expression("d3.layout.pack"),
"can handle an empty children array": function(pack) {
var p = pack();
assert.deepEqual(p.nodes({children: [{children: []}, {value: 1}]}).map(layout), [
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 0.5},
{value: 0, depth: 1, x: 0.0, y: 0.5, r: 0.0},
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 0.5}
]);
},
"can handle zero-valued nodes": function(pack) {
var p = pack();
assert.deepEqual(p.nodes({children: [{value: 0}, {value: 1}]}).map(layout), [
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 0.5},
{value: 0, depth: 1, x: 0.0, y: 0.5, r: 0.0},
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 0.5}
]);
},
"can handle small nodes": function(pack) {
assert.deepEqual(pack().sort(null).nodes({children: [
{value: .01},
{value: 2},
{value: 2},
{value: 1}
]}).map(layout), [
{y: 0.5, x: 0.5, value: 5.01, r: 0.5, depth: 0},
{y: 0.5084543199854831, x: 0.4682608366855136, value: 0.01, r: 0.016411496513964046, depth: 1},
{y: 0.5084543199854831, x: 0.7167659426883449, value: 2, r: 0.23209360948886723, depth: 1},
{y: 0.34256315498862167, x: 0.2832340573116551, value: 2, r: 0.23209360948886723, depth: 1},
{y: 0.7254154893606051, x: 0.38524055061025186, value: 1, r: 0.16411496513964044, depth: 1}
]);
assert.deepEqual(pack().sort(null).nodes({children: [
{value: 2},
{value: 2},
{value: 1},
{value: .01}
]}).map(layout), [
{y: 0.5, x: 0.5, value: 5.01, r: 0.5, depth: 0},
{y: 0.6274712284943809, x: 0.26624891409386664, value: 2, r: 0.23375108590613333, depth: 1},
{y: 0.6274712284943809, x: 0.7337510859061334, value: 2, r: 0.23375108590613333, depth: 1},
{y: 0.30406466355343187, x: 0.5, value: 1, r: 0.1652869779539461, depth: 1},
{y: 0.3878967195987758, x: 0.3386645534068854, value: 0.01, r: 0.01652869779539461, depth: 1}
]);
},
"can handle residual floating point error": function(pack) {
var p = pack();
var result = p.nodes({children: [
{value: 0.005348322447389364},
{value: 0.8065882022492588},
{value: 0}
]}).map(layout);
assert.isFalse(result.map(function(d) { return d.depth; }).some(isNaN));
assert.isFalse(result.map(function(d) { return d.value; }).some(isNaN));
assert.isFalse(result.map(function(d) { return d.x; }).some(isNaN));
assert.isFalse(result.map(function(d) { return d.y; }).some(isNaN));
assert.isFalse(result.map(function(d) { return d.r; }).some(isNaN));
},
"avoids coincident circles": function(pack) {
var p = pack();
var result = p({children: [
{children: [{value: 17010}, {value: 5842}, {value: 0}, {value: 0}]},
{children: [
{children: [{value: 721}, {value: 4294}, {value: 9800}, {value: 1314}, {value: 2220}]},
{value: 1759}, {value: 2165}, {value: 586}, {value: 3331}, {value: 772}, {value: 3322}
]}
]}).map(layout);
result.sort(function(a, b) {
return a.x < b.x && a.y < b.y ? -1 : 1;
});
assert.isFalse(result.slice(1).some(function(d, i) {
return d.x === result[i].x && d.y === result[i].y && d.value > 0;
}));
},
"radius defaults to automatic scaling": function(pack) {
assert.equal(pack().radius(), null);
},
"radius can be specified using a custom function of value": function(pack) {
var r = function(value) { return Math.sqrt(value) * 10; },
p = pack().radius(r);
assert.strictEqual(p.radius(), r);
assert.deepEqual(p.nodes({children: [{value: 1}]}).map(layout), [
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 10},
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 10}
]);
},
"radius can be specified as a constant": function(pack) {
var p = pack().radius(5);
assert.equal(p.radius(), 5);
assert.deepEqual(p.nodes({children: [{value: 1}]}).map(layout), [
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 5},
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 5}
]);
},
"radius constant value is coerced to a number": function(pack) {
var p = pack().radius("5");
assert.equal(p.radius(), 5);
assert.deepEqual(p.nodes({children: [{value: 1}]}).map(layout), [
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 5},
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 5}
]);
},
"radius function value is coerced to a number": function(pack) {
var p = pack().radius(function() { return "5"; });
assert.deepEqual(p.nodes({children: [{value: 1}]}).map(layout), [
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 5},
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 5}
]);
}
}
});
function layout(node) {
return {
value: node.value,
depth: node.depth,
r: node.r,
x: node.x,
y: node.y
};
}
suite.export(module);