-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_test.go
95 lines (83 loc) · 1.63 KB
/
stack_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package gspooling
import "testing"
func TestWrongBufferSize(t *testing.T) {
bff, err := newStackBuffer(-1)
if err != nil {
t.Log(err)
return
}
t.Log(bff.put(1))
t.Log("Buffer size: ", bff.csize)
}
func TestFullBuffer(t *testing.T) {
bff, _ := newStackBuffer(1)
err := bff.put(1)
err = bff.put(2)
if err != nil {
t.Log(err)
} else {
t.Error("Buffer should be full, something wrong happend.")
}
}
func TestPutDataIntoBuffer(t *testing.T) {
size := 10
bff, _ := newStackBuffer(size)
for i := 0; i < size; i++ {
err := bff.put(i + 1)
if err != nil {
t.Error(err)
return
} else {
t.Log(bff.stBff)
}
}
if bff.msize == bff.csize {
t.Log("Stack is full of data")
}
t.Log("Stack size: ", bff.csize)
}
func TestBufferEmpty(t *testing.T) {
bff, _ := newStackBuffer(10)
_, err := bff.get()
if err != nil {
t.Log(err)
} else {
t.Error("Stack is empty, but error does not came out.")
}
}
func TestPutThenGetDataBuffer(t *testing.T) {
size := 10
bff, _ := newStackBuffer(size)
for i := 0; i < size; i++ {
err := bff.put(i + 1)
if err != nil {
t.Error(err)
return
} else {
t.Log(bff.stBff)
}
}
for j := 0; j < size; j++ {
d, err := bff.get()
if err != nil {
t.Error(err)
return
} else {
t.Log("Data from the stack: ", d)
t.Log("stack: ", bff.stBff)
t.Log("stack size: ", bff.csize)
}
}
}
func TestFixBufferFunc(t *testing.T) {
rawbff := make([]interface{}, 10)
rawbff[9] = 1
rawbff[4] = 2
rawbff[1] = 3
fixStackBuffer(rawbff)
if rawbff[0] == 3 && rawbff[1] == 2 && rawbff[2] == 1 {
t.Log("fixed raw stack", rawbff)
} else {
t.Error("fixStack func error")
}
}