Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1184)

Delta Between Two Patch Sets: src/pkg/fmt/fmt_test.go

Issue 7002055: code review 7002055: testing: add AllocsPerRun
Left Patch Set: diff -r f4e5087c1c19 http://code.google.com/p/go Created 11 years, 3 months ago
Right Patch Set: diff -r f50a112bfe3b http://code.google.com/p/go Created 11 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 88
89 type S struct { 89 type S struct {
90 F F // a struct field that Formats 90 F F // a struct field that Formats
91 G G // a struct field that GoStrings 91 G G // a struct field that GoStrings
92 } 92 }
93 93
94 type SI struct { 94 type SI struct {
95 I interface{} 95 I interface{}
96 } 96 }
97 97
98 // A type with a String method with pointer receiver for testing %p 98 // P is a type with a String method with pointer receiver for testing %p.
99 type P int 99 type P int
100 100
101 var pValue P 101 var pValue P
102 102
103 func (p *P) String() string { 103 func (p *P) String() string {
104 return "String(p)" 104 return "String(p)"
105 } 105 }
106 106
107 var b byte 107 var b byte
108 108
(...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
659 {"%+v", "{a:abc b:def c:123}"}, 657 {"%+v", "{a:abc b:def c:123}"},
660 } 658 }
661 for _, tt := range tests { 659 for _, tt := range tests {
662 out := Sprintf(tt.fmt, s) 660 out := Sprintf(tt.fmt, s)
663 if out != tt.out { 661 if out != tt.out {
664 t.Errorf("Sprintf(%q, &s) = %q, want %q", tt.fmt, out, t t.out) 662 t.Errorf("Sprintf(%q, &s) = %q, want %q", tt.fmt, out, t t.out)
665 } 663 }
666 } 664 }
667 } 665 }
668 666
669 // Check map printing using substrings so we don't depend on the print order. 667 // presentInMap checks map printing using substrings so we don't depend on the
668 // print order.
670 func presentInMap(s string, a []string, t *testing.T) { 669 func presentInMap(s string, a []string, t *testing.T) {
671 for i := 0; i < len(a); i++ { 670 for i := 0; i < len(a); i++ {
672 loc := strings.Index(s, a[i]) 671 loc := strings.Index(s, a[i])
673 if loc < 0 { 672 if loc < 0 {
674 t.Errorf("map print: expected to find %q in %q", a[i], s ) 673 t.Errorf("map print: expected to find %q in %q", a[i], s )
675 } 674 }
676 // make sure the match ends here 675 // make sure the match ends here
677 loc += len(a[i]) 676 loc += len(a[i])
678 if loc >= len(s) || (s[loc] != ' ' && s[loc] != ']') { 677 if loc >= len(s) || (s[loc] != ' ' && s[loc] != ']') {
679 t.Errorf("map print: %q not properly terminated in %q", a[i], s) 678 t.Errorf("map print: %q not properly terminated in %q", a[i], s)
(...skipping 20 matching lines...) Expand all
700 if s != emptyMapStr { 699 if s != emptyMapStr {
701 t.Errorf("nil map printed as %q not %q", s, emptyMapStr) 700 t.Errorf("nil map printed as %q not %q", s, emptyMapStr)
702 } 701 }
703 m = make(map[string]int) 702 m = make(map[string]int)
704 s = Sprint(m) 703 s = Sprint(m)
705 if s != emptyMapStr { 704 if s != emptyMapStr {
706 t.Errorf("empty map printed as %q not %q", s, emptyMapStr) 705 t.Errorf("empty map printed as %q not %q", s, emptyMapStr)
707 } 706 }
708 } 707 }
709 708
710 // Check that Sprint (and hence Print, Fprint) puts spaces in the right places, 709 // TestBlank checks that Sprint (and hence Print, Fprint) puts spaces in the
711 // that is, between arg pairs in which neither is a string. 710 // right places, that is, between arg pairs in which neither is a string.
712 func TestBlank(t *testing.T) { 711 func TestBlank(t *testing.T) {
713 got := Sprint("<", 1, ">:", 1, 2, 3, "!") 712 got := Sprint("<", 1, ">:", 1, 2, 3, "!")
714 expect := "<1>:1 2 3!" 713 expect := "<1>:1 2 3!"
715 if got != expect { 714 if got != expect {
716 t.Errorf("got %q expected %q", got, expect) 715 t.Errorf("got %q expected %q", got, expect)
717 } 716 }
718 } 717 }
719 718
720 // Check that Sprintln (and hence Println, Fprintln) puts spaces in the right pl aces, 719 // TestBlankln checks that Sprintln (and hence Println, Fprintln) puts spaces in
721 // that is, between all arg pairs. 720 // the right places, that is, between all arg pairs.
722 func TestBlankln(t *testing.T) { 721 func TestBlankln(t *testing.T) {
723 got := Sprintln("<", 1, ">:", 1, 2, 3, "!") 722 got := Sprintln("<", 1, ">:", 1, 2, 3, "!")
724 expect := "< 1 >: 1 2 3 !\n" 723 expect := "< 1 >: 1 2 3 !\n"
725 if got != expect { 724 if got != expect {
726 t.Errorf("got %q expected %q", got, expect) 725 t.Errorf("got %q expected %q", got, expect)
727 } 726 }
728 } 727 }
729 728
730 // Check Formatter with Sprint, Sprintln, Sprintf 729 // TestFormatterPrintln checks Formatter with Sprint, Sprintln, Sprintf.
731 func TestFormatterPrintln(t *testing.T) { 730 func TestFormatterPrintln(t *testing.T) {
732 f := F(1) 731 f := F(1)
733 expect := "<v=F(1)>\n" 732 expect := "<v=F(1)>\n"
734 s := Sprint(f, "\n") 733 s := Sprint(f, "\n")
735 if s != expect { 734 if s != expect {
736 t.Errorf("Sprint wrong with Formatter: expected %q got %q", expe ct, s) 735 t.Errorf("Sprint wrong with Formatter: expected %q got %q", expe ct, s)
737 } 736 }
738 s = Sprintln(f) 737 s = Sprintln(f)
739 if s != expect { 738 if s != expect {
740 t.Errorf("Sprintln wrong with Formatter: expected %q got %q", ex pect, s) 739 t.Errorf("Sprintln wrong with Formatter: expected %q got %q", ex pect, s)
(...skipping 28 matching lines...) Expand all
769 768
770 func TestWidthAndPrecision(t *testing.T) { 769 func TestWidthAndPrecision(t *testing.T) {
771 for _, tt := range startests { 770 for _, tt := range startests {
772 s := Sprintf(tt.fmt, tt.in...) 771 s := Sprintf(tt.fmt, tt.in...)
773 if s != tt.out { 772 if s != tt.out {
774 t.Errorf("%q: got %q expected %q", tt.fmt, s, tt.out) 773 t.Errorf("%q: got %q expected %q", tt.fmt, s, tt.out)
775 } 774 }
776 } 775 }
777 } 776 }
778 777
779 // A type that panics in String. 778 // Panic is a type that panics in String.
780 type Panic struct { 779 type Panic struct {
781 message interface{} 780 message interface{}
782 } 781 }
783 782
784 // Value receiver. 783 // Value receiver.
785 func (p Panic) GoString() string { 784 func (p Panic) GoString() string {
786 panic(p.message) 785 panic(p.message)
787 } 786 }
788 787
789 // Value receiver. 788 // Value receiver.
790 func (p Panic) String() string { 789 func (p Panic) String() string {
791 panic(p.message) 790 panic(p.message)
792 } 791 }
793 792
794 // A type that panics in Format. 793 // PanicF is a type that panics in Format.
795 type PanicF struct { 794 type PanicF struct {
796 message interface{} 795 message interface{}
797 } 796 }
798 797
799 // Value receiver. 798 // Value receiver.
800 func (p PanicF) Format(f State, c rune) { 799 func (p PanicF) Format(f State, c rune) {
801 panic(p.message) 800 panic(p.message)
802 } 801 }
803 802
804 var panictests = []struct { 803 var panictests = []struct {
(...skipping 17 matching lines...) Expand all
822 821
823 func TestPanics(t *testing.T) { 822 func TestPanics(t *testing.T) {
824 for _, tt := range panictests { 823 for _, tt := range panictests {
825 s := Sprintf(tt.fmt, tt.in) 824 s := Sprintf(tt.fmt, tt.in)
826 if s != tt.out { 825 if s != tt.out {
827 t.Errorf("%q: got %q expected %q", tt.fmt, s, tt.out) 826 t.Errorf("%q: got %q expected %q", tt.fmt, s, tt.out)
828 } 827 }
829 } 828 }
830 } 829 }
831 830
832 // Test that erroneous String routine doesn't cause fatal recursion. 831 // recurCount tests that erroneous String routine doesn't cause fatal recursion.
833 var recurCount = 0 832 var recurCount = 0
834 833
835 type Recur struct { 834 type Recur struct {
836 i int 835 i int
837 failed *bool 836 failed *bool
838 } 837 }
839 838
840 func (r Recur) String() string { 839 func (r Recur) String() string {
841 if recurCount++; recurCount > 10 { 840 if recurCount++; recurCount > 10 {
842 *r.failed = true 841 *r.failed = true
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
877 type A struct{} 876 type A struct{}
878 type B struct{} 877 type B struct{}
879 var a *A = nil 878 var a *A = nil
880 var b B = B{} 879 var b B = B{}
881 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)
882 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 >)"
883 if got != expect { 882 if got != expect {
884 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)
885 } 884 }
886 } 885 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b