github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/internal/native/sse/fastint_test.go (about)

     1  // +build amd64 
     2  
     3  
     4  // Code generated by Makefile, DO NOT EDIT.
     5  
     6  // Code generated by Makefile, DO NOT EDIT.
     7  
     8  /*
     9   * Copyright 2021 ByteDance Inc.
    10   *
    11   * Licensed under the Apache License, Version 2.0 (the "License");
    12   * you may not use this file except in compliance with the License.
    13   * You may obtain a copy of the License at
    14   *
    15   *     http://www.apache.org/licenses/LICENSE-2.0
    16   *
    17   * Unless required by applicable law or agreed to in writing, software
    18   * distributed under the License is distributed on an "AS IS" BASIS,
    19   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    20   * See the License for the specific language governing permissions and
    21   * limitations under the License.
    22   */
    23  
    24  package sse
    25  
    26  import (
    27      `strconv`
    28      `testing`
    29      `fmt`
    30  
    31      `github.com/stretchr/testify/assert`
    32  )
    33  
    34  func TestFastInt_IntToString(t *testing.T) {
    35      var buf [32]byte
    36      assert.Equal(t, "0"                    , string(buf[:i64toa(&buf[0], 0)]))
    37      assert.Equal(t, "1"                    , string(buf[:i64toa(&buf[0], 1)]))
    38      assert.Equal(t, "12"                   , string(buf[:i64toa(&buf[0], 12)]))
    39      assert.Equal(t, "123"                  , string(buf[:i64toa(&buf[0], 123)]))
    40      assert.Equal(t, "1234"                 , string(buf[:i64toa(&buf[0], 1234)]))
    41      assert.Equal(t, "12345"                , string(buf[:i64toa(&buf[0], 12345)]))
    42      assert.Equal(t, "123456"               , string(buf[:i64toa(&buf[0], 123456)]))
    43      assert.Equal(t, "1234567"              , string(buf[:i64toa(&buf[0], 1234567)]))
    44      assert.Equal(t, "12345678"             , string(buf[:i64toa(&buf[0], 12345678)]))
    45      assert.Equal(t, "123456789"            , string(buf[:i64toa(&buf[0], 123456789)]))
    46      assert.Equal(t, "1234567890"           , string(buf[:i64toa(&buf[0], 1234567890)]))
    47      assert.Equal(t, "12345678901"          , string(buf[:i64toa(&buf[0], 12345678901)]))
    48      assert.Equal(t, "123456789012"         , string(buf[:i64toa(&buf[0], 123456789012)]))
    49      assert.Equal(t, "1234567890123"        , string(buf[:i64toa(&buf[0], 1234567890123)]))
    50      assert.Equal(t, "12345678901234"       , string(buf[:i64toa(&buf[0], 12345678901234)]))
    51      assert.Equal(t, "123456789012345"      , string(buf[:i64toa(&buf[0], 123456789012345)]))
    52      assert.Equal(t, "1234567890123456"     , string(buf[:i64toa(&buf[0], 1234567890123456)]))
    53      assert.Equal(t, "12345678901234567"    , string(buf[:i64toa(&buf[0], 12345678901234567)]))
    54      assert.Equal(t, "123456789012345678"   , string(buf[:i64toa(&buf[0], 123456789012345678)]))
    55      assert.Equal(t, "1234567890123456789"  , string(buf[:i64toa(&buf[0], 1234567890123456789)]))
    56      assert.Equal(t, "9223372036854775807"  , string(buf[:i64toa(&buf[0], 9223372036854775807)]))
    57      assert.Equal(t, "-1"                   , string(buf[:i64toa(&buf[0], -1)]))
    58      assert.Equal(t, "-12"                  , string(buf[:i64toa(&buf[0], -12)]))
    59      assert.Equal(t, "-123"                 , string(buf[:i64toa(&buf[0], -123)]))
    60      assert.Equal(t, "-1234"                , string(buf[:i64toa(&buf[0], -1234)]))
    61      assert.Equal(t, "-12345"               , string(buf[:i64toa(&buf[0], -12345)]))
    62      assert.Equal(t, "-123456"              , string(buf[:i64toa(&buf[0], -123456)]))
    63      assert.Equal(t, "-1234567"             , string(buf[:i64toa(&buf[0], -1234567)]))
    64      assert.Equal(t, "-12345678"            , string(buf[:i64toa(&buf[0], -12345678)]))
    65      assert.Equal(t, "-123456789"           , string(buf[:i64toa(&buf[0], -123456789)]))
    66      assert.Equal(t, "-1234567890"          , string(buf[:i64toa(&buf[0], -1234567890)]))
    67      assert.Equal(t, "-12345678901"         , string(buf[:i64toa(&buf[0], -12345678901)]))
    68      assert.Equal(t, "-123456789012"        , string(buf[:i64toa(&buf[0], -123456789012)]))
    69      assert.Equal(t, "-1234567890123"       , string(buf[:i64toa(&buf[0], -1234567890123)]))
    70      assert.Equal(t, "-12345678901234"      , string(buf[:i64toa(&buf[0], -12345678901234)]))
    71      assert.Equal(t, "-123456789012345"     , string(buf[:i64toa(&buf[0], -123456789012345)]))
    72      assert.Equal(t, "-1234567890123456"    , string(buf[:i64toa(&buf[0], -1234567890123456)]))
    73      assert.Equal(t, "-12345678901234567"   , string(buf[:i64toa(&buf[0], -12345678901234567)]))
    74      assert.Equal(t, "-123456789012345678"  , string(buf[:i64toa(&buf[0], -123456789012345678)]))
    75      assert.Equal(t, "-1234567890123456789" , string(buf[:i64toa(&buf[0], -1234567890123456789)]))
    76      assert.Equal(t, "-9223372036854775808" , string(buf[:i64toa(&buf[0], -9223372036854775808)]))
    77  }
    78  
    79  func TestFastInt_UintToString(t *testing.T) {
    80      var buf [32]byte
    81      assert.Equal(t, "0"                    , string(buf[:u64toa(&buf[0], 0)]))
    82      assert.Equal(t, "1"                    , string(buf[:u64toa(&buf[0], 1)]))
    83      assert.Equal(t, "12"                   , string(buf[:u64toa(&buf[0], 12)]))
    84      assert.Equal(t, "123"                  , string(buf[:u64toa(&buf[0], 123)]))
    85      assert.Equal(t, "1234"                 , string(buf[:u64toa(&buf[0], 1234)]))
    86      assert.Equal(t, "12345"                , string(buf[:u64toa(&buf[0], 12345)]))
    87      assert.Equal(t, "123456"               , string(buf[:u64toa(&buf[0], 123456)]))
    88      assert.Equal(t, "1234567"              , string(buf[:u64toa(&buf[0], 1234567)]))
    89      assert.Equal(t, "12345678"             , string(buf[:u64toa(&buf[0], 12345678)]))
    90      assert.Equal(t, "123456789"            , string(buf[:u64toa(&buf[0], 123456789)]))
    91      assert.Equal(t, "1234567890"           , string(buf[:u64toa(&buf[0], 1234567890)]))
    92      assert.Equal(t, "12345678901"          , string(buf[:u64toa(&buf[0], 12345678901)]))
    93      assert.Equal(t, "123456789012"         , string(buf[:u64toa(&buf[0], 123456789012)]))
    94      assert.Equal(t, "1234567890123"        , string(buf[:u64toa(&buf[0], 1234567890123)]))
    95      assert.Equal(t, "12345678901234"       , string(buf[:u64toa(&buf[0], 12345678901234)]))
    96      assert.Equal(t, "123456789012345"      , string(buf[:u64toa(&buf[0], 123456789012345)]))
    97      assert.Equal(t, "1234567890123456"     , string(buf[:u64toa(&buf[0], 1234567890123456)]))
    98      assert.Equal(t, "12345678901234567"    , string(buf[:u64toa(&buf[0], 12345678901234567)]))
    99      assert.Equal(t, "123456789012345678"   , string(buf[:u64toa(&buf[0], 123456789012345678)]))
   100      assert.Equal(t, "1234567890123456789"  , string(buf[:u64toa(&buf[0], 1234567890123456789)]))
   101      assert.Equal(t, "12345678901234567890" , string(buf[:u64toa(&buf[0], 12345678901234567890)]))
   102      assert.Equal(t, "18446744073709551615" , string(buf[:u64toa(&buf[0], 18446744073709551615)]))
   103  }
   104  
   105  func BenchmarkFastInt_IntToString(b *testing.B) {
   106      benchmarks := []struct {
   107          name string
   108          test func(*testing.B)
   109      }{{
   110          name: "StdLib-Positive",
   111          test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { strconv.AppendInt(buf[:0], int64(i), 10) }},
   112      }, {
   113          name: "StdLib-Negative",
   114          test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { strconv.AppendInt(buf[:0], -int64(i), 10) }},
   115      }, {
   116          name: "FastInt-Positive",
   117          test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { i64toa(&buf[0], int64(i)) }},
   118      }, {
   119          name: "FastInt-Negative",
   120          test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { i64toa(&buf[0], -int64(i)) }},
   121      }}
   122      for _, bm := range benchmarks {
   123          b.Run(bm.name, bm.test)
   124      }
   125  }
   126  
   127  type utoaBench struct {
   128      name string
   129      num  uint64
   130  }
   131  
   132  func BenchmarkFastInt_UintToString(b *testing.B) {
   133      maxUint := "18446744073709551615"
   134      benchs := make([]utoaBench, len(maxUint) + 1)
   135      benchs[0].name = "Zero"
   136      benchs[0].num  = 0
   137      for i := 1; i <= len(maxUint); i++ {
   138          benchs[i].name = strconv.FormatInt(int64(i), 10) + "-Digs"
   139          benchs[i].num, _ = strconv.ParseUint(string(maxUint[:i]), 10, 64)
   140      }
   141  
   142      for _, t := range(benchs) {
   143          benchmarks := []struct {
   144              name string
   145              test func(*testing.B)
   146          }{{
   147              name: "StdLib",
   148              test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { strconv.AppendUint(buf[:0], t.num, 10) }},
   149          }, {
   150              name: "FastInt",
   151              test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { u64toa(&buf[0], t.num) }},
   152          }}
   153          for _, bm := range benchmarks {
   154              name := fmt.Sprintf("%s_%s", bm.name, t.name)
   155              b.Run(name, bm.test)
   156          }
   157      }
   158  }