github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/use_number_test.go (about) 1 //go:build amd64 && go1.16 && !go1.22 2 // +build amd64,go1.16,!go1.22 3 4 /* 5 * Copyright 2021 ByteDance Inc. 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 package sonic 21 22 import ( 23 "encoding/json" 24 "reflect" 25 "testing" 26 27 "github.com/goshafaq/sonic/decoder" 28 ) 29 30 type useInt64Test struct { 31 in string 32 out int64 33 } 34 35 type useFloatTest struct { 36 in string 37 out float64 38 } 39 40 var useinttest = []useInt64Test{ 41 // int64 42 {"0", 0}, 43 {"1", 1}, 44 {"-1", -1}, 45 {"100", 100}, 46 47 {"-9223372036854775807", -9223372036854775807}, 48 {"-9223372036854775808", -9223372036854775808}, //min int64 49 {"9223372036854775807", 9223372036854775807}, //max int64 50 {"9223372036854775806", 9223372036854775806}, 51 } 52 53 var usefloattest = []useFloatTest{ 54 // float64 55 {"-9223372036854775809", -9223372036854775809}, // int64 overflow 56 {"9223372036854775808", 9223372036854775808}, // int64 overflow 57 {"1e2", 1e2}, 58 {"1e-20", 1e-20}, 59 {"1.0", 1}, 60 } 61 62 func TestUseInt64(t *testing.T) { 63 for i, tt := range useinttest { 64 var sout interface{} 65 dc := decoder.NewDecoder(tt.in) 66 dc.UseInt64() 67 serr := dc.Decode(&sout) 68 if !reflect.DeepEqual(sout, tt.out) { 69 t.Errorf("Test %d, %#v\ngot:\n %#v\nexp:\n %#v\n", i, tt.in, sout, tt.in) 70 } 71 if serr != nil { 72 t.Errorf("Test %d, %#v\ngot:\n %#v\nexp:\n nil\n", i, tt, serr) 73 } 74 } 75 76 for i, tt := range usefloattest { 77 var sout interface{} 78 dc := decoder.NewDecoder(tt.in) 79 dc.UseInt64() 80 //the input string is not int64, still return float64 81 serr := dc.Decode(&sout) 82 if !reflect.DeepEqual(sout, tt.out) { 83 t.Errorf("Test %d, %#v\ngot:\n %#v\nexp:\n %#v\n", i, tt.in, sout, tt.in) 84 } 85 if serr != nil { 86 t.Errorf("Test %d, %#v\ngot:\n %#v\nexp:\n nil\n", i, tt, serr) 87 } 88 } 89 } 90 91 func TestUseNumber(t *testing.T) { 92 for i, tt := range useinttest { 93 var sout interface{} 94 dc := decoder.NewDecoder(tt.in) 95 dc.UseNumber() 96 serr := dc.Decode(&sout) 97 if !reflect.DeepEqual(sout, json.Number(tt.in)) { 98 t.Errorf("Test %d, %#v\ngot:\n %#v\nexp:\n %#v\n", i, tt.in, sout, tt.out) 99 } 100 if serr != nil { 101 t.Errorf("Test %d, %#v\ngot:\n %#v\nexp:\n nil\n", i, tt, serr) 102 } 103 } 104 105 for i, tt := range usefloattest { 106 var sout interface{} 107 dc := decoder.NewDecoder(tt.in) 108 dc.UseNumber() 109 serr := dc.Decode(&sout) 110 if !reflect.DeepEqual(sout, json.Number(tt.in)) { 111 t.Errorf("Test %d, %#v\ngot:\n %#v\nexp:\n %#v\n", i, tt.in, sout, tt.out) 112 } 113 if serr != nil { 114 t.Errorf("Test %d, %#v\ngot:\n %#v\nexp:\n nil\n", i, tt, serr) 115 } 116 } 117 }