URL
https://opencores.org/ocsvn/openrisc/openrisc/trunk
Subversion Repositories openrisc
[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [testing/] [example.go] - Rev 847
Go to most recent revision | Compare with Previous | Blame | View Log
// Copyright 2009 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package testingimport ("bytes""fmt""io""os""strings""time")type InternalExample struct {Name stringF func()Output string}func RunExamples(examples []InternalExample) (ok bool) {ok = truevar eg InternalExamplestdout, stderr := os.Stdout, os.Stderrfor _, eg = range examples {if *chatty {fmt.Printf("=== RUN: %s\n", eg.Name)}// capture stdout and stderrr, w, err := os.Pipe()if err != nil {fmt.Fprintln(os.Stderr, err)os.Exit(1)}os.Stdout, os.Stderr = w, woutC := make(chan string)go func() {buf := new(bytes.Buffer)_, err := io.Copy(buf, r)if err != nil {fmt.Fprintf(stderr, "testing: copying pipe: %v\n", err)os.Exit(1)}outC <- buf.String()}()// run examplet0 := time.Now()eg.F()dt := time.Now().Sub(t0)// close pipe, restore stdout/stderr, get outputw.Close()os.Stdout, os.Stderr = stdout, stderrout := <-outC// report any errorststr := fmt.Sprintf("(%.2f seconds)", dt.Seconds())if g, e := strings.TrimSpace(out), strings.TrimSpace(eg.Output); g != e {fmt.Printf("--- FAIL: %s %s\ngot:\n%s\nwant:\n%s\n",eg.Name, tstr, g, e)ok = false} else if *chatty {fmt.Printf("--- PASS: %s %s\n", eg.Name, tstr)}}return}
Go to most recent revision | Compare with Previous | Blame | View Log
