ftostr_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package ftoa
  2. import (
  3. "math"
  4. "strconv"
  5. "testing"
  6. )
  7. func _testFToStr(num float64, mode FToStrMode, precision int, expected string, t *testing.T) {
  8. buf := FToStr(num, mode, precision, nil)
  9. if s := string(buf); s != expected {
  10. t.Fatalf("expected: '%s', actual: '%s", expected, s)
  11. }
  12. if !math.IsNaN(num) && num != 0 && !math.Signbit(num) {
  13. _testFToStr(-num, mode, precision, "-"+expected, t)
  14. }
  15. }
  16. func testFToStr(num float64, mode FToStrMode, precision int, expected string, t *testing.T) {
  17. t.Run("", func(t *testing.T) {
  18. t.Parallel()
  19. _testFToStr(num, mode, precision, expected, t)
  20. })
  21. }
  22. func TestDtostr(t *testing.T) {
  23. testFToStr(0, ModeStandard, 0, "0", t)
  24. testFToStr(1, ModeStandard, 0, "1", t)
  25. testFToStr(9007199254740991, ModeStandard, 0, "9007199254740991", t)
  26. testFToStr(math.MaxInt64, ModeStandardExponential, 0, "9.223372036854776e+18", t)
  27. testFToStr(1e-5, ModeFixed, 1, "0.0", t)
  28. testFToStr(8.85, ModeExponential, 2, "8.8e+0", t)
  29. testFToStr(885, ModeExponential, 2, "8.9e+2", t)
  30. testFToStr(25, ModeExponential, 1, "3e+1", t)
  31. testFToStr(1e-6, ModeFixed, 7, "0.0000010", t)
  32. testFToStr(math.Pi, ModeStandardExponential, 0, "3.141592653589793e+0", t)
  33. testFToStr(math.Inf(1), ModeStandard, 0, "Infinity", t)
  34. testFToStr(math.NaN(), ModeStandard, 0, "NaN", t)
  35. testFToStr(math.SmallestNonzeroFloat64, ModeExponential, 40, "4.940656458412465441765687928682213723651e-324", t)
  36. testFToStr(3.5844466002796428e+298, ModeStandard, 0, "3.5844466002796428e+298", t)
  37. testFToStr(math.Float64frombits(0x0010000000000000), ModeStandard, 0, "2.2250738585072014e-308", t) // smallest normal
  38. testFToStr(math.Float64frombits(0x000FFFFFFFFFFFFF), ModeStandard, 0, "2.225073858507201e-308", t) // largest denormal
  39. testFToStr(4294967272.0, ModePrecision, 14, "4294967272.0000", t)
  40. }
  41. func BenchmarkDtostrSmall(b *testing.B) {
  42. var buf [128]byte
  43. b.ReportAllocs()
  44. for i := 0; i < b.N; i++ {
  45. FToStr(math.Pi, ModeStandardExponential, 0, buf[:0])
  46. }
  47. }
  48. func BenchmarkDtostrShort(b *testing.B) {
  49. var buf [128]byte
  50. b.ReportAllocs()
  51. for i := 0; i < b.N; i++ {
  52. FToStr(3.1415, ModeStandard, 0, buf[:0])
  53. }
  54. }
  55. func BenchmarkDtostrFixed(b *testing.B) {
  56. var buf [128]byte
  57. b.ReportAllocs()
  58. for i := 0; i < b.N; i++ {
  59. FToStr(math.Pi, ModeFixed, 4, buf[:0])
  60. }
  61. }
  62. func BenchmarkDtostrBig(b *testing.B) {
  63. var buf [128]byte
  64. b.ReportAllocs()
  65. for i := 0; i < b.N; i++ {
  66. FToStr(math.SmallestNonzeroFloat64, ModeExponential, 40, buf[:0])
  67. }
  68. }
  69. func BenchmarkAppendFloatBig(b *testing.B) {
  70. var buf [128]byte
  71. b.ReportAllocs()
  72. for i := 0; i < b.N; i++ {
  73. strconv.AppendFloat(buf[:0], math.SmallestNonzeroFloat64, 'e', 40, 64)
  74. }
  75. }
  76. func BenchmarkAppendFloatSmall(b *testing.B) {
  77. var buf [128]byte
  78. b.ReportAllocs()
  79. for i := 0; i < b.N; i++ {
  80. strconv.AppendFloat(buf[:0], math.Pi, 'e', -1, 64)
  81. }
  82. }