diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2011-12-14 15:41:54 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2011-12-14 15:41:54 +0000 |
commit | d5363590597572228d4e0d0ae13f3469176ceb14 (patch) | |
tree | e3de46cbc89d82ca1f49843fe2e1e670db67795e /libgo/go/fmt/fmt_test.go | |
parent | ef0d4c4d9937276c8ff818ecb0b92925d322d3bd (diff) | |
download | gcc-d5363590597572228d4e0d0ae13f3469176ceb14.tar.gz |
libgo: Update to weekly.2011-12-06.
From-SVN: r182338
Diffstat (limited to 'libgo/go/fmt/fmt_test.go')
-rw-r--r-- | libgo/go/fmt/fmt_test.go | 64 |
1 files changed, 42 insertions, 22 deletions
diff --git a/libgo/go/fmt/fmt_test.go b/libgo/go/fmt/fmt_test.go index 00aac798cb0..63c33380a25 100644 --- a/libgo/go/fmt/fmt_test.go +++ b/libgo/go/fmt/fmt_test.go @@ -12,6 +12,7 @@ import ( "runtime" // for the malloc count test only "strings" "testing" + "time" ) type ( @@ -352,7 +353,7 @@ var fmttests = []struct { {"%s", I(23), `<23>`}, {"%q", I(23), `"<23>"`}, {"%x", I(23), `3c32333e`}, - {"%d", I(23), `%!d(string=<23>)`}, + {"%d", I(23), `23`}, // Stringer applies only to string formats. // go syntax {"%#v", A{1, 2, "a", []int{1, 2}}, `fmt_test.A{i:1, j:0x2, s:"a", x:[]int{1, 2}}`}, @@ -361,8 +362,8 @@ var fmttests = []struct { {"%#v", make(chan int), "(chan int)(0xPTR)"}, {"%#v", uint64(1<<64 - 1), "0xffffffffffffffff"}, {"%#v", 1000000000, "1000000000"}, - {"%#v", map[string]int{"a": 1}, `map[string] int{"a":1}`}, - {"%#v", map[string]B{"a": {1, 2}}, `map[string] fmt_test.B{"a":fmt_test.B{I:1, j:2}}`}, + {"%#v", map[string]int{"a": 1}, `map[string]int{"a":1}`}, + {"%#v", map[string]B{"a": {1, 2}}, `map[string]fmt_test.B{"a":fmt_test.B{I:1, j:2}}`}, {"%#v", []string{"a", "b"}, `[]string{"a", "b"}`}, {"%#v", SI{}, `fmt_test.SI{I:interface {}(nil)}`}, {"%#v", []int(nil), `[]int(nil)`}, @@ -371,8 +372,8 @@ var fmttests = []struct { {"%#v", &array, `&[5]int{1, 2, 3, 4, 5}`}, {"%#v", iarray, `[4]interface {}{1, "hello", 2.5, interface {}(nil)}`}, {"%#v", &iarray, `&[4]interface {}{1, "hello", 2.5, interface {}(nil)}`}, - {"%#v", map[int]byte(nil), `map[int] uint8(nil)`}, - {"%#v", map[int]byte{}, `map[int] uint8{}`}, + {"%#v", map[int]byte(nil), `map[int]uint8(nil)`}, + {"%#v", map[int]byte{}, `map[int]uint8{}`}, // slices with other formats {"%#x", []int{1, 2, 15}, `[0x1 0x2 0xf]`}, @@ -430,6 +431,10 @@ var fmttests = []struct { {"%p", make([]int, 1), "0xPTR"}, {"%p", 27, "%!p(int=27)"}, // not a pointer at all + // %d on Stringer should give integer if possible + {"%s", time.Time{}.Month(), "January"}, + {"%d", time.Time{}.Month(), "1"}, + // erroneous things {"%s %", "hello", "hello %!(NOVERB)"}, {"%s %.2", "hello", "hello %!(NOVERB)"}, @@ -495,69 +500,84 @@ func BenchmarkSprintfPrefixedInt(b *testing.B) { } } +func BenchmarkSprintfFloat(b *testing.B) { + for i := 0; i < b.N; i++ { + Sprintf("%g", 5.23184) + } +} + func TestCountMallocs(t *testing.T) { if testing.Short() { return } + const N = 100 runtime.UpdateMemStats() mallocs := 0 - runtime.MemStats.Mallocs - for i := 0; i < 100; i++ { + for i := 0; i < N; i++ { Sprintf("") } runtime.UpdateMemStats() mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Sprintf(\"\"): %d\n", mallocs/100) + Printf("mallocs per Sprintf(\"\"): %d\n", mallocs/N) runtime.UpdateMemStats() mallocs = 0 - runtime.MemStats.Mallocs - for i := 0; i < 100; i++ { + for i := 0; i < N; i++ { Sprintf("xxx") } runtime.UpdateMemStats() mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Sprintf(\"xxx\"): %d\n", mallocs/100) + Printf("mallocs per Sprintf(\"xxx\"): %d\n", mallocs/N) runtime.UpdateMemStats() mallocs = 0 - runtime.MemStats.Mallocs - for i := 0; i < 100; i++ { + for i := 0; i < N; i++ { Sprintf("%x", i) } runtime.UpdateMemStats() mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Sprintf(\"%%x\"): %d\n", mallocs/100) + Printf("mallocs per Sprintf(\"%%x\"): %d\n", mallocs/N) runtime.UpdateMemStats() mallocs = 0 - runtime.MemStats.Mallocs - for i := 0; i < 100; i++ { + for i := 0; i < N; i++ { Sprintf("%s", "hello") } runtime.UpdateMemStats() mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Sprintf(\"%%s\"): %d\n", mallocs/100) + Printf("mallocs per Sprintf(\"%%s\"): %d\n", mallocs/N) runtime.UpdateMemStats() mallocs = 0 - runtime.MemStats.Mallocs - for i := 0; i < 100; i++ { + for i := 0; i < N; i++ { Sprintf("%x %x", i, i) } runtime.UpdateMemStats() mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Sprintf(\"%%x %%x\"): %d\n", mallocs/100) + Printf("mallocs per Sprintf(\"%%x %%x\"): %d\n", mallocs/N) + runtime.UpdateMemStats() + mallocs = 0 - runtime.MemStats.Mallocs + for i := 0; i < N; i++ { + Sprintf("%g", 3.14159) + } + runtime.UpdateMemStats() + mallocs += runtime.MemStats.Mallocs + Printf("mallocs per Sprintf(\"%%g\"): %d\n", mallocs/N) buf := new(bytes.Buffer) runtime.UpdateMemStats() mallocs = 0 - runtime.MemStats.Mallocs - for i := 0; i < 100; i++ { + for i := 0; i < N; i++ { buf.Reset() Fprintf(buf, "%x %x %x", i, i, i) } runtime.UpdateMemStats() mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Fprintf(buf, \"%%x %%x %%x\"): %d\n", mallocs/100) + Printf("mallocs per Fprintf(buf, \"%%x %%x %%x\"): %d\n", mallocs/N) runtime.UpdateMemStats() mallocs = 0 - runtime.MemStats.Mallocs - for i := 0; i < 100; i++ { + for i := 0; i < N; i++ { buf.Reset() Fprintf(buf, "%s", "hello") } runtime.UpdateMemStats() mallocs += runtime.MemStats.Mallocs - Printf("mallocs per Fprintf(buf, \"%%s\"): %d\n", mallocs/100) + Printf("mallocs per Fprintf(buf, \"%%s\"): %d\n", mallocs/N) } type flagPrinter struct{} @@ -772,9 +792,9 @@ var panictests = []struct { out string }{ // String - {"%d", (*Panic)(nil), "<nil>"}, // nil pointer special case - {"%d", Panic{io.ErrUnexpectedEOF}, "%d(PANIC=unexpected EOF)"}, - {"%d", Panic{3}, "%d(PANIC=3)"}, + {"%s", (*Panic)(nil), "<nil>"}, // nil pointer special case + {"%s", Panic{io.ErrUnexpectedEOF}, "%s(PANIC=unexpected EOF)"}, + {"%s", Panic{3}, "%s(PANIC=3)"}, // GoString {"%#v", (*Panic)(nil), "<nil>"}, // nil pointer special case {"%#v", Panic{io.ErrUnexpectedEOF}, "%v(PANIC=unexpected EOF)"}, |