fastfloat_test.tmpl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // +build amd64
  2. // Code generated by Makefile, DO NOT EDIT.
  3. // Code generated by Makefile, DO NOT EDIT.
  4. /*
  5. * Copyright 2021 ByteDance Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. package {{PACKAGE}}
  20. import (
  21. `encoding/json`
  22. `math`
  23. `math/rand`
  24. `strconv`
  25. `testing`
  26. `github.com/stretchr/testify/assert`
  27. )
  28. func TestFastFloat_Encode(t *testing.T) {
  29. var buf [64]byte
  30. assert.Equal(t, "0" , string(buf[:f64toa(&buf[0], 0)]))
  31. assert.Equal(t, "-0" , string(buf[:f64toa(&buf[0], math.Float64frombits(0x8000000000000000))]))
  32. assert.Equal(t, "12340000000" , string(buf[:f64toa(&buf[0], 1234e7)]))
  33. assert.Equal(t, "12.34" , string(buf[:f64toa(&buf[0], 1234e-2)]))
  34. assert.Equal(t, "0.001234" , string(buf[:f64toa(&buf[0], 1234e-6)]))
  35. assert.Equal(t, "1e+30" , string(buf[:f64toa(&buf[0], 1e30)]))
  36. assert.Equal(t, "1.234e+33" , string(buf[:f64toa(&buf[0], 1234e30)]))
  37. assert.Equal(t, "1.234e+308" , string(buf[:f64toa(&buf[0], 1234e305)]))
  38. assert.Equal(t, "1.234e-317" , string(buf[:f64toa(&buf[0], 1234e-320)]))
  39. assert.Equal(t, "1.7976931348623157e+308" , string(buf[:f64toa(&buf[0], 1.7976931348623157e308)]))
  40. assert.Equal(t, "-12340000000" , string(buf[:f64toa(&buf[0], -1234e7)]))
  41. assert.Equal(t, "-12.34" , string(buf[:f64toa(&buf[0], -1234e-2)]))
  42. assert.Equal(t, "-0.001234" , string(buf[:f64toa(&buf[0], -1234e-6)]))
  43. assert.Equal(t, "-1e+30" , string(buf[:f64toa(&buf[0], -1e30)]))
  44. assert.Equal(t, "-1.234e+33" , string(buf[:f64toa(&buf[0], -1234e30)]))
  45. assert.Equal(t, "-1.234e+308" , string(buf[:f64toa(&buf[0], -1234e305)]))
  46. assert.Equal(t, "-1.234e-317" , string(buf[:f64toa(&buf[0], -1234e-320)]))
  47. assert.Equal(t, "-2.2250738585072014e-308" , string(buf[:f64toa(&buf[0], -2.2250738585072014e-308)]))
  48. }
  49. func TestFastFloat_Random(t *testing.T) {
  50. var buf [64]byte
  51. N := 10000
  52. for i := 0; i < N; i++ {
  53. b64 := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
  54. f64 := math.Float64frombits(b64)
  55. jout, jerr := json.Marshal(f64)
  56. n := f64toa(&buf[0], f64)
  57. if jerr == nil {
  58. assert.Equal(t, jout, buf[:n])
  59. } else {
  60. assert.True(t, n == 0)
  61. }
  62. f32 := math.Float32frombits(rand.Uint32())
  63. jout, jerr = json.Marshal(f32)
  64. n = f32toa(&buf[0], f32)
  65. if jerr == nil {
  66. assert.Equal(t, jout, buf[:n])
  67. } else {
  68. assert.True(t, n == 0)
  69. }
  70. }
  71. }
  72. func BenchmarkParseFloat64(b *testing.B) {
  73. var f64toaBenches = []struct {
  74. name string
  75. float float64
  76. }{
  77. {"Zero", 0},
  78. {"Decimal", 33909},
  79. {"Float", 339.7784},
  80. {"Exp", -5.09e75},
  81. {"NegExp", -5.11e-95},
  82. {"LongExp", 1.234567890123456e-78},
  83. {"Big", 123456789123456789123456789},
  84. }
  85. for _, c := range f64toaBenches {
  86. f64bench := []struct {
  87. name string
  88. test func(*testing.B)
  89. }{{
  90. name: "StdLib",
  91. 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) }},
  92. }, {
  93. name: "FastFloat",
  94. test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { f64toa(&buf[0], c.float) }},
  95. }}
  96. for _, bm := range f64bench {
  97. name := bm.name + "_" + c.name
  98. b.Run(name, bm.test)
  99. }
  100. }
  101. }
  102. func BenchmarkParseFloat32(b *testing.B) {
  103. var f32toaBenches = []struct {
  104. name string
  105. float float32
  106. }{
  107. {"Zero", 0},
  108. {"Integer", 33909},
  109. {"ExactFraction", 3.375},
  110. {"Point", 339.7784},
  111. {"Exp", -5.09e25},
  112. {"NegExp", -5.11e-25},
  113. {"Shortest", 1.234567e-8},
  114. }
  115. for _, c := range f32toaBenches {
  116. bench := []struct {
  117. name string
  118. test func(*testing.B)
  119. }{{
  120. name: "StdLib32",
  121. 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) }},
  122. }, {
  123. name: "FastFloat32",
  124. test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { f32toa(&buf[0], c.float) }},
  125. }}
  126. for _, bm := range bench {
  127. name := bm.name + "_" + c.name
  128. b.Run(name, bm.test)
  129. }
  130. }
  131. }