-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfifo_test.go
56 lines (50 loc) · 1.54 KB
/
fifo_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
// Created by Yaz Saito on 06/15/12.
// Modified by Geert-Johan Riemer, Foize B.V.
package fifo
import (
"math/rand"
"testing"
)
//++ TODO: Add test for empty queue
//++ TODO: Find a way to test the thread-safety
//++ TODO: Add test for large queue
func testAssert(t *testing.T, b bool, objs ...interface{}) {
if !b {
t.Fatal(objs...)
}
}
func TestBasic(t *testing.T) {
q := NewQueue()
testAssert(t, q.Len() == 0, "Could not assert that new Queue has length zero (0).")
q.Add(10)
testAssert(t, q.Len() == 1, "Could not assert that Queue has lenght 1 after adding one item.")
testAssert(t, q.Next().(int) == 10, "Could not retrieve item from Queue correctly.")
testAssert(t, q.Len() == 0, "Could not assert that Queue has length 0 after retrieving item.")
q.Add(11)
testAssert(t, q.Len() == 1, "Could not assert that Queue has length 1 after adding one item the second time.")
testAssert(t, q.Next().(int) == 11, "Could not retrieve item from Queue correctly the second time.")
testAssert(t, q.Len() == 0, "Could not assert that Queue has length 0 after retrieving item the second time.")
}
func TestRandomized(t *testing.T) {
var first, last int
q := NewQueue()
for i := 0; i < 10000; i++ {
if rand.Intn(2) == 0 {
count := rand.Intn(128)
for j := 0; j < count; j++ {
q.Add(last)
last++
}
} else {
count := rand.Intn(128)
if count > (last - first) {
count = last - first
}
for i := 0; i < count; i++ {
testAssert(t, q.Len() > 0, "len==0", q.Len())
testAssert(t, q.Next().(int) == first)
first++
}
}
}
}