github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/fmt/fmt_test.go (about) 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. 4 5 package fmt_test 6 7 import ( 8 "bytes" 9 . "fmt" 10 "internal/race" 11 "io" 12 "math" 13 "reflect" 14 "runtime" 15 "strings" 16 "testing" 17 "time" 18 "unicode" 19 ) 20 21 type ( 22 renamedBool bool 23 renamedInt int 24 renamedInt8 int8 25 renamedInt16 int16 26 renamedInt32 int32 27 renamedInt64 int64 28 renamedUint uint 29 renamedUint8 uint8 30 renamedUint16 uint16 31 renamedUint32 uint32 32 renamedUint64 uint64 33 renamedUintptr uintptr 34 renamedString string 35 renamedBytes []byte 36 renamedFloat32 float32 37 renamedFloat64 float64 38 renamedComplex64 complex64 39 renamedComplex128 complex128 40 ) 41 42 func TestFmtInterface(t *testing.T) { 43 var i1 interface{} 44 i1 = "abc" 45 s := Sprintf("%s", i1) 46 if s != "abc" { 47 t.Errorf(`Sprintf("%%s", empty("abc")) = %q want %q`, s, "abc") 48 } 49 } 50 51 var ( 52 NaN = math.NaN() 53 posInf = math.Inf(1) 54 negInf = math.Inf(-1) 55 56 intVar = 0 57 58 array = [5]int{1, 2, 3, 4, 5} 59 iarray = [4]interface{}{1, "hello", 2.5, nil} 60 slice = array[:] 61 islice = iarray[:] 62 ) 63 64 type A struct { 65 i int 66 j uint 67 s string 68 x []int 69 } 70 71 type I int 72 73 func (i I) String() string { return Sprintf("<%d>", int(i)) } 74 75 type B struct { 76 I I 77 j int 78 } 79 80 type C struct { 81 i int 82 B 83 } 84 85 type F int 86 87 func (f F) Format(s State, c rune) { 88 Fprintf(s, "<%c=F(%d)>", c, int(f)) 89 } 90 91 type G int 92 93 func (g G) GoString() string { 94 return Sprintf("GoString(%d)", int(g)) 95 } 96 97 type S struct { 98 F F // a struct field that Formats 99 G G // a struct field that GoStrings 100 } 101 102 type SI struct { 103 I interface{} 104 } 105 106 // P is a type with a String method with pointer receiver for testing %p. 107 type P int 108 109 var pValue P 110 111 func (p *P) String() string { 112 return "String(p)" 113 } 114 115 var barray = [5]renamedUint8{1, 2, 3, 4, 5} 116 var bslice = barray[:] 117 118 type byteStringer byte 119 120 func (byteStringer) String() string { 121 return "X" 122 } 123 124 var byteStringerSlice = []byteStringer{'h', 'e', 'l', 'l', 'o'} 125 126 type byteFormatter byte 127 128 func (byteFormatter) Format(f State, _ rune) { 129 Fprint(f, "X") 130 } 131 132 var byteFormatterSlice = []byteFormatter{'h', 'e', 'l', 'l', 'o'} 133 134 type writeStringFormatter string 135 136 func (sf writeStringFormatter) Format(f State, c rune) { 137 if sw, ok := f.(io.StringWriter); ok { 138 sw.WriteString("***" + string(sf) + "***") 139 } 140 } 141 142 var fmtTests = []struct { 143 fmt string 144 val interface{} 145 out string 146 }{ 147 {"%d", 12345, "12345"}, 148 {"%v", 12345, "12345"}, 149 {"%t", true, "true"}, 150 151 // basic string 152 {"%s", "abc", "abc"}, 153 {"%q", "abc", `"abc"`}, 154 {"%x", "abc", "616263"}, 155 {"%x", "\xff\xf0\x0f\xff", "fff00fff"}, 156 {"%X", "\xff\xf0\x0f\xff", "FFF00FFF"}, 157 {"%x", "", ""}, 158 {"% x", "", ""}, 159 {"%#x", "", ""}, 160 {"%# x", "", ""}, 161 {"%x", "xyz", "78797a"}, 162 {"%X", "xyz", "78797A"}, 163 {"% x", "xyz", "78 79 7a"}, 164 {"% X", "xyz", "78 79 7A"}, 165 {"%#x", "xyz", "0x78797a"}, 166 {"%#X", "xyz", "0X78797A"}, 167 {"%# x", "xyz", "0x78 0x79 0x7a"}, 168 {"%# X", "xyz", "0X78 0X79 0X7A"}, 169 170 // basic bytes 171 {"%s", []byte("abc"), "abc"}, 172 {"%s", [3]byte{'a', 'b', 'c'}, "abc"}, 173 {"%s", &[3]byte{'a', 'b', 'c'}, "&abc"}, 174 {"%q", []byte("abc"), `"abc"`}, 175 {"%x", []byte("abc"), "616263"}, 176 {"%x", []byte("\xff\xf0\x0f\xff"), "fff00fff"}, 177 {"%X", []byte("\xff\xf0\x0f\xff"), "FFF00FFF"}, 178 {"%x", []byte(""), ""}, 179 {"% x", []byte(""), ""}, 180 {"%#x", []byte(""), ""}, 181 {"%# x", []byte(""), ""}, 182 {"%x", []byte("xyz"), "78797a"}, 183 {"%X", []byte("xyz"), "78797A"}, 184 {"% x", []byte("xyz"), "78 79 7a"}, 185 {"% X", []byte("xyz"), "78 79 7A"}, 186 {"%#x", []byte("xyz"), "0x78797a"}, 187 {"%#X", []byte("xyz"), "0X78797A"}, 188 {"%# x", []byte("xyz"), "0x78 0x79 0x7a"}, 189 {"%# X", []byte("xyz"), "0X78 0X79 0X7A"}, 190 191 // escaped strings 192 {"%q", "", `""`}, 193 {"%#q", "", "``"}, 194 {"%q", "\"", `"\""`}, 195 {"%#q", "\"", "`\"`"}, 196 {"%q", "`", `"` + "`" + `"`}, 197 {"%#q", "`", `"` + "`" + `"`}, 198 {"%q", "\n", `"\n"`}, 199 {"%#q", "\n", `"\n"`}, 200 {"%q", `\n`, `"\\n"`}, 201 {"%#q", `\n`, "`\\n`"}, 202 {"%q", "abc", `"abc"`}, 203 {"%#q", "abc", "`abc`"}, 204 {"%q", "日本語", `"日本語"`}, 205 {"%+q", "日本語", `"\u65e5\u672c\u8a9e"`}, 206 {"%#q", "日本語", "`日本語`"}, 207 {"%#+q", "日本語", "`日本語`"}, 208 {"%q", "\a\b\f\n\r\t\v\"\\", `"\a\b\f\n\r\t\v\"\\"`}, 209 {"%+q", "\a\b\f\n\r\t\v\"\\", `"\a\b\f\n\r\t\v\"\\"`}, 210 {"%#q", "\a\b\f\n\r\t\v\"\\", `"\a\b\f\n\r\t\v\"\\"`}, 211 {"%#+q", "\a\b\f\n\r\t\v\"\\", `"\a\b\f\n\r\t\v\"\\"`}, 212 {"%q", "☺", `"☺"`}, 213 {"% q", "☺", `"☺"`}, // The space modifier should have no effect. 214 {"%+q", "☺", `"\u263a"`}, 215 {"%#q", "☺", "`☺`"}, 216 {"%#+q", "☺", "`☺`"}, 217 {"%10q", "⌘", ` "⌘"`}, 218 {"%+10q", "⌘", ` "\u2318"`}, 219 {"%-10q", "⌘", `"⌘" `}, 220 {"%+-10q", "⌘", `"\u2318" `}, 221 {"%010q", "⌘", `0000000"⌘"`}, 222 {"%+010q", "⌘", `00"\u2318"`}, 223 {"%-010q", "⌘", `"⌘" `}, // 0 has no effect when - is present. 224 {"%+-010q", "⌘", `"\u2318" `}, 225 {"%#8q", "\n", ` "\n"`}, 226 {"%#+8q", "\r", ` "\r"`}, 227 {"%#-8q", "\t", "` ` "}, 228 {"%#+-8q", "\b", `"\b" `}, 229 {"%q", "abc\xffdef", `"abc\xffdef"`}, 230 {"%+q", "abc\xffdef", `"abc\xffdef"`}, 231 {"%#q", "abc\xffdef", `"abc\xffdef"`}, 232 {"%#+q", "abc\xffdef", `"abc\xffdef"`}, 233 // Runes that are not printable. 234 {"%q", "\U0010ffff", `"\U0010ffff"`}, 235 {"%+q", "\U0010ffff", `"\U0010ffff"`}, 236 {"%#q", "\U0010ffff", "``"}, 237 {"%#+q", "\U0010ffff", "``"}, 238 // Runes that are not valid. 239 {"%q", string(0x110000), `"�"`}, 240 {"%+q", string(0x110000), `"\ufffd"`}, 241 {"%#q", string(0x110000), "`�`"}, 242 {"%#+q", string(0x110000), "`�`"}, 243 244 // characters 245 {"%c", uint('x'), "x"}, 246 {"%c", 0xe4, "ä"}, 247 {"%c", 0x672c, "本"}, 248 {"%c", '日', "日"}, 249 {"%.0c", '⌘', "⌘"}, // Specifying precision should have no effect. 250 {"%3c", '⌘', " ⌘"}, 251 {"%-3c", '⌘', "⌘ "}, 252 // Runes that are not printable. 253 {"%c", '\U00000e00', "\u0e00"}, 254 {"%c", '\U0010ffff', "\U0010ffff"}, 255 // Runes that are not valid. 256 {"%c", -1, "�"}, 257 {"%c", 0xDC80, "�"}, 258 {"%c", rune(0x110000), "�"}, 259 {"%c", int64(0xFFFFFFFFF), "�"}, 260 {"%c", uint64(0xFFFFFFFFF), "�"}, 261 262 // escaped characters 263 {"%q", uint(0), `'\x00'`}, 264 {"%+q", uint(0), `'\x00'`}, 265 {"%q", '"', `'"'`}, 266 {"%+q", '"', `'"'`}, 267 {"%q", '\'', `'\''`}, 268 {"%+q", '\'', `'\''`}, 269 {"%q", '`', "'`'"}, 270 {"%+q", '`', "'`'"}, 271 {"%q", 'x', `'x'`}, 272 {"%+q", 'x', `'x'`}, 273 {"%q", 'ÿ', `'ÿ'`}, 274 {"%+q", 'ÿ', `'\u00ff'`}, 275 {"%q", '\n', `'\n'`}, 276 {"%+q", '\n', `'\n'`}, 277 {"%q", '☺', `'☺'`}, 278 {"%+q", '☺', `'\u263a'`}, 279 {"% q", '☺', `'☺'`}, // The space modifier should have no effect. 280 {"%.0q", '☺', `'☺'`}, // Specifying precision should have no effect. 281 {"%10q", '⌘', ` '⌘'`}, 282 {"%+10q", '⌘', ` '\u2318'`}, 283 {"%-10q", '⌘', `'⌘' `}, 284 {"%+-10q", '⌘', `'\u2318' `}, 285 {"%010q", '⌘', `0000000'⌘'`}, 286 {"%+010q", '⌘', `00'\u2318'`}, 287 {"%-010q", '⌘', `'⌘' `}, // 0 has no effect when - is present. 288 {"%+-010q", '⌘', `'\u2318' `}, 289 // Runes that are not printable. 290 {"%q", '\U00000e00', `'\u0e00'`}, 291 {"%q", '\U0010ffff', `'\U0010ffff'`}, 292 // Runes that are not valid. 293 {"%q", int32(-1), "%!q(int32=-1)"}, 294 {"%q", 0xDC80, `'�'`}, 295 {"%q", rune(0x110000), "%!q(int32=1114112)"}, 296 {"%q", int64(0xFFFFFFFFF), "%!q(int64=68719476735)"}, 297 {"%q", uint64(0xFFFFFFFFF), "%!q(uint64=68719476735)"}, 298 299 // width 300 {"%5s", "abc", " abc"}, 301 {"%5s", []byte("abc"), " abc"}, 302 {"%2s", "\u263a", " ☺"}, 303 {"%2s", []byte("\u263a"), " ☺"}, 304 {"%-5s", "abc", "abc "}, 305 {"%-5s", []byte("abc"), "abc "}, 306 {"%05s", "abc", "00abc"}, 307 {"%05s", []byte("abc"), "00abc"}, 308 {"%5s", "abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz"}, 309 {"%5s", []byte("abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz"}, 310 {"%.5s", "abcdefghijklmnopqrstuvwxyz", "abcde"}, 311 {"%.5s", []byte("abcdefghijklmnopqrstuvwxyz"), "abcde"}, 312 {"%.0s", "日本語日本語", ""}, 313 {"%.0s", []byte("日本語日本語"), ""}, 314 {"%.5s", "日本語日本語", "日本語日本"}, 315 {"%.5s", []byte("日本語日本語"), "日本語日本"}, 316 {"%.10s", "日本語日本語", "日本語日本語"}, 317 {"%.10s", []byte("日本語日本語"), "日本語日本語"}, 318 {"%08q", "abc", `000"abc"`}, 319 {"%08q", []byte("abc"), `000"abc"`}, 320 {"%-8q", "abc", `"abc" `}, 321 {"%-8q", []byte("abc"), `"abc" `}, 322 {"%.5q", "abcdefghijklmnopqrstuvwxyz", `"abcde"`}, 323 {"%.5q", []byte("abcdefghijklmnopqrstuvwxyz"), `"abcde"`}, 324 {"%.5x", "abcdefghijklmnopqrstuvwxyz", "6162636465"}, 325 {"%.5x", []byte("abcdefghijklmnopqrstuvwxyz"), "6162636465"}, 326 {"%.3q", "日本語日本語", `"日本語"`}, 327 {"%.3q", []byte("日本語日本語"), `"日本語"`}, 328 {"%.1q", "日本語", `"日"`}, 329 {"%.1q", []byte("日本語"), `"日"`}, 330 {"%.1x", "日本語", "e6"}, 331 {"%.1X", []byte("日本語"), "E6"}, 332 {"%10.1q", "日本語日本語", ` "日"`}, 333 {"%10.1q", []byte("日本語日本語"), ` "日"`}, 334 {"%10v", nil, " <nil>"}, 335 {"%-10v", nil, "<nil> "}, 336 337 // integers 338 {"%d", uint(12345), "12345"}, 339 {"%d", int(-12345), "-12345"}, 340 {"%d", ^uint8(0), "255"}, 341 {"%d", ^uint16(0), "65535"}, 342 {"%d", ^uint32(0), "4294967295"}, 343 {"%d", ^uint64(0), "18446744073709551615"}, 344 {"%d", int8(-1 << 7), "-128"}, 345 {"%d", int16(-1 << 15), "-32768"}, 346 {"%d", int32(-1 << 31), "-2147483648"}, 347 {"%d", int64(-1 << 63), "-9223372036854775808"}, 348 {"%.d", 0, ""}, 349 {"%.0d", 0, ""}, 350 {"%6.0d", 0, " "}, 351 {"%06.0d", 0, " "}, 352 {"% d", 12345, " 12345"}, 353 {"%+d", 12345, "+12345"}, 354 {"%+d", -12345, "-12345"}, 355 {"%b", 7, "111"}, 356 {"%b", -6, "-110"}, 357 {"%b", ^uint32(0), "11111111111111111111111111111111"}, 358 {"%b", ^uint64(0), "1111111111111111111111111111111111111111111111111111111111111111"}, 359 {"%b", int64(-1 << 63), zeroFill("-1", 63, "")}, 360 {"%o", 01234, "1234"}, 361 {"%#o", 01234, "01234"}, 362 {"%o", ^uint32(0), "37777777777"}, 363 {"%o", ^uint64(0), "1777777777777777777777"}, 364 {"%#X", 0, "0X0"}, 365 {"%x", 0x12abcdef, "12abcdef"}, 366 {"%X", 0x12abcdef, "12ABCDEF"}, 367 {"%x", ^uint32(0), "ffffffff"}, 368 {"%X", ^uint64(0), "FFFFFFFFFFFFFFFF"}, 369 {"%.20b", 7, "00000000000000000111"}, 370 {"%10d", 12345, " 12345"}, 371 {"%10d", -12345, " -12345"}, 372 {"%+10d", 12345, " +12345"}, 373 {"%010d", 12345, "0000012345"}, 374 {"%010d", -12345, "-000012345"}, 375 {"%20.8d", 1234, " 00001234"}, 376 {"%20.8d", -1234, " -00001234"}, 377 {"%020.8d", 1234, " 00001234"}, 378 {"%020.8d", -1234, " -00001234"}, 379 {"%-20.8d", 1234, "00001234 "}, 380 {"%-20.8d", -1234, "-00001234 "}, 381 {"%-#20.8x", 0x1234abc, "0x01234abc "}, 382 {"%-#20.8X", 0x1234abc, "0X01234ABC "}, 383 {"%-#20.8o", 01234, "00001234 "}, 384 385 // Test correct f.intbuf overflow checks. 386 {"%068d", 1, zeroFill("", 68, "1")}, 387 {"%068d", -1, zeroFill("-", 67, "1")}, 388 {"%#.68x", 42, zeroFill("0x", 68, "2a")}, 389 {"%.68d", -42, zeroFill("-", 68, "42")}, 390 {"%+.68d", 42, zeroFill("+", 68, "42")}, 391 {"% .68d", 42, zeroFill(" ", 68, "42")}, 392 {"% +.68d", 42, zeroFill("+", 68, "42")}, 393 394 // unicode format 395 {"%U", 0, "U+0000"}, 396 {"%U", -1, "U+FFFFFFFFFFFFFFFF"}, 397 {"%U", '\n', `U+000A`}, 398 {"%#U", '\n', `U+000A`}, 399 {"%+U", 'x', `U+0078`}, // Plus flag should have no effect. 400 {"%# U", 'x', `U+0078 'x'`}, // Space flag should have no effect. 401 {"%#.2U", 'x', `U+0078 'x'`}, // Precisions below 4 should print 4 digits. 402 {"%U", '\u263a', `U+263A`}, 403 {"%#U", '\u263a', `U+263A '☺'`}, 404 {"%U", '\U0001D6C2', `U+1D6C2`}, 405 {"%#U", '\U0001D6C2', `U+1D6C2 '𝛂'`}, 406 {"%#14.6U", '⌘', " U+002318 '⌘'"}, 407 {"%#-14.6U", '⌘', "U+002318 '⌘' "}, 408 {"%#014.6U", '⌘', " U+002318 '⌘'"}, 409 {"%#-014.6U", '⌘', "U+002318 '⌘' "}, 410 {"%.68U", uint(42), zeroFill("U+", 68, "2A")}, 411 {"%#.68U", '日', zeroFill("U+", 68, "65E5") + " '日'"}, 412 413 // floats 414 {"%+.3e", 0.0, "+0.000e+00"}, 415 {"%+.3e", 1.0, "+1.000e+00"}, 416 {"%+.3f", -1.0, "-1.000"}, 417 {"%+.3F", -1.0, "-1.000"}, 418 {"%+.3F", float32(-1.0), "-1.000"}, 419 {"%+07.2f", 1.0, "+001.00"}, 420 {"%+07.2f", -1.0, "-001.00"}, 421 {"%-07.2f", 1.0, "1.00 "}, 422 {"%-07.2f", -1.0, "-1.00 "}, 423 {"%+-07.2f", 1.0, "+1.00 "}, 424 {"%+-07.2f", -1.0, "-1.00 "}, 425 {"%-+07.2f", 1.0, "+1.00 "}, 426 {"%-+07.2f", -1.0, "-1.00 "}, 427 {"%+10.2f", +1.0, " +1.00"}, 428 {"%+10.2f", -1.0, " -1.00"}, 429 {"% .3E", -1.0, "-1.000E+00"}, 430 {"% .3e", 1.0, " 1.000e+00"}, 431 {"%+.3g", 0.0, "+0"}, 432 {"%+.3g", 1.0, "+1"}, 433 {"%+.3g", -1.0, "-1"}, 434 {"% .3g", -1.0, "-1"}, 435 {"% .3g", 1.0, " 1"}, 436 {"%b", float32(1.0), "8388608p-23"}, 437 {"%b", 1.0, "4503599627370496p-52"}, 438 // Test sharp flag used with floats. 439 {"%#g", 1e-323, "1.00000e-323"}, 440 {"%#g", -1.0, "-1.00000"}, 441 {"%#g", 1.1, "1.10000"}, 442 {"%#g", 123456.0, "123456."}, 443 {"%#g", 1234567.0, "1.234567e+06"}, 444 {"%#g", 1230000.0, "1.23000e+06"}, 445 {"%#g", 1000000.0, "1.00000e+06"}, 446 {"%#.0f", 1.0, "1."}, 447 {"%#.0e", 1.0, "1.e+00"}, 448 {"%#.0g", 1.0, "1."}, 449 {"%#.0g", 1100000.0, "1.e+06"}, 450 {"%#.4f", 1.0, "1.0000"}, 451 {"%#.4e", 1.0, "1.0000e+00"}, 452 {"%#.4g", 1.0, "1.000"}, 453 {"%#.4g", 100000.0, "1.000e+05"}, 454 {"%#.0f", 123.0, "123."}, 455 {"%#.0e", 123.0, "1.e+02"}, 456 {"%#.0g", 123.0, "1.e+02"}, 457 {"%#.4f", 123.0, "123.0000"}, 458 {"%#.4e", 123.0, "1.2300e+02"}, 459 {"%#.4g", 123.0, "123.0"}, 460 {"%#.4g", 123000.0, "1.230e+05"}, 461 {"%#9.4g", 1.0, " 1.000"}, 462 // The sharp flag has no effect for binary float format. 463 {"%#b", 1.0, "4503599627370496p-52"}, 464 // Precision has no effect for binary float format. 465 {"%.4b", float32(1.0), "8388608p-23"}, 466 {"%.4b", -1.0, "-4503599627370496p-52"}, 467 // Test correct f.intbuf boundary checks. 468 {"%.68f", 1.0, zeroFill("1.", 68, "")}, 469 {"%.68f", -1.0, zeroFill("-1.", 68, "")}, 470 // float infinites and NaNs 471 {"%f", posInf, "+Inf"}, 472 {"%.1f", negInf, "-Inf"}, 473 {"% f", NaN, " NaN"}, 474 {"%20f", posInf, " +Inf"}, 475 {"% 20F", posInf, " Inf"}, 476 {"% 20e", negInf, " -Inf"}, 477 {"%+20E", negInf, " -Inf"}, 478 {"% +20g", negInf, " -Inf"}, 479 {"%+-20G", posInf, "+Inf "}, 480 {"%20e", NaN, " NaN"}, 481 {"% +20E", NaN, " +NaN"}, 482 {"% -20g", NaN, " NaN "}, 483 {"%+-20G", NaN, "+NaN "}, 484 // Zero padding does not apply to infinities and NaN. 485 {"%+020e", posInf, " +Inf"}, 486 {"%-020f", negInf, "-Inf "}, 487 {"%-020E", NaN, "NaN "}, 488 489 // complex values 490 {"%.f", 0i, "(0+0i)"}, 491 {"% .f", 0i, "( 0+0i)"}, 492 {"%+.f", 0i, "(+0+0i)"}, 493 {"% +.f", 0i, "(+0+0i)"}, 494 {"%+.3e", 0i, "(+0.000e+00+0.000e+00i)"}, 495 {"%+.3f", 0i, "(+0.000+0.000i)"}, 496 {"%+.3g", 0i, "(+0+0i)"}, 497 {"%+.3e", 1 + 2i, "(+1.000e+00+2.000e+00i)"}, 498 {"%+.3f", 1 + 2i, "(+1.000+2.000i)"}, 499 {"%+.3g", 1 + 2i, "(+1+2i)"}, 500 {"%.3e", 0i, "(0.000e+00+0.000e+00i)"}, 501 {"%.3f", 0i, "(0.000+0.000i)"}, 502 {"%.3F", 0i, "(0.000+0.000i)"}, 503 {"%.3F", complex64(0i), "(0.000+0.000i)"}, 504 {"%.3g", 0i, "(0+0i)"}, 505 {"%.3e", 1 + 2i, "(1.000e+00+2.000e+00i)"}, 506 {"%.3f", 1 + 2i, "(1.000+2.000i)"}, 507 {"%.3g", 1 + 2i, "(1+2i)"}, 508 {"%.3e", -1 - 2i, "(-1.000e+00-2.000e+00i)"}, 509 {"%.3f", -1 - 2i, "(-1.000-2.000i)"}, 510 {"%.3g", -1 - 2i, "(-1-2i)"}, 511 {"% .3E", -1 - 2i, "(-1.000E+00-2.000E+00i)"}, 512 {"%+.3g", 1 + 2i, "(+1+2i)"}, 513 {"%+.3g", complex64(1 + 2i), "(+1+2i)"}, 514 {"%#g", 1 + 2i, "(1.00000+2.00000i)"}, 515 {"%#g", 123456 + 789012i, "(123456.+789012.i)"}, 516 {"%#g", 1e-10i, "(0.00000+1.00000e-10i)"}, 517 {"%#g", -1e10 - 1.11e100i, "(-1.00000e+10-1.11000e+100i)"}, 518 {"%#.0f", 1.23 + 1.0i, "(1.+1.i)"}, 519 {"%#.0e", 1.23 + 1.0i, "(1.e+00+1.e+00i)"}, 520 {"%#.0g", 1.23 + 1.0i, "(1.+1.i)"}, 521 {"%#.0g", 0 + 100000i, "(0.+1.e+05i)"}, 522 {"%#.0g", 1230000 + 0i, "(1.e+06+0.i)"}, 523 {"%#.4f", 1 + 1.23i, "(1.0000+1.2300i)"}, 524 {"%#.4e", 123 + 1i, "(1.2300e+02+1.0000e+00i)"}, 525 {"%#.4g", 123 + 1.23i, "(123.0+1.230i)"}, 526 {"%#12.5g", 0 + 100000i, "( 0.0000 +1.0000e+05i)"}, 527 {"%#12.5g", 1230000 - 0i, "( 1.2300e+06 +0.0000i)"}, 528 {"%b", 1 + 2i, "(4503599627370496p-52+4503599627370496p-51i)"}, 529 {"%b", complex64(1 + 2i), "(8388608p-23+8388608p-22i)"}, 530 // The sharp flag has no effect for binary complex format. 531 {"%#b", 1 + 2i, "(4503599627370496p-52+4503599627370496p-51i)"}, 532 // Precision has no effect for binary complex format. 533 {"%.4b", 1 + 2i, "(4503599627370496p-52+4503599627370496p-51i)"}, 534 {"%.4b", complex64(1 + 2i), "(8388608p-23+8388608p-22i)"}, 535 // complex infinites and NaNs 536 {"%f", complex(posInf, posInf), "(+Inf+Infi)"}, 537 {"%f", complex(negInf, negInf), "(-Inf-Infi)"}, 538 {"%f", complex(NaN, NaN), "(NaN+NaNi)"}, 539 {"%.1f", complex(posInf, posInf), "(+Inf+Infi)"}, 540 {"% f", complex(posInf, posInf), "( Inf+Infi)"}, 541 {"% f", complex(negInf, negInf), "(-Inf-Infi)"}, 542 {"% f", complex(NaN, NaN), "( NaN+NaNi)"}, 543 {"%8e", complex(posInf, posInf), "( +Inf +Infi)"}, 544 {"% 8E", complex(posInf, posInf), "( Inf +Infi)"}, 545 {"%+8f", complex(negInf, negInf), "( -Inf -Infi)"}, 546 {"% +8g", complex(negInf, negInf), "( -Inf -Infi)"}, 547 {"% -8G", complex(NaN, NaN), "( NaN +NaN i)"}, 548 {"%+-8b", complex(NaN, NaN), "(+NaN +NaN i)"}, 549 // Zero padding does not apply to infinities and NaN. 550 {"%08f", complex(posInf, posInf), "( +Inf +Infi)"}, 551 {"%-08g", complex(negInf, negInf), "(-Inf -Inf i)"}, 552 {"%-08G", complex(NaN, NaN), "(NaN +NaN i)"}, 553 554 // old test/fmt_test.go 555 {"%e", 1.0, "1.000000e+00"}, 556 {"%e", 1234.5678e3, "1.234568e+06"}, 557 {"%e", 1234.5678e-8, "1.234568e-05"}, 558 {"%e", -7.0, "-7.000000e+00"}, 559 {"%e", -1e-9, "-1.000000e-09"}, 560 {"%f", 1234.5678e3, "1234567.800000"}, 561 {"%f", 1234.5678e-8, "0.000012"}, 562 {"%f", -7.0, "-7.000000"}, 563 {"%f", -1e-9, "-0.000000"}, 564 {"%g", 1234.5678e3, "1.2345678e+06"}, 565 {"%g", float32(1234.5678e3), "1.2345678e+06"}, 566 {"%g", 1234.5678e-8, "1.2345678e-05"}, 567 {"%g", -7.0, "-7"}, 568 {"%g", -1e-9, "-1e-09"}, 569 {"%g", float32(-1e-9), "-1e-09"}, 570 {"%E", 1.0, "1.000000E+00"}, 571 {"%E", 1234.5678e3, "1.234568E+06"}, 572 {"%E", 1234.5678e-8, "1.234568E-05"}, 573 {"%E", -7.0, "-7.000000E+00"}, 574 {"%E", -1e-9, "-1.000000E-09"}, 575 {"%G", 1234.5678e3, "1.2345678E+06"}, 576 {"%G", float32(1234.5678e3), "1.2345678E+06"}, 577 {"%G", 1234.5678e-8, "1.2345678E-05"}, 578 {"%G", -7.0, "-7"}, 579 {"%G", -1e-9, "-1E-09"}, 580 {"%G", float32(-1e-9), "-1E-09"}, 581 {"%20.5s", "qwertyuiop", " qwert"}, 582 {"%.5s", "qwertyuiop", "qwert"}, 583 {"%-20.5s", "qwertyuiop", "qwert "}, 584 {"%20c", 'x', " x"}, 585 {"%-20c", 'x', "x "}, 586 {"%20.6e", 1.2345e3, " 1.234500e+03"}, 587 {"%20.6e", 1.2345e-3, " 1.234500e-03"}, 588 {"%20e", 1.2345e3, " 1.234500e+03"}, 589 {"%20e", 1.2345e-3, " 1.234500e-03"}, 590 {"%20.8e", 1.2345e3, " 1.23450000e+03"}, 591 {"%20f", 1.23456789e3, " 1234.567890"}, 592 {"%20f", 1.23456789e-3, " 0.001235"}, 593 {"%20f", 12345678901.23456789, " 12345678901.234568"}, 594 {"%-20f", 1.23456789e3, "1234.567890 "}, 595 {"%20.8f", 1.23456789e3, " 1234.56789000"}, 596 {"%20.8f", 1.23456789e-3, " 0.00123457"}, 597 {"%g", 1.23456789e3, "1234.56789"}, 598 {"%g", 1.23456789e-3, "0.00123456789"}, 599 {"%g", 1.23456789e20, "1.23456789e+20"}, 600 601 // arrays 602 {"%v", array, "[1 2 3 4 5]"}, 603 {"%v", iarray, "[1 hello 2.5 <nil>]"}, 604 {"%v", barray, "[1 2 3 4 5]"}, 605 {"%v", &array, "&[1 2 3 4 5]"}, 606 {"%v", &iarray, "&[1 hello 2.5 <nil>]"}, 607 {"%v", &barray, "&[1 2 3 4 5]"}, 608 609 // slices 610 {"%v", slice, "[1 2 3 4 5]"}, 611 {"%v", islice, "[1 hello 2.5 <nil>]"}, 612 {"%v", bslice, "[1 2 3 4 5]"}, 613 {"%v", &slice, "&[1 2 3 4 5]"}, 614 {"%v", &islice, "&[1 hello 2.5 <nil>]"}, 615 {"%v", &bslice, "&[1 2 3 4 5]"}, 616 617 // byte arrays and slices with %b,%c,%d,%o,%U and %v 618 {"%b", [3]byte{65, 66, 67}, "[1000001 1000010 1000011]"}, 619 {"%c", [3]byte{65, 66, 67}, "[A B C]"}, 620 {"%d", [3]byte{65, 66, 67}, "[65 66 67]"}, 621 {"%o", [3]byte{65, 66, 67}, "[101 102 103]"}, 622 {"%U", [3]byte{65, 66, 67}, "[U+0041 U+0042 U+0043]"}, 623 {"%v", [3]byte{65, 66, 67}, "[65 66 67]"}, 624 {"%v", [1]byte{123}, "[123]"}, 625 {"%012v", []byte{}, "[]"}, 626 {"%#012v", []byte{}, "[]byte{}"}, 627 {"%6v", []byte{1, 11, 111}, "[ 1 11 111]"}, 628 {"%06v", []byte{1, 11, 111}, "[000001 000011 000111]"}, 629 {"%-6v", []byte{1, 11, 111}, "[1 11 111 ]"}, 630 {"%-06v", []byte{1, 11, 111}, "[1 11 111 ]"}, 631 {"%#v", []byte{1, 11, 111}, "[]byte{0x1, 0xb, 0x6f}"}, 632 {"%#6v", []byte{1, 11, 111}, "[]byte{ 0x1, 0xb, 0x6f}"}, 633 {"%#06v", []byte{1, 11, 111}, "[]byte{0x000001, 0x00000b, 0x00006f}"}, 634 {"%#-6v", []byte{1, 11, 111}, "[]byte{0x1 , 0xb , 0x6f }"}, 635 {"%#-06v", []byte{1, 11, 111}, "[]byte{0x1 , 0xb , 0x6f }"}, 636 // f.space should and f.plus should not have an effect with %v. 637 {"% v", []byte{1, 11, 111}, "[ 1 11 111]"}, 638 {"%+v", [3]byte{1, 11, 111}, "[1 11 111]"}, 639 {"%# -6v", []byte{1, 11, 111}, "[]byte{ 0x1 , 0xb , 0x6f }"}, 640 {"%#+-6v", [3]byte{1, 11, 111}, "[3]uint8{0x1 , 0xb , 0x6f }"}, 641 // f.space and f.plus should have an effect with %d. 642 {"% d", []byte{1, 11, 111}, "[ 1 11 111]"}, 643 {"%+d", [3]byte{1, 11, 111}, "[+1 +11 +111]"}, 644 {"%# -6d", []byte{1, 11, 111}, "[ 1 11 111 ]"}, 645 {"%#+-6d", [3]byte{1, 11, 111}, "[+1 +11 +111 ]"}, 646 647 // floates with %v 648 {"%v", 1.2345678, "1.2345678"}, 649 {"%v", float32(1.2345678), "1.2345678"}, 650 651 // complexes with %v 652 {"%v", 1 + 2i, "(1+2i)"}, 653 {"%v", complex64(1 + 2i), "(1+2i)"}, 654 655 // structs 656 {"%v", A{1, 2, "a", []int{1, 2}}, `{1 2 a [1 2]}`}, 657 {"%+v", A{1, 2, "a", []int{1, 2}}, `{i:1 j:2 s:a x:[1 2]}`}, 658 659 // +v on structs with Stringable items 660 {"%+v", B{1, 2}, `{I:<1> j:2}`}, 661 {"%+v", C{1, B{2, 3}}, `{i:1 B:{I:<2> j:3}}`}, 662 663 // other formats on Stringable items 664 {"%s", I(23), `<23>`}, 665 {"%q", I(23), `"<23>"`}, 666 {"%x", I(23), `3c32333e`}, 667 {"%#x", I(23), `0x3c32333e`}, 668 {"%# x", I(23), `0x3c 0x32 0x33 0x3e`}, 669 // Stringer applies only to string formats. 670 {"%d", I(23), `23`}, 671 // Stringer applies to the extracted value. 672 {"%s", reflect.ValueOf(I(23)), `<23>`}, 673 674 // go syntax 675 {"%#v", A{1, 2, "a", []int{1, 2}}, `fmt_test.A{i:1, j:0x2, s:"a", x:[]int{1, 2}}`}, 676 {"%#v", new(byte), "(*uint8)(0xPTR)"}, 677 {"%#v", TestFmtInterface, "(func(*testing.T))(0xPTR)"}, 678 {"%#v", make(chan int), "(chan int)(0xPTR)"}, 679 {"%#v", uint64(1<<64 - 1), "0xffffffffffffffff"}, 680 {"%#v", 1000000000, "1000000000"}, 681 {"%#v", map[string]int{"a": 1}, `map[string]int{"a":1}`}, 682 {"%#v", map[string]B{"a": {1, 2}}, `map[string]fmt_test.B{"a":fmt_test.B{I:1, j:2}}`}, 683 {"%#v", []string{"a", "b"}, `[]string{"a", "b"}`}, 684 {"%#v", SI{}, `fmt_test.SI{I:interface {}(nil)}`}, 685 {"%#v", []int(nil), `[]int(nil)`}, 686 {"%#v", []int{}, `[]int{}`}, 687 {"%#v", array, `[5]int{1, 2, 3, 4, 5}`}, 688 {"%#v", &array, `&[5]int{1, 2, 3, 4, 5}`}, 689 {"%#v", iarray, `[4]interface {}{1, "hello", 2.5, interface {}(nil)}`}, 690 {"%#v", &iarray, `&[4]interface {}{1, "hello", 2.5, interface {}(nil)}`}, 691 {"%#v", map[int]byte(nil), `map[int]uint8(nil)`}, 692 {"%#v", map[int]byte{}, `map[int]uint8{}`}, 693 {"%#v", "foo", `"foo"`}, 694 {"%#v", barray, `[5]fmt_test.renamedUint8{0x1, 0x2, 0x3, 0x4, 0x5}`}, 695 {"%#v", bslice, `[]fmt_test.renamedUint8{0x1, 0x2, 0x3, 0x4, 0x5}`}, 696 {"%#v", []int32(nil), "[]int32(nil)"}, 697 {"%#v", 1.2345678, "1.2345678"}, 698 {"%#v", float32(1.2345678), "1.2345678"}, 699 700 // Whole number floats are printed without decimals. See Issue 27634. 701 {"%#v", 1.0, "1"}, 702 {"%#v", 1000000.0, "1e+06"}, 703 {"%#v", float32(1.0), "1"}, 704 {"%#v", float32(1000000.0), "1e+06"}, 705 706 // Only print []byte and []uint8 as type []byte if they appear at the top level. 707 {"%#v", []byte(nil), "[]byte(nil)"}, 708 {"%#v", []uint8(nil), "[]byte(nil)"}, 709 {"%#v", []byte{}, "[]byte{}"}, 710 {"%#v", []uint8{}, "[]byte{}"}, 711 {"%#v", reflect.ValueOf([]byte{}), "[]uint8{}"}, 712 {"%#v", reflect.ValueOf([]uint8{}), "[]uint8{}"}, 713 {"%#v", &[]byte{}, "&[]uint8{}"}, 714 {"%#v", &[]byte{}, "&[]uint8{}"}, 715 {"%#v", [3]byte{}, "[3]uint8{0x0, 0x0, 0x0}"}, 716 {"%#v", [3]uint8{}, "[3]uint8{0x0, 0x0, 0x0}"}, 717 718 // slices with other formats 719 {"%#x", []int{1, 2, 15}, `[0x1 0x2 0xf]`}, 720 {"%x", []int{1, 2, 15}, `[1 2 f]`}, 721 {"%d", []int{1, 2, 15}, `[1 2 15]`}, 722 {"%d", []byte{1, 2, 15}, `[1 2 15]`}, 723 {"%q", []string{"a", "b"}, `["a" "b"]`}, 724 {"% 02x", []byte{1}, "01"}, 725 {"% 02x", []byte{1, 2, 3}, "01 02 03"}, 726 727 // Padding with byte slices. 728 {"%2x", []byte{}, " "}, 729 {"%#2x", []byte{}, " "}, 730 {"% 02x", []byte{}, "00"}, 731 {"%# 02x", []byte{}, "00"}, 732 {"%-2x", []byte{}, " "}, 733 {"%-02x", []byte{}, " "}, 734 {"%8x", []byte{0xab}, " ab"}, 735 {"% 8x", []byte{0xab}, " ab"}, 736 {"%#8x", []byte{0xab}, " 0xab"}, 737 {"%# 8x", []byte{0xab}, " 0xab"}, 738 {"%08x", []byte{0xab}, "000000ab"}, 739 {"% 08x", []byte{0xab}, "000000ab"}, 740 {"%#08x", []byte{0xab}, "00000xab"}, 741 {"%# 08x", []byte{0xab}, "00000xab"}, 742 {"%10x", []byte{0xab, 0xcd}, " abcd"}, 743 {"% 10x", []byte{0xab, 0xcd}, " ab cd"}, 744 {"%#10x", []byte{0xab, 0xcd}, " 0xabcd"}, 745 {"%# 10x", []byte{0xab, 0xcd}, " 0xab 0xcd"}, 746 {"%010x", []byte{0xab, 0xcd}, "000000abcd"}, 747 {"% 010x", []byte{0xab, 0xcd}, "00000ab cd"}, 748 {"%#010x", []byte{0xab, 0xcd}, "00000xabcd"}, 749 {"%# 010x", []byte{0xab, 0xcd}, "00xab 0xcd"}, 750 {"%-10X", []byte{0xab}, "AB "}, 751 {"% -010X", []byte{0xab}, "AB "}, 752 {"%#-10X", []byte{0xab, 0xcd}, "0XABCD "}, 753 {"%# -010X", []byte{0xab, 0xcd}, "0XAB 0XCD "}, 754 // Same for strings 755 {"%2x", "", " "}, 756 {"%#2x", "", " "}, 757 {"% 02x", "", "00"}, 758 {"%# 02x", "", "00"}, 759 {"%-2x", "", " "}, 760 {"%-02x", "", " "}, 761 {"%8x", "\xab", " ab"}, 762 {"% 8x", "\xab", " ab"}, 763 {"%#8x", "\xab", " 0xab"}, 764 {"%# 8x", "\xab", " 0xab"}, 765 {"%08x", "\xab", "000000ab"}, 766 {"% 08x", "\xab", "000000ab"}, 767 {"%#08x", "\xab", "00000xab"}, 768 {"%# 08x", "\xab", "00000xab"}, 769 {"%10x", "\xab\xcd", " abcd"}, 770 {"% 10x", "\xab\xcd", " ab cd"}, 771 {"%#10x", "\xab\xcd", " 0xabcd"}, 772 {"%# 10x", "\xab\xcd", " 0xab 0xcd"}, 773 {"%010x", "\xab\xcd", "000000abcd"}, 774 {"% 010x", "\xab\xcd", "00000ab cd"}, 775 {"%#010x", "\xab\xcd", "00000xabcd"}, 776 {"%# 010x", "\xab\xcd", "00xab 0xcd"}, 777 {"%-10X", "\xab", "AB "}, 778 {"% -010X", "\xab", "AB "}, 779 {"%#-10X", "\xab\xcd", "0XABCD "}, 780 {"%# -010X", "\xab\xcd", "0XAB 0XCD "}, 781 782 // renamings 783 {"%v", renamedBool(true), "true"}, 784 {"%d", renamedBool(true), "%!d(fmt_test.renamedBool=true)"}, 785 {"%o", renamedInt(8), "10"}, 786 {"%d", renamedInt8(-9), "-9"}, 787 {"%v", renamedInt16(10), "10"}, 788 {"%v", renamedInt32(-11), "-11"}, 789 {"%X", renamedInt64(255), "FF"}, 790 {"%v", renamedUint(13), "13"}, 791 {"%o", renamedUint8(14), "16"}, 792 {"%X", renamedUint16(15), "F"}, 793 {"%d", renamedUint32(16), "16"}, 794 {"%X", renamedUint64(17), "11"}, 795 {"%o", renamedUintptr(18), "22"}, 796 {"%x", renamedString("thing"), "7468696e67"}, 797 {"%d", renamedBytes([]byte{1, 2, 15}), `[1 2 15]`}, 798 {"%q", renamedBytes([]byte("hello")), `"hello"`}, 799 {"%x", []renamedUint8{'h', 'e', 'l', 'l', 'o'}, "68656c6c6f"}, 800 {"%X", []renamedUint8{'h', 'e', 'l', 'l', 'o'}, "68656C6C6F"}, 801 {"%s", []renamedUint8{'h', 'e', 'l', 'l', 'o'}, "hello"}, 802 {"%q", []renamedUint8{'h', 'e', 'l', 'l', 'o'}, `"hello"`}, 803 {"%v", renamedFloat32(22), "22"}, 804 {"%v", renamedFloat64(33), "33"}, 805 {"%v", renamedComplex64(3 + 4i), "(3+4i)"}, 806 {"%v", renamedComplex128(4 - 3i), "(4-3i)"}, 807 808 // Formatter 809 {"%x", F(1), "<x=F(1)>"}, 810 {"%x", G(2), "2"}, 811 {"%+v", S{F(4), G(5)}, "{F:<v=F(4)> G:5}"}, 812 813 // GoStringer 814 {"%#v", G(6), "GoString(6)"}, 815 {"%#v", S{F(7), G(8)}, "fmt_test.S{F:<v=F(7)>, G:GoString(8)}"}, 816 817 // %T 818 {"%T", byte(0), "uint8"}, 819 {"%T", reflect.ValueOf(nil), "reflect.Value"}, 820 {"%T", (4 - 3i), "complex128"}, 821 {"%T", renamedComplex128(4 - 3i), "fmt_test.renamedComplex128"}, 822 {"%T", intVar, "int"}, 823 {"%6T", &intVar, " *int"}, 824 {"%10T", nil, " <nil>"}, 825 {"%-10T", nil, "<nil> "}, 826 827 // %p with pointers 828 {"%p", (*int)(nil), "0x0"}, 829 {"%#p", (*int)(nil), "0"}, 830 {"%p", &intVar, "0xPTR"}, 831 {"%#p", &intVar, "PTR"}, 832 {"%p", &array, "0xPTR"}, 833 {"%p", &slice, "0xPTR"}, 834 {"%8.2p", (*int)(nil), " 0x00"}, 835 {"%-20.16p", &intVar, "0xPTR "}, 836 // %p on non-pointers 837 {"%p", make(chan int), "0xPTR"}, 838 {"%p", make(map[int]int), "0xPTR"}, 839 {"%p", func() {}, "0xPTR"}, 840 {"%p", 27, "%!p(int=27)"}, // not a pointer at all 841 {"%p", nil, "%!p(<nil>)"}, // nil on its own has no type ... 842 {"%#p", nil, "%!p(<nil>)"}, // ... and hence is not a pointer type. 843 // pointers with specified base 844 {"%b", &intVar, "PTR_b"}, 845 {"%d", &intVar, "PTR_d"}, 846 {"%o", &intVar, "PTR_o"}, 847 {"%x", &intVar, "PTR_x"}, 848 {"%X", &intVar, "PTR_X"}, 849 // %v on pointers 850 {"%v", nil, "<nil>"}, 851 {"%#v", nil, "<nil>"}, 852 {"%v", (*int)(nil), "<nil>"}, 853 {"%#v", (*int)(nil), "(*int)(nil)"}, 854 {"%v", &intVar, "0xPTR"}, 855 {"%#v", &intVar, "(*int)(0xPTR)"}, 856 {"%8.2v", (*int)(nil), " <nil>"}, 857 {"%-20.16v", &intVar, "0xPTR "}, 858 // string method on pointer 859 {"%s", &pValue, "String(p)"}, // String method... 860 {"%p", &pValue, "0xPTR"}, // ... is not called with %p. 861 862 // %d on Stringer should give integer if possible 863 {"%s", time.Time{}.Month(), "January"}, 864 {"%d", time.Time{}.Month(), "1"}, 865 866 // erroneous things 867 {"", nil, "%!(EXTRA <nil>)"}, 868 {"", 2, "%!(EXTRA int=2)"}, 869 {"no args", "hello", "no args%!(EXTRA string=hello)"}, 870 {"%s %", "hello", "hello %!(NOVERB)"}, 871 {"%s %.2", "hello", "hello %!(NOVERB)"}, 872 {"%017091901790959340919092959340919017929593813360", 0, "%!(NOVERB)%!(EXTRA int=0)"}, 873 {"%184467440737095516170v", 0, "%!(NOVERB)%!(EXTRA int=0)"}, 874 // Extra argument errors should format without flags set. 875 {"%010.2", "12345", "%!(NOVERB)%!(EXTRA string=12345)"}, 876 877 // Test that maps with non-reflexive keys print all keys and values. 878 {"%v", map[float64]int{NaN: 1, NaN: 1}, "map[NaN:1 NaN:1]"}, 879 880 // Comparison of padding rules with C printf. 881 /* 882 C program: 883 #include <stdio.h> 884 885 char *format[] = { 886 "[%.2f]", 887 "[% .2f]", 888 "[%+.2f]", 889 "[%7.2f]", 890 "[% 7.2f]", 891 "[%+7.2f]", 892 "[% +7.2f]", 893 "[%07.2f]", 894 "[% 07.2f]", 895 "[%+07.2f]", 896 "[% +07.2f]" 897 }; 898 899 int main(void) { 900 int i; 901 for(i = 0; i < 11; i++) { 902 printf("%s: ", format[i]); 903 printf(format[i], 1.0); 904 printf(" "); 905 printf(format[i], -1.0); 906 printf("\n"); 907 } 908 } 909 910 Output: 911 [%.2f]: [1.00] [-1.00] 912 [% .2f]: [ 1.00] [-1.00] 913 [%+.2f]: [+1.00] [-1.00] 914 [%7.2f]: [ 1.00] [ -1.00] 915 [% 7.2f]: [ 1.00] [ -1.00] 916 [%+7.2f]: [ +1.00] [ -1.00] 917 [% +7.2f]: [ +1.00] [ -1.00] 918 [%07.2f]: [0001.00] [-001.00] 919 [% 07.2f]: [ 001.00] [-001.00] 920 [%+07.2f]: [+001.00] [-001.00] 921 [% +07.2f]: [+001.00] [-001.00] 922 923 */ 924 {"%.2f", 1.0, "1.00"}, 925 {"%.2f", -1.0, "-1.00"}, 926 {"% .2f", 1.0, " 1.00"}, 927 {"% .2f", -1.0, "-1.00"}, 928 {"%+.2f", 1.0, "+1.00"}, 929 {"%+.2f", -1.0, "-1.00"}, 930 {"%7.2f", 1.0, " 1.00"}, 931 {"%7.2f", -1.0, " -1.00"}, 932 {"% 7.2f", 1.0, " 1.00"}, 933 {"% 7.2f", -1.0, " -1.00"}, 934 {"%+7.2f", 1.0, " +1.00"}, 935 {"%+7.2f", -1.0, " -1.00"}, 936 {"% +7.2f", 1.0, " +1.00"}, 937 {"% +7.2f", -1.0, " -1.00"}, 938 {"%07.2f", 1.0, "0001.00"}, 939 {"%07.2f", -1.0, "-001.00"}, 940 {"% 07.2f", 1.0, " 001.00"}, 941 {"% 07.2f", -1.0, "-001.00"}, 942 {"%+07.2f", 1.0, "+001.00"}, 943 {"%+07.2f", -1.0, "-001.00"}, 944 {"% +07.2f", 1.0, "+001.00"}, 945 {"% +07.2f", -1.0, "-001.00"}, 946 947 // Complex numbers: exhaustively tested in TestComplexFormatting. 948 {"%7.2f", 1 + 2i, "( 1.00 +2.00i)"}, 949 {"%+07.2f", -1 - 2i, "(-001.00-002.00i)"}, 950 951 // Use spaces instead of zero if padding to the right. 952 {"%0-5s", "abc", "abc "}, 953 {"%-05.1f", 1.0, "1.0 "}, 954 955 // float and complex formatting should not change the padding width 956 // for other elements. See issue 14642. 957 {"%06v", []interface{}{+10.0, 10}, "[000010 000010]"}, 958 {"%06v", []interface{}{-10.0, 10}, "[-00010 000010]"}, 959 {"%06v", []interface{}{+10.0 + 10i, 10}, "[(000010+00010i) 000010]"}, 960 {"%06v", []interface{}{-10.0 + 10i, 10}, "[(-00010+00010i) 000010]"}, 961 962 // integer formatting should not alter padding for other elements. 963 {"%03.6v", []interface{}{1, 2.0, "x"}, "[000001 002 00x]"}, 964 {"%03.0v", []interface{}{0, 2.0, "x"}, "[ 002 000]"}, 965 966 // Complex fmt used to leave the plus flag set for future entries in the array 967 // causing +2+0i and +3+0i instead of 2+0i and 3+0i. 968 {"%v", []complex64{1, 2, 3}, "[(1+0i) (2+0i) (3+0i)]"}, 969 {"%v", []complex128{1, 2, 3}, "[(1+0i) (2+0i) (3+0i)]"}, 970 971 // Incomplete format specification caused crash. 972 {"%.", 3, "%!.(int=3)"}, 973 974 // Padding for complex numbers. Has been bad, then fixed, then bad again. 975 {"%+10.2f", +104.66 + 440.51i, "( +104.66 +440.51i)"}, 976 {"%+10.2f", -104.66 + 440.51i, "( -104.66 +440.51i)"}, 977 {"%+10.2f", +104.66 - 440.51i, "( +104.66 -440.51i)"}, 978 {"%+10.2f", -104.66 - 440.51i, "( -104.66 -440.51i)"}, 979 {"%+010.2f", +104.66 + 440.51i, "(+000104.66+000440.51i)"}, 980 {"%+010.2f", -104.66 + 440.51i, "(-000104.66+000440.51i)"}, 981 {"%+010.2f", +104.66 - 440.51i, "(+000104.66-000440.51i)"}, 982 {"%+010.2f", -104.66 - 440.51i, "(-000104.66-000440.51i)"}, 983 984 // []T where type T is a byte with a Stringer method. 985 {"%v", byteStringerSlice, "[X X X X X]"}, 986 {"%s", byteStringerSlice, "hello"}, 987 {"%q", byteStringerSlice, "\"hello\""}, 988 {"%x", byteStringerSlice, "68656c6c6f"}, 989 {"%X", byteStringerSlice, "68656C6C6F"}, 990 {"%#v", byteStringerSlice, "[]fmt_test.byteStringer{0x68, 0x65, 0x6c, 0x6c, 0x6f}"}, 991 992 // And the same for Formatter. 993 {"%v", byteFormatterSlice, "[X X X X X]"}, 994 {"%s", byteFormatterSlice, "hello"}, 995 {"%q", byteFormatterSlice, "\"hello\""}, 996 {"%x", byteFormatterSlice, "68656c6c6f"}, 997 {"%X", byteFormatterSlice, "68656C6C6F"}, 998 // This next case seems wrong, but the docs say the Formatter wins here. 999 {"%#v", byteFormatterSlice, "[]fmt_test.byteFormatter{X, X, X, X, X}"}, 1000 1001 // pp.WriteString 1002 {"%s", writeStringFormatter(""), "******"}, 1003 {"%s", writeStringFormatter("xyz"), "***xyz***"}, 1004 {"%s", writeStringFormatter("⌘/⌘"), "***⌘/⌘***"}, 1005 1006 // reflect.Value handled specially in Go 1.5, making it possible to 1007 // see inside non-exported fields (which cannot be accessed with Interface()). 1008 // Issue 8965. 1009 {"%v", reflect.ValueOf(A{}).Field(0).String(), "<int Value>"}, // Equivalent to the old way. 1010 {"%v", reflect.ValueOf(A{}).Field(0), "0"}, // Sees inside the field. 1011 1012 // verbs apply to the extracted value too. 1013 {"%s", reflect.ValueOf("hello"), "hello"}, 1014 {"%q", reflect.ValueOf("hello"), `"hello"`}, 1015 {"%#04x", reflect.ValueOf(256), "0x0100"}, 1016 1017 // invalid reflect.Value doesn't crash. 1018 {"%v", reflect.Value{}, "<invalid reflect.Value>"}, 1019 {"%v", &reflect.Value{}, "<invalid Value>"}, 1020 {"%v", SI{reflect.Value{}}, "{<invalid Value>}"}, 1021 1022 // Tests to check that not supported verbs generate an error string. 1023 {"%☠", nil, "%!☠(<nil>)"}, 1024 {"%☠", interface{}(nil), "%!☠(<nil>)"}, 1025 {"%☠", int(0), "%!☠(int=0)"}, 1026 {"%☠", uint(0), "%!☠(uint=0)"}, 1027 {"%☠", []byte{0, 1}, "[%!☠(uint8=0) %!☠(uint8=1)]"}, 1028 {"%☠", []uint8{0, 1}, "[%!☠(uint8=0) %!☠(uint8=1)]"}, 1029 {"%☠", [1]byte{0}, "[%!☠(uint8=0)]"}, 1030 {"%☠", [1]uint8{0}, "[%!☠(uint8=0)]"}, 1031 {"%☠", "hello", "%!☠(string=hello)"}, 1032 {"%☠", 1.2345678, "%!☠(float64=1.2345678)"}, 1033 {"%☠", float32(1.2345678), "%!☠(float32=1.2345678)"}, 1034 {"%☠", 1.2345678 + 1.2345678i, "%!☠(complex128=(1.2345678+1.2345678i))"}, 1035 {"%☠", complex64(1.2345678 + 1.2345678i), "%!☠(complex64=(1.2345678+1.2345678i))"}, 1036 {"%☠", &intVar, "%!☠(*int=0xPTR)"}, 1037 {"%☠", make(chan int), "%!☠(chan int=0xPTR)"}, 1038 {"%☠", func() {}, "%!☠(func()=0xPTR)"}, 1039 {"%☠", reflect.ValueOf(renamedInt(0)), "%!☠(fmt_test.renamedInt=0)"}, 1040 {"%☠", SI{renamedInt(0)}, "{%!☠(fmt_test.renamedInt=0)}"}, 1041 {"%☠", &[]interface{}{I(1), G(2)}, "&[%!☠(fmt_test.I=1) %!☠(fmt_test.G=2)]"}, 1042 {"%☠", SI{&[]interface{}{I(1), G(2)}}, "{%!☠(*[]interface {}=&[1 2])}"}, 1043 {"%☠", reflect.Value{}, "<invalid reflect.Value>"}, 1044 {"%☠", map[float64]int{NaN: 1}, "map[%!☠(float64=NaN):%!☠(int=1)]"}, 1045 } 1046 1047 // zeroFill generates zero-filled strings of the specified width. The length 1048 // of the suffix (but not the prefix) is compensated for in the width calculation. 1049 func zeroFill(prefix string, width int, suffix string) string { 1050 return prefix + strings.Repeat("0", width-len(suffix)) + suffix 1051 } 1052 1053 func TestSprintf(t *testing.T) { 1054 for _, tt := range fmtTests { 1055 s := Sprintf(tt.fmt, tt.val) 1056 i := strings.Index(tt.out, "PTR") 1057 if i >= 0 && i < len(s) { 1058 var pattern, chars string 1059 switch { 1060 case strings.HasPrefix(tt.out[i:], "PTR_b"): 1061 pattern = "PTR_b" 1062 chars = "01" 1063 case strings.HasPrefix(tt.out[i:], "PTR_o"): 1064 pattern = "PTR_o" 1065 chars = "01234567" 1066 case strings.HasPrefix(tt.out[i:], "PTR_d"): 1067 pattern = "PTR_d" 1068 chars = "0123456789" 1069 case strings.HasPrefix(tt.out[i:], "PTR_x"): 1070 pattern = "PTR_x" 1071 chars = "0123456789abcdef" 1072 case strings.HasPrefix(tt.out[i:], "PTR_X"): 1073 pattern = "PTR_X" 1074 chars = "0123456789ABCDEF" 1075 default: 1076 pattern = "PTR" 1077 chars = "0123456789abcdefABCDEF" 1078 } 1079 p := s[:i] + pattern 1080 for j := i; j < len(s); j++ { 1081 if !strings.ContainsRune(chars, rune(s[j])) { 1082 p += s[j:] 1083 break 1084 } 1085 } 1086 s = p 1087 } 1088 if s != tt.out { 1089 if _, ok := tt.val.(string); ok { 1090 // Don't requote the already-quoted strings. 1091 // It's too confusing to read the errors. 1092 t.Errorf("Sprintf(%q, %q) = <%s> want <%s>", tt.fmt, tt.val, s, tt.out) 1093 } else { 1094 t.Errorf("Sprintf(%q, %v) = %q want %q", tt.fmt, tt.val, s, tt.out) 1095 } 1096 } 1097 } 1098 } 1099 1100 // TestComplexFormatting checks that a complex always formats to the same 1101 // thing as if done by hand with two singleton prints. 1102 func TestComplexFormatting(t *testing.T) { 1103 var yesNo = []bool{true, false} 1104 var values = []float64{1, 0, -1, posInf, negInf, NaN} 1105 for _, plus := range yesNo { 1106 for _, zero := range yesNo { 1107 for _, space := range yesNo { 1108 for _, char := range "fFeEgG" { 1109 realFmt := "%" 1110 if zero { 1111 realFmt += "0" 1112 } 1113 if space { 1114 realFmt += " " 1115 } 1116 if plus { 1117 realFmt += "+" 1118 } 1119 realFmt += "10.2" 1120 realFmt += string(char) 1121 // Imaginary part always has a sign, so force + and ignore space. 1122 imagFmt := "%" 1123 if zero { 1124 imagFmt += "0" 1125 } 1126 imagFmt += "+" 1127 imagFmt += "10.2" 1128 imagFmt += string(char) 1129 for _, realValue := range values { 1130 for _, imagValue := range values { 1131 one := Sprintf(realFmt, complex(realValue, imagValue)) 1132 two := Sprintf("("+realFmt+imagFmt+"i)", realValue, imagValue) 1133 if one != two { 1134 t.Error(f, one, two) 1135 } 1136 } 1137 } 1138 } 1139 } 1140 } 1141 } 1142 } 1143 1144 type SE []interface{} // slice of empty; notational compactness. 1145 1146 var reorderTests = []struct { 1147 fmt string 1148 val SE 1149 out string 1150 }{ 1151 {"%[1]d", SE{1}, "1"}, 1152 {"%[2]d", SE{2, 1}, "1"}, 1153 {"%[2]d %[1]d", SE{1, 2}, "2 1"}, 1154 {"%[2]*[1]d", SE{2, 5}, " 2"}, 1155 {"%6.2f", SE{12.0}, " 12.00"}, // Explicit version of next line. 1156 {"%[3]*.[2]*[1]f", SE{12.0, 2, 6}, " 12.00"}, 1157 {"%[1]*.[2]*[3]f", SE{6, 2, 12.0}, " 12.00"}, 1158 {"%10f", SE{12.0}, " 12.000000"}, 1159 {"%[1]*[3]f", SE{10, 99, 12.0}, " 12.000000"}, 1160 {"%.6f", SE{12.0}, "12.000000"}, // Explicit version of next line. 1161 {"%.[1]*[3]f", SE{6, 99, 12.0}, "12.000000"}, 1162 {"%6.f", SE{12.0}, " 12"}, // // Explicit version of next line; empty precision means zero. 1163 {"%[1]*.[3]f", SE{6, 3, 12.0}, " 12"}, 1164 // An actual use! Print the same arguments twice. 1165 {"%d %d %d %#[1]o %#o %#o", SE{11, 12, 13}, "11 12 13 013 014 015"}, 1166 1167 // Erroneous cases. 1168 {"%[d", SE{2, 1}, "%!d(BADINDEX)"}, 1169 {"%]d", SE{2, 1}, "%!](int=2)d%!(EXTRA int=1)"}, 1170 {"%[]d", SE{2, 1}, "%!d(BADINDEX)"}, 1171 {"%[-3]d", SE{2, 1}, "%!d(BADINDEX)"}, 1172 {"%[99]d", SE{2, 1}, "%!d(BADINDEX)"}, 1173 {"%[3]", SE{2, 1}, "%!(NOVERB)"}, 1174 {"%[1].2d", SE{5, 6}, "%!d(BADINDEX)"}, 1175 {"%[1]2d", SE{2, 1}, "%!d(BADINDEX)"}, 1176 {"%3.[2]d", SE{7}, "%!d(BADINDEX)"}, 1177 {"%.[2]d", SE{7}, "%!d(BADINDEX)"}, 1178 {"%d %d %d %#[1]o %#o %#o %#o", SE{11, 12, 13}, "11 12 13 013 014 015 %!o(MISSING)"}, 1179 {"%[5]d %[2]d %d", SE{1, 2, 3}, "%!d(BADINDEX) 2 3"}, 1180 {"%d %[3]d %d", SE{1, 2}, "1 %!d(BADINDEX) 2"}, // Erroneous index does not affect sequence. 1181 {"%.[]", SE{}, "%!](BADINDEX)"}, // Issue 10675 1182 {"%.-3d", SE{42}, "%!-(int=42)3d"}, // TODO: Should this set return better error messages? 1183 {"%2147483648d", SE{42}, "%!(NOVERB)%!(EXTRA int=42)"}, 1184 {"%-2147483648d", SE{42}, "%!(NOVERB)%!(EXTRA int=42)"}, 1185 {"%.2147483648d", SE{42}, "%!(NOVERB)%!(EXTRA int=42)"}, 1186 } 1187 1188 func TestReorder(t *testing.T) { 1189 for _, tt := range reorderTests { 1190 s := Sprintf(tt.fmt, tt.val...) 1191 if s != tt.out { 1192 t.Errorf("Sprintf(%q, %v) = <%s> want <%s>", tt.fmt, tt.val, s, tt.out) 1193 } else { 1194 } 1195 } 1196 } 1197 1198 func BenchmarkSprintfPadding(b *testing.B) { 1199 b.RunParallel(func(pb *testing.PB) { 1200 for pb.Next() { 1201 Sprintf("%16f", 1.0) 1202 } 1203 }) 1204 } 1205 1206 func BenchmarkSprintfEmpty(b *testing.B) { 1207 b.RunParallel(func(pb *testing.PB) { 1208 for pb.Next() { 1209 Sprintf("") 1210 } 1211 }) 1212 } 1213 1214 func BenchmarkSprintfString(b *testing.B) { 1215 b.RunParallel(func(pb *testing.PB) { 1216 for pb.Next() { 1217 Sprintf("%s", "hello") 1218 } 1219 }) 1220 } 1221 1222 func BenchmarkSprintfTruncateString(b *testing.B) { 1223 b.RunParallel(func(pb *testing.PB) { 1224 for pb.Next() { 1225 Sprintf("%.3s", "日本語日本語日本語日本語") 1226 } 1227 }) 1228 } 1229 1230 func BenchmarkSprintfTruncateBytes(b *testing.B) { 1231 var bytes interface{} = []byte("日本語日本語日本語日本語") 1232 b.RunParallel(func(pb *testing.PB) { 1233 for pb.Next() { 1234 Sprintf("%.3s", bytes) 1235 } 1236 }) 1237 } 1238 1239 func BenchmarkSprintfSlowParsingPath(b *testing.B) { 1240 b.RunParallel(func(pb *testing.PB) { 1241 for pb.Next() { 1242 Sprintf("%.v", nil) 1243 } 1244 }) 1245 } 1246 1247 func BenchmarkSprintfQuoteString(b *testing.B) { 1248 b.RunParallel(func(pb *testing.PB) { 1249 for pb.Next() { 1250 Sprintf("%q", "日本語日本語日本語") 1251 } 1252 }) 1253 } 1254 1255 func BenchmarkSprintfInt(b *testing.B) { 1256 b.RunParallel(func(pb *testing.PB) { 1257 for pb.Next() { 1258 Sprintf("%d", 5) 1259 } 1260 }) 1261 } 1262 1263 func BenchmarkSprintfIntInt(b *testing.B) { 1264 b.RunParallel(func(pb *testing.PB) { 1265 for pb.Next() { 1266 Sprintf("%d %d", 5, 6) 1267 } 1268 }) 1269 } 1270 1271 func BenchmarkSprintfPrefixedInt(b *testing.B) { 1272 b.RunParallel(func(pb *testing.PB) { 1273 for pb.Next() { 1274 Sprintf("This is some meaningless prefix text that needs to be scanned %d", 6) 1275 } 1276 }) 1277 } 1278 1279 func BenchmarkSprintfFloat(b *testing.B) { 1280 b.RunParallel(func(pb *testing.PB) { 1281 for pb.Next() { 1282 Sprintf("%g", 5.23184) 1283 } 1284 }) 1285 } 1286 1287 func BenchmarkSprintfComplex(b *testing.B) { 1288 b.RunParallel(func(pb *testing.PB) { 1289 for pb.Next() { 1290 Sprintf("%f", 5.23184+5.23184i) 1291 } 1292 }) 1293 } 1294 1295 func BenchmarkSprintfBoolean(b *testing.B) { 1296 b.RunParallel(func(pb *testing.PB) { 1297 for pb.Next() { 1298 Sprintf("%t", true) 1299 } 1300 }) 1301 } 1302 1303 func BenchmarkSprintfHexString(b *testing.B) { 1304 b.RunParallel(func(pb *testing.PB) { 1305 for pb.Next() { 1306 Sprintf("% #x", "0123456789abcdef") 1307 } 1308 }) 1309 } 1310 1311 func BenchmarkSprintfHexBytes(b *testing.B) { 1312 data := []byte("0123456789abcdef") 1313 b.RunParallel(func(pb *testing.PB) { 1314 for pb.Next() { 1315 Sprintf("% #x", data) 1316 } 1317 }) 1318 } 1319 1320 func BenchmarkSprintfBytes(b *testing.B) { 1321 data := []byte("0123456789abcdef") 1322 b.RunParallel(func(pb *testing.PB) { 1323 for pb.Next() { 1324 Sprintf("%v", data) 1325 } 1326 }) 1327 } 1328 1329 func BenchmarkSprintfStringer(b *testing.B) { 1330 stringer := I(12345) 1331 b.RunParallel(func(pb *testing.PB) { 1332 for pb.Next() { 1333 Sprintf("%v", stringer) 1334 } 1335 }) 1336 } 1337 1338 func BenchmarkSprintfStructure(b *testing.B) { 1339 s := &[]interface{}{SI{12345}, map[int]string{0: "hello"}} 1340 b.RunParallel(func(pb *testing.PB) { 1341 for pb.Next() { 1342 Sprintf("%#v", s) 1343 } 1344 }) 1345 } 1346 1347 func BenchmarkManyArgs(b *testing.B) { 1348 b.RunParallel(func(pb *testing.PB) { 1349 var buf bytes.Buffer 1350 for pb.Next() { 1351 buf.Reset() 1352 Fprintf(&buf, "%2d/%2d/%2d %d:%d:%d %s %s\n", 3, 4, 5, 11, 12, 13, "hello", "world") 1353 } 1354 }) 1355 } 1356 1357 func BenchmarkFprintInt(b *testing.B) { 1358 var buf bytes.Buffer 1359 for i := 0; i < b.N; i++ { 1360 buf.Reset() 1361 Fprint(&buf, 123456) 1362 } 1363 } 1364 1365 func BenchmarkFprintfBytes(b *testing.B) { 1366 data := []byte(string("0123456789")) 1367 var buf bytes.Buffer 1368 for i := 0; i < b.N; i++ { 1369 buf.Reset() 1370 Fprintf(&buf, "%s", data) 1371 } 1372 } 1373 1374 func BenchmarkFprintIntNoAlloc(b *testing.B) { 1375 var x interface{} = 123456 1376 var buf bytes.Buffer 1377 for i := 0; i < b.N; i++ { 1378 buf.Reset() 1379 Fprint(&buf, x) 1380 } 1381 } 1382 1383 var mallocBuf bytes.Buffer 1384 var mallocPointer *int // A pointer so we know the interface value won't allocate. 1385 1386 var mallocTest = []struct { 1387 count int 1388 desc string 1389 fn func() 1390 }{ 1391 {0, `Sprintf("")`, func() { Sprintf("") }}, 1392 {1, `Sprintf("xxx")`, func() { Sprintf("xxx") }}, 1393 {2, `Sprintf("%x")`, func() { Sprintf("%x", 7) }}, 1394 {2, `Sprintf("%s")`, func() { Sprintf("%s", "hello") }}, 1395 {3, `Sprintf("%x %x")`, func() { Sprintf("%x %x", 7, 112) }}, 1396 {2, `Sprintf("%g")`, func() { Sprintf("%g", float32(3.14159)) }}, // TODO: Can this be 1? 1397 {1, `Fprintf(buf, "%s")`, func() { mallocBuf.Reset(); Fprintf(&mallocBuf, "%s", "hello") }}, 1398 // If the interface value doesn't need to allocate, amortized allocation overhead should be zero. 1399 {0, `Fprintf(buf, "%x %x %x")`, func() { 1400 mallocBuf.Reset() 1401 Fprintf(&mallocBuf, "%x %x %x", mallocPointer, mallocPointer, mallocPointer) 1402 }}, 1403 } 1404 1405 var _ bytes.Buffer 1406 1407 func TestCountMallocs(t *testing.T) { 1408 switch { 1409 case testing.Short(): 1410 t.Skip("skipping malloc count in short mode") 1411 case runtime.GOMAXPROCS(0) > 1: 1412 t.Skip("skipping; GOMAXPROCS>1") 1413 case race.Enabled: 1414 t.Skip("skipping malloc count under race detector") 1415 } 1416 for _, mt := range mallocTest { 1417 mallocs := testing.AllocsPerRun(100, mt.fn) 1418 if got, max := mallocs, float64(mt.count); got > max { 1419 t.Errorf("%s: got %v allocs, want <=%v", mt.desc, got, max) 1420 } 1421 } 1422 } 1423 1424 type flagPrinter struct{} 1425 1426 func (flagPrinter) Format(f State, c rune) { 1427 s := "%" 1428 for i := 0; i < 128; i++ { 1429 if f.Flag(i) { 1430 s += string(i) 1431 } 1432 } 1433 if w, ok := f.Width(); ok { 1434 s += Sprintf("%d", w) 1435 } 1436 if p, ok := f.Precision(); ok { 1437 s += Sprintf(".%d", p) 1438 } 1439 s += string(c) 1440 io.WriteString(f, "["+s+"]") 1441 } 1442 1443 var flagtests = []struct { 1444 in string 1445 out string 1446 }{ 1447 {"%a", "[%a]"}, 1448 {"%-a", "[%-a]"}, 1449 {"%+a", "[%+a]"}, 1450 {"%#a", "[%#a]"}, 1451 {"% a", "[% a]"}, 1452 {"%0a", "[%0a]"}, 1453 {"%1.2a", "[%1.2a]"}, 1454 {"%-1.2a", "[%-1.2a]"}, 1455 {"%+1.2a", "[%+1.2a]"}, 1456 {"%-+1.2a", "[%+-1.2a]"}, 1457 {"%-+1.2abc", "[%+-1.2a]bc"}, 1458 {"%-1.2abc", "[%-1.2a]bc"}, 1459 } 1460 1461 func TestFlagParser(t *testing.T) { 1462 var flagprinter flagPrinter 1463 for _, tt := range flagtests { 1464 s := Sprintf(tt.in, &flagprinter) 1465 if s != tt.out { 1466 t.Errorf("Sprintf(%q, &flagprinter) => %q, want %q", tt.in, s, tt.out) 1467 } 1468 } 1469 } 1470 1471 func TestStructPrinter(t *testing.T) { 1472 type T struct { 1473 a string 1474 b string 1475 c int 1476 } 1477 var s T 1478 s.a = "abc" 1479 s.b = "def" 1480 s.c = 123 1481 var tests = []struct { 1482 fmt string 1483 out string 1484 }{ 1485 {"%v", "{abc def 123}"}, 1486 {"%+v", "{a:abc b:def c:123}"}, 1487 {"%#v", `fmt_test.T{a:"abc", b:"def", c:123}`}, 1488 } 1489 for _, tt := range tests { 1490 out := Sprintf(tt.fmt, s) 1491 if out != tt.out { 1492 t.Errorf("Sprintf(%q, s) = %#q, want %#q", tt.fmt, out, tt.out) 1493 } 1494 // The same but with a pointer. 1495 out = Sprintf(tt.fmt, &s) 1496 if out != "&"+tt.out { 1497 t.Errorf("Sprintf(%q, &s) = %#q, want %#q", tt.fmt, out, "&"+tt.out) 1498 } 1499 } 1500 } 1501 1502 func TestSlicePrinter(t *testing.T) { 1503 slice := []int{} 1504 s := Sprint(slice) 1505 if s != "[]" { 1506 t.Errorf("empty slice printed as %q not %q", s, "[]") 1507 } 1508 slice = []int{1, 2, 3} 1509 s = Sprint(slice) 1510 if s != "[1 2 3]" { 1511 t.Errorf("slice: got %q expected %q", s, "[1 2 3]") 1512 } 1513 s = Sprint(&slice) 1514 if s != "&[1 2 3]" { 1515 t.Errorf("&slice: got %q expected %q", s, "&[1 2 3]") 1516 } 1517 } 1518 1519 // presentInMap checks map printing using substrings so we don't depend on the 1520 // print order. 1521 func presentInMap(s string, a []string, t *testing.T) { 1522 for i := 0; i < len(a); i++ { 1523 loc := strings.Index(s, a[i]) 1524 if loc < 0 { 1525 t.Errorf("map print: expected to find %q in %q", a[i], s) 1526 } 1527 // make sure the match ends here 1528 loc += len(a[i]) 1529 if loc >= len(s) || (s[loc] != ' ' && s[loc] != ']') { 1530 t.Errorf("map print: %q not properly terminated in %q", a[i], s) 1531 } 1532 } 1533 } 1534 1535 func TestMapPrinter(t *testing.T) { 1536 m0 := make(map[int]string) 1537 s := Sprint(m0) 1538 if s != "map[]" { 1539 t.Errorf("empty map printed as %q not %q", s, "map[]") 1540 } 1541 m1 := map[int]string{1: "one", 2: "two", 3: "three"} 1542 a := []string{"1:one", "2:two", "3:three"} 1543 presentInMap(Sprintf("%v", m1), a, t) 1544 presentInMap(Sprint(m1), a, t) 1545 // Pointer to map prints the same but with initial &. 1546 if !strings.HasPrefix(Sprint(&m1), "&") { 1547 t.Errorf("no initial & for address of map") 1548 } 1549 presentInMap(Sprintf("%v", &m1), a, t) 1550 presentInMap(Sprint(&m1), a, t) 1551 } 1552 1553 func TestEmptyMap(t *testing.T) { 1554 const emptyMapStr = "map[]" 1555 var m map[string]int 1556 s := Sprint(m) 1557 if s != emptyMapStr { 1558 t.Errorf("nil map printed as %q not %q", s, emptyMapStr) 1559 } 1560 m = make(map[string]int) 1561 s = Sprint(m) 1562 if s != emptyMapStr { 1563 t.Errorf("empty map printed as %q not %q", s, emptyMapStr) 1564 } 1565 } 1566 1567 // TestBlank checks that Sprint (and hence Print, Fprint) puts spaces in the 1568 // right places, that is, between arg pairs in which neither is a string. 1569 func TestBlank(t *testing.T) { 1570 got := Sprint("<", 1, ">:", 1, 2, 3, "!") 1571 expect := "<1>:1 2 3!" 1572 if got != expect { 1573 t.Errorf("got %q expected %q", got, expect) 1574 } 1575 } 1576 1577 // TestBlankln checks that Sprintln (and hence Println, Fprintln) puts spaces in 1578 // the right places, that is, between all arg pairs. 1579 func TestBlankln(t *testing.T) { 1580 got := Sprintln("<", 1, ">:", 1, 2, 3, "!") 1581 expect := "< 1 >: 1 2 3 !\n" 1582 if got != expect { 1583 t.Errorf("got %q expected %q", got, expect) 1584 } 1585 } 1586 1587 // TestFormatterPrintln checks Formatter with Sprint, Sprintln, Sprintf. 1588 func TestFormatterPrintln(t *testing.T) { 1589 f := F(1) 1590 expect := "<v=F(1)>\n" 1591 s := Sprint(f, "\n") 1592 if s != expect { 1593 t.Errorf("Sprint wrong with Formatter: expected %q got %q", expect, s) 1594 } 1595 s = Sprintln(f) 1596 if s != expect { 1597 t.Errorf("Sprintln wrong with Formatter: expected %q got %q", expect, s) 1598 } 1599 s = Sprintf("%v\n", f) 1600 if s != expect { 1601 t.Errorf("Sprintf wrong with Formatter: expected %q got %q", expect, s) 1602 } 1603 } 1604 1605 func args(a ...interface{}) []interface{} { return a } 1606 1607 var startests = []struct { 1608 fmt string 1609 in []interface{} 1610 out string 1611 }{ 1612 {"%*d", args(4, 42), " 42"}, 1613 {"%-*d", args(4, 42), "42 "}, 1614 {"%*d", args(-4, 42), "42 "}, 1615 {"%-*d", args(-4, 42), "42 "}, 1616 {"%.*d", args(4, 42), "0042"}, 1617 {"%*.*d", args(8, 4, 42), " 0042"}, 1618 {"%0*d", args(4, 42), "0042"}, 1619 // Some non-int types for width. (Issue 10732). 1620 {"%0*d", args(uint(4), 42), "0042"}, 1621 {"%0*d", args(uint64(4), 42), "0042"}, 1622 {"%0*d", args('\x04', 42), "0042"}, 1623 {"%0*d", args(uintptr(4), 42), "0042"}, 1624 1625 // erroneous 1626 {"%*d", args(nil, 42), "%!(BADWIDTH)42"}, 1627 {"%*d", args(int(1e7), 42), "%!(BADWIDTH)42"}, 1628 {"%*d", args(int(-1e7), 42), "%!(BADWIDTH)42"}, 1629 {"%.*d", args(nil, 42), "%!(BADPREC)42"}, 1630 {"%.*d", args(-1, 42), "%!(BADPREC)42"}, 1631 {"%.*d", args(int(1e7), 42), "%!(BADPREC)42"}, 1632 {"%.*d", args(uint(1e7), 42), "%!(BADPREC)42"}, 1633 {"%.*d", args(uint64(1<<63), 42), "%!(BADPREC)42"}, // Huge negative (-inf). 1634 {"%.*d", args(uint64(1<<64-1), 42), "%!(BADPREC)42"}, // Small negative (-1). 1635 {"%*d", args(5, "foo"), "%!d(string= foo)"}, 1636 {"%*% %d", args(20, 5), "% 5"}, 1637 {"%*", args(4), "%!(NOVERB)"}, 1638 } 1639 1640 func TestWidthAndPrecision(t *testing.T) { 1641 for i, tt := range startests { 1642 s := Sprintf(tt.fmt, tt.in...) 1643 if s != tt.out { 1644 t.Errorf("#%d: %q: got %q expected %q", i, tt.fmt, s, tt.out) 1645 } 1646 } 1647 } 1648 1649 // PanicS is a type that panics in String. 1650 type PanicS struct { 1651 message interface{} 1652 } 1653 1654 // Value receiver. 1655 func (p PanicS) String() string { 1656 panic(p.message) 1657 } 1658 1659 // PanicGo is a type that panics in GoString. 1660 type PanicGo struct { 1661 message interface{} 1662 } 1663 1664 // Value receiver. 1665 func (p PanicGo) GoString() string { 1666 panic(p.message) 1667 } 1668 1669 // PanicF is a type that panics in Format. 1670 type PanicF struct { 1671 message interface{} 1672 } 1673 1674 // Value receiver. 1675 func (p PanicF) Format(f State, c rune) { 1676 panic(p.message) 1677 } 1678 1679 var panictests = []struct { 1680 fmt string 1681 in interface{} 1682 out string 1683 }{ 1684 // String 1685 {"%s", (*PanicS)(nil), "<nil>"}, // nil pointer special case 1686 {"%s", PanicS{io.ErrUnexpectedEOF}, "%!s(PANIC=String method: unexpected EOF)"}, 1687 {"%s", PanicS{3}, "%!s(PANIC=String method: 3)"}, 1688 // GoString 1689 {"%#v", (*PanicGo)(nil), "<nil>"}, // nil pointer special case 1690 {"%#v", PanicGo{io.ErrUnexpectedEOF}, "%!v(PANIC=GoString method: unexpected EOF)"}, 1691 {"%#v", PanicGo{3}, "%!v(PANIC=GoString method: 3)"}, 1692 // Issue 18282. catchPanic should not clear fmtFlags permanently. 1693 {"%#v", []interface{}{PanicGo{3}, PanicGo{3}}, "[]interface {}{%!v(PANIC=GoString method: 3), %!v(PANIC=GoString method: 3)}"}, 1694 // Format 1695 {"%s", (*PanicF)(nil), "<nil>"}, // nil pointer special case 1696 {"%s", PanicF{io.ErrUnexpectedEOF}, "%!s(PANIC=Format method: unexpected EOF)"}, 1697 {"%s", PanicF{3}, "%!s(PANIC=Format method: 3)"}, 1698 } 1699 1700 func TestPanics(t *testing.T) { 1701 for i, tt := range panictests { 1702 s := Sprintf(tt.fmt, tt.in) 1703 if s != tt.out { 1704 t.Errorf("%d: %q: got %q expected %q", i, tt.fmt, s, tt.out) 1705 } 1706 } 1707 } 1708 1709 // recurCount tests that erroneous String routine doesn't cause fatal recursion. 1710 var recurCount = 0 1711 1712 type Recur struct { 1713 i int 1714 failed *bool 1715 } 1716 1717 func (r *Recur) String() string { 1718 if recurCount++; recurCount > 10 { 1719 *r.failed = true 1720 return "FAIL" 1721 } 1722 // This will call badVerb. Before the fix, that would cause us to recur into 1723 // this routine to print %!p(value). Now we don't call the user's method 1724 // during an error. 1725 return Sprintf("recur@%p value: %d", r, r.i) 1726 } 1727 1728 func TestBadVerbRecursion(t *testing.T) { 1729 failed := false 1730 r := &Recur{3, &failed} 1731 Sprintf("recur@%p value: %d\n", &r, r.i) 1732 if failed { 1733 t.Error("fail with pointer") 1734 } 1735 failed = false 1736 r = &Recur{4, &failed} 1737 Sprintf("recur@%p, value: %d\n", r, r.i) 1738 if failed { 1739 t.Error("fail with value") 1740 } 1741 } 1742 1743 func TestIsSpace(t *testing.T) { 1744 // This tests the internal isSpace function. 1745 // IsSpace = isSpace is defined in export_test.go. 1746 for i := rune(0); i <= unicode.MaxRune; i++ { 1747 if IsSpace(i) != unicode.IsSpace(i) { 1748 t.Errorf("isSpace(%U) = %v, want %v", i, IsSpace(i), unicode.IsSpace(i)) 1749 } 1750 } 1751 } 1752 1753 func hideFromVet(s string) string { return s } 1754 1755 func TestNilDoesNotBecomeTyped(t *testing.T) { 1756 type A struct{} 1757 type B struct{} 1758 var a *A = nil 1759 var b B = B{} 1760 got := Sprintf(hideFromVet("%s %s %s %s %s"), nil, a, nil, b, nil) 1761 const expect = "%!s(<nil>) %!s(*fmt_test.A=<nil>) %!s(<nil>) {} %!s(<nil>)" 1762 if got != expect { 1763 t.Errorf("expected:\n\t%q\ngot:\n\t%q", expect, got) 1764 } 1765 } 1766 1767 var formatterFlagTests = []struct { 1768 in string 1769 val interface{} 1770 out string 1771 }{ 1772 // scalar values with the (unused by fmt) 'a' verb. 1773 {"%a", flagPrinter{}, "[%a]"}, 1774 {"%-a", flagPrinter{}, "[%-a]"}, 1775 {"%+a", flagPrinter{}, "[%+a]"}, 1776 {"%#a", flagPrinter{}, "[%#a]"}, 1777 {"% a", flagPrinter{}, "[% a]"}, 1778 {"%0a", flagPrinter{}, "[%0a]"}, 1779 {"%1.2a", flagPrinter{}, "[%1.2a]"}, 1780 {"%-1.2a", flagPrinter{}, "[%-1.2a]"}, 1781 {"%+1.2a", flagPrinter{}, "[%+1.2a]"}, 1782 {"%-+1.2a", flagPrinter{}, "[%+-1.2a]"}, 1783 {"%-+1.2abc", flagPrinter{}, "[%+-1.2a]bc"}, 1784 {"%-1.2abc", flagPrinter{}, "[%-1.2a]bc"}, 1785 1786 // composite values with the 'a' verb 1787 {"%a", [1]flagPrinter{}, "[[%a]]"}, 1788 {"%-a", [1]flagPrinter{}, "[[%-a]]"}, 1789 {"%+a", [1]flagPrinter{}, "[[%+a]]"}, 1790 {"%#a", [1]flagPrinter{}, "[[%#a]]"}, 1791 {"% a", [1]flagPrinter{}, "[[% a]]"}, 1792 {"%0a", [1]flagPrinter{}, "[[%0a]]"}, 1793 {"%1.2a", [1]flagPrinter{}, "[[%1.2a]]"}, 1794 {"%-1.2a", [1]flagPrinter{}, "[[%-1.2a]]"}, 1795 {"%+1.2a", [1]flagPrinter{}, "[[%+1.2a]]"}, 1796 {"%-+1.2a", [1]flagPrinter{}, "[[%+-1.2a]]"}, 1797 {"%-+1.2abc", [1]flagPrinter{}, "[[%+-1.2a]]bc"}, 1798 {"%-1.2abc", [1]flagPrinter{}, "[[%-1.2a]]bc"}, 1799 1800 // simple values with the 'v' verb 1801 {"%v", flagPrinter{}, "[%v]"}, 1802 {"%-v", flagPrinter{}, "[%-v]"}, 1803 {"%+v", flagPrinter{}, "[%+v]"}, 1804 {"%#v", flagPrinter{}, "[%#v]"}, 1805 {"% v", flagPrinter{}, "[% v]"}, 1806 {"%0v", flagPrinter{}, "[%0v]"}, 1807 {"%1.2v", flagPrinter{}, "[%1.2v]"}, 1808 {"%-1.2v", flagPrinter{}, "[%-1.2v]"}, 1809 {"%+1.2v", flagPrinter{}, "[%+1.2v]"}, 1810 {"%-+1.2v", flagPrinter{}, "[%+-1.2v]"}, 1811 {"%-+1.2vbc", flagPrinter{}, "[%+-1.2v]bc"}, 1812 {"%-1.2vbc", flagPrinter{}, "[%-1.2v]bc"}, 1813 1814 // composite values with the 'v' verb. 1815 {"%v", [1]flagPrinter{}, "[[%v]]"}, 1816 {"%-v", [1]flagPrinter{}, "[[%-v]]"}, 1817 {"%+v", [1]flagPrinter{}, "[[%+v]]"}, 1818 {"%#v", [1]flagPrinter{}, "[1]fmt_test.flagPrinter{[%#v]}"}, 1819 {"% v", [1]flagPrinter{}, "[[% v]]"}, 1820 {"%0v", [1]flagPrinter{}, "[[%0v]]"}, 1821 {"%1.2v", [1]flagPrinter{}, "[[%1.2v]]"}, 1822 {"%-1.2v", [1]flagPrinter{}, "[[%-1.2v]]"}, 1823 {"%+1.2v", [1]flagPrinter{}, "[[%+1.2v]]"}, 1824 {"%-+1.2v", [1]flagPrinter{}, "[[%+-1.2v]]"}, 1825 {"%-+1.2vbc", [1]flagPrinter{}, "[[%+-1.2v]]bc"}, 1826 {"%-1.2vbc", [1]flagPrinter{}, "[[%-1.2v]]bc"}, 1827 } 1828 1829 func TestFormatterFlags(t *testing.T) { 1830 for _, tt := range formatterFlagTests { 1831 s := Sprintf(tt.in, tt.val) 1832 if s != tt.out { 1833 t.Errorf("Sprintf(%q, %T) = %q, want %q", tt.in, tt.val, s, tt.out) 1834 } 1835 } 1836 } 1837 1838 func TestParsenum(t *testing.T) { 1839 testCases := []struct { 1840 s string 1841 start, end int 1842 num int 1843 isnum bool 1844 newi int 1845 }{ 1846 {"a123", 0, 4, 0, false, 0}, 1847 {"1234", 1, 1, 0, false, 1}, 1848 {"123a", 0, 4, 123, true, 3}, 1849 {"12a3", 0, 4, 12, true, 2}, 1850 {"1234", 0, 4, 1234, true, 4}, 1851 {"1a234", 1, 3, 0, false, 1}, 1852 } 1853 for _, tt := range testCases { 1854 num, isnum, newi := Parsenum(tt.s, tt.start, tt.end) 1855 if num != tt.num || isnum != tt.isnum || newi != tt.newi { 1856 t.Errorf("parsenum(%q, %d, %d) = %d, %v, %d, want %d, %v, %d", tt.s, tt.start, tt.end, num, isnum, newi, tt.num, tt.isnum, tt.newi) 1857 } 1858 } 1859 }