gojay

high performance JSON encoder/decoder with stream API for Golang
git clone git://git.lair.cx/gojay
Log | Files | Refs | README | LICENSE

commit d39ebfd77b4514e74dbbee7e589ea1d54eaeb82f
parent 10bb7e7376901d07a8650d2a26f55aa3e1094548
Author: francoispqt <francois@parquet.ninja>
Date:   Sun, 13 May 2018 17:27:08 +0800

add tests for numbers

Diffstat:
Mdecode_number.go | 4+---
Mdecode_number_test.go | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++------
2 files changed, 54 insertions(+), 9 deletions(-)

diff --git a/decode_number.go b/decode_number.go @@ -536,8 +536,6 @@ func (dec *Decoder) getInt32(b byte) (int32, error) { case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': end = j continue - case ' ', '\n', '\t', '\r': - continue case '.': // if dot is found // look for exponent (e,E) as exponent can change the @@ -583,7 +581,7 @@ func (dec *Decoder) getInt32(b byte) (int32, error) { case 'e', 'E': // get init n return dec.getInt32WithExp(dec.atoi32(start, end), j+1) - case ',', '}', ']': + case ' ', '\n', '\t', '\r', ',', '}', ']': dec.cursor = j return dec.atoi32(start, end), nil } diff --git a/decode_number_test.go b/decode_number_test.go @@ -250,7 +250,7 @@ func TestDecoderInt64(t *testing.T) { }, { name: "basic-positive2", - json: "1039405", + json: " 1039405", expectedResult: 1039405, }, { @@ -277,7 +277,7 @@ func TestDecoderInt64(t *testing.T) { }, { name: "basic-big-overflow", - json: "9223372036854775808", + json: " 9223372036854775808", expectedResult: 0, err: true, }, @@ -458,7 +458,7 @@ func TestDecoderUint64(t *testing.T) { }, { name: "basic-positive2", - json: "1039405", + json: " 1039405", expectedResult: 1039405, }, { @@ -517,6 +517,13 @@ func TestDecoderUint64(t *testing.T) { err: true, }, { + name: "error", + json: "-83zez4", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { name: "invalid-type", json: `"string"`, expectedResult: 0, @@ -589,7 +596,7 @@ func TestDecoderInt32(t *testing.T) { }, { name: "basic-positive2", - json: "1039405", + json: " 1039405", expectedResult: 1039405, }, { @@ -616,7 +623,7 @@ func TestDecoderInt32(t *testing.T) { }, { name: "basic-big", - json: "2147483647", + json: " 2147483647", expectedResult: 2147483647, }, { @@ -713,7 +720,14 @@ func TestDecoderInt32(t *testing.T) { expectedResult: -800000, }, { - name: "error3", + name: "error", + json: "83zez4", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { + name: "error2", json: "-8e+00$aa5", expectedResult: 0, err: true, @@ -846,6 +860,20 @@ func TestDecoderUint32(t *testing.T) { expectedResult: 7, }, { + name: "error", + json: "83zez4", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { + name: "error", + json: "-83zez4", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { name: "invalid-type", json: `"string"`, expectedResult: 0, @@ -1003,6 +1031,11 @@ func TestDecoderFloat64(t *testing.T) { }, { name: "basic-float2", + json: "877", + expectedResult: 877, + }, + { + name: "basic-float2", json: "-7.8876", expectedResult: -7.8876, }, @@ -1017,6 +1050,20 @@ func TestDecoderFloat64(t *testing.T) { expectedResult: -788.76, }, { + name: "error", + json: "83zez4", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { + name: "error", + json: "-83zez4", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { name: "invalid-type", json: `"string"`, expectedResult: 0,