stack.go
package stack
import "errors"
type Stack []interface {}
func (stack Stack) Len() int {
return len(stack)
}
func (stack Stack) IsEmpty() bool {
return len(stack) == 0
}
func (stack Stack) Cap() int {
return cap(stack)
}
func (stack *Stack) Push(value interface{}) {
*stack = append(*stack, value)
}
func (stack Stack) Top() (interface{}, error) {
if len(stack) == 0 {
return nil, errors.New("Out of index, len is 0")
}
return stack[len(stack) - 1], nil
}
func (stack *Stack) Pop() (interface{}, error) {
theStack := *stack
if len(theStack) == 0 {
return nil, errors.New("Out of index, len is 0")
}
value := theStack[len(theStack) - 1]
*stack = theStack[:len(theStack) - 1]
return value, nil
}
stack_test.go
package stack
import (
"testing"
)
func TestStack_Len(t *testing.T) {
var myStack Stack
myStack.Push(1)
myStack.Push("test")
if myStack.Len() == 2 {
t.Log("Pass Stack.Len")
} else {
t.Error("Failed Stack.Len")
}
}
func TestStack_IsEmpty(t *testing.T) {
var mStack Stack
if mStack.IsEmpty() {
t.Log("Pass Stack.IsEmpty")
} else {
t.Error("Failed Stack.IsEmpty")
}
}
func TestStack_Cap(t *testing.T) {
myStack := make(Stack, 3)
if myStack.Cap() == 3 {
t.Log("Pass Stack.Cap")
} else {
t.Error("Failed Stack.Cap")
}
}
func TestStack_Push(t *testing.T) {
var mStack Stack
mStack.Push(3)
if mStack.Len() == 1 {
t.Log("Pass Stack.Push")
} else {
t.Error("Failed Stack.Push")
}
}
func TestStack_Top(t *testing.T) {
var mStack Stack
if _, err := mStack.Top(); err == nil {
t.Error("Failed Stack.Top")
}
mStack.Push(3)
if value, _ := mStack.Top(); value == 3 {
t.Log("Pass Stack.Top")
} else {
t.Errorf("Failed Stack.Top, value is %d", value)
}
}
func TestStack_Pop(t *testing.T) {
var mStack Stack
if _, err := mStack.Pop(); err == nil {
t.Error("Failed Stack.Top")
}
mStack.Push("test")
mStack.Push(3)
if value, _ := mStack.Pop(); value == 3 && mStack.Len() == 1 {
t.Log("Pass Stack.Pop")
} else {
t.Errorf("Failed Stack.Pop, value is %d, len is %d", value, mStack.Len())
}
}