LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package fmt_test | 5 package fmt_test |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 . "fmt" | 9 . "fmt" |
10 "io" | 10 "io" |
(...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
578 // For %g we use a float32, not float64, to guarantee passing the argume
nt | 578 // For %g we use a float32, not float64, to guarantee passing the argume
nt |
579 // does not need to allocate memory to store the result in a pointer-siz
ed word. | 579 // does not need to allocate memory to store the result in a pointer-siz
ed word. |
580 {2, `Sprintf("%g")`, func() { Sprintf("%g", float32(3.14159)) }}, | 580 {2, `Sprintf("%g")`, func() { Sprintf("%g", float32(3.14159)) }}, |
581 {0, `Fprintf(buf, "%x %x %x")`, func() { mallocBuf.Reset(); Fprintf(&mal
locBuf, "%x %x %x", 7, 8, 9) }}, | 581 {0, `Fprintf(buf, "%x %x %x")`, func() { mallocBuf.Reset(); Fprintf(&mal
locBuf, "%x %x %x", 7, 8, 9) }}, |
582 {1, `Fprintf(buf, "%s")`, func() { mallocBuf.Reset(); Fprintf(&mallocBuf
, "%s", "hello") }}, | 582 {1, `Fprintf(buf, "%s")`, func() { mallocBuf.Reset(); Fprintf(&mallocBuf
, "%s", "hello") }}, |
583 } | 583 } |
584 | 584 |
585 var _ bytes.Buffer | 585 var _ bytes.Buffer |
586 | 586 |
587 func TestCountMallocs(t *testing.T) { | 587 func TestCountMallocs(t *testing.T) { |
588 const N = 100 | |
589 | |
590 for _, mt := range mallocTest { | 588 for _, mt := range mallocTest { |
591 » » mallocs := testing.AllocsPerRun(N, mt.fn) | 589 » » mallocs := testing.AllocsPerRun(100, mt.fn) |
592 if got, max := mallocs, float64(mt.count); got > max { | 590 if got, max := mallocs, float64(mt.count); got > max { |
593 t.Errorf("%s: got %v allocs, want <=%v", mt.desc, got, m
ax) | 591 t.Errorf("%s: got %v allocs, want <=%v", mt.desc, got, m
ax) |
594 } | 592 } |
595 } | 593 } |
596 } | 594 } |
597 | 595 |
598 type flagPrinter struct{} | 596 type flagPrinter struct{} |
599 | 597 |
600 func (*flagPrinter) Format(f State, c rune) { | 598 func (*flagPrinter) Format(f State, c rune) { |
601 s := "%" | 599 s := "%" |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
878 type A struct{} | 876 type A struct{} |
879 type B struct{} | 877 type B struct{} |
880 var a *A = nil | 878 var a *A = nil |
881 var b B = B{} | 879 var b B = B{} |
882 got := Sprintf("%s %s %s %s %s", nil, a, nil, b, nil) | 880 got := Sprintf("%s %s %s %s %s", nil, a, nil, b, nil) |
883 const expect = "%!s(<nil>) %!s(*fmt_test.A=<nil>) %!s(<nil>) {} %!s(<nil
>)" | 881 const expect = "%!s(<nil>) %!s(*fmt_test.A=<nil>) %!s(<nil>) {} %!s(<nil
>)" |
884 if got != expect { | 882 if got != expect { |
885 t.Errorf("expected:\n\t%q\ngot:\n\t%q", expect, got) | 883 t.Errorf("expected:\n\t%q\ngot:\n\t%q", expect, got) |
886 } | 884 } |
887 } | 885 } |
LEFT | RIGHT |