blob: 215e56c7f64de8ba5db97187bbed69acee6fbc12 [file] [log] [blame]
Rob Pike2738f422008-08-11 13:32:13 -07001// $G $F.go && $L $F.$A && ./$A.out
2
3// Copyright 2009 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
Rob Pike2738f422008-08-11 13:32:13 -07007package main
8
Rob Pike62b06fa2008-12-20 16:07:43 -08009import (
Rob Pike4f61fc92010-09-04 10:36:13 +100010 "fmt"
Russ Cox408f0b12012-01-26 16:25:07 -050011 "math"
Rob Pike4f61fc92010-09-04 10:36:13 +100012 "strconv"
Russ Cox6ebf8a62012-01-30 13:41:38 -050013 "time"
Rob Pike62b06fa2008-12-20 16:07:43 -080014)
Rob Pike2738f422008-08-11 13:32:13 -070015
Rob Pike4f61fc92010-09-04 10:36:13 +100016const count = 100
Rob Pike2738f422008-08-11 13:32:13 -070017
Russ Cox839a6842009-01-20 14:40:40 -080018func P(a []string) string {
Rob Pike4f61fc92010-09-04 10:36:13 +100019 s := "{"
Rob Pike2738f422008-08-11 13:32:13 -070020 for i := 0; i < len(a); i++ {
21 if i > 0 {
22 s += ","
23 }
Rob Pike4f61fc92010-09-04 10:36:13 +100024 s += `"` + a[i] + `"`
Rob Pike2738f422008-08-11 13:32:13 -070025 }
Russ Coxf2b5a072011-01-19 23:09:00 -050026 s += "}"
Rob Pike4f61fc92010-09-04 10:36:13 +100027 return s
Rob Pike2738f422008-08-11 13:32:13 -070028}
29
30func main() {
Russ Cox6ebf8a62012-01-30 13:41:38 -050031 testbasic()
32 testfloat()
33 testnan()
34}
35
36func testbasic() {
Rob Pike62b06fa2008-12-20 16:07:43 -080037 // Test a map literal.
Russ Coxf2b5a072011-01-19 23:09:00 -050038 mlit := map[string]int{"0": 0, "1": 1, "2": 2, "3": 3, "4": 4}
Rob Pike62b06fa2008-12-20 16:07:43 -080039 for i := 0; i < len(mlit); i++ {
Russ Coxf2b5a072011-01-19 23:09:00 -050040 s := string([]byte{byte(i) + '0'})
Rob Pike62b06fa2008-12-20 16:07:43 -080041 if mlit[s] != i {
Rob Pike61f33022009-01-15 13:48:11 -080042 fmt.Printf("mlit[%s] = %d\n", s, mlit[s])
Rob Pike62b06fa2008-12-20 16:07:43 -080043 }
44 }
Rob Pike2738f422008-08-11 13:32:13 -070045
Russ Coxf2b5a072011-01-19 23:09:00 -050046 mib := make(map[int]bool)
47 mii := make(map[int]int)
48 mfi := make(map[float32]int)
49 mif := make(map[int]float32)
50 msi := make(map[string]int)
51 mis := make(map[int]string)
52 mss := make(map[string]string)
53 mspa := make(map[string][]string)
Rob Pike2738f422008-08-11 13:32:13 -070054 // BUG need an interface map both ways too
55
56 type T struct {
Russ Coxf2b5a072011-01-19 23:09:00 -050057 i int64 // can't use string here; struct values are only compared at the top level
58 f float32
Rob Pike4f61fc92010-09-04 10:36:13 +100059 }
Russ Coxf2b5a072011-01-19 23:09:00 -050060 mipT := make(map[int]*T)
61 mpTi := make(map[*T]int)
62 mit := make(map[int]T)
63 // mti := make(map[T] int)
Rob Pike2738f422008-08-11 13:32:13 -070064
Russ Coxf2b5a072011-01-19 23:09:00 -050065 type M map[int]int
66 mipM := make(map[int]M)
Rob Pike2738f422008-08-11 13:32:13 -070067
Russ Coxf2b5a072011-01-19 23:09:00 -050068 var apT [2 * count]*T
Rob Pike2738f422008-08-11 13:32:13 -070069
70 for i := 0; i < count; i++ {
Rob Pike4f61fc92010-09-04 10:36:13 +100071 s := strconv.Itoa(i)
Russ Coxf2b5a072011-01-19 23:09:00 -050072 s10 := strconv.Itoa(i * 10)
73 f := float32(i)
74 t := T{int64(i), f}
Rob Pike4f61fc92010-09-04 10:36:13 +100075 apT[i] = new(T)
76 apT[i].i = int64(i)
77 apT[i].f = f
Russ Coxf2b5a072011-01-19 23:09:00 -050078 apT[2*i] = new(T) // need twice as many entries as we use, for the nonexistence check
Rob Pike4f61fc92010-09-04 10:36:13 +100079 apT[2*i].i = int64(i)
80 apT[2*i].f = f
Russ Coxf2b5a072011-01-19 23:09:00 -050081 m := M{i: i + 1}
Rob Pike4f61fc92010-09-04 10:36:13 +100082 mib[i] = (i != 0)
Russ Coxf2b5a072011-01-19 23:09:00 -050083 mii[i] = 10 * i
84 mfi[float32(i)] = 10 * i
85 mif[i] = 10.0 * f
Rob Pike4f61fc92010-09-04 10:36:13 +100086 mis[i] = s
87 msi[s] = i
88 mss[s] = s10
89 mss[s] = s10
90 as := make([]string, 2)
Russ Coxf2b5a072011-01-19 23:09:00 -050091 as[0] = s10
92 as[1] = s10
Rob Pike4f61fc92010-09-04 10:36:13 +100093 mspa[s] = as
94 mipT[i] = apT[i]
95 mpTi[apT[i]] = i
96 mipM[i] = m
97 mit[i] = t
Russ Coxf2b5a072011-01-19 23:09:00 -050098 // mti[t] = i
Rob Pike2738f422008-08-11 13:32:13 -070099 }
100
101 // test len
102 if len(mib) != count {
Rob Pike4f61fc92010-09-04 10:36:13 +1000103 fmt.Printf("len(mib) = %d\n", len(mib))
Rob Pike2738f422008-08-11 13:32:13 -0700104 }
105 if len(mii) != count {
Rob Pike4f61fc92010-09-04 10:36:13 +1000106 fmt.Printf("len(mii) = %d\n", len(mii))
Rob Pike2738f422008-08-11 13:32:13 -0700107 }
108 if len(mfi) != count {
Rob Pike4f61fc92010-09-04 10:36:13 +1000109 fmt.Printf("len(mfi) = %d\n", len(mfi))
Rob Pike2738f422008-08-11 13:32:13 -0700110 }
111 if len(mif) != count {
Rob Pike4f61fc92010-09-04 10:36:13 +1000112 fmt.Printf("len(mif) = %d\n", len(mif))
Rob Pike2738f422008-08-11 13:32:13 -0700113 }
114 if len(msi) != count {
Rob Pike4f61fc92010-09-04 10:36:13 +1000115 fmt.Printf("len(msi) = %d\n", len(msi))
Rob Pike2738f422008-08-11 13:32:13 -0700116 }
117 if len(mis) != count {
Rob Pike4f61fc92010-09-04 10:36:13 +1000118 fmt.Printf("len(mis) = %d\n", len(mis))
Rob Pike2738f422008-08-11 13:32:13 -0700119 }
120 if len(mss) != count {
Rob Pike4f61fc92010-09-04 10:36:13 +1000121 fmt.Printf("len(mss) = %d\n", len(mss))
Rob Pike2738f422008-08-11 13:32:13 -0700122 }
123 if len(mspa) != count {
Rob Pike4f61fc92010-09-04 10:36:13 +1000124 fmt.Printf("len(mspa) = %d\n", len(mspa))
Rob Pike2738f422008-08-11 13:32:13 -0700125 }
126 if len(mipT) != count {
Rob Pike4f61fc92010-09-04 10:36:13 +1000127 fmt.Printf("len(mipT) = %d\n", len(mipT))
Rob Pike2738f422008-08-11 13:32:13 -0700128 }
129 if len(mpTi) != count {
Rob Pike4f61fc92010-09-04 10:36:13 +1000130 fmt.Printf("len(mpTi) = %d\n", len(mpTi))
Rob Pike2738f422008-08-11 13:32:13 -0700131 }
Russ Coxf2b5a072011-01-19 23:09:00 -0500132 // if len(mti) != count {
133 // fmt.Printf("len(mti) = %d\n", len(mti))
134 // }
Rob Pike2738f422008-08-11 13:32:13 -0700135 if len(mipM) != count {
Rob Pike4f61fc92010-09-04 10:36:13 +1000136 fmt.Printf("len(mipM) = %d\n", len(mipM))
Rob Pike62b06fa2008-12-20 16:07:43 -0800137 }
Russ Coxf2b5a072011-01-19 23:09:00 -0500138 // if len(mti) != count {
139 // fmt.Printf("len(mti) = %d\n", len(mti))
140 // }
Rob Pike62b06fa2008-12-20 16:07:43 -0800141 if len(mit) != count {
Rob Pike4f61fc92010-09-04 10:36:13 +1000142 fmt.Printf("len(mit) = %d\n", len(mit))
Rob Pike2738f422008-08-11 13:32:13 -0700143 }
Russ Cox08ca30b2008-12-19 03:05:37 -0800144
Rob Pike2738f422008-08-11 13:32:13 -0700145 // test construction directly
146 for i := 0; i < count; i++ {
Rob Pike4f61fc92010-09-04 10:36:13 +1000147 s := strconv.Itoa(i)
Russ Coxf2b5a072011-01-19 23:09:00 -0500148 s10 := strconv.Itoa(i * 10)
149 f := float32(i)
Rob Pike4f61fc92010-09-04 10:36:13 +1000150 // BUG m := M(i, i+1)
Rob Pike2738f422008-08-11 13:32:13 -0700151 if mib[i] != (i != 0) {
Rob Pike4f61fc92010-09-04 10:36:13 +1000152 fmt.Printf("mib[%d] = %t\n", i, mib[i])
Rob Pike2738f422008-08-11 13:32:13 -0700153 }
Russ Coxf2b5a072011-01-19 23:09:00 -0500154 if mii[i] != 10*i {
Rob Pike4f61fc92010-09-04 10:36:13 +1000155 fmt.Printf("mii[%d] = %d\n", i, mii[i])
Rob Pike2738f422008-08-11 13:32:13 -0700156 }
Russ Coxf2b5a072011-01-19 23:09:00 -0500157 if mfi[f] != 10*i {
Rob Pike4f61fc92010-09-04 10:36:13 +1000158 fmt.Printf("mfi[%d] = %d\n", i, mfi[f])
Rob Pike2738f422008-08-11 13:32:13 -0700159 }
Russ Coxf2b5a072011-01-19 23:09:00 -0500160 if mif[i] != 10.0*f {
Rob Pike4f61fc92010-09-04 10:36:13 +1000161 fmt.Printf("mif[%d] = %g\n", i, mif[i])
Rob Pike2738f422008-08-11 13:32:13 -0700162 }
Russ Coxf2b5a072011-01-19 23:09:00 -0500163 if mis[i] != s {
Rob Pike4f61fc92010-09-04 10:36:13 +1000164 fmt.Printf("mis[%d] = %s\n", i, mis[i])
Rob Pike2738f422008-08-11 13:32:13 -0700165 }
Russ Coxf2b5a072011-01-19 23:09:00 -0500166 if msi[s] != i {
Rob Pike4f61fc92010-09-04 10:36:13 +1000167 fmt.Printf("msi[%s] = %d\n", s, msi[s])
Rob Pike2738f422008-08-11 13:32:13 -0700168 }
Rob Pike62b06fa2008-12-20 16:07:43 -0800169 if mss[s] != s10 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000170 fmt.Printf("mss[%s] = %g\n", s, mss[s])
Rob Pike2738f422008-08-11 13:32:13 -0700171 }
Russ Coxebd27d62009-10-09 11:18:32 -0700172 for j := 0; j < len(mspa[s]); j++ {
Rob Pike62b06fa2008-12-20 16:07:43 -0800173 if mspa[s][j] != s10 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000174 fmt.Printf("mspa[%s][%d] = %s\n", s, j, mspa[s][j])
Rob Pike2738f422008-08-11 13:32:13 -0700175 }
176 }
Russ Coxf2b5a072011-01-19 23:09:00 -0500177 if mipT[i].i != int64(i) || mipT[i].f != f {
Rob Pike4f61fc92010-09-04 10:36:13 +1000178 fmt.Printf("mipT[%d] = %v\n", i, mipT[i])
Rob Pike2738f422008-08-11 13:32:13 -0700179 }
Russ Coxf2b5a072011-01-19 23:09:00 -0500180 if mpTi[apT[i]] != i {
Rob Pike4f61fc92010-09-04 10:36:13 +1000181 fmt.Printf("mpTi[apT[%d]] = %d\n", i, mpTi[apT[i]])
Rob Pike2738f422008-08-11 13:32:13 -0700182 }
Russ Coxf2b5a072011-01-19 23:09:00 -0500183 // if(mti[t] != i) {
184 // fmt.Printf("mti[%s] = %s\n", s, mti[t])
185 // }
186 if mipM[i][i] != i+1 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000187 fmt.Printf("mipM[%d][%d] = %d\n", i, i, mipM[i][i])
Rob Pike62b06fa2008-12-20 16:07:43 -0800188 }
Russ Coxf2b5a072011-01-19 23:09:00 -0500189 // if(mti[t] != i) {
190 // fmt.Printf("mti[%v] = %d\n", t, mti[t])
191 // }
192 if mit[i].i != int64(i) || mit[i].f != f {
Rob Pike4f61fc92010-09-04 10:36:13 +1000193 fmt.Printf("mit[%d] = {%d %g}\n", i, mit[i].i, mit[i].f)
Rob Pike2738f422008-08-11 13:32:13 -0700194 }
195 }
196
Rob Pike2738f422008-08-11 13:32:13 -0700197 // test existence with tuple check
198 // failed lookups yield a false value for the boolean.
199 for i := 0; i < count; i++ {
Rob Pike4f61fc92010-09-04 10:36:13 +1000200 s := strconv.Itoa(i)
Russ Coxf2b5a072011-01-19 23:09:00 -0500201 f := float32(i)
Rob Pike2738f422008-08-11 13:32:13 -0700202 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000203 _, b := mib[i]
Rob Pike2738f422008-08-11 13:32:13 -0700204 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000205 fmt.Printf("tuple existence decl: mib[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700206 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000207 _, b = mib[i]
Rob Pike2738f422008-08-11 13:32:13 -0700208 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000209 fmt.Printf("tuple existence assign: mib[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700210 }
211 }
212 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000213 _, b := mii[i]
Rob Pike2738f422008-08-11 13:32:13 -0700214 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000215 fmt.Printf("tuple existence decl: mii[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700216 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000217 _, b = mii[i]
Rob Pike2738f422008-08-11 13:32:13 -0700218 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000219 fmt.Printf("tuple existence assign: mii[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700220 }
221 }
222 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000223 _, b := mfi[f]
Rob Pike2738f422008-08-11 13:32:13 -0700224 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000225 fmt.Printf("tuple existence decl: mfi[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700226 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000227 _, b = mfi[f]
Rob Pike2738f422008-08-11 13:32:13 -0700228 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000229 fmt.Printf("tuple existence assign: mfi[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700230 }
231 }
232 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000233 _, b := mif[i]
Rob Pike2738f422008-08-11 13:32:13 -0700234 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000235 fmt.Printf("tuple existence decl: mif[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700236 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000237 _, b = mif[i]
Rob Pike2738f422008-08-11 13:32:13 -0700238 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000239 fmt.Printf("tuple existence assign: mif[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700240 }
241 }
242 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000243 _, b := mis[i]
Rob Pike2738f422008-08-11 13:32:13 -0700244 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000245 fmt.Printf("tuple existence decl: mis[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700246 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000247 _, b = mis[i]
Rob Pike2738f422008-08-11 13:32:13 -0700248 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000249 fmt.Printf("tuple existence assign: mis[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700250 }
251 }
252 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000253 _, b := msi[s]
Rob Pike2738f422008-08-11 13:32:13 -0700254 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000255 fmt.Printf("tuple existence decl: msi[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700256 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000257 _, b = msi[s]
Rob Pike2738f422008-08-11 13:32:13 -0700258 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000259 fmt.Printf("tuple existence assign: msi[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700260 }
261 }
262 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000263 _, b := mss[s]
Rob Pike2738f422008-08-11 13:32:13 -0700264 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000265 fmt.Printf("tuple existence decl: mss[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700266 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000267 _, b = mss[s]
Rob Pike2738f422008-08-11 13:32:13 -0700268 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000269 fmt.Printf("tuple existence assign: mss[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700270 }
271 }
272 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000273 _, b := mspa[s]
Rob Pike2738f422008-08-11 13:32:13 -0700274 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000275 fmt.Printf("tuple existence decl: mspa[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700276 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000277 _, b = mspa[s]
Rob Pike2738f422008-08-11 13:32:13 -0700278 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000279 fmt.Printf("tuple existence assign: mspa[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700280 }
281 }
282 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000283 _, b := mipT[i]
Rob Pike2738f422008-08-11 13:32:13 -0700284 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000285 fmt.Printf("tuple existence decl: mipT[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700286 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000287 _, b = mipT[i]
Rob Pike2738f422008-08-11 13:32:13 -0700288 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000289 fmt.Printf("tuple existence assign: mipT[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700290 }
291 }
292 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000293 _, b := mpTi[apT[i]]
Rob Pike2738f422008-08-11 13:32:13 -0700294 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000295 fmt.Printf("tuple existence decl: mpTi[apT[%d]]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700296 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000297 _, b = mpTi[apT[i]]
Rob Pike2738f422008-08-11 13:32:13 -0700298 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000299 fmt.Printf("tuple existence assign: mpTi[apT[%d]]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700300 }
301 }
Rob Pike2738f422008-08-11 13:32:13 -0700302 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000303 _, b := mipM[i]
Rob Pike2738f422008-08-11 13:32:13 -0700304 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000305 fmt.Printf("tuple existence decl: mipM[%d]\n", i)
Rob Pike2738f422008-08-11 13:32:13 -0700306 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000307 _, b = mipM[i]
Rob Pike2738f422008-08-11 13:32:13 -0700308 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000309 fmt.Printf("tuple existence assign: mipM[%d]\n", i)
Rob Pike62b06fa2008-12-20 16:07:43 -0800310 }
311 }
312 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000313 _, b := mit[i]
Rob Pike62b06fa2008-12-20 16:07:43 -0800314 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000315 fmt.Printf("tuple existence decl: mit[%d]\n", i)
Rob Pike62b06fa2008-12-20 16:07:43 -0800316 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000317 _, b = mit[i]
Rob Pike62b06fa2008-12-20 16:07:43 -0800318 if !b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000319 fmt.Printf("tuple existence assign: mit[%d]\n", i)
Rob Pike62b06fa2008-12-20 16:07:43 -0800320 }
321 }
Russ Coxf2b5a072011-01-19 23:09:00 -0500322 // {
323 // _, b := mti[t]
324 // if !b {
325 // fmt.Printf("tuple existence decl: mti[%d]\n", i)
326 // }
327 // _, b = mti[t]
328 // if !b {
329 // fmt.Printf("tuple existence assign: mti[%d]\n", i)
330 // }
331 // }
Rob Pike2738f422008-08-11 13:32:13 -0700332 }
333
334 // test nonexistence with tuple check
335 // failed lookups yield a false value for the boolean.
336 for i := count; i < 2*count; i++ {
Rob Pike4f61fc92010-09-04 10:36:13 +1000337 s := strconv.Itoa(i)
Russ Coxf2b5a072011-01-19 23:09:00 -0500338 f := float32(i)
Rob Pike2738f422008-08-11 13:32:13 -0700339 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000340 _, b := mib[i]
Rob Pike2738f422008-08-11 13:32:13 -0700341 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000342 fmt.Printf("tuple nonexistence decl: mib[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700343 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000344 _, b = mib[i]
Rob Pike2738f422008-08-11 13:32:13 -0700345 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000346 fmt.Printf("tuple nonexistence assign: mib[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700347 }
348 }
349 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000350 _, b := mii[i]
Rob Pike2738f422008-08-11 13:32:13 -0700351 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000352 fmt.Printf("tuple nonexistence decl: mii[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700353 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000354 _, b = mii[i]
Rob Pike2738f422008-08-11 13:32:13 -0700355 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000356 fmt.Printf("tuple nonexistence assign: mii[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700357 }
358 }
359 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000360 _, b := mfi[f]
Rob Pike2738f422008-08-11 13:32:13 -0700361 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000362 fmt.Printf("tuple nonexistence decl: mfi[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700363 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000364 _, b = mfi[f]
Rob Pike2738f422008-08-11 13:32:13 -0700365 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000366 fmt.Printf("tuple nonexistence assign: mfi[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700367 }
368 }
369 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000370 _, b := mif[i]
Rob Pike2738f422008-08-11 13:32:13 -0700371 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000372 fmt.Printf("tuple nonexistence decl: mif[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700373 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000374 _, b = mif[i]
Rob Pike2738f422008-08-11 13:32:13 -0700375 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000376 fmt.Printf("tuple nonexistence assign: mif[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700377 }
378 }
379 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000380 _, b := mis[i]
Rob Pike2738f422008-08-11 13:32:13 -0700381 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000382 fmt.Printf("tuple nonexistence decl: mis[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700383 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000384 _, b = mis[i]
Rob Pike2738f422008-08-11 13:32:13 -0700385 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000386 fmt.Printf("tuple nonexistence assign: mis[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700387 }
388 }
389 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000390 _, b := msi[s]
Rob Pike2738f422008-08-11 13:32:13 -0700391 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000392 fmt.Printf("tuple nonexistence decl: msi[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700393 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000394 _, b = msi[s]
Rob Pike2738f422008-08-11 13:32:13 -0700395 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000396 fmt.Printf("tuple nonexistence assign: msi[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700397 }
398 }
399 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000400 _, b := mss[s]
Rob Pike2738f422008-08-11 13:32:13 -0700401 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000402 fmt.Printf("tuple nonexistence decl: mss[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700403 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000404 _, b = mss[s]
Rob Pike2738f422008-08-11 13:32:13 -0700405 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000406 fmt.Printf("tuple nonexistence assign: mss[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700407 }
408 }
409 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000410 _, b := mspa[s]
Rob Pike2738f422008-08-11 13:32:13 -0700411 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000412 fmt.Printf("tuple nonexistence decl: mspa[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700413 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000414 _, b = mspa[s]
Rob Pike2738f422008-08-11 13:32:13 -0700415 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000416 fmt.Printf("tuple nonexistence assign: mspa[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700417 }
418 }
419 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000420 _, b := mipT[i]
Rob Pike2738f422008-08-11 13:32:13 -0700421 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000422 fmt.Printf("tuple nonexistence decl: mipT[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700423 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000424 _, b = mipT[i]
Rob Pike2738f422008-08-11 13:32:13 -0700425 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000426 fmt.Printf("tuple nonexistence assign: mipT[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700427 }
428 }
429 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000430 _, b := mpTi[apT[i]]
Rob Pike2738f422008-08-11 13:32:13 -0700431 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000432 fmt.Printf("tuple nonexistence decl: mpTi[apt[%d]]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700433 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000434 _, b = mpTi[apT[i]]
Rob Pike2738f422008-08-11 13:32:13 -0700435 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000436 fmt.Printf("tuple nonexistence assign: mpTi[apT[%d]]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700437 }
438 }
Rob Pike2738f422008-08-11 13:32:13 -0700439 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000440 _, b := mipM[i]
Rob Pike2738f422008-08-11 13:32:13 -0700441 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000442 fmt.Printf("tuple nonexistence decl: mipM[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700443 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000444 _, b = mipM[i]
Rob Pike2738f422008-08-11 13:32:13 -0700445 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000446 fmt.Printf("tuple nonexistence assign: mipM[%d]", i)
Rob Pike62b06fa2008-12-20 16:07:43 -0800447 }
448 }
Russ Coxf2b5a072011-01-19 23:09:00 -0500449 // {
450 // _, b := mti[t]
451 // if b {
452 // fmt.Printf("tuple nonexistence decl: mti[%d]", i)
453 // }
454 // _, b = mti[t]
455 // if b {
456 // fmt.Printf("tuple nonexistence assign: mti[%d]", i)
457 // }
458 // }
Rob Pike62b06fa2008-12-20 16:07:43 -0800459 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000460 _, b := mit[i]
Rob Pike62b06fa2008-12-20 16:07:43 -0800461 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000462 fmt.Printf("tuple nonexistence decl: mit[%d]", i)
Rob Pike62b06fa2008-12-20 16:07:43 -0800463 }
Rob Pike4f61fc92010-09-04 10:36:13 +1000464 _, b = mit[i]
Rob Pike62b06fa2008-12-20 16:07:43 -0800465 if b {
Rob Pike4f61fc92010-09-04 10:36:13 +1000466 fmt.Printf("tuple nonexistence assign: mit[%d]", i)
Rob Pike2738f422008-08-11 13:32:13 -0700467 }
468 }
469 }
Russ Cox08ca30b2008-12-19 03:05:37 -0800470
Rob Pike2738f422008-08-11 13:32:13 -0700471 // tests for structured map element updates
472 for i := 0; i < count; i++ {
Rob Pike4f61fc92010-09-04 10:36:13 +1000473 s := strconv.Itoa(i)
Russ Coxf2b5a072011-01-19 23:09:00 -0500474 mspa[s][i%2] = "deleted"
475 if mspa[s][i%2] != "deleted" {
476 fmt.Printf("update mspa[%s][%d] = %s\n", s, i%2, mspa[s][i%2])
Rob Pike2738f422008-08-11 13:32:13 -0700477 }
Rob Pike62b06fa2008-12-20 16:07:43 -0800478
Rob Pike4f61fc92010-09-04 10:36:13 +1000479 mipT[i].i += 1
Rob Pike62b06fa2008-12-20 16:07:43 -0800480 if mipT[i].i != int64(i)+1 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000481 fmt.Printf("update mipT[%d].i = %d\n", i, mipT[i].i)
Rob Pike2738f422008-08-11 13:32:13 -0700482 }
Russ Coxf2b5a072011-01-19 23:09:00 -0500483 mipT[i].f = float32(i + 1)
484 if mipT[i].f != float32(i+1) {
Rob Pike4f61fc92010-09-04 10:36:13 +1000485 fmt.Printf("update mipT[%d].f = %g\n", i, mipT[i].f)
Rob Pike2738f422008-08-11 13:32:13 -0700486 }
Rob Pike62b06fa2008-12-20 16:07:43 -0800487
Rob Pike4f61fc92010-09-04 10:36:13 +1000488 mipM[i][i]++
Russ Coxf2b5a072011-01-19 23:09:00 -0500489 if mipM[i][i] != (i+1)+1 {
Rob Pike4f61fc92010-09-04 10:36:13 +1000490 fmt.Printf("update mipM[%d][%d] = %i\n", i, i, mipM[i][i])
Rob Pike2738f422008-08-11 13:32:13 -0700491 }
492 }
Russ Coxae54cf72009-09-15 12:42:24 -0700493
Russ Cox86145612009-03-23 18:32:37 -0700494 // test range on nil map
Russ Coxf2b5a072011-01-19 23:09:00 -0500495 var mnil map[string]int
Russ Coxae54cf72009-09-15 12:42:24 -0700496 for _, _ = range mnil {
Rob Pike4f61fc92010-09-04 10:36:13 +1000497 panic("range mnil")
Russ Cox86145612009-03-23 18:32:37 -0700498 }
Russ Cox408f0b12012-01-26 16:25:07 -0500499}
500
501func testfloat() {
502 // Test floating point numbers in maps.
503 // Two map keys refer to the same entry if the keys are ==.
504 // The special cases, then, are that +0 == -0 and that NaN != NaN.
505
506 {
507 var (
508 pz = float32(0)
509 nz = math.Float32frombits(1 << 31)
510 nana = float32(math.NaN())
511 nanb = math.Float32frombits(math.Float32bits(nana) ^ 2)
512 )
513
514 m := map[float32]string{
515 pz: "+0",
516 nana: "NaN",
517 nanb: "NaN",
518 }
519 if m[pz] != "+0" {
520 fmt.Println("float32 map cannot read back m[+0]:", m[pz])
521 }
522 if m[nz] != "+0" {
523 fmt.Println("float32 map does not treat", pz, "and", nz, "as equal for read")
524 fmt.Println("float32 map does not treat -0 and +0 as equal for read")
525 }
526 m[nz] = "-0"
527 if m[pz] != "-0" {
528 fmt.Println("float32 map does not treat -0 and +0 as equal for write")
529 }
530 if _, ok := m[nana]; ok {
531 fmt.Println("float32 map allows NaN lookup (a)")
532 }
533 if _, ok := m[nanb]; ok {
534 fmt.Println("float32 map allows NaN lookup (b)")
535 }
536 if len(m) != 3 {
537 fmt.Println("float32 map should have 3 entries:", m)
538 }
539 m[nana] = "NaN"
540 m[nanb] = "NaN"
541 if len(m) != 5 {
542 fmt.Println("float32 map should have 5 entries:", m)
543 }
544 }
545
546 {
547 var (
548 pz = float64(0)
549 nz = math.Float64frombits(1 << 63)
550 nana = float64(math.NaN())
551 nanb = math.Float64frombits(math.Float64bits(nana) ^ 2)
552 )
553
554 m := map[float64]string{
555 pz: "+0",
556 nana: "NaN",
557 nanb: "NaN",
558 }
559 if m[nz] != "+0" {
560 fmt.Println("float64 map does not treat -0 and +0 as equal for read")
561 }
562 m[nz] = "-0"
563 if m[pz] != "-0" {
564 fmt.Println("float64 map does not treat -0 and +0 as equal for write")
565 }
566 if _, ok := m[nana]; ok {
567 fmt.Println("float64 map allows NaN lookup (a)")
568 }
569 if _, ok := m[nanb]; ok {
570 fmt.Println("float64 map allows NaN lookup (b)")
571 }
572 if len(m) != 3 {
573 fmt.Println("float64 map should have 3 entries:", m)
574 }
575 m[nana] = "NaN"
576 m[nanb] = "NaN"
577 if len(m) != 5 {
578 fmt.Println("float64 map should have 5 entries:", m)
579 }
580 }
581
582 {
583 var (
584 pz = complex64(0)
585 nz = complex(0, math.Float32frombits(1<<31))
586 nana = complex(5, float32(math.NaN()))
587 nanb = complex(5, math.Float32frombits(math.Float32bits(float32(math.NaN()))^2))
588 )
589
590 m := map[complex64]string{
591 pz: "+0",
592 nana: "NaN",
593 nanb: "NaN",
594 }
595 if m[nz] != "+0" {
596 fmt.Println("complex64 map does not treat -0 and +0 as equal for read")
597 }
598 m[nz] = "-0"
599 if m[pz] != "-0" {
600 fmt.Println("complex64 map does not treat -0 and +0 as equal for write")
601 }
602 if _, ok := m[nana]; ok {
603 fmt.Println("complex64 map allows NaN lookup (a)")
604 }
605 if _, ok := m[nanb]; ok {
606 fmt.Println("complex64 map allows NaN lookup (b)")
607 }
608 if len(m) != 3 {
609 fmt.Println("complex64 map should have 3 entries:", m)
610 }
611 m[nana] = "NaN"
612 m[nanb] = "NaN"
613 if len(m) != 5 {
614 fmt.Println("complex64 map should have 5 entries:", m)
615 }
616 }
617
618 {
619 var (
620 pz = complex128(0)
621 nz = complex(0, math.Float64frombits(1<<63))
622 nana = complex(5, float64(math.NaN()))
623 nanb = complex(5, math.Float64frombits(math.Float64bits(float64(math.NaN()))^2))
624 )
625
626 m := map[complex128]string{
627 pz: "+0",
628 nana: "NaN",
629 nanb: "NaN",
630 }
631 if m[nz] != "+0" {
632 fmt.Println("complex128 map does not treat -0 and +0 as equal for read")
633 }
634 m[nz] = "-0"
635 if m[pz] != "-0" {
636 fmt.Println("complex128 map does not treat -0 and +0 as equal for write")
637 }
638 if _, ok := m[nana]; ok {
639 fmt.Println("complex128 map allows NaN lookup (a)")
640 }
641 if _, ok := m[nanb]; ok {
642 fmt.Println("complex128 map allows NaN lookup (b)")
643 }
644 if len(m) != 3 {
645 fmt.Println("complex128 map should have 3 entries:", m)
646 }
647 m[nana] = "NaN"
648 m[nanb] = "NaN"
649 if len(m) != 5 {
650 fmt.Println("complex128 map should have 5 entries:", m)
651 }
652 }
Rob Pike2738f422008-08-11 13:32:13 -0700653}
Russ Cox6ebf8a62012-01-30 13:41:38 -0500654
655func testnan() {
656 // Test that NaNs in maps don't go quadratic.
657 t := func(n int) time.Duration {
658 t0 := time.Now()
659 m := map[float64]int{}
660 nan := math.NaN()
661 for i := 0; i < n; i++ {
662 m[nan] = 1
663 }
664 if len(m) != n {
665 panic("wrong size map after nan insertion")
666 }
667 return time.Since(t0)
668 }
669
Brad Fitzpatrick69a5b232012-02-02 11:49:28 -0800670 // Depending on the machine and OS, this test might be too fast
671 // to measure with accurate enough granularity. On failure,
672 // make it run longer, hoping that the timing granularity
673 // is eventually sufficient.
674
675 n := 30000 // 0.02 seconds on a MacBook Air
676 fails := 0
677 for {
678 t1 := t(n)
679 t2 := t(2 * n)
680 // should be 2x (linear); allow up to 3x
681 if t2 < 3*t1 {
682 return
683 }
684 fails++
685 if fails == 4 {
686 fmt.Printf("too slow: %d inserts: %v; %d inserts: %v\n", n, t1, 2*n, t2)
687 return
688 }
689 n *= 2
Russ Cox6ebf8a62012-01-30 13:41:38 -0500690 }
691}