2008-12-19 13:05:22 -07:00
|
|
|
// $G $D/$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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os";
|
|
|
|
"fmt";
|
|
|
|
)
|
|
|
|
|
|
|
|
type T struct {
|
|
|
|
a float64;
|
|
|
|
b int64;
|
|
|
|
c string;
|
|
|
|
d byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
var a = []int{ 1, 2, 3 }
|
|
|
|
var NIL []int;
|
|
|
|
|
|
|
|
func arraycmptest() {
|
|
|
|
a1 := a;
|
|
|
|
if NIL != nil {
|
|
|
|
println("fail1:", NIL, "!= nil");
|
|
|
|
}
|
|
|
|
if nil != NIL {
|
|
|
|
println("fail2: nil !=", NIL);
|
|
|
|
}
|
|
|
|
if a == nil || nil == a {
|
|
|
|
println("fail3:", a, "== nil");
|
|
|
|
}
|
2008-12-19 18:11:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func SameArray(a, b []int) bool {
|
|
|
|
if len(a) != len(b) || cap(a) != cap(b) {
|
|
|
|
return false;
|
2008-12-19 13:05:22 -07:00
|
|
|
}
|
2008-12-19 18:11:54 -07:00
|
|
|
if len(a) > 0 && &a[0] != &b[0] {
|
|
|
|
return false;
|
2008-12-19 13:05:22 -07:00
|
|
|
}
|
2008-12-19 18:11:54 -07:00
|
|
|
return true;
|
2008-12-19 13:05:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var t = T{1.5, 123, "hello", 255}
|
|
|
|
var mt = new(map[int]T)
|
|
|
|
var ma = new(map[int][]int)
|
|
|
|
|
|
|
|
func maptest() {
|
|
|
|
mt[0] = t;
|
|
|
|
t1 := mt[0];
|
|
|
|
if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d {
|
|
|
|
println("fail: map val struct", t1.a, t1.b, t1.c, t1.d);
|
|
|
|
}
|
|
|
|
|
|
|
|
ma[1] = a;
|
|
|
|
a1 := ma[1];
|
2008-12-19 18:11:54 -07:00
|
|
|
if !SameArray(a, a1) {
|
2008-12-19 13:05:22 -07:00
|
|
|
println("fail: map val array", a, a1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var mt1 = new(map[T]int)
|
|
|
|
var ma1 = new(map[[]int] int)
|
|
|
|
|
|
|
|
func maptest2() {
|
|
|
|
mt1[t] = 123;
|
|
|
|
t1 := t;
|
|
|
|
val, ok := mt1[t1];
|
|
|
|
if val != 123 || !ok {
|
|
|
|
println("fail: map key struct", val, ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
ma1[a] = 345;
|
|
|
|
a1 := a;
|
|
|
|
val, ok = ma1[a1];
|
|
|
|
if val != 345 || !ok {
|
|
|
|
panic("map key array", val, ok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var ct = new(chan T)
|
|
|
|
var ca = new(chan []int)
|
|
|
|
|
|
|
|
func send() {
|
|
|
|
ct <- t;
|
|
|
|
ca <- a;
|
|
|
|
}
|
|
|
|
|
|
|
|
func chantest() {
|
|
|
|
go send();
|
|
|
|
|
|
|
|
t1 := <-ct;
|
|
|
|
if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d {
|
2008-12-19 18:11:54 -07:00
|
|
|
println("fail: map val struct", t1.a, t1.b, t1.c, t1.d);
|
2008-12-19 13:05:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
a1 := <-ca;
|
2008-12-19 18:11:54 -07:00
|
|
|
if !SameArray(a, a1) {
|
|
|
|
println("fail: map val array", a, a1);
|
2008-12-19 13:05:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-19 18:11:54 -07:00
|
|
|
type E struct { }
|
|
|
|
var e E
|
|
|
|
|
|
|
|
func interfacetest() {
|
|
|
|
var i interface{};
|
|
|
|
|
|
|
|
i = a;
|
|
|
|
a1 := i.([]int);
|
|
|
|
if !SameArray(a, a1) {
|
|
|
|
println("interface <-> []int", a, a1);
|
|
|
|
}
|
|
|
|
pa := new(*[]int);
|
|
|
|
*pa = a;
|
|
|
|
i = pa;
|
|
|
|
a1 = *i.(*[]int);
|
|
|
|
if !SameArray(a, a1) {
|
|
|
|
println("interface <-> *[]int", a, a1);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = t;
|
|
|
|
t1 := i.(T);
|
|
|
|
if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d {
|
|
|
|
println("interface <-> struct", t1.a, t1.b, t1.c, t1.d);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = e;
|
|
|
|
e1 := i.(E);
|
|
|
|
// nothing to check; just verify it doesn't crash
|
|
|
|
}
|
|
|
|
|
2008-12-19 13:05:22 -07:00
|
|
|
func main() {
|
|
|
|
arraycmptest();
|
|
|
|
maptest();
|
|
|
|
maptest2();
|
|
|
|
chantest();
|
2008-12-19 18:11:54 -07:00
|
|
|
interfacetest();
|
2008-12-19 13:05:22 -07:00
|
|
|
}
|