63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package pacificatlanticwaterflow
|
|
|
|
import (
|
|
"sort"
|
|
"testing"
|
|
)
|
|
|
|
func sortResult(result [][]int) {
|
|
sort.Slice(result, func(i, j int) bool {
|
|
if result[i][0] != result[j][0] {
|
|
return result[i][0] < result[j][0]
|
|
}
|
|
return result[i][1] < result[j][1]
|
|
})
|
|
}
|
|
|
|
func TestPacificAtlantic(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
heights [][]int
|
|
expected [][]int
|
|
}{
|
|
{
|
|
name: "5x5 grid",
|
|
heights: [][]int{
|
|
{1, 2, 2, 3, 5},
|
|
{3, 2, 3, 4, 4},
|
|
{2, 4, 5, 3, 1},
|
|
{6, 7, 1, 4, 5},
|
|
{5, 1, 1, 2, 4},
|
|
},
|
|
expected: [][]int{{0, 4}, {1, 3}, {1, 4}, {2, 2}, {3, 0}, {3, 1}, {4, 0}},
|
|
},
|
|
{
|
|
name: "3x2 all ones",
|
|
heights: [][]int{
|
|
{1, 1},
|
|
{1, 1},
|
|
{1, 1},
|
|
},
|
|
expected: [][]int{{0, 0}, {0, 1}, {1, 0}, {1, 1}, {2, 0}, {2, 1}},
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
result := PacificAtlantic(tc.heights)
|
|
sortResult(result)
|
|
sortResult(tc.expected)
|
|
|
|
if len(result) != len(tc.expected) {
|
|
t.Fatalf("expected %d results, got %d: %v", len(tc.expected), len(result), result)
|
|
}
|
|
|
|
for i := range tc.expected {
|
|
if result[i][0] != tc.expected[i][0] || result[i][1] != tc.expected[i][1] {
|
|
t.Errorf("result[%d] = %v, expected %v", i, result[i], tc.expected[i])
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|