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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [nil.go] - Blame information for rev 700

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 700 jeremybenn
// $G $F.go && $L $F.$A && ./$A.out
2
 
3
// Copyright 2009 The Go Authors. All rights reserved.
4
// Use of this source code is governed by a BSD-style
5
// license that can be found in the LICENSE file.
6
 
7
package main
8
 
9
import (
10
        "fmt"
11
        "time"
12
)
13
 
14
type T struct {
15
        i int
16
}
17
 
18
type IN interface{}
19
 
20
func main() {
21
        var i *int
22
        var f *float32
23
        var s *string
24
        var m map[float32]*int
25
        var c chan int
26
        var t *T
27
        var in IN
28
        var ta []IN
29
 
30
        i = nil
31
        f = nil
32
        s = nil
33
        m = nil
34
        c = nil
35
        t = nil
36
        i = nil
37
        ta = make([]IN, 1)
38
        ta[0] = nil
39
 
40
        _, _, _, _, _, _, _, _ = i, f, s, m, c, t, in, ta
41
 
42
        arraytest()
43
        chantest()
44
        maptest()
45
        slicetest()
46
}
47
 
48
func shouldPanic(f func()) {
49
        defer func() {
50
                if recover() == nil {
51
                        panic("not panicking")
52
                }
53
        }()
54
        f()
55
}
56
 
57
func shouldBlock(f func()) {
58
        go func() {
59
                f()
60
                panic("did not block")
61
        }()
62
        time.Sleep(1e7)
63
}
64
 
65
// nil array pointer
66
 
67
func arraytest() {
68
        var p *[10]int
69
 
70
        // Looping over indices is fine.
71
        s := 0
72
        for i := range p {
73
                s += i
74
        }
75
        if s != 45 {
76
                panic(s)
77
        }
78
 
79
        s = 0
80
        for i := 0; i < len(p); i++ {
81
                s += i
82
        }
83
        if s != 45 {
84
                panic(s)
85
        }
86
 
87
        // Looping over values is not.
88
        shouldPanic(func() {
89
                for i, v := range p {
90
                        s += i + v
91
                }
92
        })
93
 
94
        shouldPanic(func() {
95
                for i := 0; i < len(p); i++ {
96
                        s += p[i]
97
                }
98
        })
99
}
100
 
101
// nil channel
102
// select tests already handle select on nil channel
103
 
104
func chantest() {
105
        var ch chan int
106
 
107
        // nil channel is never ready
108
        shouldBlock(func() {
109
                ch <- 1
110
        })
111
        shouldBlock(func() {
112
                <-ch
113
        })
114
        shouldBlock(func() {
115
                x, ok := <-ch
116
                println(x, ok)
117
        })
118
 
119
        if len(ch) != 0 {
120
                panic(len(ch))
121
        }
122
        if cap(ch) != 0 {
123
                panic(cap(ch))
124
        }
125
}
126
 
127
// nil map
128
 
129
func maptest() {
130
        var m map[int]int
131
 
132
        // nil map appears empty
133
        if len(m) != 0 {
134
                panic(len(m))
135
        }
136
        if m[1] != 0 {
137
                panic(m[1])
138
        }
139
        if x, ok := m[1]; x != 0 || ok {
140
                panic(fmt.Sprint(x, ok))
141
        }
142
 
143
        for k, v := range m {
144
                panic(k)
145
                panic(v)
146
        }
147
 
148
        // but cannot be written to
149
        shouldPanic(func() {
150
                m[2] = 3
151
        })
152
        shouldPanic(func() {
153
                delete(m, 2)
154
        })
155
}
156
 
157
// nil slice
158
 
159
func slicetest() {
160
        var x []int
161
 
162
        // nil slice is just a 0-element slice.
163
        if len(x) != 0 {
164
                panic(len(x))
165
        }
166
        if cap(x) != 0 {
167
                panic(cap(x))
168
        }
169
 
170
        // no 0-element slices can be read from or written to
171
        var s int
172
        shouldPanic(func() {
173
                s += x[1]
174
        })
175
        shouldPanic(func() {
176
                x[2] = s
177
        })
178
}

powered by: WebSVN 2.1.0

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