General Stack Tester
package genstack import ( "fmt" "testing" "time" "math/rand" ) func TestSingle(t *testing.T) { fmt.Println("Single test") var s Stack if !s.Empty() { t.Error("New stack not empty.") } s.Push(23) if s.Empty() { t.Error("Singleton stack empty.") } r,ok := s.Pop() if ok { if r.(int) != 23 { t.Error("Incorrect value popped from singleton stack. ", r.(int), "instead of 23.") } } else { t.Error("Unable to pop singleton stack.") } if !s.Empty() { t.Error("Popped singleton stack not empty.") } } func TestReverse(t *testing.T) { fmt.Println("Reverse test") const N = 40 rand.Seed(time.Now().UnixNano()) // Push some random numbers onto the stack, and into an array. var s1 Stack values := make([]int,0) for i := 0; i < N; i++ { val := rand.Intn(50000) - 100000 s1.Push(val) values = append(values, val) } // See if the same values come off in reverse order. for i := N-1; i >= 0; i-- { val,ok := s1.Pop() if ok { if val.(int) != values[i] { t.Error("Incorrect popped value") } } else { t.Error("Stack pop should not have failed.") } } if !s1.Empty() { t.Error("Stack should be empty") } }