| 1 |
package schema |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
"math/rand/v2" |
| 6 |
"strconv" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
// formatFloatFixed must be indistinguishable from the strconv call it |
| 11 |
// replaces, so every case here checks against it. |
| 12 |
func checkFloatFixed(t *testing.T, f float64, bitSize int) { |
| 13 |
t.Helper() |
| 14 |
got := formatFloatFixed(f, bitSize) |
| 15 |
want := strconv.FormatFloat(f, 'f', encFloatPrec, bitSize) |
| 16 |
if got != want { |
| 17 |
t.Fatalf("formatFloatFixed(%v, %d) = %q, want %q", f, bitSize, got, want) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
// The formatter hard-codes the scale and buffer sizes that go with the |
| 22 |
// precision; pin them to encFloatPrec. |
| 23 |
func TestFormatFloatFixedConstants(t *testing.T) { |
| 24 |
t.Parallel() |
| 25 |
|
| 26 |
scale := 1 |
| 27 |
for i := 0; i < encFloatPrec; i++ { |
| 28 |
scale *= 10 |
| 29 |
} |
| 30 |
if scale != scaleFixed { |
| 31 |
t.Fatalf("scaleFixed = %d, want 10^%d = %d", scaleFixed, encFloatPrec, scale) |
| 32 |
} |
| 33 |
// The fraction is zero-padded by formatting scaleFixed+frac and dropping |
| 34 |
// the leading '1', so the scale must be exactly one digit longer. |
| 35 |
if got := len(strconv.Itoa(scaleFixed)); got != encFloatPrec+1 { |
| 36 |
t.Fatalf("scaleFixed has %d digits, want %d", got, encFloatPrec+1) |
| 37 |
} |
| 38 |
// The widest fast-path output must fit the stack buffer the formatter uses. |
| 39 |
widest := len(formatFloatFixed(-fastFixedLimit, 64)) |
| 40 |
if widest > 24 { |
| 41 |
t.Fatalf("widest fast-path output is %d bytes, buffer holds 24", widest) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestFormatFloatFixedEdgeCases(t *testing.T) { |
| 46 |
values := []float64{ |
| 47 |
0, math.Copysign(0, -1), |
| 48 |
1, -1, 0.5, -0.5, 3.14, 3.14159, 6.28, -6.28, |
| 49 |
0.1, 0.2, 0.3, 1.0 / 3.0, 2.0 / 3.0, |
| 50 |
1e-7, 1e-6, 1e-9, 0.0000005, -0.0000005, 0.0000015, 0.0000025, |
| 51 |
0.1234565, 0.1234575, 999999.9999994, 999999.9999995, |
| 52 |
123456789.987654321, 1e12, 1e13, 1e15, 1e16, |
| 53 |
4503599627370495.5, 4503599627370496, |
| 54 |
math.SmallestNonzeroFloat64, -math.SmallestNonzeroFloat64, |
| 55 |
math.MaxFloat64, -math.MaxFloat64, |
| 56 |
math.MaxFloat32, -math.MaxFloat32, |
| 57 |
math.Inf(1), math.Inf(-1), math.NaN(), |
| 58 |
} |
| 59 |
for _, f := range values { |
| 60 |
checkFloatFixed(t, f, 64) |
| 61 |
checkFloatFixed(t, f, 32) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// A float's exact expansion terminates at its last significand bit, so every |
| 66 |
// tie at six decimals is a dyadic rational m/2^k with small k — the only |
| 67 |
// inputs where round-half-to-even can disagree with round-half-up. |
| 68 |
func TestFormatFloatFixedTies(t *testing.T) { |
| 69 |
for k := 1; k <= 12; k++ { |
| 70 |
unit := math.Ldexp(1, -k) |
| 71 |
for m := int64(-200_000); m <= 200_000; m++ { |
| 72 |
checkFloatFixed(t, float64(m)*unit, 64) |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// One ULP either side of a x.xxxxxx5 boundary is where a rounding bug shows |
| 78 |
// up without being an exact tie. |
| 79 |
func TestFormatFloatFixedBoundaries(t *testing.T) { |
| 80 |
for m := int64(0); m < 200_000; m++ { |
| 81 |
f := float64(m)/1e6 + 5e-7 |
| 82 |
for _, v := range [...]float64{ |
| 83 |
math.Nextafter(f, math.Inf(-1)), f, math.Nextafter(f, math.Inf(1)), |
| 84 |
} { |
| 85 |
checkFloatFixed(t, v, 64) |
| 86 |
checkFloatFixed(t, -v, 64) |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func TestFormatFloatFixedRandom(t *testing.T) { |
| 92 |
r := rand.New(rand.NewPCG(0x5eed, 0xf10a7)) |
| 93 |
for i := 0; i < 200_000; i++ { |
| 94 |
var f float64 |
| 95 |
switch i % 5 { |
| 96 |
case 0: |
| 97 |
f = math.Float64frombits(r.Uint64()) |
| 98 |
case 1: |
| 99 |
f = (r.Float64() - 0.5) * math.Pow(10, float64(r.IntN(26)-13)) |
| 100 |
case 2: |
| 101 |
f = float64(r.Int64N(2_000_000_000)) / 1e6 |
| 102 |
case 3: |
| 103 |
f = float64(r.Int64N(4_000_000)-2_000_000) / 2e6 |
| 104 |
case 4: |
| 105 |
f = float64(r.Int64N(1_000_000_000_000)) + r.Float64() |
| 106 |
} |
| 107 |
if math.IsNaN(f) { |
| 108 |
continue |
| 109 |
} |
| 110 |
checkFloatFixed(t, f, 64) |
| 111 |
if a := math.Abs(f); a == 0 || (a <= math.MaxFloat32 && a > 1e-40) { |
| 112 |
checkFloatFixed(t, f, 32) |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// Sweeping float32 bit patterns covers subnormals and the whole exponent |
| 118 |
// range the 32-bit encoder path can see. |
| 119 |
func TestFormatFloatFixedFloat32Sweep(t *testing.T) { |
| 120 |
for u := uint64(0); u < 1<<32; u += 4127 { |
| 121 |
f := float64(math.Float32frombits(uint32(u))) |
| 122 |
if math.IsNaN(f) || math.IsInf(f, 0) { |
| 123 |
continue |
| 124 |
} |
| 125 |
checkFloatFixed(t, f, 32) |
| 126 |
checkFloatFixed(t, f, 64) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
func FuzzFormatFloatFixed(f *testing.F) { |
| 131 |
f.Add(uint64(0), false) |
| 132 |
f.Add(math.Float64bits(3.14159), false) |
| 133 |
f.Add(math.Float64bits(0.0000005), true) |
| 134 |
f.Fuzz(func(t *testing.T, u uint64, as32 bool) { |
| 135 |
v := math.Float64frombits(u) |
| 136 |
bitSize := 64 |
| 137 |
if as32 { |
| 138 |
bitSize = 32 |
| 139 |
} |
| 140 |
got := formatFloatFixed(v, bitSize) |
| 141 |
want := strconv.FormatFloat(v, 'f', encFloatPrec, bitSize) |
| 142 |
if got != want { |
| 143 |
t.Fatalf("formatFloatFixed(%v, %d) = %q, want %q", v, bitSize, got, want) |
| 144 |
} |
| 145 |
}) |
| 146 |
} |
| 147 |
|
| 148 |
func BenchmarkFormatFloatFixed(b *testing.B) { |
| 149 |
b.ReportAllocs() |
| 150 |
var s string |
| 151 |
for b.Loop() { |
| 152 |
s = formatFloatFixed(3.14159, 64) |
| 153 |
} |
| 154 |
_ = s |
| 155 |
} |