1 // 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.
18 var basicTypes
= []typeT
{
27 func getTypeUnlocked(name
string, rt reflect
.Type
) gobType
{
29 defer typeLock
.Unlock()
30 t
, err
:= getBaseType(name
, rt
)
32 panic("getTypeUnlocked: " + err
.Error())
38 func TestBasic(t
*testing
.T
) {
39 for _
, tt
:= range basicTypes
{
40 if tt
.id
.string() != tt
.str
{
41 t
.Errorf("checkType: expected %q got %s", tt
.str
, tt
.id
.string())
44 t
.Errorf("id for %q is zero", tt
.str
)
49 // Reregister some basic types to check registration is idempotent.
50 func TestReregistration(t
*testing
.T
) {
51 newtyp
:= getTypeUnlocked("int", reflect
.TypeOf(int(0)))
52 if newtyp
!= tInt
.gobType() {
53 t
.Errorf("reregistration of %s got new type", newtyp
.string())
55 newtyp
= getTypeUnlocked("uint", reflect
.TypeOf(uint(0)))
56 if newtyp
!= tUint
.gobType() {
57 t
.Errorf("reregistration of %s got new type", newtyp
.string())
59 newtyp
= getTypeUnlocked("string", reflect
.TypeOf("hello"))
60 if newtyp
!= tString
.gobType() {
61 t
.Errorf("reregistration of %s got new type", newtyp
.string())
65 func TestArrayType(t
*testing
.T
) {
67 a3int
:= getTypeUnlocked("foo", reflect
.TypeOf(a3
))
68 newa3int
:= getTypeUnlocked("bar", reflect
.TypeOf(a3
))
69 if a3int
!= newa3int
{
70 t
.Errorf("second registration of [3]int creates new type")
73 a4int
:= getTypeUnlocked("goo", reflect
.TypeOf(a4
))
75 t
.Errorf("registration of [3]int creates same type as [4]int")
78 a3bool
:= getTypeUnlocked("", reflect
.TypeOf(b3
))
80 t
.Errorf("registration of [3]bool creates same type as [3]int")
82 str
:= a3bool
.string()
85 t
.Errorf("array printed as %q; expected %q", str
, expected
)
89 func TestSliceType(t
*testing
.T
) {
91 sint
:= getTypeUnlocked("slice", reflect
.TypeOf(s
))
93 newsint
:= getTypeUnlocked("slice1", reflect
.TypeOf(news
))
95 t
.Errorf("second registration of []int creates new type")
98 sbool
:= getTypeUnlocked("", reflect
.TypeOf(b
))
100 t
.Errorf("registration of []bool creates same type as []int")
102 str
:= sbool
.string()
105 t
.Errorf("slice printed as %q; expected %q", str
, expected
)
109 func TestMapType(t
*testing
.T
) {
111 mapStringInt
:= getTypeUnlocked("map", reflect
.TypeOf(m
))
112 var newm
map[string]int
113 newMapStringInt
:= getTypeUnlocked("map1", reflect
.TypeOf(newm
))
114 if mapStringInt
!= newMapStringInt
{
115 t
.Errorf("second registration of map[string]int creates new type")
117 var b
map[string]bool
118 mapStringBool
:= getTypeUnlocked("", reflect
.TypeOf(b
))
119 if mapStringBool
== mapStringInt
{
120 t
.Errorf("registration of map[string]bool creates same type as map[string]int")
122 str
:= mapStringBool
.string()
123 expected
:= "map[string]bool"
125 t
.Errorf("map printed as %q; expected %q", str
, expected
)
133 // This structure has pointers and refers to itself, making it a good test case.
136 B
int32 // will become int
139 E
*float64 // will become float64
140 F
****float64 // will become float64
142 H
*Bar
// should not interpolate the definition of Bar again
143 I
*Foo
// will not explode
146 func TestStructType(t
*testing
.T
) {
147 sstruct
:= getTypeUnlocked("Foo", reflect
.TypeOf(Foo
{}))
148 str
:= sstruct
.string()
149 // If we can print it correctly, we built it correctly.
150 expected
:= "Foo = struct { A int; B int; C string; D bytes; E float; F float; G Bar = struct { X string; }; H Bar; I Foo; }"
152 t
.Errorf("struct printed as %q; expected %q", str
, expected
)
156 // Should be OK to register the same type multiple times, as long as they're
157 // at the same level of indirection.
158 func TestRegistration(t
*testing
.T
) {
159 type T
struct{ a
int }
167 // See comment in type.go/Register.
168 func TestRegistrationNaming(t
*testing
.T
) {
169 testCases
:= []struct {
174 {N2
{}, "encoding/gob.N2"},
177 for _
, tc
:= range testCases
{
180 tct
:= reflect
.TypeOf(tc
.t
)
181 ct
, _
:= nameToConcreteType
.Load(tc
.name
)
183 t
.Errorf("nameToConcreteType[%q] = %v, want %v", tc
.name
, ct
, tct
)
185 // concreteTypeToName is keyed off the base type.
186 if tct
.Kind() == reflect
.Ptr
{
189 if n
, _
:= concreteTypeToName
.Load(tct
); n
!= tc
.name
{
190 t
.Errorf("concreteTypeToName[%v] got %v, want %v", tct
, n
, tc
.name
)
195 func TestStressParallel(t
*testing
.T
) {
196 type T2
struct{ A
int }
199 for i
:= 0; i
< N
; i
++ {
203 b
:= new(bytes
.Buffer
)
207 t
.Error("encoder fail:", err
)
212 t
.Error("decoder fail:", err
)
217 for i
:= 0; i
< N
; i
++ {