~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/strconv/ftoa_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
163
163
        for i := 0; i < N; i++ {
164
164
                bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
165
165
                x := math.Float64frombits(bits)
 
166
 
166
167
                shortFast := FormatFloat(x, 'g', -1, 64)
167
168
                SetOptimize(false)
168
169
                shortSlow := FormatFloat(x, 'g', -1, 64)
170
171
                if shortSlow != shortFast {
171
172
                        t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow)
172
173
                }
173
 
        }
174
 
}
175
174
 
176
 
func TestAppendFloatDoesntAllocate(t *testing.T) {
177
 
        n := numAllocations(func() {
178
 
                var buf [64]byte
179
 
                AppendFloat(buf[:0], 1.23, 'g', 5, 64)
180
 
        })
181
 
        want := 1 // TODO(bradfitz): this might be 0, once escape analysis is better
182
 
        if n != want {
183
 
                t.Errorf("with local buffer, did %d allocations, want %d", n, want)
184
 
        }
185
 
        n = numAllocations(func() {
186
 
                AppendFloat(globalBuf[:0], 1.23, 'g', 5, 64)
187
 
        })
188
 
        if n != 0 {
189
 
                t.Errorf("with reused buffer, did %d allocations, want 0", n)
 
175
                prec := rand.Intn(12) + 5
 
176
                shortFast = FormatFloat(x, 'e', prec, 64)
 
177
                SetOptimize(false)
 
178
                shortSlow = FormatFloat(x, 'e', prec, 64)
 
179
                SetOptimize(true)
 
180
                if shortSlow != shortFast {
 
181
                        t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow)
 
182
                }
190
183
        }
191
184
}
192
185
 
220
213
        }
221
214
}
222
215
 
223
 
func BenchmarkAppendFloatDecimal(b *testing.B) {
224
 
        dst := make([]byte, 0, 30)
225
 
        for i := 0; i < b.N; i++ {
226
 
                AppendFloat(dst, 33909, 'g', -1, 64)
227
 
        }
228
 
}
229
 
 
230
 
func BenchmarkAppendFloat(b *testing.B) {
231
 
        dst := make([]byte, 0, 30)
232
 
        for i := 0; i < b.N; i++ {
233
 
                AppendFloat(dst, 339.7784, 'g', -1, 64)
234
 
        }
235
 
}
236
 
 
237
 
func BenchmarkAppendFloatExp(b *testing.B) {
238
 
        dst := make([]byte, 0, 30)
239
 
        for i := 0; i < b.N; i++ {
240
 
                AppendFloat(dst, -5.09e75, 'g', -1, 64)
241
 
        }
242
 
}
243
 
 
244
 
func BenchmarkAppendFloatNegExp(b *testing.B) {
245
 
        dst := make([]byte, 0, 30)
246
 
        for i := 0; i < b.N; i++ {
247
 
                AppendFloat(dst, -5.11e-95, 'g', -1, 64)
248
 
        }
249
 
}
250
 
 
 
216
func benchmarkAppendFloat(b *testing.B, f float64, fmt byte, prec, bitSize int) {
 
217
        dst := make([]byte, 30)
 
218
        for i := 0; i < b.N; i++ {
 
219
                AppendFloat(dst[:0], f, fmt, prec, bitSize)
 
220
        }
 
221
}
 
222
 
 
223
func BenchmarkAppendFloatDecimal(b *testing.B) { benchmarkAppendFloat(b, 33909, 'g', -1, 64) }
 
224
func BenchmarkAppendFloat(b *testing.B)        { benchmarkAppendFloat(b, 339.7784, 'g', -1, 64) }
 
225
func BenchmarkAppendFloatExp(b *testing.B)     { benchmarkAppendFloat(b, -5.09e75, 'g', -1, 64) }
 
226
func BenchmarkAppendFloatNegExp(b *testing.B)  { benchmarkAppendFloat(b, -5.11e-95, 'g', -1, 64) }
251
227
func BenchmarkAppendFloatBig(b *testing.B) {
252
 
        dst := make([]byte, 0, 30)
253
 
        for i := 0; i < b.N; i++ {
254
 
                AppendFloat(dst, 123456789123456789123456789, 'g', -1, 64)
255
 
        }
 
228
        benchmarkAppendFloat(b, 123456789123456789123456789, 'g', -1, 64)
256
229
}
 
230
 
 
231
func BenchmarkAppendFloat32Integer(b *testing.B)       { benchmarkAppendFloat(b, 33909, 'g', -1, 32) }
 
232
func BenchmarkAppendFloat32ExactFraction(b *testing.B) { benchmarkAppendFloat(b, 3.375, 'g', -1, 32) }
 
233
func BenchmarkAppendFloat32Point(b *testing.B)         { benchmarkAppendFloat(b, 339.7784, 'g', -1, 32) }
 
234
func BenchmarkAppendFloat32Exp(b *testing.B)           { benchmarkAppendFloat(b, -5.09e25, 'g', -1, 32) }
 
235
func BenchmarkAppendFloat32NegExp(b *testing.B)        { benchmarkAppendFloat(b, -5.11e-25, 'g', -1, 32) }
 
236
 
 
237
func BenchmarkAppendFloat64Fixed1(b *testing.B) { benchmarkAppendFloat(b, 123456, 'e', 3, 64) }
 
238
func BenchmarkAppendFloat64Fixed2(b *testing.B) { benchmarkAppendFloat(b, 123.456, 'e', 3, 64) }
 
239
func BenchmarkAppendFloat64Fixed3(b *testing.B) { benchmarkAppendFloat(b, 1.23456e+78, 'e', 3, 64) }
 
240
func BenchmarkAppendFloat64Fixed4(b *testing.B) { benchmarkAppendFloat(b, 1.23456e-78, 'e', 3, 64) }