2015-04-25 14:06:45 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func TestStack(t *testing.T) {
|
|
|
|
size := 10
|
2015-05-01 21:49:19 +08:00
|
|
|
s := NewStackWithSize(size)
|
2015-04-25 14:06:45 +08:00
|
|
|
|
|
|
|
for i := 0; i < size+5; i++ {
|
|
|
|
s.Push(i)
|
|
|
|
l := len(s.Values)
|
|
|
|
|
|
|
|
if l < size {
|
|
|
|
if l != i+1 {
|
2015-04-25 14:07:33 +08:00
|
|
|
t.Fatalf("len is incorrect. expecting %d, got %d", i, l)
|
2015-04-25 14:06:45 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if l != size {
|
2015-04-25 14:07:33 +08:00
|
|
|
t.Fatalf("len is incorrect. expecting %d, got %d", size, l)
|
2015-04-25 14:06:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-01 04:54:54 +08:00
|
|
|
|
2015-05-02 06:21:52 +08:00
|
|
|
if s.Front().(int) != 14 {
|
2015-05-01 04:54:54 +08:00
|
|
|
t.Fatalf("Front returns wrong value: expecting %d, got %d", 14, s.Front())
|
|
|
|
}
|
2015-05-02 14:35:39 +08:00
|
|
|
|
|
|
|
s1 := NewStackWithSize(3)
|
|
|
|
s1.Push(true)
|
|
|
|
s1.Push(false)
|
|
|
|
s1.Push(true)
|
|
|
|
|
|
|
|
ints1 := s1.IntValues()
|
|
|
|
if len(ints1) != 3 {
|
2015-05-03 23:03:49 +08:00
|
|
|
t.Fatalf("expecting len of to be %d, but got %d", 3, len(ints1))
|
2015-05-02 14:35:39 +08:00
|
|
|
}
|
|
|
|
if ints1[0] != 1 || ints1[1] != 0 || ints1[2] != 1 {
|
2015-05-03 23:03:49 +08:00
|
|
|
t.Fatalf("bool values converted to int incorrectly: %v", ints1)
|
2015-05-02 14:35:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
s2 := NewStackWithSize(3)
|
|
|
|
s2.Push(0.1)
|
|
|
|
s2.Push(0.5)
|
|
|
|
s2.Push(0.03)
|
|
|
|
|
|
|
|
ints2 := s2.IntValues()
|
|
|
|
if len(ints2) != 3 {
|
2015-05-03 23:03:49 +08:00
|
|
|
t.Fatalf("expecting len to be %d, but got %d", 3, len(ints2))
|
2015-05-02 14:35:39 +08:00
|
|
|
}
|
|
|
|
if ints2[0] != 10 || ints2[1] != 50 || ints2[2] != 3 {
|
2015-05-03 23:03:49 +08:00
|
|
|
t.Fatalf("float values converted to int incorrectly: %v", ints2)
|
2015-05-02 14:35:39 +08:00
|
|
|
}
|
2015-04-25 14:06:45 +08:00
|
|
|
}
|