Middle
Write tests to check boundary cases for the maximum distance problem solution.
sobes.tech AI
Answer from AI
For testing boundary cases of the maximum distance problem, it is important to check:
- Empty input (no points)
- One point
- Two points
- Multiple points with the same coordinates
- The maximum possible number of points
- Points with negative coordinates
Example tests in Go:
package main
import (
"testing"
)
func TestMaxDistance(t *testing.T) {
tests := []struct {
name string
points [][2]int
want float64
}{
{"empty input", nil, 0},
{"single point", [][2]int{{0, 0}}, 0},
{"two points", [][2]int{{0, 0}, {3, 4}}, 5},
{"identical points", [][2]int{{1, 1}, {1, 1}, {1, 1}}, 0},
{"negative coordinates", [][2]int{{-1, -1}, {2, 3}}, 5},
// Add a test with a large number of points if needed
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := MaxDistance(tt.points)
if got != tt.want {
t.Errorf("MaxDistance() = %v, want %v", got, tt.want)
}
})
}
}
// Example function to compute the maximum distance
func MaxDistance(points [][2]int) float64 {
maxDist := 0.0
for i := 0; i < len(points); i++ {
for j := i + 1; j < len(points); j++ {
dx := points[i][0] - points[j][0]
dy := points[i][1] - points[j][1]
dist := float64(dx*dx + dy*dy)
if dist > maxDist {
maxDist = dist
}
}
}
return maxDist
}
It is important to verify the correctness of the function with minimal and maximal input data, as well as in special cases (for example, when all points coincide).