forked from scratchfoundation/scratch-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection_db_test.js
309 lines (266 loc) · 10.6 KB
/
connection_db_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* @license
* Blockly Tests
*
* Copyright 2015 Google Inc.
* https://developers.google.com/blockly/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
function verify_DB_(msg, expected, db) {
var equal = (expected.length == db.length);
if (equal) {
for (var i = 0; i < expected.length; i++) {
if (expected[i] != db[i]) {
equal = false;
break;
}
}
}
if (equal) {
assertTrue(msg, true);
} else {
assertEquals(msg, expected, db);
}
}
function test_DB_addConnection() {
var db = new Blockly.ConnectionDB();
var o2 = {y_: 2, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o2);
verify_DB_('Adding connection #2', [o2], db);
var o4 = {y_: 4, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o4);
verify_DB_('Adding connection #4', [o2, o4], db);
var o1 = {y_: 1, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o1);
verify_DB_('Adding connection #1', [o1, o2, o4], db);
var o3a = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o3a);
verify_DB_('Adding connection #3a', [o1, o2, o3a, o4], db);
var o3b = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o3b);
verify_DB_('Adding connection #3b', [o1, o2, o3b, o3a, o4], db);
}
function test_DB_removeConnection() {
var db = new Blockly.ConnectionDB();
var o1 = {y_: 1, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o2 = {y_: 2, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o3a = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o3b = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o3c = {y_: 3, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
var o4 = {y_: 4, sourceBlock_: {},
getSourceBlock: Blockly.Connection.prototype.getSourceBlock};
db.addConnection(o1);
db.addConnection(o2);
db.addConnection(o3c);
db.addConnection(o3b);
db.addConnection(o3a);
db.addConnection(o4);
verify_DB_('Adding connections 1-4', [o1, o2, o3a, o3b, o3c, o4], db);
db.removeConnection_(o2);
verify_DB_('Removing connection #2', [o1, o3a, o3b, o3c, o4], db);
db.removeConnection_(o4);
verify_DB_('Removing connection #4', [o1, o3a, o3b, o3c], db);
db.removeConnection_(o1);
verify_DB_('Removing connection #1', [o3a, o3b, o3c], db);
db.removeConnection_(o3a);
verify_DB_('Removing connection #3a', [o3b, o3c], db);
db.removeConnection_(o3c);
verify_DB_('Removing connection #3c', [o3b], db);
db.removeConnection_(o3b);
verify_DB_('Removing connection #3b', [], db);
}
function test_DB_getNeighbours() {
var db = new Blockly.ConnectionDB();
// Search an empty list.
assertEquals(helper_getNeighbours(db,
10 /* x */, 10 /* y */, 100 /* radius */).length, 0);
// Set up some connections.
for (var i = 0; i < 10; i++) {
db.addConnection(helper_createConnection(0, i,
Blockly.PREVIOUS_STATEMENT, null, true));
}
// Test block belongs at beginning.
var result = helper_getNeighbours(db, 0, 0, 4);
assertEquals(5, result.length);
for (i = 0; i < result.length; i++) {
assertNotEquals(result.indexOf(db[i]), -1); // contains
}
// Test block belongs at middle.
result = helper_getNeighbours(db, 0, 4, 2);
assertEquals(5, result.length);
for (i = 0; i < result.length; i++) {
assertNotEquals(result.indexOf(db[i + 2]), -1); // contains
}
// Test block belongs at end.
result = helper_getNeighbours(db, 0, 9, 4);
assertEquals(5, result.length);
for (i = 0; i < result.length; i++) {
assertNotEquals(result.indexOf(db[i + 5]), -1); // contains
}
// Test block has no neighbours due to being out of range in the x direction.
result = helper_getNeighbours(db, 10, 9, 4);
assertEquals(result.length, 0);
// Test block has no neighbours due to being out of range in the y direction.
result = helper_getNeighbours(db, 0, 19, 4);
assertEquals(result.length, 0);
// Test block has no neighbours due to being out of range diagonally.
result = helper_getNeighbours(db, -2, -2, 2);
assertEquals(result.length, 0);
}
function test_DB_findPositionForConnection() {
var db = new Blockly.ConnectionDB();
db.addConnection(helper_createConnection(0, 0, Blockly.PREVIOUS_STATEMENT,
null, true));
db.addConnection(helper_createConnection(0, 1, Blockly.PREVIOUS_STATEMENT,
null, true));
db.addConnection(helper_createConnection(0, 2, Blockly.PREVIOUS_STATEMENT,
null, true));
db.addConnection(helper_createConnection(0, 4, Blockly.PREVIOUS_STATEMENT,
null, true));
db.addConnection(helper_createConnection(0, 5, Blockly.PREVIOUS_STATEMENT,
null, true));
assertEquals(5, db.length);
var conn = helper_createConnection(0, 3, Blockly.PREVIOUS_STATEMENT, null,
true);
assertEquals(3, db.findPositionForConnection_(conn));
}
function test_DB_findConnection() {
var db = new Blockly.ConnectionDB();
for (var i = 0; i < 10; i++) {
db.addConnection(helper_createConnection(i, 0,
Blockly.PREVIOUS_STATEMENT, null, true));
db.addConnection(helper_createConnection(0, i,
Blockly.PREVIOUS_STATEMENT, null, true));
}
var conn = helper_createConnection(3, 3, Blockly.PREVIOUS_STATEMENT, null,
true);
db.addConnection(conn);
assertEquals(conn, db[db.findConnection(conn)]);
conn = helper_createConnection(3, 3, Blockly.PREVIOUS_STATEMENT, null, true);
assertEquals(-1, db.findConnection(conn));
}
function test_DB_ordering() {
var db = new Blockly.ConnectionDB();
for (var i = 0; i < 10; i++) {
db.addConnection(helper_createConnection(0, 9 - i,
Blockly.PREVIOUS_STATEMENT), null, true);
}
for (i = 0; i < 10; i++) {
assertEquals(i, db[i].y_);
}
// quasi-random
var xCoords = [-29, -47, -77, 2, 43, 34, -59, -52, -90, -36, -91, 38, 87, -20,
60, 4, -57, 65, -37, -81, 57, 58, -96, 1, 67, -79, 34, 93, -90, -99, -62,
4, 11, -36, -51, -72, 3, -50, -24, -45, -92, -38, 37, 24, -47, -73, 79,
-20, 99, 43, -10, -87, 19, 35, -62, -36, 49, 86, -24, -47, -89, 33, -44,
25, -73, -91, 85, 6, 0, 89, -94, 36, -35, 84, -9, 96, -21, 52, 10, -95, 7,
-67, -70, 62, 9, -40, -95, -9, -94, 55, 57, -96, 55, 8, -48, -57, -87, 81,
23, 65];
var yCoords = [-81, 82, 5, 47, 30, 57, -12, 28, 38, 92, -25, -20, 23, -51, 73,
-90, 8, 28, -51, -15, 81, -60, -6, -16, 77, -62, -42, -24, 35, 95, -46,
-7, 61, -16, 14, 91, 57, -38, 27, -39, 92, 47, -98, 11, -33, -72, 64, 38,
-64, -88, -35, -59, -76, -94, 45, -25, -100, -95, 63, -97, 45, 98, 99, 34,
27, 52, -18, -45, 66, -32, -38, 70, -73, -23, 5, -2, -13, -9, 48, 74, -97,
-11, 35, -79, -16, -77, 83, -57, -53, 35, -44, 100, -27, -15, 5, 39, 33,
-19, -20, -95];
for (i = 0; i < xCoords.length; i++) {
db.addConnection(helper_createConnection(xCoords[i], yCoords[i],
Blockly.PREVIOUS_STATEMENT), null, true);
}
for (i = 1; i < xCoords.length; i++) {
assertTrue(db[i].y_ >= db[i - 1].y_);
}
}
function test_SearchForClosest() {
var db = new Blockly.ConnectionDB();
var sharedWorkspace = {id: "Shared workspace"};
// Search an empty list.
assertEquals(null, helper_searchDB(db, 10 /* x */, 10 /* y */,
100 /* radius */));
db.addConnection(helper_createConnection(100, 0, Blockly.PREVIOUS_STATEMENT,
sharedWorkspace, true));
assertEquals(null, helper_searchDB(db, 0, 0, 5, sharedWorkspace));
db = new Blockly.ConnectionDB();
for (var i = 0; i < 10; i++) {
var tempConn = helper_createConnection(0, i, Blockly.PREVIOUS_STATEMENT,
sharedWorkspace, true);
tempConn.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
db.addConnection(tempConn);
}
// Should be at 0, 9.
var last = db[db.length - 1];
// Correct connection is last in db; many connections in radius.
assertEquals(last, helper_searchDB(db, 0, 10, 15, sharedWorkspace));
// Nothing nearby.
assertEquals(null, helper_searchDB(db, 100, 100, 3, sharedWorkspace));
// First in db, exact match.
assertEquals(db[0], helper_searchDB(db, 0, 0, 0, sharedWorkspace));
tempConn = helper_createConnection(6, 6, Blockly.PREVIOUS_STATEMENT,
sharedWorkspace, true);
tempConn.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
db.addConnection(tempConn);
tempConn = helper_createConnection(5, 5, Blockly.PREVIOUS_STATEMENT,
sharedWorkspace, true);
tempConn.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
db.addConnection(tempConn);
var result = helper_searchDB(db, 4, 6, 3, sharedWorkspace);
assertEquals(5, result.x_);
assertEquals(5, result.y_);
}
function helper_getNeighbours(db, x, y, radius) {
return db.getNeighbours(helper_createConnection(x, y, Blockly.NEXT_STATEMENT,
null, true),
radius);
}
function helper_searchDB(db, x, y, radius, shared_workspace) {
var tempConn = helper_createConnection(x, y,
Blockly.NEXT_STATEMENT, shared_workspace, true);
tempConn.sourceBlock_ = helper_makeSourceBlock(shared_workspace);
var closest = db.searchForClosest(tempConn, radius, {x: 0, y: 0});
return closest.connection;
}
function helper_makeSourceBlock(sharedWorkspace) {
return {workspace: sharedWorkspace,
parentBlock_: null,
getParent: function() { return null; },
movable_: true,
isMovable: function() { return true; },
isShadow: function() { return false; },
isInsertionMarker: function() { return false; },
getFirstStatementConnection: function() { return null; }
};
}
function helper_createConnection(x, y, type, opt_shared_workspace,
opt_rendered) {
var workspace = opt_shared_workspace ? opt_shared_workspace : {};
if (opt_rendered) {
var conn = new Blockly.RenderedConnection({workspace: workspace}, type);
} else {
var conn = new Blockly.Connection({workspace: workspace}, type);
}
conn.x_ = x;
conn.y_ = y;
return conn;
}