-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposition_bk_test.go
More file actions
575 lines (527 loc) · 22.8 KB
/
Copy pathposition_bk_test.go
File metadata and controls
575 lines (527 loc) · 22.8 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package dagro
import (
"math"
"reflect"
"testing"
)
func newBKTestGraph() *Graph {
return NewGraph().SetGraph(Attrs{})
}
func newBKConflictGraph() (*Graph, [][]string) {
g := newBKTestGraph().SetDefaultEdgeLabel(func(string, string, *string) any { return Attrs{} })
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0)})
g.SetNode("b", Attrs{"rank": float64(0), "order": float64(1)})
g.SetNode("c", Attrs{"rank": float64(1), "order": float64(0)})
g.SetNode("d", Attrs{"rank": float64(1), "order": float64(1)})
g.SetEdge("a", "d")
g.SetEdge("b", "c")
return g, buildLayerMatrix(g)
}
func requirePositionStringMap(t *testing.T, got, want map[string]string) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %#v, want %#v", got, want)
}
}
func requirePositionFloatMap(t *testing.T, got, want map[string]float64) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %#v, want %#v", got, want)
}
}
func TestFindType1ConflictsDoesNotMarkEdgesWithoutConflict(t *testing.T) {
g, layering := newBKConflictGraph()
g.RemoveEdgeByArgs("a", "d")
g.RemoveEdgeByArgs("b", "c")
g.SetEdge("a", "c")
g.SetEdge("b", "d")
conflicts := findType1Conflicts(g, layering)
if hasConflict(conflicts, "a", "c") || hasConflict(conflicts, "b", "d") {
t.Fatalf("unexpected conflict: %#v", conflicts)
}
}
func TestFindType1ConflictsDoesNotMarkType0Conflicts(t *testing.T) {
for _, dummy := range []string{"", "a", "b", "c", "d"} {
t.Run(dummy, func(t *testing.T) {
g, layering := newBKConflictGraph()
if dummy != "" {
asAttrs(g.Node(dummy))["dummy"] = true
}
conflicts := findType1Conflicts(g, layering)
if hasConflict(conflicts, "a", "d") || hasConflict(conflicts, "b", "c") {
t.Fatalf("unexpected conflict: %#v", conflicts)
}
})
}
}
func TestFindType1ConflictsMarksType1Conflicts(t *testing.T) {
for _, nonDummy := range []string{"a", "b", "c", "d"} {
t.Run(nonDummy, func(t *testing.T) {
g, layering := newBKConflictGraph()
for _, v := range []string{"a", "b", "c", "d"} {
if v != nonDummy {
asAttrs(g.Node(v))["dummy"] = true
}
}
conflicts := findType1Conflicts(g, layering)
ad, bc := hasConflict(conflicts, "a", "d"), hasConflict(conflicts, "b", "c")
wantAD := nonDummy == "a" || nonDummy == "d"
if ad != wantAD || bc == wantAD {
t.Fatalf("conflicts (a,d)=%v (b,c)=%v, non-dummy=%q", ad, bc, nonDummy)
}
})
}
}
func TestFindType1ConflictsDoesNotMarkType2Conflicts(t *testing.T) {
g, layering := newBKConflictGraph()
for _, v := range []string{"a", "b", "c", "d"} {
asAttrs(g.Node(v))["dummy"] = true
}
conflicts := findType1Conflicts(g, layering)
if hasConflict(conflicts, "a", "d") || hasConflict(conflicts, "b", "c") {
t.Fatalf("unexpected conflict: %#v", conflicts)
}
}
func TestFindType2ConflictsFavorsBorderSegments(t *testing.T) {
t.Run("first crossing", func(t *testing.T) {
g, layering := newBKConflictGraph()
for _, v := range []string{"a", "d"} {
asAttrs(g.Node(v))["dummy"] = true
}
for _, v := range []string{"b", "c"} {
asAttrs(g.Node(v))["dummy"] = "border"
}
conflicts := findType2Conflicts(g, layering)
if !hasConflict(conflicts, "a", "d") || hasConflict(conflicts, "b", "c") {
t.Fatalf("unexpected conflicts: %#v", conflicts)
}
})
t.Run("second crossing", func(t *testing.T) {
g, layering := newBKConflictGraph()
for _, v := range []string{"b", "c"} {
asAttrs(g.Node(v))["dummy"] = true
}
for _, v := range []string{"a", "d"} {
asAttrs(g.Node(v))["dummy"] = "border"
}
conflicts := findType2Conflicts(g, layering)
if hasConflict(conflicts, "a", "d") || !hasConflict(conflicts, "b", "c") {
t.Fatalf("unexpected conflicts: %#v", conflicts)
}
})
}
func TestFindType2ConflictsStartsNorthBoundaryAtMinusOne(t *testing.T) {
g := newBKTestGraph()
g.SetNode("north", Attrs{"dummy": true, "order": float64(-2)})
g.SetNode("south", Attrs{"dummy": true, "order": float64(0)})
g.SetEdge("north", "south")
conflicts := findType2Conflicts(g, [][]string{{"north"}, {"south"}})
if !hasConflict(conflicts, "north", "south") {
t.Fatalf("missing conflict with modern -1 north boundary: %#v", conflicts)
}
}
func TestPositionConflictsAreOrientationIndependentAndComposable(t *testing.T) {
conflicts := positionConflicts{}
addConflict(conflicts, "b", "a")
if !hasConflict(conflicts, "a", "b") || !hasConflict(conflicts, "b", "a") {
t.Fatal("conflict was orientation-dependent")
}
addConflict(conflicts, "a", "c")
if !hasConflict(conflicts, "a", "b") || !hasConflict(conflicts, "a", "c") {
t.Fatal("multiple conflicts with one node were not retained")
}
}
func TestPositionConflictMergeUsesModernShallowOverwrite(t *testing.T) {
got := shallowMergePositionConflicts(
positionConflicts{
"a": {"b": true, "c": true},
"e": {"f": true},
},
positionConflicts{"a": {"d": true}},
)
if hasConflict(got, "a", "b") || hasConflict(got, "a", "c") || !hasConflict(got, "a", "d") {
t.Fatalf("colliding conflict key was not replaced: %#v", got)
}
if !hasConflict(got, "e", "f") {
t.Fatalf("non-colliding conflict key was lost: %#v", got)
}
}
func TestVerticalAlignment(t *testing.T) {
t.Run("ignores neighbors missing from the layering", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("outside", Attrs{})
g.SetNode("inside", Attrs{"rank": float64(0), "order": float64(0)})
g.SetEdge("outside", "inside")
got := verticalAlignment(g, [][]string{{"inside"}}, positionConflicts{}, g.Predecessors)
if got.root["inside"] != "inside" || got.align["inside"] != "inside" {
t.Fatalf("missing neighbor changed alignment: %#v", got)
}
})
t.Run("self without adjacencies", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0)})
g.SetNode("b", Attrs{"rank": float64(1), "order": float64(0)})
got := verticalAlignment(g, buildLayerMatrix(g), positionConflicts{}, g.Predecessors)
requirePositionStringMap(t, got.root, map[string]string{"a": "a", "b": "b"})
requirePositionStringMap(t, got.align, map[string]string{"a": "a", "b": "b"})
})
t.Run("sole adjacency", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0)})
g.SetNode("b", Attrs{"rank": float64(1), "order": float64(0)})
g.SetEdge("a", "b")
got := verticalAlignment(g, buildLayerMatrix(g), positionConflicts{}, g.Predecessors)
requirePositionStringMap(t, got.root, map[string]string{"a": "a", "b": "a"})
requirePositionStringMap(t, got.align, map[string]string{"a": "b", "b": "a"})
})
t.Run("left median", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0)})
g.SetNode("b", Attrs{"rank": float64(0), "order": float64(1)})
g.SetNode("c", Attrs{"rank": float64(1), "order": float64(0)})
g.SetEdge("a", "c")
g.SetEdge("b", "c")
got := verticalAlignment(g, buildLayerMatrix(g), positionConflicts{}, g.Predecessors)
requirePositionStringMap(t, got.root, map[string]string{"a": "a", "b": "b", "c": "a"})
requirePositionStringMap(t, got.align, map[string]string{"a": "c", "b": "b", "c": "a"})
})
t.Run("node name and insertion order", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("b", Attrs{"rank": float64(0), "order": float64(1)})
g.SetNode("c", Attrs{"rank": float64(1), "order": float64(0)})
g.SetNode("z", Attrs{"rank": float64(0), "order": float64(0)})
g.SetEdge("z", "c")
g.SetEdge("b", "c")
got := verticalAlignment(g, buildLayerMatrix(g), positionConflicts{}, g.Predecessors)
requirePositionStringMap(t, got.root, map[string]string{"z": "z", "b": "b", "c": "z"})
requirePositionStringMap(t, got.align, map[string]string{"z": "c", "b": "b", "c": "z"})
})
t.Run("right median when left unavailable", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0)})
g.SetNode("b", Attrs{"rank": float64(0), "order": float64(1)})
g.SetNode("c", Attrs{"rank": float64(1), "order": float64(0)})
g.SetEdge("a", "c")
g.SetEdge("b", "c")
conflicts := positionConflicts{}
addConflict(conflicts, "a", "c")
got := verticalAlignment(g, buildLayerMatrix(g), conflicts, g.Predecessors)
requirePositionStringMap(t, got.root, map[string]string{"a": "a", "b": "b", "c": "b"})
requirePositionStringMap(t, got.align, map[string]string{"a": "a", "b": "c", "c": "b"})
})
t.Run("neither median available", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0)})
g.SetNode("b", Attrs{"rank": float64(0), "order": float64(1)})
g.SetNode("c", Attrs{"rank": float64(1), "order": float64(0)})
g.SetNode("d", Attrs{"rank": float64(1), "order": float64(1)})
g.SetEdge("a", "d")
g.SetEdge("b", "c")
g.SetEdge("b", "d")
got := verticalAlignment(g, buildLayerMatrix(g), positionConflicts{}, g.Predecessors)
requirePositionStringMap(t, got.root, map[string]string{"a": "a", "b": "b", "c": "b", "d": "d"})
requirePositionStringMap(t, got.align, map[string]string{"a": "a", "b": "c", "c": "b", "d": "d"})
})
t.Run("odd number of adjacencies", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0)})
g.SetNode("b", Attrs{"rank": float64(0), "order": float64(1)})
g.SetNode("c", Attrs{"rank": float64(0), "order": float64(2)})
g.SetNode("d", Attrs{"rank": float64(1), "order": float64(0)})
g.SetEdge("a", "d")
g.SetEdge("b", "d")
g.SetEdge("c", "d")
got := verticalAlignment(g, buildLayerMatrix(g), positionConflicts{}, g.Predecessors)
requirePositionStringMap(t, got.root, map[string]string{"a": "a", "b": "b", "c": "c", "d": "b"})
requirePositionStringMap(t, got.align, map[string]string{"a": "a", "b": "d", "c": "c", "d": "b"})
})
t.Run("blocks across layers", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0)})
g.SetNode("b", Attrs{"rank": float64(1), "order": float64(0)})
g.SetNode("c", Attrs{"rank": float64(1), "order": float64(1)})
g.SetNode("d", Attrs{"rank": float64(2), "order": float64(0)})
g.SetPath([]string{"a", "b", "d"})
g.SetPath([]string{"a", "c", "d"})
got := verticalAlignment(g, buildLayerMatrix(g), positionConflicts{}, g.Predecessors)
requirePositionStringMap(t, got.root, map[string]string{"a": "a", "b": "a", "c": "c", "d": "a"})
requirePositionStringMap(t, got.align, map[string]string{"a": "b", "b": "d", "c": "c", "d": "a"})
})
}
func TestHorizontalCompaction(t *testing.T) {
t.Run("single node at origin", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0)})
xs := horizontalCompaction(g, buildLayerMatrix(g), map[string]string{"a": "a"}, map[string]string{"a": "a"})
requirePositionFloatMap(t, xs, map[string]float64{"a": 0})
})
t.Run("node separation", func(t *testing.T) {
g := newBKTestGraph()
asAttrs(g.Graph())["nodesep"] = float64(100)
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0), "width": float64(100)})
g.SetNode("b", Attrs{"rank": float64(0), "order": float64(1), "width": float64(200)})
xs := horizontalCompaction(g, buildLayerMatrix(g), map[string]string{"a": "a", "b": "b"}, map[string]string{"a": "a", "b": "b"})
requirePositionFloatMap(t, xs, map[string]float64{"a": 0, "b": 100.0/2 + 100 + 200.0/2})
})
t.Run("edge separation", func(t *testing.T) {
g := newBKTestGraph()
asAttrs(g.Graph())["edgesep"] = float64(20)
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0), "width": float64(100), "dummy": true})
g.SetNode("b", Attrs{"rank": float64(0), "order": float64(1), "width": float64(200), "dummy": true})
xs := horizontalCompaction(g, buildLayerMatrix(g), map[string]string{"a": "a", "b": "b"}, map[string]string{"a": "a", "b": "b"})
requirePositionFloatMap(t, xs, map[string]float64{"a": 0, "b": 100.0/2 + 20 + 200.0/2})
})
t.Run("same block centers", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0), "width": float64(100)})
g.SetNode("b", Attrs{"rank": float64(1), "order": float64(0), "width": float64(200)})
xs := horizontalCompaction(g, buildLayerMatrix(g), map[string]string{"a": "a", "b": "a"}, map[string]string{"a": "b", "b": "a"})
requirePositionFloatMap(t, xs, map[string]float64{"a": 0, "b": 0})
})
t.Run("block separation", func(t *testing.T) {
g := newBKTestGraph()
asAttrs(g.Graph())["nodesep"] = float64(75)
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0), "width": float64(100)})
g.SetNode("b", Attrs{"rank": float64(1), "order": float64(1), "width": float64(200)})
g.SetNode("c", Attrs{"rank": float64(1), "order": float64(0), "width": float64(50)})
xs := horizontalCompaction(g, buildLayerMatrix(g), map[string]string{"a": "a", "b": "a", "c": "c"}, map[string]string{"a": "b", "b": "a", "c": "c"})
want := 50.0/2 + 75 + 200.0/2
requirePositionFloatMap(t, xs, map[string]float64{"a": want, "b": want, "c": 0})
})
t.Run("class separation", func(t *testing.T) {
g := newBKTestGraph()
asAttrs(g.Graph())["nodesep"] = float64(75)
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0), "width": float64(100)})
g.SetNode("b", Attrs{"rank": float64(0), "order": float64(1), "width": float64(200)})
g.SetNode("c", Attrs{"rank": float64(1), "order": float64(0), "width": float64(50)})
g.SetNode("d", Attrs{"rank": float64(1), "order": float64(1), "width": float64(80)})
xs := horizontalCompaction(g, buildLayerMatrix(g),
map[string]string{"a": "a", "b": "b", "c": "c", "d": "b"},
map[string]string{"a": "a", "b": "d", "c": "c", "d": "b"})
b := 100.0/2 + 75 + 200.0/2
requirePositionFloatMap(t, xs, map[string]float64{
"a": 0, "b": b, "c": b - 80.0/2 - 75 - 50.0/2, "d": b,
})
})
for _, tc := range []struct {
name string
bWidth float64
dWidth float64
wantB float64
}{
{"maximum separation from first layer", 150, 70, 50.0/2 + 75 + 150.0/2},
{"maximum separation from second layer", 70, 150, 60.0/2 + 75 + 150.0/2},
} {
t.Run(tc.name, func(t *testing.T) {
g := newBKTestGraph()
asAttrs(g.Graph())["nodesep"] = float64(75)
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0), "width": float64(50)})
g.SetNode("b", Attrs{"rank": float64(0), "order": float64(1), "width": tc.bWidth})
g.SetNode("c", Attrs{"rank": float64(1), "order": float64(0), "width": float64(60)})
g.SetNode("d", Attrs{"rank": float64(1), "order": float64(1), "width": tc.dWidth})
xs := horizontalCompaction(g, buildLayerMatrix(g),
map[string]string{"a": "a", "b": "b", "c": "a", "d": "b"},
map[string]string{"a": "c", "b": "d", "c": "a", "d": "b"})
requirePositionFloatMap(t, xs, map[string]float64{"a": 0, "b": tc.wantB, "c": 0, "d": tc.wantB})
})
}
t.Run("cascaded class shift", func(t *testing.T) {
g := newBKTestGraph()
asAttrs(g.Graph())["nodesep"] = float64(75)
for _, node := range []struct {
v string
rank, order float64
}{
{"a", 0, 0}, {"b", 0, 1}, {"c", 1, 0}, {"d", 1, 1},
{"e", 1, 2}, {"f", 2, 0}, {"g", 2, 1},
} {
g.SetNode(node.v, Attrs{"rank": node.rank, "order": node.order, "width": float64(50)})
}
xs := horizontalCompaction(g, buildLayerMatrix(g),
map[string]string{"a": "a", "b": "b", "c": "c", "d": "d", "e": "b", "f": "f", "g": "d"},
map[string]string{"a": "a", "b": "e", "c": "c", "d": "g", "e": "b", "f": "f", "g": "d"})
sep := 50.0/2 + 75 + 50.0/2
if xs["a"] != xs["b"]-sep || xs["b"] != xs["e"] || xs["c"] != xs["f"] ||
xs["d"] != xs["c"]+sep || xs["e"] != xs["d"]+sep || xs["g"] != xs["f"]+sep {
t.Fatalf("unexpected cascade: %#v", xs)
}
})
for _, tc := range []struct {
labelPos string
wantB float64
wantCInc float64
}{
{"l", 100.0/2 + 50 + 200, 0 + 50 + 300.0/2},
{"c", 100.0/2 + 50 + 200.0/2, 200.0/2 + 50 + 300.0/2},
{"r", 100.0/2 + 50 + 0, 200 + 50 + 300.0/2},
} {
t.Run("labelpos "+tc.labelPos, func(t *testing.T) {
g := newBKTestGraph()
asAttrs(g.Graph())["edgesep"] = float64(50)
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0), "width": float64(100), "dummy": "edge"})
g.SetNode("b", Attrs{"rank": float64(0), "order": float64(1), "width": float64(200), "dummy": "edge-label", "labelpos": tc.labelPos})
g.SetNode("c", Attrs{"rank": float64(0), "order": float64(2), "width": float64(300), "dummy": "edge"})
xs := horizontalCompaction(g, buildLayerMatrix(g),
map[string]string{"a": "a", "b": "b", "c": "c"},
map[string]string{"a": "a", "b": "b", "c": "c"})
requirePositionFloatMap(t, xs, map[string]float64{"a": 0, "b": tc.wantB, "c": tc.wantB + tc.wantCInc})
})
}
}
func TestBuildBlockGraphUsesJavaScriptFallbackForNaNPreviousMaximum(t *testing.T) {
g := newBKTestGraph()
asAttrs(g.Graph())["nodesep"] = float64(0)
g.SetNode("a", Attrs{"width": math.NaN()})
g.SetNode("b", Attrs{"width": float64(0)})
g.SetNode("c", Attrs{"width": float64(0)})
g.SetNode("d", Attrs{"width": float64(0)})
root := map[string]string{"a": "left", "b": "right", "c": "left", "d": "right"}
blockGraph := buildBlockGraph(g, [][]string{{"a", "b"}, {"c", "d"}}, root, false)
if got := number(blockGraph.EdgeByArgs("left", "right")); got != 0 {
t.Fatalf("replaced NaN maximum = %v, want 0", got)
}
}
func TestAlignCoordinates(t *testing.T) {
t.Run("single node", func(t *testing.T) {
xss := positionAlignments{
"ul": {"a": 50}, "ur": {"a": 100}, "dl": {"a": 50}, "dr": {"a": 200},
}
alignCoordinates(xss, xss["ul"])
for alignment, xs := range xss {
requirePositionFloatMap(t, xs, map[string]float64{"a": 50})
_ = alignment
}
})
t.Run("multiple nodes", func(t *testing.T) {
xss := positionAlignments{
"ul": {"a": 50, "b": 1000}, "ur": {"a": 100, "b": 900},
"dl": {"a": 150, "b": 800}, "dr": {"a": 200, "b": 700},
}
alignCoordinates(xss, xss["ul"])
requirePositionFloatMap(t, xss["ul"], map[string]float64{"a": 50, "b": 1000})
requirePositionFloatMap(t, xss["ur"], map[string]float64{"a": 200, "b": 1000})
requirePositionFloatMap(t, xss["dl"], map[string]float64{"a": 50, "b": 700})
requirePositionFloatMap(t, xss["dr"], map[string]float64{"a": 500, "b": 1000})
})
}
func TestFindSmallestWidthAlignment(t *testing.T) {
t.Run("smallest width", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("a", Attrs{"width": float64(50)})
g.SetNode("b", Attrs{"width": float64(50)})
xss := positionAlignments{
"ul": {"a": 0, "b": 1000}, "ur": {"a": -5, "b": 1000},
"dl": {"a": 5, "b": 2000}, "dr": {"a": 0, "b": 200},
}
requirePositionFloatMap(t, findSmallestWidthAlignment(g, xss), xss["dr"])
})
t.Run("node width", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("a", Attrs{"width": float64(50)})
g.SetNode("b", Attrs{"width": float64(50)})
g.SetNode("c", Attrs{"width": float64(200)})
xss := positionAlignments{
"ul": {"a": 0, "b": 100, "c": 75}, "ur": {"a": 0, "b": 100, "c": 80},
"dl": {"a": 0, "b": 100, "c": 85}, "dr": {"a": 0, "b": 100, "c": 90},
}
requirePositionFloatMap(t, findSmallestWidthAlignment(g, xss), xss["ul"])
})
}
func TestBalance(t *testing.T) {
for _, tc := range []struct {
name string
xss positionAlignments
want map[string]float64
}{
{
"shared median",
positionAlignments{"ul": {"a": 0}, "ur": {"a": 100}, "dl": {"a": 100}, "dr": {"a": 200}},
map[string]float64{"a": 100},
},
{
"average different medians",
positionAlignments{"ul": {"a": 0}, "ur": {"a": 75}, "dl": {"a": 125}, "dr": {"a": 200}},
map[string]float64{"a": 100},
},
{
"multiple nodes",
positionAlignments{
"ul": {"a": 0, "b": 50}, "ur": {"a": 75, "b": 0},
"dl": {"a": 125, "b": 60}, "dr": {"a": 200, "b": 75},
},
map[string]float64{"a": 100, "b": 55},
},
} {
t.Run(tc.name, func(t *testing.T) {
requirePositionFloatMap(t, balance(tc.xss, ""), tc.want)
})
}
}
func TestPositionX(t *testing.T) {
t.Run("single node", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0), "width": float64(100)})
requirePositionFloatMap(t, positionX(g), map[string]float64{"a": 0})
})
t.Run("single node block", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0), "width": float64(100)})
g.SetNode("b", Attrs{"rank": float64(1), "order": float64(0), "width": float64(100)})
g.SetEdge("a", "b")
requirePositionFloatMap(t, positionX(g), map[string]float64{"a": 0, "b": 0})
})
t.Run("single block with differing sizes", func(t *testing.T) {
g := newBKTestGraph()
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0), "width": float64(40)})
g.SetNode("b", Attrs{"rank": float64(1), "order": float64(0), "width": float64(500)})
g.SetNode("c", Attrs{"rank": float64(2), "order": float64(0), "width": float64(20)})
g.SetPath([]string{"a", "b", "c"})
requirePositionFloatMap(t, positionX(g), map[string]float64{"a": 0, "b": 0, "c": 0})
})
t.Run("predecessor centered over same-sized nodes", func(t *testing.T) {
g := newBKTestGraph()
asAttrs(g.Graph())["nodesep"] = float64(10)
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0), "width": float64(20)})
g.SetNode("b", Attrs{"rank": float64(1), "order": float64(0), "width": float64(50)})
g.SetNode("c", Attrs{"rank": float64(1), "order": float64(1), "width": float64(50)})
g.SetEdge("a", "b")
g.SetEdge("a", "c")
got := positionX(g)
a := got["a"]
requirePositionFloatMap(t, got, map[string]float64{"a": a, "b": a - 30, "c": a + 30})
})
t.Run("blocks on both sides", func(t *testing.T) {
g := newBKTestGraph()
asAttrs(g.Graph())["nodesep"] = float64(10)
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0), "width": float64(50)})
g.SetNode("b", Attrs{"rank": float64(0), "order": float64(1), "width": float64(60)})
g.SetNode("c", Attrs{"rank": float64(1), "order": float64(0), "width": float64(70)})
g.SetNode("d", Attrs{"rank": float64(1), "order": float64(1), "width": float64(80)})
g.SetEdge("b", "c")
got := positionX(g)
b := got["b"]
requirePositionFloatMap(t, got, map[string]float64{
"a": b - 60.0/2 - 10 - 50.0/2, "b": b, "c": b, "d": b + 70.0/2 + 10 + 80.0/2,
})
})
t.Run("inner segments", func(t *testing.T) {
g := newBKTestGraph()
asAttrs(g.Graph())["nodesep"] = float64(10)
// Layout supplies both separations before calling positionX. Making the
// edge separation explicit also avoids the upstream test's accidental
// undefined/NaN arithmetic while preserving its intended assertions.
asAttrs(g.Graph())["edgesep"] = float64(10)
g.SetNode("a", Attrs{"rank": float64(0), "order": float64(0), "width": float64(50), "dummy": true})
g.SetNode("b", Attrs{"rank": float64(0), "order": float64(1), "width": float64(60)})
g.SetNode("c", Attrs{"rank": float64(1), "order": float64(0), "width": float64(70)})
g.SetNode("d", Attrs{"rank": float64(1), "order": float64(1), "width": float64(80), "dummy": true})
g.SetEdge("b", "c")
g.SetEdge("a", "d")
got := positionX(g)
a := got["a"]
requirePositionFloatMap(t, got, map[string]float64{
"a": a, "b": a + 50.0/2 + 10 + 60.0/2,
"c": a - 70.0/2 - 10 - 80.0/2, "d": a,
})
})
}