-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathsliding_max_test.go
55 lines (46 loc) · 1.37 KB
/
sliding_max_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
47
48
49
50
51
52
53
54
55
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 slice of integers and a positive integer k, return the maximum of each sub-slice of the
slice when a window of length k is moved from left to right and we can only see k elements that
are in the window.
For example given {1,4,5,-2,4,6} and k=3, we will have the following windows:
{1,4,5} -> 5
{4,5,-2} -> 5
{5,-2,4} -> 5
{-2,4,6} -> 6
So return {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)
}
}