Improved stack

This commit is contained in:
Ivan Daniluk 2015-05-02 09:35:39 +03:00
parent 4a6ead5d7e
commit 349b3c056f
2 changed files with 34 additions and 0 deletions

View File

@ -53,8 +53,16 @@ func (s *Stack) IntValues() []int {
continue
}
f, ok := v.(float64)
if ok {
// 12.34 (float) -> 1234 (int)
ret[i] = int(f * 100)
continue
}
b, ok := v.(bool)
if ok {
// false => 0, true = 1
if b {
ret[i] = 1
} else {

View File

@ -24,4 +24,30 @@ func TestStack(t *testing.T) {
if s.Front().(int) != 14 {
t.Fatalf("Front returns wrong value: expecting %d, got %d", 14, s.Front())
}
s1 := NewStackWithSize(3)
s1.Push(true)
s1.Push(false)
s1.Push(true)
ints1 := s1.IntValues()
if len(ints1) != 3 {
t.Fatal("expecting len of to be %d, but got %d", 3, len(ints1))
}
if ints1[0] != 1 || ints1[1] != 0 || ints1[2] != 1 {
t.Fatal("bool values converted to int incorrectly: %v", ints1)
}
s2 := NewStackWithSize(3)
s2.Push(0.1)
s2.Push(0.5)
s2.Push(0.03)
ints2 := s2.IntValues()
if len(ints2) != 3 {
t.Fatal("expecting len to be %d, but got %d", 3, len(ints2))
}
if ints2[0] != 10 || ints2[1] != 50 || ints2[2] != 3 {
t.Fatal("float values converted to int incorrectly: %v", ints2)
}
}