| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
stdjson "encoding/json" |
| 5 |
"fmt" |
| 6 |
"math" |
| 7 |
"math/rand" |
| 8 |
"reflect" |
| 9 |
"strconv" |
| 10 |
"testing" |
| 11 |
|
| 12 |
json "github.com/goccy/go-json" |
| 13 |
) |
| 14 |
|
| 15 |
// The numbers are parsed in one pass, and a float64 is computed directly when that is exact. These tests |
| 16 |
// compare the results with encoding/json. |
| 17 |
|
| 18 |
type numberFields struct { |
| 19 |
I int |
| 20 |
I8 int8 |
| 21 |
I16 int16 |
| 22 |
I32 int32 |
| 23 |
I64 int64 |
| 24 |
U uint |
| 25 |
U8 uint8 |
| 26 |
U16 uint16 |
| 27 |
U32 uint32 |
| 28 |
U64 uint64 |
| 29 |
F64 float64 |
| 30 |
A any |
| 31 |
} |
| 32 |
|
| 33 |
func compareNumber(t *testing.T, field, literal string) { |
| 34 |
t.Helper() |
| 35 |
in := fmt.Sprintf(`{%q:%s}`, field, literal) |
| 36 |
var want, got numberFields |
| 37 |
errStd := stdjson.Unmarshal([]byte(in), &want) |
| 38 |
err := json.Unmarshal([]byte(in), &got) |
| 39 |
if (errStd == nil) != (err == nil) { |
| 40 |
t.Fatalf("%s: encoding/json: %v, go-json: %v", in, errStd, err) |
| 41 |
} |
| 42 |
if errStd != nil { |
| 43 |
var typeErr *json.UnmarshalTypeError |
| 44 |
var stdTypeErr *stdjson.UnmarshalTypeError |
| 45 |
if isStd, is := errorAs(errStd, &stdTypeErr), errorAs(err, &typeErr); isStd != is { |
| 46 |
t.Fatalf("%s: encoding/json: %v, go-json: %v", in, errStd, err) |
| 47 |
} |
| 48 |
return |
| 49 |
} |
| 50 |
if !reflect.DeepEqual(got, want) { |
| 51 |
t.Fatalf("%s:\n got %+v\nwant %+v", in, got, want) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func errorAs[T error](err error, target *T) bool { |
| 56 |
t, ok := err.(T) |
| 57 |
if ok { |
| 58 |
*target = t |
| 59 |
} |
| 60 |
return ok |
| 61 |
} |
| 62 |
|
| 63 |
func TestDecodeIntegerLimits(t *testing.T) { |
| 64 |
literals := []string{ |
| 65 |
"0", "-0", "1", "-1", "7", "10", "99", |
| 66 |
"127", "128", "-128", "-129", "255", "256", |
| 67 |
"32767", "32768", "-32768", "-32769", "65535", "65536", |
| 68 |
"2147483647", "2147483648", "-2147483648", "-2147483649", "4294967295", "4294967296", |
| 69 |
"9223372036854775807", "9223372036854775808", "-9223372036854775808", "-9223372036854775809", |
| 70 |
"18446744073709551615", "18446744073709551616", "99999999999999999999", "100000000000000000000", |
| 71 |
"1234567890123456789012345", |
| 72 |
// not integers |
| 73 |
"1.0", "100.0", "1e2", "1E2", "-1.5", "0.5", "1e-2", |
| 74 |
} |
| 75 |
for _, field := range []string{"I", "I8", "I16", "I32", "I64", "U", "U8", "U16", "U32", "U64"} { |
| 76 |
for _, literal := range literals { |
| 77 |
compareNumber(t, field, literal) |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func TestDecodeFloatAsStrconv(t *testing.T) { |
| 83 |
// The float64 of a number is the one strconv.ParseFloat gives, bit for bit, whichever way it is computed. |
| 84 |
r := rand.New(rand.NewSource(3)) |
| 85 |
var literals []string |
| 86 |
for i := 0; i < 20000; i++ { |
| 87 |
var f float64 |
| 88 |
switch i % 4 { |
| 89 |
case 0: |
| 90 |
f = r.Float64() * math.Pow(10, float64(r.Intn(40)-20)) |
| 91 |
case 1: |
| 92 |
f = float64(r.Int63n(1 << 53)) |
| 93 |
case 2: |
| 94 |
f = math.Float64frombits(r.Uint64()) |
| 95 |
if math.IsNaN(f) || math.IsInf(f, 0) { |
| 96 |
continue |
| 97 |
} |
| 98 |
case 3: |
| 99 |
f = float64(r.Intn(1000000)) / 1000 |
| 100 |
} |
| 101 |
if r.Intn(2) == 0 { |
| 102 |
f = -f |
| 103 |
} |
| 104 |
for _, format := range []byte{'g', 'e', 'f'} { |
| 105 |
literals = append(literals, strconv.FormatFloat(f, format, -1, 64)) |
| 106 |
} |
| 107 |
literals = append(literals, strconv.FormatFloat(f, 'e', r.Intn(20), 64)) |
| 108 |
} |
| 109 |
literals = append(literals, |
| 110 |
"0", "-0", "0.0", "-0.0", "1e22", "1e23", "9007199254740992", "9007199254740993", "9007199254740993.0", |
| 111 |
"123456789012345678", "1234567890123456789", "12345678901234567890", "0.1", "0.30000000000000004", |
| 112 |
"1e-22", "1e-23", "4.9e-324", "1.7976931348623157e308", "2.2250738585072014e-308", "1E+2", "1e-0", |
| 113 |
"0.000000000000000000000000000001", "1000000000000000000000000.5", |
| 114 |
) |
| 115 |
for _, literal := range literals { |
| 116 |
want, err := strconv.ParseFloat(literal, 64) |
| 117 |
if err != nil { |
| 118 |
continue |
| 119 |
} |
| 120 |
var v struct { |
| 121 |
F float64 |
| 122 |
A any |
| 123 |
} |
| 124 |
if err := json.Unmarshal([]byte(`{"F":`+literal+`,"A":`+literal+`}`), &v); err != nil { |
| 125 |
t.Fatalf("%s: %v", literal, err) |
| 126 |
} |
| 127 |
if math.Float64bits(v.F) != math.Float64bits(want) { |
| 128 |
t.Fatalf("%s: got %v ( %x ), want %v ( %x )", literal, v.F, math.Float64bits(v.F), want, math.Float64bits(want)) |
| 129 |
} |
| 130 |
if a, ok := v.A.(float64); !ok || math.Float64bits(a) != math.Float64bits(want) { |
| 131 |
t.Fatalf("%s: into interface{}: got %v, want %v", literal, v.A, want) |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func TestDecodeNumberMalformed(t *testing.T) { |
| 137 |
// What is not a number by the grammar is an error. |
| 138 |
// A fraction without a digit is still taken as a float, as before; it is left to the work on the grammar ( issue 394 ). |
| 139 |
lenient := map[string]bool{"1.": true, "1.e2": true} |
| 140 |
for _, literal := range []string{"-", "--1", "-a", "+1", "1-", "1e", "1e+", "1.e2", "1.", ".1", "0x10"} { |
| 141 |
for _, field := range []string{"I", "U", "F64", "A"} { |
| 142 |
var v numberFields |
| 143 |
err := json.Unmarshal([]byte(fmt.Sprintf(`{%q:%s}`, field, literal)), &v) |
| 144 |
if err == nil && !(lenient[literal] && (field == "F64" || field == "A")) { |
| 145 |
t.Fatalf("%s into %s: accepted as %+v", literal, field, v) |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |