OSDN Git Service

f1acb97e1b64cccb4db2fac7a1cbe2090d09c70c
[pf3gnuchains/gcc-fork.git] / libgo / go / testing / testing.go
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 testing provides support for automated testing of Go packages.
6 // It is intended to be used in concert with the ``go test'' command, which automates
7 // execution of any function of the form
8 //     func TestXxx(*testing.T)
9 // where Xxx can be any alphanumeric string (but the first letter must not be in
10 // [a-z]) and serves to identify the test routine.
11 // These TestXxx routines should be declared within the package they are testing.
12 //
13 // Functions of the form
14 //     func BenchmarkXxx(*testing.B)
15 // are considered benchmarks, and are executed by gotest when the -test.bench
16 // flag is provided.
17 //
18 // A sample benchmark function looks like this:
19 //     func BenchmarkHello(b *testing.B) {
20 //         for i := 0; i < b.N; i++ {
21 //             fmt.Sprintf("hello")
22 //         }
23 //     }
24 //
25 // The benchmark package will vary b.N until the benchmark function lasts
26 // long enough to be timed reliably.  The output
27 //     testing.BenchmarkHello    10000000    282 ns/op
28 // means that the loop ran 10000000 times at a speed of 282 ns per loop.
29 //
30 // If a benchmark needs some expensive setup before running, the timer
31 // may be stopped:
32 //     func BenchmarkBigLen(b *testing.B) {
33 //         b.StopTimer()
34 //         big := NewBig()
35 //         b.StartTimer()
36 //         for i := 0; i < b.N; i++ {
37 //             big.Len()
38 //         }
39 //     }
40 //
41 // The package also runs and verifies example code. Example functions
42 // include an introductory comment that is compared with the standard output
43 // of the function when the tests are run, as in this example of an example:
44 //
45 //     // hello
46 //     func ExampleHello() {
47 //             fmt.Println("hello")
48 //     }
49 //
50 // Example functions without comments are compiled but not executed.
51 //
52 // The naming convention to declare examples for a function F, a type T and
53 // method M on type T are:
54 //
55 //     func ExampleF() { ... }
56 //     func ExampleT() { ... }
57 //     func ExampleT_M() { ... }
58 //
59 // Multiple example functions for a type/function/method may be provided by
60 // appending a distinct suffix to the name. The suffix must start with a
61 // lower-case letter.
62 //
63 //     func ExampleF_suffix() { ... }
64 //     func ExampleT_suffix() { ... }
65 //     func ExampleT_M_suffix() { ... }
66 //
67 package testing
68
69 import (
70         "flag"
71         "fmt"
72         "os"
73         "runtime"
74         "runtime/pprof"
75         "strconv"
76         "strings"
77         "time"
78 )
79
80 var (
81         // The short flag requests that tests run more quickly, but its functionality
82         // is provided by test writers themselves.  The testing package is just its
83         // home.  The all.bash installation script sets it to make installation more
84         // efficient, but by default the flag is off so a plain "gotest" will do a
85         // full test of the package.
86         short = flag.Bool("test.short", false, "run smaller test suite to save time")
87
88         // Report as tests are run; default is silent for success.
89         chatty         = flag.Bool("test.v", false, "verbose: print additional output")
90         match          = flag.String("test.run", "", "regular expression to select tests to run")
91         memProfile     = flag.String("test.memprofile", "", "write a memory profile to the named file after execution")
92         memProfileRate = flag.Int("test.memprofilerate", 0, "if >=0, sets runtime.MemProfileRate")
93         cpuProfile     = flag.String("test.cpuprofile", "", "write a cpu profile to the named file during execution")
94         timeout        = flag.Duration("test.timeout", 0, "if positive, sets an aggregate time limit for all tests")
95         cpuListStr     = flag.String("test.cpu", "", "comma-separated list of number of CPUs to use for each test")
96         parallel       = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "maximum test parallelism")
97
98         cpuList []int
99 )
100
101 // common holds the elements common between T and B and
102 // captures common methods such as Errorf.
103 type common struct {
104         output   []byte    // Output generated by test or benchmark.
105         failed   bool      // Test or benchmark has failed.
106         start    time.Time // Time test or benchmark started
107         duration time.Duration
108         self     interface{}      // To be sent on signal channel when done.
109         signal   chan interface{} // Output for serial tests.
110 }
111
112 // Short reports whether the -test.short flag is set.
113 func Short() bool {
114         return *short
115 }
116
117 // decorate inserts the final newline if needed and indentation tabs for formatting.
118 // If addFileLine is true, it also prefixes the string with the file and line of the call site.
119 func decorate(s string, addFileLine bool) string {
120         if addFileLine {
121                 _, file, line, ok := runtime.Caller(3) // decorate + log + public function.
122                 if ok {
123                         // Truncate file name at last file name separator.
124                         if index := strings.LastIndex(file, "/"); index >= 0 {
125                                 file = file[index+1:]
126                         } else if index = strings.LastIndex(file, "\\"); index >= 0 {
127                                 file = file[index+1:]
128                         }
129                 } else {
130                         file = "???"
131                         line = 1
132                 }
133                 s = fmt.Sprintf("%s:%d: %s", file, line, s)
134         }
135         s = "\t" + s // Every line is indented at least one tab.
136         n := len(s)
137         if n > 0 && s[n-1] != '\n' {
138                 s += "\n"
139                 n++
140         }
141         for i := 0; i < n-1; i++ { // -1 to avoid final newline
142                 if s[i] == '\n' {
143                         // Second and subsequent lines are indented an extra tab.
144                         return s[0:i+1] + "\t" + decorate(s[i+1:n], false)
145                 }
146         }
147         return s
148 }
149
150 // T is a type passed to Test functions to manage test state and support formatted test logs.
151 // Logs are accumulated during execution and dumped to standard error when done.
152 type T struct {
153         common
154         name          string    // Name of test.
155         startParallel chan bool // Parallel tests will wait on this.
156 }
157
158 // Fail marks the function as having failed but continues execution.
159 func (c *common) Fail() { c.failed = true }
160
161 // Failed returns whether the function has failed.
162 func (c *common) Failed() bool { return c.failed }
163
164 // FailNow marks the function as having failed and stops its execution.
165 // Execution will continue at the next Test.
166 func (c *common) FailNow() {
167         c.Fail()
168
169         // Calling runtime.Goexit will exit the goroutine, which
170         // will run the deferred functions in this goroutine,
171         // which will eventually run the deferred lines in tRunner,
172         // which will signal to the test loop that this test is done.
173         //
174         // A previous version of this code said:
175         //
176         //      c.duration = ...
177         //      c.signal <- c.self
178         //      runtime.Goexit()
179         //
180         // This previous version duplicated code (those lines are in
181         // tRunner no matter what), but worse the goroutine teardown
182         // implicit in runtime.Goexit was not guaranteed to complete
183         // before the test exited.  If a test deferred an important cleanup
184         // function (like removing temporary files), there was no guarantee
185         // it would run on a test failure.  Because we send on c.signal during
186         // a top-of-stack deferred function now, we know that the send
187         // only happens after any other stacked defers have completed.
188         runtime.Goexit()
189 }
190
191 // log generates the output. It's always at the same stack depth.
192 func (c *common) log(s string) {
193         c.output = append(c.output, decorate(s, true)...)
194 }
195
196 // Log formats its arguments using default formatting, analogous to Println(),
197 // and records the text in the error log.
198 func (c *common) Log(args ...interface{}) { c.log(fmt.Sprintln(args...)) }
199
200 // Logf formats its arguments according to the format, analogous to Printf(),
201 // and records the text in the error log.
202 func (c *common) Logf(format string, args ...interface{}) { c.log(fmt.Sprintf(format, args...)) }
203
204 // Error is equivalent to Log() followed by Fail().
205 func (c *common) Error(args ...interface{}) {
206         c.log(fmt.Sprintln(args...))
207         c.Fail()
208 }
209
210 // Errorf is equivalent to Logf() followed by Fail().
211 func (c *common) Errorf(format string, args ...interface{}) {
212         c.log(fmt.Sprintf(format, args...))
213         c.Fail()
214 }
215
216 // Fatal is equivalent to Log() followed by FailNow().
217 func (c *common) Fatal(args ...interface{}) {
218         c.log(fmt.Sprintln(args...))
219         c.FailNow()
220 }
221
222 // Fatalf is equivalent to Logf() followed by FailNow().
223 func (c *common) Fatalf(format string, args ...interface{}) {
224         c.log(fmt.Sprintf(format, args...))
225         c.FailNow()
226 }
227
228 // Parallel signals that this test is to be run in parallel with (and only with) 
229 // other parallel tests in this CPU group.
230 func (t *T) Parallel() {
231         t.signal <- (*T)(nil) // Release main testing loop
232         <-t.startParallel     // Wait for serial tests to finish
233 }
234
235 // An internal type but exported because it is cross-package; part of the implementation
236 // of gotest.
237 type InternalTest struct {
238         Name string
239         F    func(*T)
240 }
241
242 func tRunner(t *T, test *InternalTest) {
243         t.start = time.Now()
244
245         // When this goroutine is done, either because test.F(t)
246         // returned normally or because a test failure triggered 
247         // a call to runtime.Goexit, record the duration and send
248         // a signal saying that the test is done.
249         defer func() {
250                 t.duration = time.Now().Sub(t.start)
251                 t.signal <- t
252         }()
253
254         test.F(t)
255 }
256
257 // An internal function but exported because it is cross-package; part of the implementation
258 // of gotest.
259 func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) {
260         flag.Parse()
261         parseCpuList()
262
263         before()
264         startAlarm()
265         testOk := RunTests(matchString, tests)
266         exampleOk := RunExamples(examples)
267         if !testOk || !exampleOk {
268                 fmt.Println("FAIL")
269                 os.Exit(1)
270         }
271         fmt.Println("PASS")
272         stopAlarm()
273         RunBenchmarks(matchString, benchmarks)
274         after()
275 }
276
277 func (t *T) report() {
278         tstr := fmt.Sprintf("(%.2f seconds)", t.duration.Seconds())
279         format := "--- %s: %s %s\n%s"
280         if t.failed {
281                 fmt.Printf(format, "FAIL", t.name, tstr, t.output)
282         } else if *chatty {
283                 fmt.Printf(format, "PASS", t.name, tstr, t.output)
284         }
285 }
286
287 func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool) {
288         ok = true
289         if len(tests) == 0 {
290                 fmt.Fprintln(os.Stderr, "testing: warning: no tests to run")
291                 return
292         }
293         for _, procs := range cpuList {
294                 runtime.GOMAXPROCS(procs)
295                 // We build a new channel tree for each run of the loop.
296                 // collector merges in one channel all the upstream signals from parallel tests.
297                 // If all tests pump to the same channel, a bug can occur where a test
298                 // kicks off a goroutine that Fails, yet the test still delivers a completion signal,
299                 // which skews the counting.
300                 var collector = make(chan interface{})
301
302                 numParallel := 0
303                 startParallel := make(chan bool)
304
305                 for i := 0; i < len(tests); i++ {
306                         matched, err := matchString(*match, tests[i].Name)
307                         if err != nil {
308                                 fmt.Fprintf(os.Stderr, "testing: invalid regexp for -test.run: %s\n", err)
309                                 os.Exit(1)
310                         }
311                         if !matched {
312                                 continue
313                         }
314                         testName := tests[i].Name
315                         if procs != 1 {
316                                 testName = fmt.Sprintf("%s-%d", tests[i].Name, procs)
317                         }
318                         t := &T{
319                                 common: common{
320                                         signal: make(chan interface{}),
321                                 },
322                                 name:          testName,
323                                 startParallel: startParallel,
324                         }
325                         t.self = t
326                         if *chatty {
327                                 fmt.Printf("=== RUN %s\n", t.name)
328                         }
329                         go tRunner(t, &tests[i])
330                         out := (<-t.signal).(*T)
331                         if out == nil { // Parallel run.
332                                 go func() {
333                                         collector <- <-t.signal
334                                 }()
335                                 numParallel++
336                                 continue
337                         }
338                         t.report()
339                         ok = ok && !out.failed
340                 }
341
342                 running := 0
343                 for numParallel+running > 0 {
344                         if running < *parallel && numParallel > 0 {
345                                 startParallel <- true
346                                 running++
347                                 numParallel--
348                                 continue
349                         }
350                         t := (<-collector).(*T)
351                         t.report()
352                         ok = ok && !t.failed
353                         running--
354                 }
355         }
356         return
357 }
358
359 // before runs before all testing.
360 func before() {
361         if *memProfileRate > 0 {
362                 runtime.MemProfileRate = *memProfileRate
363         }
364         if *cpuProfile != "" {
365                 f, err := os.Create(*cpuProfile)
366                 if err != nil {
367                         fmt.Fprintf(os.Stderr, "testing: %s", err)
368                         return
369                 }
370                 if err := pprof.StartCPUProfile(f); err != nil {
371                         fmt.Fprintf(os.Stderr, "testing: can't start cpu profile: %s", err)
372                         f.Close()
373                         return
374                 }
375                 // Could save f so after can call f.Close; not worth the effort.
376         }
377
378 }
379
380 // after runs after all testing.
381 func after() {
382         if *cpuProfile != "" {
383                 pprof.StopCPUProfile() // flushes profile to disk
384         }
385         if *memProfile != "" {
386                 f, err := os.Create(*memProfile)
387                 if err != nil {
388                         fmt.Fprintf(os.Stderr, "testing: %s", err)
389                         return
390                 }
391                 if err = pprof.WriteHeapProfile(f); err != nil {
392                         fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *memProfile, err)
393                 }
394                 f.Close()
395         }
396 }
397
398 var timer *time.Timer
399
400 // startAlarm starts an alarm if requested.
401 func startAlarm() {
402         if *timeout > 0 {
403                 timer = time.AfterFunc(*timeout, alarm)
404         }
405 }
406
407 // stopAlarm turns off the alarm.
408 func stopAlarm() {
409         if *timeout > 0 {
410                 timer.Stop()
411         }
412 }
413
414 // alarm is called if the timeout expires.
415 func alarm() {
416         panic("test timed out")
417 }
418
419 func parseCpuList() {
420         if len(*cpuListStr) == 0 {
421                 cpuList = append(cpuList, runtime.GOMAXPROCS(-1))
422         } else {
423                 for _, val := range strings.Split(*cpuListStr, ",") {
424                         cpu, err := strconv.Atoi(val)
425                         if err != nil || cpu <= 0 {
426                                 fmt.Fprintf(os.Stderr, "testing: invalid value %q for -test.cpu", val)
427                                 os.Exit(1)
428                         }
429                         cpuList = append(cpuList, cpu)
430                 }
431         }
432 }