OSDN Git Service

Update Go library to r60.
[pf3gnuchains/gcc-fork.git] / libgo / go / testing / testing.go
index 8781b20..ec4a453 100644 (file)
@@ -44,6 +44,8 @@ import (
        "os"
        "runtime"
        "runtime/pprof"
+       "strings"
+       "strconv"
        "time"
 )
 
@@ -62,6 +64,9 @@ var (
        memProfileRate = flag.Int("test.memprofilerate", 0, "if >=0, sets runtime.MemProfileRate")
        cpuProfile     = flag.String("test.cpuprofile", "", "write a cpu profile to the named file during execution")
        timeout        = flag.Int64("test.timeout", 0, "if > 0, sets time limit for tests in seconds")
+       cpuListStr     = flag.String("test.cpu", "", "comma-separated list of number of CPUs to use for each test")
+
+       cpuList []int
 )
 
 // Short reports whether the -test.short flag is set.
@@ -69,7 +74,6 @@ func Short() bool {
        return *short
 }
 
-
 // Insert final newline if needed and tabs after internal newlines.
 func tabify(s string) string {
        n := len(s)
@@ -157,6 +161,7 @@ func tRunner(t *T, test *InternalTest) {
 // of gotest.
 func Main(matchString func(pat, str string) (bool, os.Error), tests []InternalTest, benchmarks []InternalBenchmark) {
        flag.Parse()
+       parseCpuList()
 
        before()
        startAlarm()
@@ -180,23 +185,34 @@ func RunTests(matchString func(pat, str string) (bool, os.Error), tests []Intern
                if !matched {
                        continue
                }
-               if *chatty {
-                       println("=== RUN ", tests[i].Name)
-               }
-               ns := -time.Nanoseconds()
-               t := new(T)
-               t.ch = make(chan *T)
-               go tRunner(t, &tests[i])
-               <-t.ch
-               ns += time.Nanoseconds()
-               tstr := fmt.Sprintf("(%.2f seconds)", float64(ns)/1e9)
-               if t.failed {
-                       println("--- FAIL:", tests[i].Name, tstr)
-                       print(t.errors)
-                       ok = false
-               } else if *chatty {
-                       println("--- PASS:", tests[i].Name, tstr)
-                       print(t.errors)
+               for _, procs := range cpuList {
+                       runtime.GOMAXPROCS(procs)
+                       testName := tests[i].Name
+                       if procs != 1 {
+                               testName = fmt.Sprintf("%s-%d", tests[i].Name, procs)
+                       }
+                       if *chatty {
+                               println("=== RUN ", testName)
+                       }
+                       ns := -time.Nanoseconds()
+                       t := new(T)
+                       t.ch = make(chan *T)
+                       go tRunner(t, &tests[i])
+                       <-t.ch
+                       ns += time.Nanoseconds()
+                       tstr := fmt.Sprintf("(%.2f seconds)", float64(ns)/1e9)
+                       if p := runtime.GOMAXPROCS(-1); t.failed == false && p != procs {
+                               t.failed = true
+                               t.errors = fmt.Sprintf("%s left GOMAXPROCS set to %d\n", testName, p)
+                       }
+                       if t.failed {
+                               println("--- FAIL:", testName, tstr)
+                               print(t.errors)
+                               ok = false
+                       } else if *chatty {
+                               println("--- PASS:", testName, tstr)
+                               print(t.errors)
+                       }
                }
        }
        if !ok {
@@ -265,3 +281,18 @@ func stopAlarm() {
 func alarm() {
        panic("test timed out")
 }
+
+func parseCpuList() {
+       if len(*cpuListStr) == 0 {
+               cpuList = append(cpuList, runtime.GOMAXPROCS(-1))
+       } else {
+               for _, val := range strings.Split(*cpuListStr, ",") {
+                       cpu, err := strconv.Atoi(val)
+                       if err != nil || cpu <= 0 {
+                               println("invalid value for -test.cpu")
+                               os.Exit(1)
+                       }
+                       cpuList = append(cpuList, cpu)
+               }
+       }
+}