package main import ( "reflect" "testing" ) func identity(x int) int { return x } var tint = reflect.TypeOf(int(0)) func TestArrayOfCanon(c *testing.T) { tarray1 := reflect.TypeOf([0]int{}) tarray2 := reflect.ArrayOf(0, tint) eq(c, tarray1, tarray2) } func TestChanOfCanon(c *testing.T) { tchan1 := reflect.TypeOf(make(chan int)) tchan2 := reflect.ChanOf(reflect.BothDir, tint) eq(c, tchan1, tchan2) } func TestFuncOfCanon(c *testing.T) { tfun1 := reflect.TypeOf(identity) tints := []reflect.Type{tint} tfun2 := reflect.FuncOf(tints, tints, false) eq(c, tfun1, tfun2) } func TestMapOfCanon(c *testing.T) { tmap1 := reflect.TypeOf(make(map[int]int)) tmap2 := reflect.MapOf(tint, tint) eq(c, tmap1, tmap2) } func TestPtrToCanon(c *testing.T) { tptr1 := reflect.TypeOf((*int)(nil)) tptr2 := reflect.PtrTo(tint) eq(c, tptr1, tptr2) } func TestSliceOfCanon(c *testing.T) { tslice1 := reflect.TypeOf([]int{}) tslice2 := reflect.SliceOf(tint) eq(c, tslice1, tslice2) } func TestStructOfCanon(c *testing.T) { tstruct1 := reflect.TypeOf(struct{ A, B int }{}) fields := []reflect.StructField{ {Name: "A", Type: tint}, {Name: "B", Type: tint}, } tstruct2 := reflect.StructOf(fields) eq(c, tstruct1, tstruct2) } func eq(c *testing.T, t1, t2 interface{}) { if t1 != t2 { c.Errorf("ERROR %v != %v\n", t1, t2) } }