2021-02-14 02:09:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func TestStackPop(t *testing.T) {
|
|
|
|
s := Stack{
|
|
|
|
values: []int{1, 2, 3},
|
|
|
|
}
|
|
|
|
i, err := s.Pop()
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err.Error())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if i != 1 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if len(s.values) != 2 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
s = Stack{
|
|
|
|
values: []int{},
|
|
|
|
}
|
|
|
|
i, err = s.Pop()
|
|
|
|
if err == nil {
|
|
|
|
t.Log("expected error")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if i != 0 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackPush(t *testing.T) {
|
|
|
|
s := Stack{
|
|
|
|
values: []int{1, 2, 3},
|
|
|
|
}
|
|
|
|
s.Push(4)
|
|
|
|
if len(s.values) != 4 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if s.values[0] != 4 || s.values[1] != 1 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
2021-02-20 20:52:27 +00:00
|
|
|
|
|
|
|
func TestStackPick(t *testing.T) {
|
|
|
|
s := Stack{}
|
|
|
|
i, err := s.Pick(0)
|
|
|
|
if err == nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if i != 0 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
s = Stack{
|
|
|
|
values: []int{1, 2, 3},
|
|
|
|
}
|
|
|
|
i, err = s.Pick(2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if i != 3 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|