forked from spring1843/go-dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
median_in_a_stream_test.go
46 lines (40 loc) · 1009 Bytes
/
median_in_a_stream_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 "testing"
/*
TestMedianInAStream tests solution(s) with the following signatures and problem description:
func newMedianKeeper() medianKeeper
func (m *medianKeeper) addNumber(num int)
Given a stream of integers like {1,2,3,4}, output the median after each input
like {1, 1.5, 2, 2.5}.
*/
func TestMedianInAStream(t *testing.T) {
tests := []struct {
numbers []int
median float64
}{
{[]int{1}, 1},
{[]int{2, 3, 4}, 3},
{[]int{1, 2, 3, 4, 5}, 3},
{[]int{1, 2, 3, 4, 5, 6}, 3.5},
{[]int{6, 5, 4, 3, 2, 1}, 3.5},
{[]int{1, 4, 5, -2, 4, 6}, 4},
}
for i, test := range tests {
heap := newMedianKeeper()
for _, number := range test.numbers {
heap.addNumber(number)
}
got := heap.median()
if got != test.median {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.median, got)
}
}
}
func TestHeap(t *testing.T) {
minHeap := new(minHeap)
minHeap.Push(1)
got := minHeap.Pop().(int)
if got != 1 {
t.Fatalf("expected %d got %d", got, 1)
}
}