OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [testing/] [example.go] - Blame information for rev 774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
6
 
7
import (
8
        "bytes"
9
        "fmt"
10
        "io"
11
        "os"
12
        "strings"
13
        "time"
14
)
15
 
16
type InternalExample struct {
17
        Name   string
18
        F      func()
19
        Output string
20
}
21
 
22
func RunExamples(examples []InternalExample) (ok bool) {
23
        ok = true
24
 
25
        var eg InternalExample
26
 
27
        stdout, stderr := os.Stdout, os.Stderr
28
 
29
        for _, eg = range examples {
30
                if *chatty {
31
                        fmt.Printf("=== RUN: %s\n", eg.Name)
32
                }
33
 
34
                // capture stdout and stderr
35
                r, w, err := os.Pipe()
36
                if err != nil {
37
                        fmt.Fprintln(os.Stderr, err)
38
                        os.Exit(1)
39
                }
40
                os.Stdout, os.Stderr = w, w
41
                outC := make(chan string)
42
                go func() {
43
                        buf := new(bytes.Buffer)
44
                        _, err := io.Copy(buf, r)
45
                        if err != nil {
46
                                fmt.Fprintf(stderr, "testing: copying pipe: %v\n", err)
47
                                os.Exit(1)
48
                        }
49
                        outC <- buf.String()
50
                }()
51
 
52
                // run example
53
                t0 := time.Now()
54
                eg.F()
55
                dt := time.Now().Sub(t0)
56
 
57
                // close pipe, restore stdout/stderr, get output
58
                w.Close()
59
                os.Stdout, os.Stderr = stdout, stderr
60
                out := <-outC
61
 
62
                // report any errors
63
                tstr := fmt.Sprintf("(%.2f seconds)", dt.Seconds())
64
                if g, e := strings.TrimSpace(out), strings.TrimSpace(eg.Output); g != e {
65
                        fmt.Printf("--- FAIL: %s %s\ngot:\n%s\nwant:\n%s\n",
66
                                eg.Name, tstr, g, e)
67
                        ok = false
68
                } else if *chatty {
69
                        fmt.Printf("--- PASS: %s %s\n", eg.Name, tstr)
70
                }
71
        }
72
 
73
        return
74
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.