1
0
Fork 0

Unit test, and correct, stack

master
Philip O Toole 8 years ago
parent 728bb09b15
commit 84d900443e

@ -25,7 +25,7 @@ func (s *stack) pop() rune {
return rune(0) return rune(0)
} }
c := s.c[len(s.c)-1] c := s.c[len(s.c)-1]
s.c = s.c[:len(s.c)] s.c = s.c[:len(s.c)-1]
return c return c
} }

@ -10,3 +10,55 @@ func Test_ScannerNew(t *testing.T) {
t.Fatalf("failed to create basic Scanner") t.Fatalf("failed to create basic Scanner")
} }
} }
func Test_stackEmpty(t *testing.T) {
s := newStack()
if !s.empty() {
t.Fatal("new stack is not empty")
}
if s.peek() != rune(0) {
t.Fatal("peek of empty stack does not return correct value")
}
}
func Test_stackSingle(t *testing.T) {
s := newStack()
s.push('x')
if s.empty() {
t.Fatal("non-empty stack marked as empty")
}
if s.peek() != 'x' {
t.Fatal("peek of single stack does not return correct value")
}
if s.pop() != 'x' {
t.Fatal("pop of single stack does not return correct value")
}
if !s.empty() {
t.Fatal("popped stack is not empty")
}
}
func Test_stackMulti(t *testing.T) {
s := newStack()
s.push('x')
s.push('y')
s.push('z')
if s.pop() != 'z' {
t.Fatal("pop of 1st multi stack does not return correct value")
}
if s.pop() != 'y' {
t.Fatal("pop of 2nd multi stack does not return correct value")
}
if s.pop() != 'x' {
t.Fatal("pop of 3rd multi stack does not return correct value")
}
if !s.empty() {
t.Fatal("popped multi stack is not empty")
}
}

Loading…
Cancel
Save