-
Notifications
You must be signed in to change notification settings - Fork 2
/
stack_test.go
72 lines (68 loc) · 2.05 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
package jzon
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestStack(t *testing.T) {
t.Run("empty", func(t *testing.T) {
s := stackPool.Get().(*stack).init()
require.Equal(t, stackElementNone, s.top())
require.Equal(t, stackElementNone, s.pop())
})
t.Run("array top", func(t *testing.T) {
s := stackPool.Get().(*stack).initArray()
require.Equal(t, stackElementArrayBegin, s.top())
require.Equal(t, stackElementArray, s.pop())
require.Equal(t, stackElementNone, s.top())
require.Equal(t, stackElementNone, s.pop())
})
t.Run("object top", func(t *testing.T) {
s := stackPool.Get().(*stack).initObject()
require.Equal(t, stackElementObjectBegin, s.top())
require.Equal(t, stackElementObject, s.pop())
require.Equal(t, stackElementNone, s.top())
require.Equal(t, stackElementNone, s.pop())
})
t.Run("nested 1", func(t *testing.T) {
s := stackPool.Get().(*stack).init()
count := 100
for i := 0; i < count; i++ {
if i&1 == 0 {
s.pushObject()
require.Equal(t, stackElementObjectBegin, s.top())
} else {
s.pushArray()
require.Equal(t, stackElementArrayBegin, s.top())
}
}
for i := count - 1; i >= 0; i-- {
if i&1 == 0 {
require.Equal(t, stackElementObjectBegin, s.top())
require.Equal(t, stackElementObject, s.pop())
} else {
require.Equal(t, stackElementArrayBegin, s.top())
require.Equal(t, stackElementArray, s.pop())
}
}
require.Equal(t, stackElementNone, s.top())
require.Equal(t, stackElementNone, s.pop())
})
}
func TestStack_Init(t *testing.T) {
t.Run("object", func(t *testing.T) {
var s stack
s.initObject()
require.Equal(t, stackElementObjectBegin, s.top())
require.Equal(t, stackElementObject, s.pop())
require.Equal(t, stackElementNone, s.top())
require.Equal(t, stackElementNone, s.pop())
})
t.Run("array", func(t *testing.T) {
var s stack
s.initArray()
require.Equal(t, stackElementArrayBegin, s.top())
require.Equal(t, stackElementArray, s.pop())
require.Equal(t, stackElementNone, s.top())
require.Equal(t, stackElementNone, s.pop())
})
}