| 1 | package utils |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | "strconv" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/require" |
| 9 | ) |
| 10 | |
| 11 | func Test_FormatUint(t *testing.T) { |
| 12 | t.Parallel() |
| 13 | tests := []uint64{ |
| 14 | 0, 1, 9, 10, 11, 99, 100, 101, 999, 1000, |
| 15 | 12345, 123456789, 9999999999, |
| 16 | math.MaxUint32, math.MaxUint64, |
| 17 | } |
| 18 | for _, tt := range tests { |
| 19 | expected := strconv.FormatUint(tt, 10) |
| 20 | result := FormatUint(tt) |
| 21 | require.Equal(t, expected, result, "FormatUint(%d)", tt) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func Test_FormatInt(t *testing.T) { |
| 26 | t.Parallel() |
| 27 | tests := []int64{ |
| 28 | 0, 1, -1, 9, -9, 10, -10, 99, -99, 100, -100, |
| 29 | 12345, -12345, 123456789, -123456789, |
| 30 | math.MaxInt32, math.MinInt32, |
| 31 | math.MaxInt64, math.MinInt64, |
| 32 | } |
| 33 | for _, tt := range tests { |
| 34 | expected := strconv.FormatInt(tt, 10) |
| 35 | result := FormatInt(tt) |
| 36 | require.Equal(t, expected, result, "FormatInt(%d)", tt) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func Test_FormatUint32(t *testing.T) { |
| 41 | t.Parallel() |
| 42 | tests := []uint32{0, 1, 99, 100, 12345, math.MaxUint32} |
| 43 | for _, tt := range tests { |
| 44 | expected := strconv.FormatUint(uint64(tt), 10) |
| 45 | result := FormatUint32(tt) |
| 46 | require.Equal(t, expected, result, "FormatUint32(%d)", tt) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func Test_FormatInt32(t *testing.T) { |
| 51 | t.Parallel() |
| 52 | tests := []int32{0, 1, -1, 99, -99, math.MaxInt32, math.MinInt32} |
| 53 | for _, tt := range tests { |
| 54 | expected := strconv.FormatInt(int64(tt), 10) |
| 55 | result := FormatInt32(tt) |
| 56 | require.Equal(t, expected, result, "FormatInt32(%d)", tt) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func Test_FormatUint16(t *testing.T) { |
| 61 | t.Parallel() |
| 62 | tests := []uint16{0, 1, 99, 100, 12345, math.MaxUint16} |
| 63 | for _, tt := range tests { |
| 64 | expected := strconv.FormatUint(uint64(tt), 10) |
| 65 | result := FormatUint16(tt) |
| 66 | require.Equal(t, expected, result, "FormatUint16(%d)", tt) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func Test_FormatInt16(t *testing.T) { |
| 71 | t.Parallel() |
| 72 | tests := []int16{0, 1, -1, 99, -99, math.MaxInt16, math.MinInt16} |
| 73 | for _, tt := range tests { |
| 74 | expected := strconv.FormatInt(int64(tt), 10) |
| 75 | result := FormatInt16(tt) |
| 76 | require.Equal(t, expected, result, "FormatInt16(%d)", tt) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func Test_FormatUint8(t *testing.T) { |
| 81 | t.Parallel() |
| 82 | tests := []uint8{0, 1, 99, 100, math.MaxUint8} |
| 83 | for _, tt := range tests { |
| 84 | expected := strconv.FormatUint(uint64(tt), 10) |
| 85 | result := FormatUint8(tt) |
| 86 | require.Equal(t, expected, result, "FormatUint8(%d)", tt) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func Test_FormatInt8(t *testing.T) { |
| 91 | t.Parallel() |
| 92 | tests := []int8{0, 1, -1, 99, -99, math.MaxInt8, math.MinInt8} |
| 93 | for _, tt := range tests { |
| 94 | expected := strconv.FormatInt(int64(tt), 10) |
| 95 | result := FormatInt8(tt) |
| 96 | require.Equal(t, expected, result, "FormatInt8(%d)", tt) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func Test_AppendUint(t *testing.T) { |
| 101 | t.Parallel() |
| 102 | tests := []uint64{0, 1, 99, 100, 12345, 123456789, math.MaxUint64} |
| 103 | for _, tt := range tests { |
| 104 | expected := strconv.AppendUint([]byte("prefix"), tt, 10) |
| 105 | result := AppendUint([]byte("prefix"), tt) |
| 106 | require.Equal(t, expected, result, "AppendUint(%d)", tt) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func Test_AppendInt(t *testing.T) { |
| 111 | t.Parallel() |
| 112 | tests := []int64{0, 1, -1, 99, -99, 100, -100, 12345, -12345, math.MaxInt64, math.MinInt64} |
| 113 | for _, tt := range tests { |
| 114 | expected := strconv.AppendInt([]byte("prefix"), tt, 10) |
| 115 | result := AppendInt([]byte("prefix"), tt) |
| 116 | require.Equal(t, expected, result, "AppendInt(%d)", tt) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func Test_AppendInt_SmallNegativeCache(t *testing.T) { |
| 121 | t.Parallel() |
| 122 | // Test all small negative integers that should use the cache (-1 to -99) |
| 123 | for i := int64(-1); i >= -99; i-- { |
| 124 | t.Run(strconv.FormatInt(i, 10), func(t *testing.T) { |
| 125 | t.Parallel() |
| 126 | expected := strconv.AppendInt([]byte("prefix"), i, 10) |
| 127 | result := AppendInt([]byte("prefix"), i) |
| 128 | require.Equal(t, expected, result) |
| 129 | }) |
| 130 | } |
| 131 | // Verify boundary: -100 should NOT use the cache |
| 132 | t.Run("boundary/-100", func(t *testing.T) { |
| 133 | t.Parallel() |
| 134 | expected := strconv.AppendInt([]byte("prefix"), -100, 10) |
| 135 | result := AppendInt([]byte("prefix"), -100) |
| 136 | require.Equal(t, expected, result) |
| 137 | }) |
| 138 | } |
| 139 | |
| 140 | // Benchmarks |
| 141 | |
| 142 | func Benchmark_FormatUint(b *testing.B) { |
| 143 | inputs := []struct { |
| 144 | name string |
| 145 | value uint64 |
| 146 | }{ |
| 147 | {"small", 42}, |
| 148 | {"medium", 123456789}, |
| 149 | {"large", math.MaxUint64}, |
| 150 | } |
| 151 | |
| 152 | for _, input := range inputs { |
| 153 | b.Run(input.name+"/fiber", func(b *testing.B) { |
| 154 | b.ReportAllocs() |
| 155 | for b.Loop() { |
| 156 | FormatUint(input.value) |
| 157 | } |
| 158 | }) |
| 159 | b.Run(input.name+"/strconv", func(b *testing.B) { |
| 160 | b.ReportAllocs() |
| 161 | for b.Loop() { |
| 162 | strconv.FormatUint(input.value, 10) |
| 163 | } |
| 164 | }) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func Benchmark_FormatInt(b *testing.B) { |
| 169 | inputs := []struct { |
| 170 | name string |
| 171 | value int64 |
| 172 | }{ |
| 173 | {"small_pos", 42}, |
| 174 | {"small_neg", -42}, |
| 175 | {"medium_pos", 123456789}, |
| 176 | {"medium_neg", -123456789}, |
| 177 | {"large_pos", math.MaxInt64}, |
| 178 | {"large_neg", math.MinInt64}, |
| 179 | } |
| 180 | |
| 181 | for _, input := range inputs { |
| 182 | b.Run(input.name+"/fiber", func(b *testing.B) { |
| 183 | b.ReportAllocs() |
| 184 | for b.Loop() { |
| 185 | FormatInt(input.value) |
| 186 | } |
| 187 | }) |
| 188 | b.Run(input.name+"/strconv", func(b *testing.B) { |
| 189 | b.ReportAllocs() |
| 190 | for b.Loop() { |
| 191 | strconv.FormatInt(input.value, 10) |
| 192 | } |
| 193 | }) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func Benchmark_FormatUint32(b *testing.B) { |
| 198 | input := uint32(123456789) |
| 199 | |
| 200 | b.Run("fiber", func(b *testing.B) { |
| 201 | b.ReportAllocs() |
| 202 | for b.Loop() { |
| 203 | FormatUint32(input) |
| 204 | } |
| 205 | }) |
| 206 | b.Run("strconv", func(b *testing.B) { |
| 207 | b.ReportAllocs() |
| 208 | for b.Loop() { |
| 209 | strconv.FormatUint(uint64(input), 10) |
| 210 | } |
| 211 | }) |
| 212 | } |
| 213 | |
| 214 | func Benchmark_FormatInt32(b *testing.B) { |
| 215 | input := int32(-123456789) |
| 216 | |
| 217 | b.Run("fiber", func(b *testing.B) { |
| 218 | b.ReportAllocs() |
| 219 | for b.Loop() { |
| 220 | FormatInt32(input) |
| 221 | } |
| 222 | }) |
| 223 | b.Run("strconv", func(b *testing.B) { |
| 224 | b.ReportAllocs() |
| 225 | for b.Loop() { |
| 226 | strconv.FormatInt(int64(input), 10) |
| 227 | } |
| 228 | }) |
| 229 | } |
| 230 | |
| 231 | func Benchmark_FormatUint16(b *testing.B) { |
| 232 | input := uint16(12345) |
| 233 | |
| 234 | b.Run("fiber", func(b *testing.B) { |
| 235 | b.ReportAllocs() |
| 236 | for b.Loop() { |
| 237 | FormatUint16(input) |
| 238 | } |
| 239 | }) |
| 240 | b.Run("strconv", func(b *testing.B) { |
| 241 | b.ReportAllocs() |
| 242 | for b.Loop() { |
| 243 | strconv.FormatUint(uint64(input), 10) |
| 244 | } |
| 245 | }) |
| 246 | } |
| 247 | |
| 248 | func Benchmark_FormatInt16(b *testing.B) { |
| 249 | input := int16(-12345) |
| 250 | |
| 251 | b.Run("fiber", func(b *testing.B) { |
| 252 | b.ReportAllocs() |
| 253 | for b.Loop() { |
| 254 | FormatInt16(input) |
| 255 | } |
| 256 | }) |
| 257 | b.Run("strconv", func(b *testing.B) { |
| 258 | b.ReportAllocs() |
| 259 | for b.Loop() { |
| 260 | strconv.FormatInt(int64(input), 10) |
| 261 | } |
| 262 | }) |
| 263 | } |
| 264 | |
| 265 | func Benchmark_FormatUint8(b *testing.B) { |
| 266 | input := uint8(255) |
| 267 | |
| 268 | b.Run("fiber", func(b *testing.B) { |
| 269 | b.ReportAllocs() |
| 270 | for b.Loop() { |
| 271 | FormatUint8(input) |
| 272 | } |
| 273 | }) |
| 274 | b.Run("strconv", func(b *testing.B) { |
| 275 | b.ReportAllocs() |
| 276 | for b.Loop() { |
| 277 | strconv.FormatUint(uint64(input), 10) |
| 278 | } |
| 279 | }) |
| 280 | } |
| 281 | |
| 282 | func Benchmark_FormatInt8(b *testing.B) { |
| 283 | input := int8(-128) |
| 284 | |
| 285 | b.Run("fiber", func(b *testing.B) { |
| 286 | b.ReportAllocs() |
| 287 | for b.Loop() { |
| 288 | FormatInt8(input) |
| 289 | } |
| 290 | }) |
| 291 | b.Run("strconv", func(b *testing.B) { |
| 292 | b.ReportAllocs() |
| 293 | for b.Loop() { |
| 294 | strconv.FormatInt(int64(input), 10) |
| 295 | } |
| 296 | }) |
| 297 | } |
| 298 | |
| 299 | func Benchmark_AppendUint(b *testing.B) { |
| 300 | input := uint64(123456789) |
| 301 | dst := make([]byte, 0, 32) |
| 302 | |
| 303 | b.Run("fiber", func(b *testing.B) { |
| 304 | b.ReportAllocs() |
| 305 | for b.Loop() { |
| 306 | AppendUint(dst, input) |
| 307 | } |
| 308 | }) |
| 309 | b.Run("strconv", func(b *testing.B) { |
| 310 | b.ReportAllocs() |
| 311 | for b.Loop() { |
| 312 | strconv.AppendUint(dst, input, 10) |
| 313 | } |
| 314 | }) |
| 315 | } |
| 316 | |
| 317 | func Benchmark_AppendInt(b *testing.B) { |
| 318 | inputs := []struct { |
| 319 | name string |
| 320 | value int64 |
| 321 | }{ |
| 322 | {"small_neg", -42}, |
| 323 | {"medium_neg", -123456789}, |
| 324 | } |
| 325 | dst := make([]byte, 0, 32) |
| 326 | |
| 327 | for _, input := range inputs { |
| 328 | b.Run(input.name+"/fiber", func(b *testing.B) { |
| 329 | b.ReportAllocs() |
| 330 | for b.Loop() { |
| 331 | AppendInt(dst, input.value) |
| 332 | } |
| 333 | }) |
| 334 | b.Run(input.name+"/strconv", func(b *testing.B) { |
| 335 | b.ReportAllocs() |
| 336 | for b.Loop() { |
| 337 | strconv.AppendInt(dst, input.value, 10) |
| 338 | } |
| 339 | }) |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | func Test_UintDigits_IntDigits(t *testing.T) { |
| 344 | t.Parallel() |
| 345 | checkU := func(n uint64) { |
| 346 | t.Helper() |
| 347 | require.Equal(t, len(strconv.FormatUint(n, 10)), uintDigits(n), "n=%d", n) |
| 348 | } |
| 349 | for n := range uint64(100000) { |
| 350 | checkU(n) |
| 351 | } |
| 352 | // Every power-of-ten boundary up to the uint64 limit. |
| 353 | p := uint64(1) |
| 354 | for range 20 { |
| 355 | checkU(p - 1) |
| 356 | checkU(p) |
| 357 | checkU(p + 1) |
| 358 | if p > math.MaxUint64/10 { |
| 359 | break |
| 360 | } |
| 361 | p *= 10 |
| 362 | } |
| 363 | checkU(math.MaxUint64) |
| 364 | |
| 365 | for n := int64(-100000); n < 100000; n++ { |
| 366 | require.Equal(t, len(strconv.FormatInt(n, 10)), intDigits(n), "n=%d", n) |
| 367 | } |
| 368 | require.Equal(t, 20, intDigits(math.MinInt64)) |
| 369 | require.Equal(t, 19, intDigits(math.MaxInt64)) |
| 370 | } |