github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/issue_test/issue460_test.go (about)

     1  /**
     2  * Copyright 2023 ByteDance Inc.
     3  *
     4  * Licensed under the Apache License, Version 2.0 (the "License");
     5  * you may not use this file except in compliance with the License.
     6  * You may obtain a copy of the License at
     7  *
     8  *     http://www.apache.org/licenses/LICENSE-2.0
     9  *
    10  * Unless required by applicable law or agreed to in writing,software
    11  * distributed under the License is distributed on an "AS IS" BASIS,
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
    13  * See the License for the specific language governing permissionsand
    14  * limitations under the License.
    15  */
    16  package issue_test
    17  import (
    18      `encoding/json`
    19      `testing`
    20      `github.com/bytedance/sonic`
    21      `github.com/stretchr/testify/require`
    22  )
    23  
    24  func TestIssue460_UnmarshalMaxFloat32(t *testing.T) {
    25      tests := []string {
    26          // max.MaxFloat32
    27          "3.40282346638528859811704183484516925440e+38",
    28  
    29          // round up to max.MaxFloat32
    30          "3.402823469e+38",
    31          "3.40282346e+38",
    32          "3.40282347e+38",
    33          "3.40282348e+38",
    34          "3.4028235e+38",
    35          // TODO: fix this case
    36          // "3.4028235677973366e+38",  // Bits: 1000111111011111111111111111111111_10000000000000000000000000000
    37  
    38          // overflow for float32, round up to max.MaxFloat32 + 1
    39          "3.402823567797337e+38", // Bits: 1000111111011111111111111111111111_10000000000000000000000000001
    40          "3.402823567797338e+38",
    41          "3.4028236e+38",
    42      }
    43  
    44      t.Run("max float32", func(t *testing.T) {
    45          for _, data := range(tests) {
    46              var f1, f2  float32
    47              se := sonic.UnmarshalString(data, &f1)
    48              je := json.Unmarshal([]byte(data), &f2)
    49              require.Equal(t, se != nil, je != nil, data, se, je)
    50              require.Equal(t, f2, f1, data)
    51          }
    52      })
    53  
    54      t.Run("min float32", func(t *testing.T) {
    55          for _, data := range(tests) {
    56              data = string("-") + data
    57              var f1, f2  float32
    58              se := sonic.UnmarshalString(data, &f1)
    59              je := json.Unmarshal([]byte(data), &f2)
    60              require.Equal(t, se != nil, je != nil, data, se, je)
    61              require.Equal(t, f2, f1, data)
    62          }
    63      })
    64  }