github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/internal/native/fastfloat_amd64_test.tmpl (about)

     1  // Code generated by Makefile, DO NOT EDIT.
     2  
     3  /*
     4   * Copyright 2021 ByteDance Inc.
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   *     http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package {{PACKAGE}}
    20  
    21  import (
    22      `encoding/json`
    23      `math`
    24      `math/rand`
    25      `strconv`
    26      `testing`
    27  
    28      `github.com/stretchr/testify/assert`
    29  )
    30  
    31  func TestFastFloat_Encode(t *testing.T) {
    32      var buf [64]byte
    33      assert.Equal(t, "0"                         , string(buf[:f64toa(&buf[0], 0)]))
    34      assert.Equal(t, "-0"                         , string(buf[:f64toa(&buf[0], math.Float64frombits(0x8000000000000000))]))
    35      assert.Equal(t, "12340000000"               , string(buf[:f64toa(&buf[0], 1234e7)]))
    36      assert.Equal(t, "12.34"                     , string(buf[:f64toa(&buf[0], 1234e-2)]))
    37      assert.Equal(t, "0.001234"                  , string(buf[:f64toa(&buf[0], 1234e-6)]))
    38      assert.Equal(t, "1e+30"                      , string(buf[:f64toa(&buf[0], 1e30)]))
    39      assert.Equal(t, "1.234e+33"                  , string(buf[:f64toa(&buf[0], 1234e30)]))
    40      assert.Equal(t, "1.234e+308"                 , string(buf[:f64toa(&buf[0], 1234e305)]))
    41      assert.Equal(t, "1.234e-317"                , string(buf[:f64toa(&buf[0], 1234e-320)]))
    42      assert.Equal(t, "1.7976931348623157e+308"    , string(buf[:f64toa(&buf[0], 1.7976931348623157e308)]))
    43      assert.Equal(t, "-12340000000"              , string(buf[:f64toa(&buf[0], -1234e7)]))
    44      assert.Equal(t, "-12.34"                    , string(buf[:f64toa(&buf[0], -1234e-2)]))
    45      assert.Equal(t, "-0.001234"                 , string(buf[:f64toa(&buf[0], -1234e-6)]))
    46      assert.Equal(t, "-1e+30"                     , string(buf[:f64toa(&buf[0], -1e30)]))
    47      assert.Equal(t, "-1.234e+33"                 , string(buf[:f64toa(&buf[0], -1234e30)]))
    48      assert.Equal(t, "-1.234e+308"                , string(buf[:f64toa(&buf[0], -1234e305)]))
    49      assert.Equal(t, "-1.234e-317"               , string(buf[:f64toa(&buf[0], -1234e-320)]))
    50      assert.Equal(t, "-2.2250738585072014e-308"  , string(buf[:f64toa(&buf[0], -2.2250738585072014e-308)]))
    51  }
    52  
    53  func TestFastFloat_Random(t *testing.T) {
    54      var buf [64]byte
    55      N := 10000
    56      for i := 0; i < N; i++ {
    57          b64 := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
    58          f64 := math.Float64frombits(b64)
    59  
    60          jout, jerr := json.Marshal(f64)
    61          n := f64toa(&buf[0], f64)
    62          if jerr == nil {
    63              assert.Equal(t, jout, buf[:n])
    64          } else {
    65              assert.True(t, n == 0)
    66          }
    67  
    68          f32 := math.Float32frombits(rand.Uint32())
    69          jout, jerr = json.Marshal(f32)
    70          n = f32toa(&buf[0], f32)
    71          if jerr == nil {
    72              assert.Equal(t, jout, buf[:n])
    73          } else {
    74              assert.True(t, n == 0)
    75          }
    76      }
    77  }
    78  
    79  func BenchmarkParseFloat64(b *testing.B) {
    80      var f64toaBenches = []struct {
    81          name    string
    82          float   float64
    83      }{
    84          {"Zero", 0},
    85          {"Decimal", 33909},
    86          {"Float", 339.7784},
    87          {"Exp", -5.09e75},
    88          {"NegExp", -5.11e-95},
    89          {"LongExp", 1.234567890123456e-78},
    90          {"Big", 123456789123456789123456789},
    91      
    92      }
    93      for _, c := range f64toaBenches {
    94          f64bench := []struct {
    95              name string
    96              test func(*testing.B)
    97          }{{
    98              name: "StdLib",
    99              test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { strconv.AppendFloat(buf[:0], c.float, 'g', -1, 64) }},
   100          }, {
   101              name: "FastFloat",
   102              test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { f64toa(&buf[0], c.float) }},
   103          }}
   104          for _, bm := range f64bench {
   105              name := bm.name + "_" + c.name
   106              b.Run(name, bm.test)
   107          }
   108      }
   109  }
   110  
   111  func BenchmarkParseFloat32(b *testing.B) {
   112      var f32toaBenches = []struct {
   113          name    string
   114          float   float32
   115      }{
   116          {"Zero", 0},
   117          {"Integer", 33909},
   118          {"ExactFraction", 3.375},
   119          {"Point", 339.7784},
   120          {"Exp", -5.09e25},
   121          {"NegExp", -5.11e-25},
   122          {"Shortest", 1.234567e-8},
   123      }
   124      for _, c := range f32toaBenches {
   125          bench := []struct {
   126              name string
   127              test func(*testing.B)
   128          }{{
   129              name: "StdLib32",
   130              test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { strconv.AppendFloat(buf[:0], float64(c.float), 'g', -1, 32) }},
   131          }, {
   132              name: "FastFloat32",
   133              test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { f32toa(&buf[0], c.float) }},
   134          }}
   135          for _, bm := range bench {
   136              name := bm.name + "_" + c.name
   137              b.Run(name, bm.test)
   138          }
   139      }
   140  }