forked from andeya/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_test.go
More file actions
90 lines (85 loc) · 2.89 KB
/
stack_test.go
File metadata and controls
90 lines (85 loc) · 2.89 KB
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
// Copyright © 2010-12 Qtrac Ltd.
//
// This program or package and any associated files are licensed under the
// Apache License, Version 2.0 (the "License"); you may not use these files
// except in compliance with the License. You can get a copy of the License
// at: http://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package stack_test
import (
"github.com/henrylee2cn/algorithm/evaler/stack"
"testing"
)
func TestStack(t *testing.T) {
count := 1
var aStack stack.Stack
assertTrue(t, aStack.Len() == 0, "expected empty Stack", count) // 1
count++
assertTrue(t, aStack.Cap() == 0, "expected empty Stack", count) // 2
count++
assertTrue(t, aStack.IsEmpty(), "expected empty Stack", count) // 3
count++
value, err := aStack.Pop()
assertTrue(t, value == nil, "expected nil value", count) // 4
count++
assertTrue(t, err != nil, "expected error", count) // 5
count++
value1, err := aStack.Top()
assertTrue(t, value1 == nil, "expected nil value", count) // 6
count++
assertTrue(t, err != nil, "expected error", count) // 7
count++
aStack.Push(1)
aStack.Push(2)
aStack.Push("three")
assertTrue(t, aStack.Len() == 3, "expected nonempty Stack", count) // 8
count++
assertTrue(t, aStack.IsEmpty() == false, "expected nonempty Stack",
count) // 9
count++
value2, err := aStack.Pop()
assertEqualString(t, value2.(string), "three", "unexpected text",
count) // 10
count++
assertTrue(t, err == nil, "no error expected", count) // 11
count++
value3, err := aStack.Top()
assertTrue(t, value3 == 2, "unexpected number", count) // 12
count++
assertTrue(t, err == nil, "no error expected", count) // 13
count++
aStack.Pop()
assertTrue(t, aStack.Len() == 1, "expected nonempty Stack", count) //14
count++
assertTrue(t, aStack.IsEmpty() == false, "expected nonempty Stack",
count) // 15
count++
value4, err := aStack.Pop()
assertTrue(t, value4 == 1, "unexpected number", count) // 16
count++
assertTrue(t, err == nil, "no error expected", count) // 17
count++
assertTrue(t, aStack.Len() == 0, "expected empty Stack", count) // 18
count++
assertTrue(t, aStack.IsEmpty(), "expected empty Stack", count) // 19
count++
}
// assertTrue() calls testing.T.Error() with the given message if the
// condition is false.
func assertTrue(t *testing.T, condition bool, message string, id int) {
if !condition {
t.Errorf("#%d: %s", id, message)
}
}
// assertEqualString() calls testing.T.Error() with the given message if
// the given strings are not equal.
func assertEqualString(t *testing.T, a, b string, message string, id int) {
if a != b {
t.Errorf("#%d: %s \"%s\" !=\n\"%s\"", id, message, a, b)
}
}