MCPcopy
hub / github.com/tinylib/msgp / TestAppendFloat

Function TestAppendFloat

msgp/write_bytes_test.go:138–183  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

136}
137
138func TestAppendFloat(t *testing.T) {
139 rng := rand.New(rand.NewSource(0))
140 const n = 1e7
141 src := make([]float64, n)
142 for i := range src {
143 // ~50% full float64, 50% converted from float32.
144 if rng.Uint32()&1 == 1 {
145 src[i] = rng.NormFloat64()
146 } else {
147 src[i] = float64(math.MaxFloat32 * (0.5 - rng.Float32()))
148 }
149 }
150
151 var buf bytes.Buffer
152 en := NewWriter(&buf)
153
154 var bts []byte
155 for _, f := range src {
156 en.WriteFloat(f)
157 bts = AppendFloat(bts, f)
158 }
159 en.Flush()
160 if buf.Len() != len(bts) {
161 t.Errorf("encoder wrote %d; append wrote %d bytes", buf.Len(), len(bts))
162 }
163 t.Logf("%f bytes/value", float64(buf.Len())/n)
164 a, b := bts, buf.Bytes()
165 for i := range a {
166 if a[i] != b[i] {
167 t.Errorf("mismatch at byte %d, %d != %d", i, a[i], b[i])
168 break
169 }
170 }
171
172 for i, want := range src {
173 var got float64
174 var err error
175 got, a, err = ReadFloat64Bytes(a)
176 if err != nil {
177 t.Fatal(err)
178 }
179 if want != got {
180 t.Errorf("value #%d: want %v; got %v", i, want, got)
181 }
182 }
183}
184
185func BenchmarkAppendFloat(b *testing.B) {
186 rng := rand.New(rand.NewSource(0))

Callers

nothing calls this directly

Calls 6

WriteFloatMethod · 0.95
FlushMethod · 0.95
NewWriterFunction · 0.85
AppendFloatFunction · 0.85
ReadFloat64BytesFunction · 0.85
LenMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…