diff options
Diffstat (limited to 'libgo/go/fmt/fmt_test.go')
-rw-r--r-- | libgo/go/fmt/fmt_test.go | 116 |
1 files changed, 99 insertions, 17 deletions
diff --git a/libgo/go/fmt/fmt_test.go b/libgo/go/fmt/fmt_test.go index ccd80904771..8f3587b5505 100644 --- a/libgo/go/fmt/fmt_test.go +++ b/libgo/go/fmt/fmt_test.go @@ -9,6 +9,7 @@ import ( . "fmt" "io" "math" + "reflect" "runtime" "strings" "testing" @@ -135,27 +136,33 @@ var fmtTests = []struct { // basic string {"%s", "abc", "abc"}, + {"%q", "abc", `"abc"`}, {"%x", "abc", "616263"}, + {"%x", "\xff\xf0\x0f\xff", "fff00fff"}, + {"%X", "\xff\xf0\x0f\xff", "FFF00FFF"}, {"%x", "xyz", "78797a"}, {"%X", "xyz", "78797A"}, - {"%q", "abc", `"abc"`}, - {"%#x", []byte("abc\xff"), "0x616263ff"}, - {"%#X", []byte("abc\xff"), "0X616263FF"}, - {"%# x", []byte("abc\xff"), "0x61 0x62 0x63 0xff"}, - {"%# X", []byte("abc\xff"), "0X61 0X62 0X63 0XFF"}, + {"% x", "xyz", "78 79 7a"}, + {"% X", "xyz", "78 79 7A"}, + {"%#x", "xyz", "0x78797a"}, + {"%#X", "xyz", "0X78797A"}, + {"%# x", "xyz", "0x78 0x79 0x7a"}, + {"%# X", "xyz", "0X78 0X79 0X7A"}, // basic bytes {"%s", []byte("abc"), "abc"}, + {"%q", []byte("abc"), `"abc"`}, {"%x", []byte("abc"), "616263"}, - {"% x", []byte("abc\xff"), "61 62 63 ff"}, - {"%#x", []byte("abc\xff"), "0x616263ff"}, - {"%#X", []byte("abc\xff"), "0X616263FF"}, - {"%# x", []byte("abc\xff"), "0x61 0x62 0x63 0xff"}, - {"%# X", []byte("abc\xff"), "0X61 0X62 0X63 0XFF"}, - {"% X", []byte("abc\xff"), "61 62 63 FF"}, + {"%x", []byte("\xff\xf0\x0f\xff"), "fff00fff"}, + {"%X", []byte("\xff\xf0\x0f\xff"), "FFF00FFF"}, {"%x", []byte("xyz"), "78797a"}, {"%X", []byte("xyz"), "78797A"}, - {"%q", []byte("abc"), `"abc"`}, + {"% x", []byte("xyz"), "78 79 7a"}, + {"% X", []byte("xyz"), "78 79 7A"}, + {"%#x", []byte("xyz"), "0x78797a"}, + {"%#X", []byte("xyz"), "0X78797A"}, + {"%# x", []byte("xyz"), "0x78 0x79 0x7a"}, + {"%# X", []byte("xyz"), "0X78 0X79 0X7A"}, // escaped strings {"%#q", `abc`, "`abc`"}, @@ -388,6 +395,8 @@ var fmtTests = []struct { {"%v", &slice, "&[1 2 3 4 5]"}, {"%v", &islice, "&[1 hello 2.5 <nil>]"}, {"%v", &bslice, "&[1 2 3 4 5]"}, + {"%v", []byte{1}, "[1]"}, + {"%v", []byte{}, "[]"}, // complexes with %v {"%v", 1 + 2i, "(1+2i)"}, @@ -441,6 +450,32 @@ var fmtTests = []struct { {"%d", []int{1, 2, 15}, `[1 2 15]`}, {"%d", []byte{1, 2, 15}, `[1 2 15]`}, {"%q", []string{"a", "b"}, `["a" "b"]`}, + {"% 02x", []byte{1}, "01"}, + {"% 02x", []byte{1, 2, 3}, "01 02 03"}, + // Padding with byte slices. + {"%x", []byte{}, ""}, + {"%02x", []byte{}, "00"}, + {"% 02x", []byte{}, "00"}, + {"%08x", []byte{0xab}, "000000ab"}, + {"% 08x", []byte{0xab}, "000000ab"}, + {"%08x", []byte{0xab, 0xcd}, "0000abcd"}, + {"% 08x", []byte{0xab, 0xcd}, "000ab cd"}, + {"%8x", []byte{0xab}, " ab"}, + {"% 8x", []byte{0xab}, " ab"}, + {"%8x", []byte{0xab, 0xcd}, " abcd"}, + {"% 8x", []byte{0xab, 0xcd}, " ab cd"}, + // Same for strings + {"%x", "", ""}, + {"%02x", "", "00"}, + {"% 02x", "", "00"}, + {"%08x", "\xab", "000000ab"}, + {"% 08x", "\xab", "000000ab"}, + {"%08x", "\xab\xcd", "0000abcd"}, + {"% 08x", "\xab\xcd", "000ab cd"}, + {"%8x", "\xab", " ab"}, + {"% 8x", "\xab", " ab"}, + {"%8x", "\xab\xcd", " abcd"}, + {"% 8x", "\xab\xcd", " ab cd"}, // renamings {"%v", renamedBool(true), "true"}, @@ -522,6 +557,8 @@ var fmtTests = []struct { {"%s", nil, "%!s(<nil>)"}, {"%T", nil, "<nil>"}, {"%-1", 100, "%!(NOVERB)%!(EXTRA int=100)"}, + {"%017091901790959340919092959340919017929593813360", 0, "%!(NOVERB)%!(EXTRA int=0)"}, + {"%184467440737095516170v", 0, "%!(NOVERB)%!(EXTRA int=0)"}, // The "<nil>" show up because maps are printed by // first obtaining a list of keys and then looking up @@ -540,6 +577,15 @@ var fmtTests = []struct { {"%0.100f", 1.0, zeroFill("1.", 100, "")}, {"%0.100f", -1.0, zeroFill("-1.", 100, "")}, + // Used to panic: integer function didn't look at f.prec, f.unicode, f.width or sign. + {"%#.80x", 42, "0x0000000000000000000000000000000000000000000000000000000000000000000000000000002a"}, + {"%.80U", 42, "U+0000000000000000000000000000000000000000000000000000000000000000000000000000002A"}, + {"%#.80U", '日', "U+000000000000000000000000000000000000000000000000000000000000000000000000000065E5 '日'"}, + {"%.65d", -44, "-00000000000000000000000000000000000000000000000000000000000000044"}, + {"%+.65d", 44, "+00000000000000000000000000000000000000000000000000000000000000044"}, + {"% .65d", 44, " 00000000000000000000000000000000000000000000000000000000000000044"}, + {"% +.65d", 44, "+00000000000000000000000000000000000000000000000000000000000000044"}, + // Comparison of padding rules with C printf. /* C program: @@ -665,6 +711,20 @@ var fmtTests = []struct { {"%x", byteFormatterSlice, "61626364"}, // This next case seems wrong, but the docs say the Formatter wins here. {"%#v", byteFormatterSlice, "[]fmt_test.byteFormatter{X, X, X, X}"}, + + // reflect.Value handled specially in Go 1.5, making it possible to + // see inside non-exported fields (which cannot be accessed with Interface()). + // Issue 8965. + {"%v", reflect.ValueOf(A{}).Field(0).String(), "<int Value>"}, // Equivalent to the old way. + {"%v", reflect.ValueOf(A{}).Field(0), "0"}, // Sees inside the field. + + // verbs apply to the extracted value too. + {"%s", reflect.ValueOf("hello"), "hello"}, + {"%q", reflect.ValueOf("hello"), `"hello"`}, + {"%#04x", reflect.ValueOf(256), "0x0100"}, + + // invalid reflect.Value doesn't crash. + {"%v", reflect.Value{}, "<invalid reflect.Value>"}, } // zeroFill generates zero-filled strings of the specified width. The length @@ -791,6 +851,11 @@ var reorderTests = []struct { {"%d %d %d %#[1]o %#o %#o %#o", SE{11, 12, 13}, "11 12 13 013 014 015 %!o(MISSING)"}, {"%[5]d %[2]d %d", SE{1, 2, 3}, "%!d(BADINDEX) 2 3"}, {"%d %[3]d %d", SE{1, 2}, "1 %!d(BADINDEX) 2"}, // Erroneous index does not affect sequence. + {"%.[]", SE{}, "%!](BADINDEX)"}, // Issue 10675 + {"%.-3d", SE{42}, "%!-(int=42)3d"}, // TODO: Should this set return better error messages? + {"%2147483648d", SE{42}, "%!(NOVERB)%!(EXTRA int=42)"}, + {"%-2147483648d", SE{42}, "%!(NOVERB)%!(EXTRA int=42)"}, + {"%.2147483648d", SE{42}, "%!(NOVERB)%!(EXTRA int=42)"}, } func TestReorder(t *testing.T) { @@ -869,6 +934,15 @@ func BenchmarkFprintInt(b *testing.B) { } } +func BenchmarkFprintfBytes(b *testing.B) { + data := []byte(string("0123456789")) + var buf bytes.Buffer + for i := 0; i < b.N; i++ { + buf.Reset() + Fprintf(&buf, "%s", data) + } +} + func BenchmarkFprintIntNoAlloc(b *testing.B) { var x interface{} = 123456 var buf bytes.Buffer @@ -905,11 +979,13 @@ var mallocTest = []struct { var _ bytes.Buffer func TestCountMallocs(t *testing.T) { - if testing.Short() { + switch { + case testing.Short(): t.Skip("skipping malloc count in short mode") - } - if runtime.GOMAXPROCS(0) > 1 { + case runtime.GOMAXPROCS(0) > 1: t.Skip("skipping; GOMAXPROCS>1") + case raceenabled: + t.Skip("skipping malloc count under race detector") } for _, mt := range mallocTest { mallocs := testing.AllocsPerRun(100, mt.fn) @@ -1108,14 +1184,20 @@ var startests = []struct { out string }{ {"%*d", args(4, 42), " 42"}, + {"%-*d", args(4, 42), "42 "}, + {"%*d", args(-4, 42), "42 "}, + {"%-*d", args(-4, 42), "42 "}, {"%.*d", args(4, 42), "0042"}, {"%*.*d", args(8, 4, 42), " 0042"}, {"%0*d", args(4, 42), "0042"}, - {"%-*d", args(4, 42), "42 "}, // erroneous {"%*d", args(nil, 42), "%!(BADWIDTH)42"}, + {"%*d", args(int(1e7), 42), "%!(BADWIDTH)42"}, + {"%*d", args(int(-1e7), 42), "%!(BADWIDTH)42"}, {"%.*d", args(nil, 42), "%!(BADPREC)42"}, + {"%.*d", args(-1, 42), "%!(BADPREC)42"}, + {"%.*d", args(int(1e7), 42), "%!(BADPREC)42"}, {"%*d", args(5, "foo"), "%!d(string= foo)"}, {"%*% %d", args(20, 5), "% 5"}, {"%*", args(4), "%!(NOVERB)"}, @@ -1233,7 +1315,7 @@ func TestNilDoesNotBecomeTyped(t *testing.T) { type B struct{} var a *A = nil var b B = B{} - got := Sprintf("%s %s %s %s %s", nil, a, nil, b, nil) + got := Sprintf("%s %s %s %s %s", nil, a, nil, b, nil) // go vet should complain about this line. const expect = "%!s(<nil>) %!s(*fmt_test.A=<nil>) %!s(<nil>) {} %!s(<nil>)" if got != expect { t.Errorf("expected:\n\t%q\ngot:\n\t%q", expect, got) |