diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2015-10-31 00:59:47 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2015-10-31 00:59:47 +0000 |
commit | af146490bb04205107cb23e301ec7a8ff927b5fc (patch) | |
tree | 13beeaed3698c61903fe93fb1ce70bd9b18d4e7f /libgo/go/testing | |
parent | 725e1be3406315d9bcc8195d7eef0a7082b3c7cc (diff) | |
download | gcc-af146490bb04205107cb23e301ec7a8ff927b5fc.zip gcc-af146490bb04205107cb23e301ec7a8ff927b5fc.tar.gz gcc-af146490bb04205107cb23e301ec7a8ff927b5fc.tar.bz2 |
runtime: Remove now unnecessary pad field from ParFor.
It is not needed due to the removal of the ctx field.
Reviewed-on: https://go-review.googlesource.com/16525
From-SVN: r229616
Diffstat (limited to 'libgo/go/testing')
-rw-r--r-- | libgo/go/testing/benchmark.go | 34 | ||||
-rw-r--r-- | libgo/go/testing/example.go | 6 | ||||
-rw-r--r-- | libgo/go/testing/iotest/logger.go | 2 | ||||
-rw-r--r-- | libgo/go/testing/quick/quick.go | 26 | ||||
-rw-r--r-- | libgo/go/testing/quick/quick_test.go | 47 | ||||
-rw-r--r-- | libgo/go/testing/testing.go | 62 |
6 files changed, 132 insertions, 45 deletions
diff --git a/libgo/go/testing/benchmark.go b/libgo/go/testing/benchmark.go index ffd5376..62e696d 100644 --- a/libgo/go/testing/benchmark.go +++ b/libgo/go/testing/benchmark.go @@ -280,6 +280,14 @@ func (r BenchmarkResult) MemString() string { r.AllocedBytesPerOp(), r.AllocsPerOp()) } +// benchmarkName returns full name of benchmark including procs suffix. +func benchmarkName(name string, n int) string { + if n != 1 { + return fmt.Sprintf("%s-%d", name, n) + } + return name +} + // An internal function but exported because it is cross-package; part of the implementation // of the "go test" command. func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark) { @@ -287,15 +295,30 @@ func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [ if len(*matchBenchmarks) == 0 { return } + // Collect matching benchmarks and determine longest name. + maxprocs := 1 + for _, procs := range cpuList { + if procs > maxprocs { + maxprocs = procs + } + } + maxlen := 0 + var bs []InternalBenchmark for _, Benchmark := range benchmarks { matched, err := matchString(*matchBenchmarks, Benchmark.Name) if err != nil { fmt.Fprintf(os.Stderr, "testing: invalid regexp for -test.bench: %s\n", err) os.Exit(1) } - if !matched { - continue + if matched { + bs = append(bs, Benchmark) + benchName := benchmarkName(Benchmark.Name, maxprocs) + if l := len(benchName); l > maxlen { + maxlen = l + } } + } + for _, Benchmark := range bs { for _, procs := range cpuList { runtime.GOMAXPROCS(procs) b := &B{ @@ -304,11 +327,8 @@ func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [ }, benchmark: Benchmark, } - benchName := Benchmark.Name - if procs != 1 { - benchName = fmt.Sprintf("%s-%d", Benchmark.Name, procs) - } - fmt.Printf("%s\t", benchName) + benchName := benchmarkName(Benchmark.Name, procs) + fmt.Printf("%-*s\t", maxlen, benchName) r := b.run() if b.failed { // The output could be very long here, but probably isn't. diff --git a/libgo/go/testing/example.go b/libgo/go/testing/example.go index f5762e4..30baf27 100644 --- a/libgo/go/testing/example.go +++ b/libgo/go/testing/example.go @@ -43,7 +43,7 @@ func RunExamples(matchString func(pat, str string) (bool, error), examples []Int func runExample(eg InternalExample) (ok bool) { if *chatty { - fmt.Printf("=== RUN: %s\n", eg.Name) + fmt.Printf("=== RUN %s\n", eg.Name) } // Capture stdout. @@ -56,8 +56,8 @@ func runExample(eg InternalExample) (ok bool) { os.Stdout = w outC := make(chan string) go func() { - buf := new(bytes.Buffer) - _, err := io.Copy(buf, r) + var buf bytes.Buffer + _, err := io.Copy(&buf, r) r.Close() if err != nil { fmt.Fprintf(os.Stderr, "testing: copying pipe: %v\n", err) diff --git a/libgo/go/testing/iotest/logger.go b/libgo/go/testing/iotest/logger.go index 1475d9b0..0aec15c 100644 --- a/libgo/go/testing/iotest/logger.go +++ b/libgo/go/testing/iotest/logger.go @@ -48,7 +48,7 @@ func (l *readLogger) Read(p []byte) (n int, err error) { // NewReadLogger returns a reader that behaves like r except // that it logs (using log.Print) each read to standard error, -// printing the prefix and the hexadecimal data written. +// printing the prefix and the hexadecimal data read. func NewReadLogger(prefix string, r io.Reader) io.Reader { return &readLogger{prefix, r} } diff --git a/libgo/go/testing/quick/quick.go b/libgo/go/testing/quick/quick.go index 909c65f..13c56cd 100644 --- a/libgo/go/testing/quick/quick.go +++ b/libgo/go/testing/quick/quick.go @@ -102,12 +102,16 @@ func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) { v.SetMapIndex(key, value) } case reflect.Ptr: - elem, ok := Value(concrete.Elem(), rand) - if !ok { - return reflect.Value{}, false + if rand.Intn(complexSize) == 0 { + v.Set(reflect.Zero(concrete)) // Generate nil pointer. + } else { + elem, ok := Value(concrete.Elem(), rand) + if !ok { + return reflect.Value{}, false + } + v.Set(reflect.New(concrete.Elem())) + v.Elem().Set(elem) } - v.Set(reflect.New(concrete.Elem())) - v.Elem().Set(elem) case reflect.Slice: numElems := rand.Intn(complexSize) v.Set(reflect.MakeSlice(concrete, numElems, numElems)) @@ -118,6 +122,14 @@ func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) { } v.Index(i).Set(elem) } + case reflect.Array: + for i := 0; i < v.Len(); i++ { + elem, ok := Value(concrete.Elem(), rand) + if !ok { + return reflect.Value{}, false + } + v.Index(i).Set(elem) + } case reflect.String: numChars := rand.Intn(complexSize) codePoints := make([]rune, numChars) @@ -153,7 +165,7 @@ type Config struct { Rand *rand.Rand // If non-nil, the Values function generates a slice of arbitrary // reflect.Values that are congruent with the arguments to the function - // being tested. Otherwise, the top-level Values function is used + // being tested. Otherwise, the top-level Value function is used // to generate them. Values func([]reflect.Value, *rand.Rand) } @@ -237,7 +249,7 @@ func Check(f interface{}, config *Config) (err error) { } if fType.NumOut() != 1 { - err = SetupError("function returns more than one value.") + err = SetupError("function does not return one value") return } if fType.Out(0).Kind() != reflect.Bool { diff --git a/libgo/go/testing/quick/quick_test.go b/libgo/go/testing/quick/quick_test.go index e925ba6..c79f30e 100644 --- a/libgo/go/testing/quick/quick_test.go +++ b/libgo/go/testing/quick/quick_test.go @@ -10,6 +10,12 @@ import ( "testing" ) +func fArray(a [4]byte) [4]byte { return a } + +type TestArrayAlias [4]byte + +func fArrayAlias(a TestArrayAlias) TestArrayAlias { return a } + func fBool(a bool) bool { return a } type TestBoolAlias bool @@ -76,6 +82,18 @@ type TestMapAlias map[int]int func fMapAlias(a TestMapAlias) TestMapAlias { return a } +func fPtr(a *int) *int { + if a == nil { + return nil + } + b := *a + return &b +} + +type TestPtrAlias *int + +func fPtrAlias(a TestPtrAlias) TestPtrAlias { return a } + func fSlice(a []byte) []byte { return a } type TestSliceAlias []byte @@ -135,15 +153,6 @@ type TestUintptrAlias uintptr func fUintptrAlias(a TestUintptrAlias) TestUintptrAlias { return a } -func fIntptr(a *int) *int { - b := *a - return &b -} - -type TestIntptrAlias *int - -func fIntptrAlias(a TestIntptrAlias) TestIntptrAlias { return a } - func reportError(property string, err error, t *testing.T) { if err != nil { t.Errorf("%s: %s", property, err) @@ -151,6 +160,8 @@ func reportError(property string, err error, t *testing.T) { } func TestCheckEqual(t *testing.T) { + reportError("fArray", CheckEqual(fArray, fArray, nil), t) + reportError("fArrayAlias", CheckEqual(fArrayAlias, fArrayAlias, nil), t) reportError("fBool", CheckEqual(fBool, fBool, nil), t) reportError("fBoolAlias", CheckEqual(fBoolAlias, fBoolAlias, nil), t) reportError("fFloat32", CheckEqual(fFloat32, fFloat32, nil), t) @@ -175,6 +186,8 @@ func TestCheckEqual(t *testing.T) { reportError("fInt32Alias", CheckEqual(fInt32Alias, fInt32Alias, nil), t) reportError("fMap", CheckEqual(fMap, fMap, nil), t) reportError("fMapAlias", CheckEqual(fMapAlias, fMapAlias, nil), t) + reportError("fPtr", CheckEqual(fPtr, fPtr, nil), t) + reportError("fPtrAlias", CheckEqual(fPtrAlias, fPtrAlias, nil), t) reportError("fSlice", CheckEqual(fSlice, fSlice, nil), t) reportError("fSliceAlias", CheckEqual(fSliceAlias, fSliceAlias, nil), t) reportError("fString", CheckEqual(fString, fString, nil), t) @@ -193,8 +206,6 @@ func TestCheckEqual(t *testing.T) { reportError("fUintAlias", CheckEqual(fUintAlias, fUintAlias, nil), t) reportError("fUintptr", CheckEqual(fUintptr, fUintptr, nil), t) reportError("fUintptrAlias", CheckEqual(fUintptrAlias, fUintptrAlias, nil), t) - reportError("fIntptr", CheckEqual(fIntptr, fIntptr, nil), t) - reportError("fIntptrAlias", CheckEqual(fIntptrAlias, fIntptrAlias, nil), t) } // This tests that ArbitraryValue is working by checking that all the arbitrary @@ -247,3 +258,17 @@ func TestFailure(t *testing.T) { t.Errorf("#3 Error was not a SetupError: %s", err) } } + +// The following test didn't terminate because nil pointers were not +// generated. +// Issue 8818. +func TestNilPointers(t *testing.T) { + type Recursive struct { + Next *Recursive + } + + f := func(rec Recursive) bool { + return true + } + Check(f, nil) +} diff --git a/libgo/go/testing/testing.go b/libgo/go/testing/testing.go index e54a3b8..9ec3869 100644 --- a/libgo/go/testing/testing.go +++ b/libgo/go/testing/testing.go @@ -34,7 +34,7 @@ // its -bench flag is provided. Benchmarks are run sequentially. // // For a description of the testing flags, see -// http://golang.org/cmd/go/#hdr-Description_of_testing_flags. +// https://golang.org/cmd/go/#hdr-Description_of_testing_flags. // // A sample benchmark function looks like this: // func BenchmarkHello(b *testing.B) { @@ -44,7 +44,7 @@ // } // // The benchmark function must run the target code b.N times. -// During benchark execution, b.N is adjusted until the benchmark function lasts +// During benchmark execution, b.N is adjusted until the benchmark function lasts // long enough to be timed reliably. The output // BenchmarkHello 10000000 282 ns/op // means that the loop ran 10000000 times at a speed of 282 ns per loop. @@ -130,13 +130,17 @@ // then the generated test will call TestMain(m) instead of running the tests // directly. TestMain runs in the main goroutine and can do whatever setup // and teardown is necessary around a call to m.Run. It should then call -// os.Exit with the result of m.Run. +// os.Exit with the result of m.Run. When TestMain is called, flag.Parse has +// not been run. If TestMain depends on command-line flags, including those +// of the testing package, it should call flag.Parse explicitly. // -// The minimal implementation of TestMain is: +// A simple implementation of TestMain is: // -// func TestMain(m *testing.M) { os.Exit(m.Run()) } +// func TestMain(m *testing.M) { +// flag.Parse() +// os.Exit(m.Run()) +// } // -// In effect, that is the implementation used when no TestMain is explicitly defined. package testing import ( @@ -168,6 +172,7 @@ var ( // Report as tests are run; default is silent for success. chatty = flag.Bool("test.v", false, "verbose: print additional output") + count = flag.Uint("test.count", 1, "run tests and benchmarks `n` times") coverProfile = flag.String("test.coverprofile", "", "write a coverage profile to the named file after execution") match = flag.String("test.run", "", "regular expression to select tests and examples to run") memProfile = flag.String("test.memprofile", "", "write a memory profile to the named file after execution") @@ -175,6 +180,7 @@ var ( cpuProfile = flag.String("test.cpuprofile", "", "write a cpu profile to the named file during execution") blockProfile = flag.String("test.blockprofile", "", "write a goroutine blocking profile to the named file after execution") blockProfileRate = flag.Int("test.blockprofilerate", 1, "if >= 0, calls runtime.SetBlockProfileRate()") + traceFile = flag.String("test.trace", "", "write an execution trace to the named file after execution") timeout = flag.Duration("test.timeout", 0, "if positive, sets an aggregate time limit for all tests") cpuListStr = flag.String("test.cpu", "", "comma-separated list of number of CPUs to use for each test") parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "maximum test parallelism") @@ -337,13 +343,15 @@ func (c *common) log(s string) { } // Log formats its arguments using default formatting, analogous to Println, -// and records the text in the error log. The text will be printed only if -// the test fails or the -test.v flag is set. +// and records the text in the error log. For tests, the text will be printed only if +// the test fails or the -test.v flag is set. For benchmarks, the text is always +// printed to avoid having performance depend on the value of the -test.v flag. func (c *common) Log(args ...interface{}) { c.log(fmt.Sprintln(args...)) } // Logf formats its arguments according to the format, analogous to Printf, -// and records the text in the error log. The text will be printed only if -// the test fails or the -test.v flag is set. +// and records the text in the error log. For tests, the text will be printed only if +// the test fails or the -test.v flag is set. For benchmarks, the text is always +// printed to avoid having performance depend on the value of the -test.v flag. func (c *common) Logf(format string, args ...interface{}) { c.log(fmt.Sprintf(format, args...)) } // Error is equivalent to Log followed by Fail. @@ -538,9 +546,6 @@ func RunTests(matchString func(pat, str string) (bool, error), tests []InternalT continue } testName := tests[i].Name - if procs != 1 { - testName = fmt.Sprintf("%s-%d", tests[i].Name, procs) - } t := &T{ common: common{ signal: make(chan interface{}), @@ -550,7 +555,7 @@ func RunTests(matchString func(pat, str string) (bool, error), tests []InternalT } t.self = t if *chatty { - fmt.Printf("=== RUN %s\n", t.name) + fmt.Printf("=== RUN %s\n", t.name) } go tRunner(t, &tests[i]) out := (<-t.signal).(*T) @@ -600,6 +605,22 @@ func before() { } // Could save f so after can call f.Close; not worth the effort. } + if *traceFile != "" { + f, err := os.Create(toOutputDir(*traceFile)) + if err != nil { + fmt.Fprintf(os.Stderr, "testing: %s", err) + return + } + /* + if err := trace.Start(f); err != nil { + fmt.Fprintf(os.Stderr, "testing: can't start tracing: %s", err) + f.Close() + return + } + */ + _ = f + // Could save f so after can call f.Close; not worth the effort. + } if *blockProfile != "" && *blockProfileRate >= 0 { runtime.SetBlockProfileRate(*blockProfileRate) } @@ -614,6 +635,11 @@ func after() { if *cpuProfile != "" { pprof.StopCPUProfile() // flushes profile to disk } + if *traceFile != "" { + /* + trace.Stop() // flushes trace to disk + */ + } if *memProfile != "" { f, err := os.Create(toOutputDir(*memProfile)) if err != nil { @@ -701,9 +727,13 @@ func parseCpuList() { fmt.Fprintf(os.Stderr, "testing: invalid value %q for -test.cpu\n", val) os.Exit(1) } - cpuList = append(cpuList, cpu) + for i := uint(0); i < *count; i++ { + cpuList = append(cpuList, cpu) + } } if cpuList == nil { - cpuList = append(cpuList, runtime.GOMAXPROCS(-1)) + for i := uint(0); i < *count; i++ { + cpuList = append(cpuList, runtime.GOMAXPROCS(-1)) + } } } |