fastfloat_test.tmpl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // +build amd64
  2. // Code generated by Makefile, DO NOT EDIT.
  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. package {{PACKAGE}}
  19. import (
  20. `encoding/json`
  21. `math`
  22. `math/rand`
  23. `strconv`
  24. `testing`
  25. `github.com/stretchr/testify/assert`
  26. )
  27. func TestFastFloat_Encode(t *testing.T) {
  28. var buf [64]byte
  29. assert.Equal(t, "0" , string(buf[:f64toa(&buf[0], 0)]))
  30. assert.Equal(t, "-0" , string(buf[:f64toa(&buf[0], math.Float64frombits(0x8000000000000000))]))
  31. assert.Equal(t, "12340000000" , string(buf[:f64toa(&buf[0], 1234e7)]))
  32. assert.Equal(t, "12.34" , string(buf[:f64toa(&buf[0], 1234e-2)]))
  33. assert.Equal(t, "0.001234" , string(buf[:f64toa(&buf[0], 1234e-6)]))
  34. assert.Equal(t, "1e+30" , string(buf[:f64toa(&buf[0], 1e30)]))
  35. assert.Equal(t, "1.234e+33" , string(buf[:f64toa(&buf[0], 1234e30)]))
  36. assert.Equal(t, "1.234e+308" , string(buf[:f64toa(&buf[0], 1234e305)]))
  37. assert.Equal(t, "1.234e-317" , string(buf[:f64toa(&buf[0], 1234e-320)]))
  38. assert.Equal(t, "1.7976931348623157e+308" , string(buf[:f64toa(&buf[0], 1.7976931348623157e308)]))
  39. assert.Equal(t, "-12340000000" , string(buf[:f64toa(&buf[0], -1234e7)]))
  40. assert.Equal(t, "-12.34" , string(buf[:f64toa(&buf[0], -1234e-2)]))
  41. assert.Equal(t, "-0.001234" , string(buf[:f64toa(&buf[0], -1234e-6)]))
  42. assert.Equal(t, "-1e+30" , string(buf[:f64toa(&buf[0], -1e30)]))
  43. assert.Equal(t, "-1.234e+33" , string(buf[:f64toa(&buf[0], -1234e30)]))
  44. assert.Equal(t, "-1.234e+308" , string(buf[:f64toa(&buf[0], -1234e305)]))
  45. assert.Equal(t, "-1.234e-317" , string(buf[:f64toa(&buf[0], -1234e-320)]))
  46. assert.Equal(t, "-2.2250738585072014e-308" , string(buf[:f64toa(&buf[0], -2.2250738585072014e-308)]))
  47. }
  48. func TestFastFloat_Random(t *testing.T) {
  49. var buf [64]byte
  50. N := 10000
  51. for i := 0; i < N; i++ {
  52. b64 := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
  53. f64 := math.Float64frombits(b64)
  54. jout, jerr := json.Marshal(f64)
  55. n := f64toa(&buf[0], f64)
  56. if jerr == nil {
  57. assert.Equal(t, jout, buf[:n])
  58. } else {
  59. assert.True(t, n == 0)
  60. }
  61. f32 := math.Float32frombits(rand.Uint32())
  62. jout, jerr = json.Marshal(f32)
  63. n = f32toa(&buf[0], f32)
  64. if jerr == nil {
  65. assert.Equal(t, jout, buf[:n])
  66. } else {
  67. assert.True(t, n == 0)
  68. }
  69. }
  70. }
  71. func BenchmarkParseFloat64(b *testing.B) {
  72. var f64toaBenches = []struct {
  73. name string
  74. float float64
  75. }{
  76. {"Zero", 0},
  77. {"Decimal", 33909},
  78. {"Float", 339.7784},
  79. {"Exp", -5.09e75},
  80. {"NegExp", -5.11e-95},
  81. {"LongExp", 1.234567890123456e-78},
  82. {"Big", 123456789123456789123456789},
  83. }
  84. for _, c := range f64toaBenches {
  85. f64bench := []struct {
  86. name string
  87. test func(*testing.B)
  88. }{{
  89. name: "StdLib",
  90. 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) }},
  91. }, {
  92. name: "FastFloat",
  93. test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { f64toa(&buf[0], c.float) }},
  94. }}
  95. for _, bm := range f64bench {
  96. name := bm.name + "_" + c.name
  97. b.Run(name, bm.test)
  98. }
  99. }
  100. }
  101. func BenchmarkParseFloat32(b *testing.B) {
  102. var f32toaBenches = []struct {
  103. name string
  104. float float32
  105. }{
  106. {"Zero", 0},
  107. {"Integer", 33909},
  108. {"ExactFraction", 3.375},
  109. {"Point", 339.7784},
  110. {"Exp", -5.09e25},
  111. {"NegExp", -5.11e-25},
  112. {"Shortest", 1.234567e-8},
  113. }
  114. for _, c := range f32toaBenches {
  115. bench := []struct {
  116. name string
  117. test func(*testing.B)
  118. }{{
  119. name: "StdLib32",
  120. 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) }},
  121. }, {
  122. name: "FastFloat32",
  123. test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { f32toa(&buf[0], c.float) }},
  124. }}
  125. for _, bm := range bench {
  126. name := bm.name + "_" + c.name
  127. b.Run(name, bm.test)
  128. }
  129. }
  130. }