forked from w8r/graphology-layout-forceatlas2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
219 lines (179 loc) · 5.01 KB
/
test.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* Graphology FA2 Layout Unit Tests
* =================================
*/
var assert = require('assert'),
Graph = require('graphology');
var helpers = require('./helpers.js'),
layout = require('./index.js');
var seedrandom = require('seedrandom');
var rng = function() {
return seedrandom('test');
};
var clusters = require('graphology-generators/random/clusters');
var empty = require('graphology-generators/classic/empty');
describe('graphology-layout-forceatlas2', function() {
describe('helpers', function() {
describe('#.graphToByteArrays', function() {
it('should work as expected.', function() {
var graph = new Graph();
var data = {
John: {
size: 4,
x: 3,
y: 4
},
Martha: {
x: 10,
y: 5
},
Ada: {
x: 23,
y: -2
}
};
for (var node in data)
graph.addNode(node, data[node]);
graph.addEdge('John', 'Martha');
graph.addEdge('Martha', 'Ada', {weight: 3});
var matrices = helpers.graphToByteArrays(graph);
assert.deepEqual(
Array.from(matrices.nodes),
[
3, 4, 0, 0, 0, 0, 2, 1, 4, 0,
10, 5, 0, 0, 0, 0, 3, 1, 1, 0,
23, -2, 0, 0, 0, 0, 2, 1, 1, 0
]
);
assert.deepEqual(
Array.from(matrices.edges),
[
0, 10, 0,
10, 20, 3
]
);
});
});
describe('#.collectLayoutChanges', function() {
it('should work as expected.', function() {
var graph = new Graph();
var data = {
John: {
size: 4,
x: 3,
y: 4
},
Martha: {
x: 10,
y: 5
},
Ada: {
x: 23,
y: -2
}
};
for (var node in data)
graph.addNode(node, data[node]);
var positions = helpers.collectLayoutChanges(graph, [
4, 5, 0, 0, 0, 0, 2, 1, 4, 0,
11, 6, 0, 0, 0, 0, 3, 1, 1, 0,
24, -1, 0, 0, 0, 0, 2, 1, 1, 0
]);
assert.deepEqual(positions, {
John: {x: 4, y: 5},
Martha: {x: 11, y: 6},
Ada: {x: 24, y: -1}
});
});
});
describe('#.applyLayoutChanges', function() {
it('should work as expecte.', function() {
var graph = new Graph();
var data = {
John: {
x: 3,
y: 4
},
Martha: {
x: 10,
y: 5
},
Ada: {
x: 23,
y: -2
}
};
for (var node in data)
graph.addNode(node, data[node]);
helpers.applyLayoutChanges(graph, [
4, 5, 0, 0, 0, 0, 2, 1, 4, 0,
11, 6, 0, 0, 0, 0, 3, 1, 1, 0,
24, -1, 0, 0, 0, 0, 2, 1, 1, 0
]);
var positions = {
John: graph.getNodeAttributes('John'),
Martha: graph.getNodeAttributes('Martha'),
Ada: graph.getNodeAttributes('Ada')
};
assert.deepEqual(positions, {
John: {x: 4, y: 5},
Martha: {x: 11, y: 6},
Ada: {x: 24, y: -1}
});
});
});
});
describe('synchronous', function() {
it('should throw if the graph is invalid.', function() {
assert.throws(function() {
layout(null);
}, /graphology/);
});
it('should throw if iterations are not valid.', function() {
assert.throws(function() {
layout(new Graph(), {});
}, /number/);
assert.throws(function() {
layout(new Graph(), -34);
}, /positive/);
});
it('should throw if settings are invalid.', function() {
assert.throws(function() {
layout(new Graph(), {iterations: 5, settings: {linLogMode: 45}});
}, /linLogMode/);
});
});
describe('#.inferSettings', function() {
it('should correctly infer settings from given graph.', function() {
var smallGraph = empty(Graph, 250),
bigGraph = empty(Graph, 5000);
var settings = layout.inferSettings(smallGraph);
assert.strictEqual(settings.barnesHutOptimize, false);
settings = layout.inferSettings(bigGraph);
assert.strictEqual(settings.barnesHutOptimize, true);
});
});
describe('Barnes-Hut optimization', function() {
it('should converge on a large random graph with small coordinate values', function() {
// Creating a random clustered graph
var graph = clusters(Graph, {
order: 1000,
size: 5000,
clusters: 5,
rng: rng()
});
graph.nodes().forEach(function(n, i) {
graph.setNodeAttribute(n, 'x', i % 2 ? -1 : 1);
graph.setNodeAttribute(n, 'y', i % 2 ? -1 : 1);
});
assert.doesNotThrow(function() {
layout(graph, {
settings: {
barnesHutOptimize: true
},
iterations: 10
});
}, /FATAL/);
});
});
});