From b33870f09c896b649b3763662ef181268e137d7e Mon Sep 17 00:00:00 2001 From: Hideo Hattori Date: Fri, 25 Sep 2015 17:18:46 +0900 Subject: [PATCH 1/2] fix panic occurs when using Float value --- stack.go | 28 ++++++++++++++++++++++++++-- stack_test.go | 7 +++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/stack.go b/stack.go index fa35098..e14ae8e 100644 --- a/stack.go +++ b/stack.go @@ -35,12 +35,36 @@ func (s *Stack) Push(val VarValue) { switch val.(type) { case int64: - if s.Max == nil || val.(int64) > s.Max.(int64) { + if s.Max == nil { s.Max = val + return + } + + switch s.Max.(type) { + case int64: + if val.(int64) > s.Max.(int64) { + s.Max = val + } + case float64: + if float64(val.(int64)) > s.Max.(float64) { + s.Max = val + } } case float64: - if s.Max == nil || val.(float64) > s.Max.(float64) { + if s.Max == nil { s.Max = val + return + } + + switch s.Max.(type) { + case int64: + if val.(float64) > float64(s.Max.(int64)) { + s.Max = val + } + case float64: + if val.(float64) > s.Max.(float64) { + s.Max = val + } } } } diff --git a/stack_test.go b/stack_test.go index a028d59..a9d643a 100644 --- a/stack_test.go +++ b/stack_test.go @@ -2,6 +2,13 @@ package main import "testing" +func TestPushWithFloatAndIntValue(t *testing.T) { + s := NewStack() + s.Push(VarValue(int64(0.0))) // from service.go:guessValue + s.Push(VarValue(5.0)) + s.Push(VarValue(float64(15.0))) +} + func TestStack(t *testing.T) { size := 10 s := NewStackWithSize(size) From 34398877f7d5cbadef1d1cfff685d3c6dbd46ad0 Mon Sep 17 00:00:00 2001 From: Ivan Daniluk Date: Fri, 25 Sep 2015 12:39:31 -0400 Subject: [PATCH 2/2] Small improvements to PR --- stack.go | 15 +++++---------- stack_test.go | 7 +++++++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/stack.go b/stack.go index e14ae8e..328c8db 100644 --- a/stack.go +++ b/stack.go @@ -33,13 +33,13 @@ func (s *Stack) Push(val VarValue) { s.Values = s.Values[1:] } + if s.Max == nil { + s.Max = val + return + } + switch val.(type) { case int64: - if s.Max == nil { - s.Max = val - return - } - switch s.Max.(type) { case int64: if val.(int64) > s.Max.(int64) { @@ -51,11 +51,6 @@ func (s *Stack) Push(val VarValue) { } } case float64: - if s.Max == nil { - s.Max = val - return - } - switch s.Max.(type) { case int64: if val.(float64) > float64(s.Max.(int64)) { diff --git a/stack_test.go b/stack_test.go index a9d643a..b57f1d3 100644 --- a/stack_test.go +++ b/stack_test.go @@ -7,6 +7,13 @@ func TestPushWithFloatAndIntValue(t *testing.T) { s.Push(VarValue(int64(0.0))) // from service.go:guessValue s.Push(VarValue(5.0)) s.Push(VarValue(float64(15.0))) + if _, ok := s.Max.(float64); !ok { + t.Fatalf("Expected Max to be float64, but it's not") + } + s.Push(VarValue(int64(25.0))) + if _, ok := s.Max.(int64); !ok { + t.Fatalf("Expected Max to be int64, but it's not") + } } func TestStack(t *testing.T) {