github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/internal/native/neon/fastint_arm64_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 neon 22 23 import ( 24 `strconv` 25 `testing` 26 `fmt` 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) { var buf [32]byte; for i := 0; i < b.N; i++ { strconv.AppendInt(buf[:0], int64(i), 10) }}, 109 }, { 110 name: "StdLib-Negative", 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: "FastInt-Positive", 114 test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { i64toa(&buf[0], int64(i)) }}, 115 }, { 116 name: "FastInt-Negative", 117 test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { i64toa(&buf[0], -int64(i)) }}, 118 }} 119 for _, bm := range benchmarks { 120 b.Run(bm.name, bm.test) 121 } 122 } 123 124 type utoaBench struct { 125 name string 126 num uint64 127 } 128 129 func BenchmarkFastInt_UintToString(b *testing.B) { 130 maxUint := "18446744073709551615" 131 benchs := make([]utoaBench, len(maxUint) + 1) 132 benchs[0].name = "Zero" 133 benchs[0].num = 0 134 for i := 1; i <= len(maxUint); i++ { 135 benchs[i].name = strconv.FormatInt(int64(i), 10) + "-Digs" 136 benchs[i].num, _ = strconv.ParseUint(string(maxUint[:i]), 10, 64) 137 } 138 139 for _, t := range(benchs) { 140 benchmarks := []struct { 141 name string 142 test func(*testing.B) 143 }{{ 144 name: "StdLib", 145 test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { strconv.AppendUint(buf[:0], t.num, 10) }}, 146 }, { 147 name: "FastInt", 148 test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { u64toa(&buf[0], t.num) }}, 149 }} 150 for _, bm := range benchmarks { 151 name := fmt.Sprintf("%s_%s", bm.name, t.name) 152 b.Run(name, bm.test) 153 } 154 } 155 }