|
| 1 | +package encoders |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | + "math/rand/v2" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/splunk/stef/go/pkg" |
| 11 | +) |
| 12 | + |
| 13 | +func equalFloat(a, b float64) bool { |
| 14 | + if math.IsNaN(a) && math.IsNaN(b) { |
| 15 | + return true |
| 16 | + } |
| 17 | + return a == b |
| 18 | +} |
| 19 | + |
| 20 | +func verifyDecode(t *testing.T, values []float64) { |
| 21 | + // Encode |
| 22 | + var buf pkg.BitsWriter |
| 23 | + limiter := &pkg.SizeLimiter{} |
| 24 | + enc := &Float64Encoder{buf: buf, limiter: limiter} |
| 25 | + |
| 26 | + for _, v := range values { |
| 27 | + enc.Encode(v) |
| 28 | + } |
| 29 | + |
| 30 | + // Get encoded bytes |
| 31 | + var bytesBuf pkg.BytesWriter |
| 32 | + enc.buf.Close() |
| 33 | + enc.WriteTo(&bytesBuf) |
| 34 | + encoded := bytesBuf.Bytes() |
| 35 | + |
| 36 | + // Decode |
| 37 | + dec := &Float64Decoder{} |
| 38 | + dec.buf.Reset(encoded) |
| 39 | + |
| 40 | + for i, want := range values { |
| 41 | + var got float64 |
| 42 | + if err := dec.Decode(&got); err != nil { |
| 43 | + t.Fatalf("decode failed at %d: %v", i, err) |
| 44 | + } |
| 45 | + if !equalFloat(got, want) { |
| 46 | + t.Errorf( |
| 47 | + "mismatch at %d: got %v (bits 0x%x), want %v (bits 0x%x)", i, got, math.Float64bits(got), want, |
| 48 | + math.Float64bits(want), |
| 49 | + ) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestFloat64EncoderDecoder_Basic(t *testing.T) { |
| 55 | + values := []float64{1.0, 1.0, 2.0, 2.0, 3.1415, 3.1415, -1.0, 0.0, -0.0, math.NaN(), math.Inf(1), math.Inf(-1)} |
| 56 | + verifyDecode(t, values) |
| 57 | +} |
| 58 | + |
| 59 | +func TestFloat64EncoderDecoder_Bits(t *testing.T) { |
| 60 | + bits := []uint64{ |
| 61 | + 0x000000000000, |
| 62 | + 0x0FFFFFFFFFF0, |
| 63 | + 0x0F0000000000, |
| 64 | + 0x0F0000000F00, |
| 65 | + 0, |
| 66 | + ^uint64(0), |
| 67 | + 0, |
| 68 | + } |
| 69 | + |
| 70 | + values := []float64{} |
| 71 | + for _, bitVal := range bits { |
| 72 | + values = append(values, math.Float64frombits(bitVal)) |
| 73 | + } |
| 74 | + |
| 75 | + verifyDecode(t, values) |
| 76 | +} |
| 77 | + |
| 78 | +func TestFloat64EncoderDecoder_LeadingTrailing(t *testing.T) { |
| 79 | + // Values with only one bit difference, to test leading/trailing zeros logic |
| 80 | + base := math.Float64frombits(0x3FF0000000000000) // 1.0 |
| 81 | + values := []float64{base} |
| 82 | + for i := 0; i < 64; i++ { |
| 83 | + values = append(values, math.Float64frombits(math.Float64bits(base)^(1<<uint(i)))) |
| 84 | + } |
| 85 | + verifyDecode(t, values) |
| 86 | +} |
| 87 | + |
| 88 | +func randFloat(random *rand.Rand) float64 { |
| 89 | + // Generate random float64, including negative values |
| 90 | + v := random.Float64()*1e10 - 5e9 // range: [-5e9, +5e9) |
| 91 | + // Occasionally insert special values |
| 92 | + rval := random.IntN(50) |
| 93 | + if rval == 0 { |
| 94 | + v = math.NaN() |
| 95 | + } else if rval == 1 { |
| 96 | + v = math.Inf(1) |
| 97 | + } else if rval == 2 { |
| 98 | + v = math.Inf(-1) |
| 99 | + } |
| 100 | + return v |
| 101 | +} |
| 102 | + |
| 103 | +func TestFloat64EncoderDecoder_RandomSequence(t *testing.T) { |
| 104 | + randSeed := uint64(time.Now().UnixNano()) |
| 105 | + fmt.Printf("Using random seed: %d\n", randSeed) |
| 106 | + random := rand.New(rand.NewPCG(randSeed, 0)) |
| 107 | + |
| 108 | + n := 1 + random.IntN(2000) |
| 109 | + |
| 110 | + var buf pkg.BitsWriter |
| 111 | + limiter := &pkg.SizeLimiter{} |
| 112 | + enc := &Float64Encoder{buf: buf, limiter: limiter} |
| 113 | + |
| 114 | + for range n { |
| 115 | + v := randFloat(random) |
| 116 | + enc.Encode(v) |
| 117 | + if random.IntN(100) == 0 { |
| 118 | + enc.Reset() |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Get encoded bytes |
| 123 | + var bytesBuf pkg.BytesWriter |
| 124 | + enc.buf.Close() |
| 125 | + enc.WriteTo(&bytesBuf) |
| 126 | + encoded := bytesBuf.Bytes() |
| 127 | + |
| 128 | + dec := &Float64Decoder{} |
| 129 | + dec.buf.Reset(encoded) |
| 130 | + |
| 131 | + random = rand.New(rand.NewPCG(randSeed, 0)) |
| 132 | + random.IntN(2000) |
| 133 | + |
| 134 | + for i := range n { |
| 135 | + want := randFloat(random) |
| 136 | + var got float64 |
| 137 | + if err := dec.Decode(&got); err != nil { |
| 138 | + t.Fatalf("decode failed at %d: %v", i, err) |
| 139 | + } |
| 140 | + if !equalFloat(got, want) { |
| 141 | + t.Errorf( |
| 142 | + "mismatch at %d: got %v (bits 0x%x), want %v (bits 0x%x)", i, got, math.Float64bits(got), want, |
| 143 | + math.Float64bits(want), |
| 144 | + ) |
| 145 | + } |
| 146 | + if random.IntN(100) == 0 { |
| 147 | + dec.Reset() |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments