forked from spring1843/go-dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sliding_maximum_test.go
46 lines (40 loc) · 1.22 KB
/
sliding_maximum_test.go
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
package heap
import (
"container/heap"
"slices"
"testing"
)
/*
TestMaxSlidingWindow tests solution(s) with the following signature and problem description:
func MaxSlidingWindow(numbers []int, k int) []int
Given a list of integers like {1, 4, 5, -2, 4, 6}, and a positive integer k like 3, return
the maximum of each slice of the array when a window of length k is moved from left to the
right in the array like {5, 5, 5, 6}.
*/
func TestMaxSlidingWindow(t *testing.T) {
tests := []struct {
numbers []int
k int
maxSliding []int
}{
{[]int{}, 2, []int{}},
{[]int{1, 4, 5, -2, 4, 6}, 1, []int{1, 4, 5, -2, 4, 6}},
{[]int{1, 4, 5, -2, 4, 6}, 2, []int{4, 5, 5, 4, 6}},
{[]int{1, 4, 5, -2, 4, 6}, 3, []int{5, 5, 5, 6}},
{[]int{1, 4, 5, -2, 4, 6}, 4, []int{5, 5, 6}},
{[]int{1, 4, 5, -2, 4, 6}, 6, []int{6}},
}
for i, test := range tests {
if got := MaxSlidingWindow(test.numbers, test.k); !slices.Equal(got, test.maxSliding) {
t.Fatalf("Failed test case #%d. Want %d got %d", i, test.maxSliding, got)
}
}
}
func TestMaxSlidingWindowPop(t *testing.T) {
pq := make(slidingWindow, 5)
heap.Init(&pq)
heap.Push(&pq, 5)
if got := heap.Pop(&pq).(int); got != 5 {
t.Fatalf("Wanted %d got %d", got, 5)
}
}