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

Side by Side Diff: src/pkg/fmt/fmt_test.go

Issue 9680043: code review 9680043: fmt.Printf: introduce notation for random access to arg... (Closed)
Patch Set: diff -r 9b11496e569e https://code.google.com/p/go/ Created 11 years, 10 months 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:
View unified diff | Download patch
« no previous file with comments | « src/pkg/fmt/doc.go ('k') | src/pkg/fmt/print.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 103
104 func (p *P) String() string { 104 func (p *P) String() string {
105 return "String(p)" 105 return "String(p)"
106 } 106 }
107 107
108 var barray = [5]renamedUint8{1, 2, 3, 4, 5} 108 var barray = [5]renamedUint8{1, 2, 3, 4, 5}
109 var bslice = barray[:] 109 var bslice = barray[:]
110 110
111 var b byte 111 var b byte
112 112
113 var fmttests = []struct { 113 var fmtTests = []struct {
114 fmt string 114 fmt string
115 val interface{} 115 val interface{}
116 out string 116 out string
117 }{ 117 }{
118 {"%d", 12345, "12345"}, 118 {"%d", 12345, "12345"},
119 {"%v", 12345, "12345"}, 119 {"%v", 12345, "12345"},
120 {"%t", true, "true"}, 120 {"%t", true, "true"},
121 121
122 // basic string 122 // basic string
123 {"%s", "abc", "abc"}, 123 {"%s", "abc", "abc"},
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 // Complex fmt used to leave the plus flag set for future entries in the array 496 // Complex fmt used to leave the plus flag set for future entries in the array
497 // causing +2+0i and +3+0i instead of 2+0i and 3+0i. 497 // causing +2+0i and +3+0i instead of 2+0i and 3+0i.
498 {"%v", []complex64{1, 2, 3}, "[(1+0i) (2+0i) (3+0i)]"}, 498 {"%v", []complex64{1, 2, 3}, "[(1+0i) (2+0i) (3+0i)]"},
499 {"%v", []complex128{1, 2, 3}, "[(1+0i) (2+0i) (3+0i)]"}, 499 {"%v", []complex128{1, 2, 3}, "[(1+0i) (2+0i) (3+0i)]"},
500 500
501 // Incomplete format specification caused crash. 501 // Incomplete format specification caused crash.
502 {"%.", 3, "%!.(int=3)"}, 502 {"%.", 3, "%!.(int=3)"},
503 } 503 }
504 504
505 func TestSprintf(t *testing.T) { 505 func TestSprintf(t *testing.T) {
506 » for _, tt := range fmttests { 506 » for _, tt := range fmtTests {
507 s := Sprintf(tt.fmt, tt.val) 507 s := Sprintf(tt.fmt, tt.val)
508 if i := strings.Index(tt.out, "PTR"); i >= 0 { 508 if i := strings.Index(tt.out, "PTR"); i >= 0 {
509 pattern := "PTR" 509 pattern := "PTR"
510 chars := "0123456789abcdefABCDEF" 510 chars := "0123456789abcdefABCDEF"
511 switch { 511 switch {
512 case strings.HasPrefix(tt.out[i:], "PTR_d"): 512 case strings.HasPrefix(tt.out[i:], "PTR_d"):
513 pattern = "PTR_d" 513 pattern = "PTR_d"
514 chars = chars[:10] 514 chars = chars[:10]
515 case strings.HasPrefix(tt.out[i:], "PTR_o"): 515 case strings.HasPrefix(tt.out[i:], "PTR_o"):
516 pattern = "PTR_o" 516 pattern = "PTR_o"
(...skipping 15 matching lines...) Expand all
532 // Don't requote the already-quoted strings. 532 // Don't requote the already-quoted strings.
533 // It's too confusing to read the errors. 533 // It's too confusing to read the errors.
534 t.Errorf("Sprintf(%q, %q) = <%s> want <%s>", tt. fmt, tt.val, s, tt.out) 534 t.Errorf("Sprintf(%q, %q) = <%s> want <%s>", tt. fmt, tt.val, s, tt.out)
535 } else { 535 } else {
536 t.Errorf("Sprintf(%q, %v) = %q want %q", tt.fmt, tt.val, s, tt.out) 536 t.Errorf("Sprintf(%q, %v) = %q want %q", tt.fmt, tt.val, s, tt.out)
537 } 537 }
538 } 538 }
539 } 539 }
540 } 540 }
541 541
542 type SE []interface{} // slice of empty; notational compactness.
543
544 var reorderTests = []struct {
545 fmt string
546 val SE
547 out string
548 }{
549 {"%[1]d", SE{1}, "1"},
550 {"%[2]d", SE{2, 1}, "1"},
551 {"%[2]d %[1]d", SE{1, 2}, "2 1"},
552 {"%[2]*[1]d", SE{2, 5}, " 2"},
553 {"%6.2f", SE{12.0}, " 12.00"},
554 {"%[3]*[2].*[1]f", SE{12.0, 2, 6}, " 12.00"},
555 {"%[1]*[2].*[3]f", SE{6, 2, 12.0}, " 12.00"},
556 // An actual use! Print the same arguments twice.
557 {"%d %d %d %#[1]o %#o %#o", SE{11, 12, 13}, "11 12 13 013 014 015"},
558
559 // Erroneous cases.
560 {"%[]d", SE{2, 1}, "%d(BADARGNUM)"},
561 {"%[-3]d", SE{2, 1}, "%d(BADARGNUM)"},
562 {"%[x]d", SE{2, 1}, "%d(BADARGNUM)"},
563 {"%[23]d", SE{2, 1}, "%d(BADARGNUM)"},
564 {"%[3]", SE{2, 1}, "%!(NOVERB)"},
565 {"%d %d %d %#[1]o %#o %#o %#o", SE{11, 12, 13}, "11 12 13 013 014 015 %o (MISSING)"},
566 }
567
568 func TestReorder(t *testing.T) {
569 for _, tt := range reorderTests {
570 s := Sprintf(tt.fmt, tt.val...)
571 if s != tt.out {
572 t.Errorf("Sprintf(%q, %v) = <%s> want <%s>", tt.fmt, tt. val, s, tt.out)
573 } else {
574 }
575 }
576 }
577
542 func BenchmarkSprintfEmpty(b *testing.B) { 578 func BenchmarkSprintfEmpty(b *testing.B) {
543 for i := 0; i < b.N; i++ { 579 for i := 0; i < b.N; i++ {
544 Sprintf("") 580 Sprintf("")
545 } 581 }
546 } 582 }
547 583
548 func BenchmarkSprintfString(b *testing.B) { 584 func BenchmarkSprintfString(b *testing.B) {
549 for i := 0; i < b.N; i++ { 585 for i := 0; i < b.N; i++ {
550 Sprintf("%s", "hello") 586 Sprintf("%s", "hello")
551 } 587 }
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
899 type A struct{} 935 type A struct{}
900 type B struct{} 936 type B struct{}
901 var a *A = nil 937 var a *A = nil
902 var b B = B{} 938 var b B = B{}
903 got := Sprintf("%s %s %s %s %s", nil, a, nil, b, nil) 939 got := Sprintf("%s %s %s %s %s", nil, a, nil, b, nil)
904 const expect = "%!s(<nil>) %!s(*fmt_test.A=<nil>) %!s(<nil>) {} %!s(<nil >)" 940 const expect = "%!s(<nil>) %!s(*fmt_test.A=<nil>) %!s(<nil>) {} %!s(<nil >)"
905 if got != expect { 941 if got != expect {
906 t.Errorf("expected:\n\t%q\ngot:\n\t%q", expect, got) 942 t.Errorf("expected:\n\t%q\ngot:\n\t%q", expect, got)
907 } 943 }
908 } 944 }
OLDNEW
« no previous file with comments | « src/pkg/fmt/doc.go ('k') | src/pkg/fmt/print.go » ('j') | no next file with comments »

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