You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.2 KiB
Go

package math
import "testing"
func TestShoelaceArea(t *testing.T) {
tests := []struct {
name string
polygon []Point
want float64
}{
{
name: "Triangle",
polygon: []Point{
{X: 0, Y: 0},
{X: 0, Y: 4},
{X: 3, Y: 0},
},
want: 6.0,
},
{
name: "Quadrilateral",
polygon: []Point{
{X: 0, Y: 0},
{X: 0, Y: 4},
{X: 3, Y: 4},
{X: 3, Y: 0},
},
want: 12.0,
},
{
name: "Invalid Polygon",
polygon: []Point{
{X: 0, Y: 0},
{X: 0, Y: 4},
},
want: 0.0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ShoelaceArea(tt.polygon)
if got != tt.want {
t.Errorf("ShoelaceArea() = %v, want %v", got, tt.want)
}
})
}
}
func TestFloodFill(t *testing.T) {
matrix := Matrix[int64]{
{1, 1, 1, 1},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 1, 1, 1},
}
row := 1
col := 1
prevColor := int64(0)
newColor := int64(2)
height := 4
width := 4
floodFill(matrix, row, col, prevColor, newColor, height, width)
expectedMatrix := Matrix[int64]{
{1, 1, 1, 1},
{1, 2, 2, 1},
{1, 2, 2, 1},
{1, 1, 1, 1},
}
if !matrix.Equal(expectedMatrix) {
t.Errorf("FloodFill() failed, expected matrix: %v, got: %v", expectedMatrix, matrix)
}
}