1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 "runtime" // for the malloc count test only
28 renamedUintptr uintptr
31 renamedFloat32 float32
32 renamedFloat64 float64
33 renamedComplex64 complex64
34 renamedComplex128 complex128
37 func TestFmtInterface(t *testing.T) {
40 s := Sprintf("%s", i1)
42 t.Errorf(`Sprintf("%%s", empty("abc")) = %q want %q`, s, "abc")
47 const b32 uint32 = 1<<32 - 1
48 const b64 uint64 = 1<<64 - 1
50 var array = []int{1, 2, 3, 4, 5}
51 var iarray = []interface{}{1, "hello", 2.5, nil}
62 func (i I) String() string { return Sprintf("<%d>", int(i)) }
76 func (f F) Format(s State, c int) {
77 Fprintf(s, "<%c=F(%d)>", c, int(f))
82 func (g G) GoString() string {
83 return Sprintf("GoString(%d)", int(g))
87 f F // a struct field that Formats
88 g G // a struct field that GoStrings
91 // A type with a String method with pointer receiver for testing %p
96 func (p *P) String() string {
102 var fmttests = []struct {
107 {"%d", 12345, "12345"},
108 {"%v", 12345, "12345"},
109 {"%t", true, "true"},
112 {"%s", "abc", "abc"},
113 {"%x", "abc", "616263"},
114 {"%x", "xyz", "78797a"},
115 {"%X", "xyz", "78797A"},
116 {"%q", "abc", `"abc"`},
119 {"%s", []byte("abc"), "abc"},
120 {"%x", []byte("abc"), "616263"},
121 {"% x", []byte("abc\xff"), "61 62 63 ff"},
122 {"% X", []byte("abc\xff"), "61 62 63 FF"},
123 {"%x", []byte("xyz"), "78797a"},
124 {"%X", []byte("xyz"), "78797A"},
125 {"%q", []byte("abc"), `"abc"`},
128 {"%#q", `abc`, "`abc`"},
129 {"%#q", `"`, "`\"`"},
130 {"1 %#q", `\n`, "1 `\\n`"},
131 {"2 %#q", "\n", `2 "\n"`},
133 {"%q", "\a\b\f\r\n\t\v", `"\a\b\f\r\n\t\v"`},
134 {"%q", "abc\xffdef", `"abc\xffdef"`},
135 {"%q", "\u263a", `"\u263a"`},
136 {"%q", "\U0010ffff", `"\U0010ffff"`},
139 {"%5s", "abc", " abc"},
140 {"%2s", "\u263a", " \u263a"},
141 {"%-5s", "abc", "abc "},
142 {"%-8q", "abc", `"abc" `},
143 {"%05s", "abc", "00abc"},
144 {"%08q", "abc", `000"abc"`},
145 {"%5s", "abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz"},
146 {"%.5s", "abcdefghijklmnopqrstuvwxyz", "abcde"},
147 {"%.5s", "日本語日本語", "日本語日本"},
148 {"%.5s", []byte("日本語日本語"), "日本語日本"},
149 {"%.5q", "abcdefghijklmnopqrstuvwxyz", `"abcde"`},
150 {"%.3q", "日本語日本語", `"\u65e5\u672c\u8a9e"`},
151 {"%.3q", []byte("日本語日本語"), `"\u65e5\u672c\u8a9e"`},
152 {"%10.1q", "日本語日本語", ` "\u65e5"`},
155 {"%d", 12345, "12345"},
156 {"%d", -12345, "-12345"},
157 {"%10d", 12345, " 12345"},
158 {"%10d", -12345, " -12345"},
159 {"%+10d", 12345, " +12345"},
160 {"%010d", 12345, "0000012345"},
161 {"%010d", -12345, "-000012345"},
162 {"%-10d", 12345, "12345 "},
163 {"%010.3d", 1, " 001"},
164 {"%010.3d", -1, " -001"},
165 {"%+d", 12345, "+12345"},
166 {"%+d", -12345, "-12345"},
169 {"% d", 12345, " 12345"},
172 {"%U", 0x1, "U+0001"},
173 {"%U", uint(0x1), "U+0001"},
174 {"%.8U", 0x2, "U+00000002"},
175 {"%U", 0x1234, "U+1234"},
176 {"%U", 0x12345, "U+12345"},
177 {"%10.6U", 0xABC, " U+000ABC"},
178 {"%-10.6U", 0xABC, "U+000ABC "},
181 {"%+.3e", 0.0, "+0.000e+00"},
182 {"%+.3e", 1.0, "+1.000e+00"},
183 {"%+.3f", -1.0, "-1.000"},
184 {"% .3E", -1.0, "-1.000E+00"},
185 {"% .3e", 1.0, " 1.000e+00"},
186 {"%+.3g", 0.0, "+0"},
187 {"%+.3g", 1.0, "+1"},
188 {"%+.3g", -1.0, "-1"},
189 {"% .3g", -1.0, "-1"},
190 {"% .3g", 1.0, " 1"},
193 {"%+.3e", 0i, "(+0.000e+00+0.000e+00i)"},
194 {"%+.3f", 0i, "(+0.000+0.000i)"},
195 {"%+.3g", 0i, "(+0+0i)"},
196 {"%+.3e", 1 + 2i, "(+1.000e+00+2.000e+00i)"},
197 {"%+.3f", 1 + 2i, "(+1.000+2.000i)"},
198 {"%+.3g", 1 + 2i, "(+1+2i)"},
199 {"%.3e", 0i, "(0.000e+00+0.000e+00i)"},
200 {"%.3f", 0i, "(0.000+0.000i)"},
201 {"%.3g", 0i, "(0+0i)"},
202 {"%.3e", 1 + 2i, "(1.000e+00+2.000e+00i)"},
203 {"%.3f", 1 + 2i, "(1.000+2.000i)"},
204 {"%.3g", 1 + 2i, "(1+2i)"},
205 {"%.3e", -1 - 2i, "(-1.000e+00-2.000e+00i)"},
206 {"%.3f", -1 - 2i, "(-1.000-2.000i)"},
207 {"%.3g", -1 - 2i, "(-1-2i)"},
208 {"% .3E", -1 - 2i, "(-1.000E+00-2.000E+00i)"},
209 {"%+.3g", complex64(1 + 2i), "(+1+2i)"},
210 {"%+.3g", complex128(1 + 2i), "(+1+2i)"},
213 {"", 2, "%!(EXTRA int=2)"},
214 {"%d", "hello", "%!d(string=hello)"},
216 // old test/fmt_test.go
217 {"%d", 1234, "1234"},
218 {"%d", -1234, "-1234"},
219 {"%d", uint(1234), "1234"},
220 {"%d", uint32(b32), "4294967295"},
221 {"%d", uint64(b64), "18446744073709551615"},
222 {"%o", 01234, "1234"},
223 {"%#o", 01234, "01234"},
224 {"%o", uint32(b32), "37777777777"},
225 {"%o", uint64(b64), "1777777777777777777777"},
226 {"%x", 0x1234abcd, "1234abcd"},
227 {"%#x", 0x1234abcd, "0x1234abcd"},
228 {"%x", b32 - 0x1234567, "fedcba98"},
229 {"%X", 0x1234abcd, "1234ABCD"},
230 {"%X", b32 - 0x1234567, "FEDCBA98"},
232 {"%x", b64, "ffffffffffffffff"},
234 {"%b", b64, "1111111111111111111111111111111111111111111111111111111111111111"},
236 {"%e", 1.0, "1.000000e+00"},
237 {"%e", 1234.5678e3, "1.234568e+06"},
238 {"%e", 1234.5678e-8, "1.234568e-05"},
239 {"%e", -7.0, "-7.000000e+00"},
240 {"%e", -1e-9, "-1.000000e-09"},
241 {"%f", 1234.5678e3, "1234567.800000"},
242 {"%f", 1234.5678e-8, "0.000012"},
243 {"%f", -7.0, "-7.000000"},
244 {"%f", -1e-9, "-0.000000"},
245 {"%g", 1234.5678e3, "1.2345678e+06"},
246 {"%g", float32(1234.5678e3), "1.2345678e+06"},
247 {"%g", 1234.5678e-8, "1.2345678e-05"},
249 {"%g", -1e-9, "-1e-09"},
250 {"%g", float32(-1e-9), "-1e-09"},
251 {"%E", 1.0, "1.000000E+00"},
252 {"%E", 1234.5678e3, "1.234568E+06"},
253 {"%E", 1234.5678e-8, "1.234568E-05"},
254 {"%E", -7.0, "-7.000000E+00"},
255 {"%E", -1e-9, "-1.000000E-09"},
256 {"%G", 1234.5678e3, "1.2345678E+06"},
257 {"%G", float32(1234.5678e3), "1.2345678E+06"},
258 {"%G", 1234.5678e-8, "1.2345678E-05"},
260 {"%G", -1e-9, "-1E-09"},
261 {"%G", float32(-1e-9), "-1E-09"},
266 {"%20.8d", 1234, " 00001234"},
267 {"%20.8d", -1234, " -00001234"},
268 {"%20d", 1234, " 1234"},
269 {"%-20.8d", 1234, "00001234 "},
270 {"%-20.8d", -1234, "-00001234 "},
271 {"%-#20.8x", 0x1234abc, "0x01234abc "},
272 {"%-#20.8X", 0x1234abc, "0X01234ABC "},
273 {"%-#20.8o", 01234, "00001234 "},
274 {"%.20b", 7, "00000000000000000111"},
275 {"%20.5s", "qwertyuiop", " qwert"},
276 {"%.5s", "qwertyuiop", "qwert"},
277 {"%-20.5s", "qwertyuiop", "qwert "},
279 {"%-20c", 'x', "x "},
280 {"%20.6e", 1.2345e3, " 1.234500e+03"},
281 {"%20.6e", 1.2345e-3, " 1.234500e-03"},
282 {"%20e", 1.2345e3, " 1.234500e+03"},
283 {"%20e", 1.2345e-3, " 1.234500e-03"},
284 {"%20.8e", 1.2345e3, " 1.23450000e+03"},
285 {"%20f", 1.23456789e3, " 1234.567890"},
286 {"%20f", 1.23456789e-3, " 0.001235"},
287 {"%20f", 12345678901.23456789, " 12345678901.234568"},
288 {"%-20f", 1.23456789e3, "1234.567890 "},
289 {"%20.8f", 1.23456789e3, " 1234.56789000"},
290 {"%20.8f", 1.23456789e-3, " 0.00123457"},
291 {"%g", 1.23456789e3, "1234.56789"},
292 {"%g", 1.23456789e-3, "0.00123456789"},
293 {"%g", 1.23456789e20, "1.23456789e+20"},
294 {"%20e", math.Inf(1), " +Inf"},
295 {"%-20f", math.Inf(-1), "-Inf "},
296 {"%20g", math.NaN(), " NaN"},
299 {"%v", array, "[1 2 3 4 5]"},
300 {"%v", iarray, "[1 hello 2.5 <nil>]"},
301 {"%v", &array, "&[1 2 3 4 5]"},
302 {"%v", &iarray, "&[1 hello 2.5 <nil>]"},
305 {"%v", 1 + 2i, "(1+2i)"},
306 {"%v", complex64(1 + 2i), "(1+2i)"},
307 {"%v", complex128(1 + 2i), "(1+2i)"},
310 {"%v", A{1, 2, "a", []int{1, 2}}, `{1 2 a [1 2]}`},
311 {"%+v", A{1, 2, "a", []int{1, 2}}, `{i:1 j:2 s:a x:[1 2]}`},
313 // +v on structs with Stringable items
314 {"%+v", B{1, 2}, `{i:<1> j:2}`},
315 {"%+v", C{1, B{2, 3}}, `{i:1 B:{i:<2> j:3}}`},
317 // q on Stringable items
318 {"%s", I(23), `<23>`},
319 {"%q", I(23), `"<23>"`},
320 {"%x", I(23), `3c32333e`},
321 {"%d", I(23), `%!d(string=<23>)`},
324 {"%#v", A{1, 2, "a", []int{1, 2}}, `fmt_test.A{i:1, j:0x2, s:"a", x:[]int{1, 2}}`},
325 {"%#v", &b, "(*uint8)(0xPTR)"},
326 {"%#v", TestFmtInterface, "(func(*testing.T))(0xPTR)"},
327 {"%#v", make(chan int), "(chan int)(0xPTR)"},
328 {"%#v", uint64(1<<64 - 1), "0xffffffffffffffff"},
329 {"%#v", 1000000000, "1000000000"},
330 {"%#v", map[string]int{"a": 1, "b": 2}, `map[string] int{"a":1, "b":2}`},
331 {"%#v", map[string]B{"a": {1, 2}, "b": {3, 4}}, `map[string] fmt_test.B{"a":fmt_test.B{i:1, j:2}, "b":fmt_test.B{i:3, j:4}}`},
332 {"%#v", []string{"a", "b"}, `[]string{"a", "b"}`},
334 // slices with other formats
335 {"%#x", []int{1, 2, 15}, `[0x1 0x2 0xf]`},
336 {"%x", []int{1, 2, 15}, `[1 2 f]`},
337 {"%d", []int{1, 2, 15}, `[1 2 15]`},
338 {"%d", []byte{1, 2, 15}, `[1 2 15]`},
339 {"%q", []string{"a", "b"}, `["a" "b"]`},
342 {"%v", renamedBool(true), "true"},
343 {"%d", renamedBool(true), "%!d(fmt_test.renamedBool=true)"},
344 {"%o", renamedInt(8), "10"},
345 {"%d", renamedInt8(-9), "-9"},
346 {"%v", renamedInt16(10), "10"},
347 {"%v", renamedInt32(-11), "-11"},
348 {"%X", renamedInt64(255), "FF"},
349 {"%v", renamedUint(13), "13"},
350 {"%o", renamedUint8(14), "16"},
351 {"%X", renamedUint16(15), "F"},
352 {"%d", renamedUint32(16), "16"},
353 {"%X", renamedUint64(17), "11"},
354 {"%o", renamedUintptr(18), "22"},
355 {"%x", renamedString("thing"), "7468696e67"},
356 {"%d", renamedBytes([]byte{1, 2, 15}), `[1 2 15]`},
357 {"%q", renamedBytes([]byte("hello")), `"hello"`},
358 {"%v", renamedFloat32(22), "22"},
359 {"%v", renamedFloat64(33), "33"},
360 {"%v", renamedComplex64(3 + 4i), "(3+4i)"},
361 {"%v", renamedComplex128(4 - 3i), "(4-3i)"},
364 {"%x", F(1), "<x=F(1)>"},
366 {"%+v", S{F(4), G(5)}, "{f:<v=F(4)> g:5}"},
369 {"%#v", G(6), "GoString(6)"},
370 {"%#v", S{F(7), G(8)}, "fmt_test.S{f:<v=F(7)>, g:GoString(8)}"},
373 {"%T", (4 - 3i), "complex128"},
374 {"%T", renamedComplex128(4 - 3i), "fmt_test.renamedComplex128"},
375 {"%T", intVal, "int"},
376 {"%6T", &intVal, " *int"},
379 {"p0=%p", new(int), "p0=0xPTR"},
380 {"p1=%s", &pValue, "p1=String(p)"}, // String method...
381 {"p2=%p", &pValue, "p2=0xPTR"}, // ... not called with %p
382 {"p4=%#p", new(int), "p4=PTR"},
384 // %p on non-pointers
385 {"%p", make(chan int), "0xPTR"},
386 {"%p", make(map[int]int), "0xPTR"},
387 {"%p", make([]int, 1), "0xPTR"},
388 {"%p", 27, "%!p(int=27)"}, // not a pointer at all
391 {"%s %", "hello", "hello %!(NOVERB)"},
392 {"%s %.2", "hello", "hello %!(NOVERB)"},
393 {"%d", "hello", "%!d(string=hello)"},
394 {"no args", "hello", "no args%!(EXTRA string=hello)"},
395 {"%s", nil, "%!s(<nil>)"},
396 {"%T", nil, "<nil>"},
397 {"%-1", 100, "%!(NOVERB)%!(EXTRA int=100)"},
400 func TestSprintf(t *testing.T) {
401 for _, tt := range fmttests {
402 s := Sprintf(tt.fmt, tt.val)
403 if i := strings.Index(tt.out, "PTR"); i >= 0 {
405 for ; j < len(s); j++ {
407 if (c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F') {
411 s = s[0:i] + "PTR" + s[j:]
414 if _, ok := tt.val.(string); ok {
415 // Don't requote the already-quoted strings.
416 // It's too confusing to read the errors.
417 t.Errorf("Sprintf(%q, %q) = <%s> want <%s>", tt.fmt, tt.val, s, tt.out)
419 t.Errorf("Sprintf(%q, %v) = %q want %q", tt.fmt, tt.val, s, tt.out)
425 func BenchmarkSprintfEmpty(b *testing.B) {
426 for i := 0; i < b.N; i++ {
431 func BenchmarkSprintfString(b *testing.B) {
432 for i := 0; i < b.N; i++ {
433 Sprintf("%s", "hello")
437 func BenchmarkSprintfInt(b *testing.B) {
438 for i := 0; i < b.N; i++ {
443 func BenchmarkSprintfIntInt(b *testing.B) {
444 for i := 0; i < b.N; i++ {
445 Sprintf("%d %d", 5, 6)
449 func BenchmarkSprintfPrefixedInt(b *testing.B) {
450 for i := 0; i < b.N; i++ {
451 Sprintf("This is some meaningless prefix text that needs to be scanned %d", 6)
455 func TestCountMallocs(t *testing.T) {
459 mallocs := 0 - runtime.MemStats.Mallocs
460 for i := 0; i < 100; i++ {
463 mallocs += runtime.MemStats.Mallocs
464 Printf("mallocs per Sprintf(\"\"): %d\n", mallocs/100)
465 mallocs = 0 - runtime.MemStats.Mallocs
466 for i := 0; i < 100; i++ {
469 mallocs += runtime.MemStats.Mallocs
470 Printf("mallocs per Sprintf(\"xxx\"): %d\n", mallocs/100)
471 mallocs = 0 - runtime.MemStats.Mallocs
472 for i := 0; i < 100; i++ {
475 mallocs += runtime.MemStats.Mallocs
476 Printf("mallocs per Sprintf(\"%%x\"): %d\n", mallocs/100)
477 mallocs = 0 - runtime.MemStats.Mallocs
478 for i := 0; i < 100; i++ {
479 Sprintf("%x %x", i, i)
481 mallocs += runtime.MemStats.Mallocs
482 Printf("mallocs per Sprintf(\"%%x %%x\"): %d\n", mallocs/100)
485 type flagPrinter struct{}
487 func (*flagPrinter) Format(f State, c int) {
489 for i := 0; i < 128; i++ {
494 if w, ok := f.Width(); ok {
495 s += Sprintf("%d", w)
497 if p, ok := f.Precision(); ok {
498 s += Sprintf(".%d", p)
501 io.WriteString(f, "["+s+"]")
504 var flagtests = []struct {
514 {"%1.2a", "[%1.2a]"},
515 {"%-1.2a", "[%-1.2a]"},
516 {"%+1.2a", "[%+1.2a]"},
517 {"%-+1.2a", "[%+-1.2a]"},
518 {"%-+1.2abc", "[%+-1.2a]bc"},
519 {"%-1.2abc", "[%-1.2a]bc"},
522 func TestFlagParser(t *testing.T) {
523 var flagprinter flagPrinter
524 for _, tt := range flagtests {
525 s := Sprintf(tt.in, &flagprinter)
527 t.Errorf("Sprintf(%q, &flagprinter) => %q, want %q", tt.in, s, tt.out)
532 func TestStructPrinter(t *testing.T) {
541 var tests = []struct {
545 {"%v", "{abc def 123}"},
546 {"%+v", "{a:abc b:def c:123}"},
548 for _, tt := range tests {
549 out := Sprintf(tt.fmt, s)
551 t.Errorf("Sprintf(%q, &s) = %q, want %q", tt.fmt, out, tt.out)
556 // Check map printing using substrings so we don't depend on the print order.
557 func presentInMap(s string, a []string, t *testing.T) {
558 for i := 0; i < len(a); i++ {
559 loc := strings.Index(s, a[i])
561 t.Errorf("map print: expected to find %q in %q", a[i], s)
563 // make sure the match ends here
565 if loc >= len(s) || (s[loc] != ' ' && s[loc] != ']') {
566 t.Errorf("map print: %q not properly terminated in %q", a[i], s)
571 func TestMapPrinter(t *testing.T) {
572 m0 := make(map[int]string)
575 t.Errorf("empty map printed as %q not %q", s, "map[]")
577 m1 := map[int]string{1: "one", 2: "two", 3: "three"}
578 a := []string{"1:one", "2:two", "3:three"}
579 presentInMap(Sprintf("%v", m1), a, t)
580 presentInMap(Sprint(m1), a, t)
583 func TestEmptyMap(t *testing.T) {
584 const emptyMapStr = "map[]"
587 if s != emptyMapStr {
588 t.Errorf("nil map printed as %q not %q", s, emptyMapStr)
590 m = make(map[string]int)
592 if s != emptyMapStr {
593 t.Errorf("empty map printed as %q not %q", s, emptyMapStr)
597 // Check that Sprint (and hence Print, Fprint) puts spaces in the right places,
598 // that is, between arg pairs in which neither is a string.
599 func TestBlank(t *testing.T) {
600 got := Sprint("<", 1, ">:", 1, 2, 3, "!")
601 expect := "<1>:1 2 3!"
603 t.Errorf("got %q expected %q", got, expect)
607 // Check that Sprintln (and hence Println, Fprintln) puts spaces in the right places,
608 // that is, between all arg pairs.
609 func TestBlankln(t *testing.T) {
610 got := Sprintln("<", 1, ">:", 1, 2, 3, "!")
611 expect := "< 1 >: 1 2 3 !\n"
613 t.Errorf("got %q expected %q", got, expect)
618 // Check Formatter with Sprint, Sprintln, Sprintf
619 func TestFormatterPrintln(t *testing.T) {
621 expect := "<v=F(1)>\n"
624 t.Errorf("Sprint wrong with Formatter: expected %q got %q", expect, s)
628 t.Errorf("Sprintln wrong with Formatter: expected %q got %q", expect, s)
630 s = Sprintf("%v\n", f)
632 t.Errorf("Sprintf wrong with Formatter: expected %q got %q", expect, s)
636 func args(a ...interface{}) []interface{} { return a }
638 var startests = []struct {
643 {"%*d", args(4, 42), " 42"},
644 {"%.*d", args(4, 42), "0042"},
645 {"%*.*d", args(8, 4, 42), " 0042"},
646 {"%0*d", args(4, 42), "0042"},
647 {"%-*d", args(4, 42), "42 "},
650 {"%*d", args(nil, 42), "%!(BADWIDTH)42"},
651 {"%.*d", args(nil, 42), "%!(BADPREC)42"},
652 {"%*d", args(5, "foo"), "%!d(string= foo)"},
653 {"%*% %d", args(20, 5), "% 5"},
654 {"%*", args(4), "%!(NOVERB)"},
655 {"%*d", args(int32(4), 42), "%!(BADWIDTH)42"},
658 func TestWidthAndPrecision(t *testing.T) {
659 for _, tt := range startests {
660 s := Sprintf(tt.fmt, tt.in...)
662 t.Errorf("%q: got %q expected %q", tt.fmt, s, tt.out)