forked from hailocab/go-geoindex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_test.go
73 lines (56 loc) · 1.23 KB
/
queue_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package geoindex
import (
"github.com/stretchr/testify/assert"
"math/rand"
"testing"
)
const (
numIterations = 1000
)
func TestQueue(t *testing.T) {
queue := newQueue(1)
assert.True(t, queue.IsEmpty())
assert.Equal(t, queue.Size(), 0)
queue.Push(1)
assert.False(t, queue.IsEmpty())
assert.Equal(t, queue.Size(), 1)
queue.Push(2)
assert.Equal(t, queue.Size(), 2)
assert.Equal(t, queue.Peek().(int), 1)
assert.Equal(t, queue.PeekBack().(int), 2)
assert.Equal(t, queue.Pop().(int), 1)
assert.Equal(t, queue.Pop().(int), 2)
for i := 1; i < 13; i++ {
queue.Push(i)
}
assert.Equal(t, queue.Peek().(int), 1)
assert.Equal(t, queue.PeekBack().(int), 12)
for i := 1; i < 10; i++ {
assert.Equal(t, queue.Pop().(int), i)
}
for i := 1; i < 10; i++ {
queue.Push(i)
}
for i := 10; i < 13; i++ {
assert.Equal(t, queue.Pop().(int), i)
}
for i := 1; i < 10; i++ {
assert.Equal(t, queue.Pop().(int), i)
}
}
func TestQueueRandomly(t *testing.T) {
testRandomly(2, 1)
testRandomly(3, 2)
testRandomly(3, 1)
}
func testRandomly(mod int, pushThreshold int) {
queue := newQueue(1)
for i := 1; i < numIterations; i++ {
op := rand.Int() % mod
if op < pushThreshold {
queue.Push(i)
} else {
queue.Pop()
}
}
}