This commit is contained in:
+12
-4
@@ -84,16 +84,24 @@ func MergePeers(existing []PeerNode, incoming []viz.Peer) []PeerNode {
|
||||
}
|
||||
|
||||
func EdgeSpawnPoint(w, h float64, seed int) Point {
|
||||
hSpan := int(h) - 80
|
||||
if hSpan < 1 {
|
||||
hSpan = 1
|
||||
}
|
||||
wSpan := int(w) - 40
|
||||
if wSpan < 1 {
|
||||
wSpan = 1
|
||||
}
|
||||
side := seed % 4
|
||||
switch side {
|
||||
case 0:
|
||||
return Point{X: -20, Y: 40 + float64((seed*17)%(int(h)-80))}
|
||||
return Point{X: -20, Y: 40 + float64((seed*17)%hSpan)}
|
||||
case 1:
|
||||
return Point{X: w + 20, Y: 40 + float64((seed*13)%(int(h)-80))}
|
||||
return Point{X: w + 20, Y: 40 + float64((seed*13)%hSpan)}
|
||||
case 2:
|
||||
return Point{X: 20 + float64((seed*11)%int(w-40)), Y: -20}
|
||||
return Point{X: 20 + float64((seed*11)%wSpan), Y: -20}
|
||||
default:
|
||||
return Point{X: 20 + float64((seed*19)%int(w-40)), Y: h + 20}
|
||||
return Point{X: 20 + float64((seed*19)%wSpan), Y: h + 20}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -81,3 +81,16 @@ func TestServicePulseColor(t *testing.T) {
|
||||
t.Fatal("fallback")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEdgeSpawnPointSmallDimensions(t *testing.T) {
|
||||
// Regression: int(h)-80 or int(w)-40 can be zero and panic on modulo.
|
||||
for _, dims := range [][2]float64{{40, 80}, {16, 16}, {1, 1}, {80, 24}} {
|
||||
w, h := dims[0], dims[1]
|
||||
for seed := 0; seed < 32; seed++ {
|
||||
p := EdgeSpawnPoint(w, h, seed)
|
||||
if math.IsNaN(p.X) || math.IsNaN(p.Y) || math.IsInf(p.X, 0) || math.IsInf(p.Y, 0) {
|
||||
t.Fatalf("bad point for w=%v h=%v seed=%d: %+v", w, h, seed, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user