URL
https://opencores.org/ocsvn/openrisc/openrisc/trunk
Subversion Repositories openrisc
[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [nil.go] - Rev 700
Compare with Previous | Blame | View Log
// $G $F.go && $L $F.$A && ./$A.out// 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 mainimport ("fmt""time")type T struct {i int}type IN interface{}func main() {var i *intvar f *float32var s *stringvar m map[float32]*intvar c chan intvar t *Tvar in INvar ta []INi = nilf = nils = nilm = nilc = nilt = nili = nilta = make([]IN, 1)ta[0] = nil_, _, _, _, _, _, _, _ = i, f, s, m, c, t, in, taarraytest()chantest()maptest()slicetest()}func shouldPanic(f func()) {defer func() {if recover() == nil {panic("not panicking")}}()f()}func shouldBlock(f func()) {go func() {f()panic("did not block")}()time.Sleep(1e7)}// nil array pointerfunc arraytest() {var p *[10]int// Looping over indices is fine.s := 0for i := range p {s += i}if s != 45 {panic(s)}s = 0for i := 0; i < len(p); i++ {s += i}if s != 45 {panic(s)}// Looping over values is not.shouldPanic(func() {for i, v := range p {s += i + v}})shouldPanic(func() {for i := 0; i < len(p); i++ {s += p[i]}})}// nil channel// select tests already handle select on nil channelfunc chantest() {var ch chan int// nil channel is never readyshouldBlock(func() {ch <- 1})shouldBlock(func() {<-ch})shouldBlock(func() {x, ok := <-chprintln(x, ok)})if len(ch) != 0 {panic(len(ch))}if cap(ch) != 0 {panic(cap(ch))}}// nil mapfunc maptest() {var m map[int]int// nil map appears emptyif len(m) != 0 {panic(len(m))}if m[1] != 0 {panic(m[1])}if x, ok := m[1]; x != 0 || ok {panic(fmt.Sprint(x, ok))}for k, v := range m {panic(k)panic(v)}// but cannot be written toshouldPanic(func() {m[2] = 3})shouldPanic(func() {delete(m, 2)})}// nil slicefunc slicetest() {var x []int// nil slice is just a 0-element slice.if len(x) != 0 {panic(len(x))}if cap(x) != 0 {panic(cap(x))}// no 0-element slices can be read from or written tovar s intshouldPanic(func() {s += x[1]})shouldPanic(func() {x[2] = s})}
