OSDN Git Service

libgo: Update to weekly 2011-11-09.
[pf3gnuchains/gcc-fork.git] / libgo / go / encoding / gob / type_test.go
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.
4
5 package gob
6
7 import (
8         "reflect"
9         "testing"
10 )
11
12 type typeT struct {
13         id  typeId
14         str string
15 }
16
17 var basicTypes = []typeT{
18         {tBool, "bool"},
19         {tInt, "int"},
20         {tUint, "uint"},
21         {tFloat, "float"},
22         {tBytes, "bytes"},
23         {tString, "string"},
24 }
25
26 func getTypeUnlocked(name string, rt reflect.Type) gobType {
27         typeLock.Lock()
28         defer typeLock.Unlock()
29         t, err := getBaseType(name, rt)
30         if err != nil {
31                 panic("getTypeUnlocked: " + err.Error())
32         }
33         return t
34 }
35
36 // Sanity checks
37 func TestBasic(t *testing.T) {
38         for _, tt := range basicTypes {
39                 if tt.id.string() != tt.str {
40                         t.Errorf("checkType: expected %q got %s", tt.str, tt.id.string())
41                 }
42                 if tt.id == 0 {
43                         t.Errorf("id for %q is zero", tt.str)
44                 }
45         }
46 }
47
48 // Reregister some basic types to check registration is idempotent.
49 func TestReregistration(t *testing.T) {
50         newtyp := getTypeUnlocked("int", reflect.TypeOf(int(0)))
51         if newtyp != tInt.gobType() {
52                 t.Errorf("reregistration of %s got new type", newtyp.string())
53         }
54         newtyp = getTypeUnlocked("uint", reflect.TypeOf(uint(0)))
55         if newtyp != tUint.gobType() {
56                 t.Errorf("reregistration of %s got new type", newtyp.string())
57         }
58         newtyp = getTypeUnlocked("string", reflect.TypeOf("hello"))
59         if newtyp != tString.gobType() {
60                 t.Errorf("reregistration of %s got new type", newtyp.string())
61         }
62 }
63
64 func TestArrayType(t *testing.T) {
65         var a3 [3]int
66         a3int := getTypeUnlocked("foo", reflect.TypeOf(a3))
67         newa3int := getTypeUnlocked("bar", reflect.TypeOf(a3))
68         if a3int != newa3int {
69                 t.Errorf("second registration of [3]int creates new type")
70         }
71         var a4 [4]int
72         a4int := getTypeUnlocked("goo", reflect.TypeOf(a4))
73         if a3int == a4int {
74                 t.Errorf("registration of [3]int creates same type as [4]int")
75         }
76         var b3 [3]bool
77         a3bool := getTypeUnlocked("", reflect.TypeOf(b3))
78         if a3int == a3bool {
79                 t.Errorf("registration of [3]bool creates same type as [3]int")
80         }
81         str := a3bool.string()
82         expected := "[3]bool"
83         if str != expected {
84                 t.Errorf("array printed as %q; expected %q", str, expected)
85         }
86 }
87
88 func TestSliceType(t *testing.T) {
89         var s []int
90         sint := getTypeUnlocked("slice", reflect.TypeOf(s))
91         var news []int
92         newsint := getTypeUnlocked("slice1", reflect.TypeOf(news))
93         if sint != newsint {
94                 t.Errorf("second registration of []int creates new type")
95         }
96         var b []bool
97         sbool := getTypeUnlocked("", reflect.TypeOf(b))
98         if sbool == sint {
99                 t.Errorf("registration of []bool creates same type as []int")
100         }
101         str := sbool.string()
102         expected := "[]bool"
103         if str != expected {
104                 t.Errorf("slice printed as %q; expected %q", str, expected)
105         }
106 }
107
108 func TestMapType(t *testing.T) {
109         var m map[string]int
110         mapStringInt := getTypeUnlocked("map", reflect.TypeOf(m))
111         var newm map[string]int
112         newMapStringInt := getTypeUnlocked("map1", reflect.TypeOf(newm))
113         if mapStringInt != newMapStringInt {
114                 t.Errorf("second registration of map[string]int creates new type")
115         }
116         var b map[string]bool
117         mapStringBool := getTypeUnlocked("", reflect.TypeOf(b))
118         if mapStringBool == mapStringInt {
119                 t.Errorf("registration of map[string]bool creates same type as map[string]int")
120         }
121         str := mapStringBool.string()
122         expected := "map[string]bool"
123         if str != expected {
124                 t.Errorf("map printed as %q; expected %q", str, expected)
125         }
126 }
127
128 type Bar struct {
129         X string
130 }
131
132 // This structure has pointers and refers to itself, making it a good test case.
133 type Foo struct {
134         A int
135         B int32 // will become int
136         C string
137         D []byte
138         E *float64    // will become float64
139         F ****float64 // will become float64
140         G *Bar
141         H *Bar // should not interpolate the definition of Bar again
142         I *Foo // will not explode
143 }
144
145 func TestStructType(t *testing.T) {
146         sstruct := getTypeUnlocked("Foo", reflect.TypeOf(Foo{}))
147         str := sstruct.string()
148         // If we can print it correctly, we built it correctly.
149         expected := "Foo = struct { A int; B int; C string; D bytes; E float; F float; G Bar = struct { X string; }; H Bar; I Foo; }"
150         if str != expected {
151                 t.Errorf("struct printed as %q; expected %q", str, expected)
152         }
153 }
154
155 // Should be OK to register the same type multiple times, as long as they're
156 // at the same level of indirection.
157 func TestRegistration(t *testing.T) {
158         type T struct{ a int }
159         Register(new(T))
160         Register(new(T))
161 }