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