From 68a803260b6b65e7c87c2a641aaa31b3c4f0d240 Mon Sep 17 00:00:00 2001 From: Gregory Roseberry Date: Wed, 17 Sep 2014 10:12:28 +0900 Subject: [PATCH] overflow and float conversion tests --- int_test.go | 1 - zero/int_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/int_test.go b/int_test.go index 72a11c4..bce5516 100644 --- a/int_test.go +++ b/int_test.go @@ -57,7 +57,6 @@ func TestUnmarshalInt(t *testing.T) { } func TestUnmarshalNonIntegerNumber(t *testing.T) { - floatJSON = []byte(`123.45`) var i Int err := json.Unmarshal(floatJSON, &i) if err == nil { diff --git a/zero/int_test.go b/zero/int_test.go index 2910b6a..6455334 100644 --- a/zero/int_test.go +++ b/zero/int_test.go @@ -2,6 +2,8 @@ package zero import ( "encoding/json" + "math" + "strconv" "testing" ) @@ -60,6 +62,30 @@ func TestUnmarshalInt(t *testing.T) { assertNullInt(t, badType, "wrong type json") } +func TestUnmarshalNonIntegerNumber(t *testing.T) { + var i Int + err := json.Unmarshal(floatJSON, &i) + if err == nil { + panic("err should be present; non-integer number coerced to int") + } +} + +func TestUnmarshalInt64Overflow(t *testing.T) { + int64Overflow := uint64(math.MaxInt64) + + // Max int64 should decode successfully + var i Int + err := json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i) + maybePanic(err) + + // Attempt to overflow + int64Overflow++ + err = json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i) + if err == nil { + panic("err should be present; decoded value overflows int64") + } +} + func TestTextUnmarshalInt(t *testing.T) { var i Int err := i.UnmarshalText([]byte("12345"))