github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/internal/native/avx2/fastint_amd64_test.go (about)

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