1 // $G $F.go && $L $F.$A && ./$A.out
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.
18 func P(a []string) string {
20 for i := 0; i < len(a); i++ {
37 // Test a map literal.
38 mlit := map[string]int{"0": 0, "1": 1, "2": 2, "3": 3, "4": 4}
39 for i := 0; i < len(mlit); i++ {
40 s := string([]byte{byte(i) + '0'})
42 fmt.Printf("mlit[%s] = %d\n", s, mlit[s])
46 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)
54 // BUG need an interface map both ways too
57 i int64 // can't use string here; struct values are only compared at the top level
60 mipT := make(map[int]*T)
61 mpTi := make(map[*T]int)
62 mit := make(map[int]T)
63 // mti := make(map[T] int)
66 mipM := make(map[int]M)
70 for i := 0; i < count; i++ {
72 s10 := strconv.Itoa(i * 10)
78 apT[2*i] = new(T) // need twice as many entries as we use, for the nonexistence check
84 mfi[float32(i)] = 10 * i
90 as := make([]string, 2)
102 if len(mib) != count {
103 fmt.Printf("len(mib) = %d\n", len(mib))
105 if len(mii) != count {
106 fmt.Printf("len(mii) = %d\n", len(mii))
108 if len(mfi) != count {
109 fmt.Printf("len(mfi) = %d\n", len(mfi))
111 if len(mif) != count {
112 fmt.Printf("len(mif) = %d\n", len(mif))
114 if len(msi) != count {
115 fmt.Printf("len(msi) = %d\n", len(msi))
117 if len(mis) != count {
118 fmt.Printf("len(mis) = %d\n", len(mis))
120 if len(mss) != count {
121 fmt.Printf("len(mss) = %d\n", len(mss))
123 if len(mspa) != count {
124 fmt.Printf("len(mspa) = %d\n", len(mspa))
126 if len(mipT) != count {
127 fmt.Printf("len(mipT) = %d\n", len(mipT))
129 if len(mpTi) != count {
130 fmt.Printf("len(mpTi) = %d\n", len(mpTi))
132 // if len(mti) != count {
133 // fmt.Printf("len(mti) = %d\n", len(mti))
135 if len(mipM) != count {
136 fmt.Printf("len(mipM) = %d\n", len(mipM))
138 // if len(mti) != count {
139 // fmt.Printf("len(mti) = %d\n", len(mti))
141 if len(mit) != count {
142 fmt.Printf("len(mit) = %d\n", len(mit))
145 // test construction directly
146 for i := 0; i < count; i++ {
148 s10 := strconv.Itoa(i * 10)
150 // BUG m := M(i, i+1)
151 if mib[i] != (i != 0) {
152 fmt.Printf("mib[%d] = %t\n", i, mib[i])
155 fmt.Printf("mii[%d] = %d\n", i, mii[i])
158 fmt.Printf("mfi[%d] = %d\n", i, mfi[f])
160 if mif[i] != 10.0*f {
161 fmt.Printf("mif[%d] = %g\n", i, mif[i])
164 fmt.Printf("mis[%d] = %s\n", i, mis[i])
167 fmt.Printf("msi[%s] = %d\n", s, msi[s])
170 fmt.Printf("mss[%s] = %g\n", s, mss[s])
172 for j := 0; j < len(mspa[s]); j++ {
173 if mspa[s][j] != s10 {
174 fmt.Printf("mspa[%s][%d] = %s\n", s, j, mspa[s][j])
177 if mipT[i].i != int64(i) || mipT[i].f != f {
178 fmt.Printf("mipT[%d] = %v\n", i, mipT[i])
180 if mpTi[apT[i]] != i {
181 fmt.Printf("mpTi[apT[%d]] = %d\n", i, mpTi[apT[i]])
184 // fmt.Printf("mti[%s] = %s\n", s, mti[t])
186 if mipM[i][i] != i+1 {
187 fmt.Printf("mipM[%d][%d] = %d\n", i, i, mipM[i][i])
190 // fmt.Printf("mti[%v] = %d\n", t, mti[t])
192 if mit[i].i != int64(i) || mit[i].f != f {
193 fmt.Printf("mit[%d] = {%d %g}\n", i, mit[i].i, mit[i].f)
197 // test existence with tuple check
198 // failed lookups yield a false value for the boolean.
199 for i := 0; i < count; i++ {
205 fmt.Printf("tuple existence decl: mib[%d]\n", i)
209 fmt.Printf("tuple existence assign: mib[%d]\n", i)
215 fmt.Printf("tuple existence decl: mii[%d]\n", i)
219 fmt.Printf("tuple existence assign: mii[%d]\n", i)
225 fmt.Printf("tuple existence decl: mfi[%d]\n", i)
229 fmt.Printf("tuple existence assign: mfi[%d]\n", i)
235 fmt.Printf("tuple existence decl: mif[%d]\n", i)
239 fmt.Printf("tuple existence assign: mif[%d]\n", i)
245 fmt.Printf("tuple existence decl: mis[%d]\n", i)
249 fmt.Printf("tuple existence assign: mis[%d]\n", i)
255 fmt.Printf("tuple existence decl: msi[%d]\n", i)
259 fmt.Printf("tuple existence assign: msi[%d]\n", i)
265 fmt.Printf("tuple existence decl: mss[%d]\n", i)
269 fmt.Printf("tuple existence assign: mss[%d]\n", i)
275 fmt.Printf("tuple existence decl: mspa[%d]\n", i)
279 fmt.Printf("tuple existence assign: mspa[%d]\n", i)
285 fmt.Printf("tuple existence decl: mipT[%d]\n", i)
289 fmt.Printf("tuple existence assign: mipT[%d]\n", i)
295 fmt.Printf("tuple existence decl: mpTi[apT[%d]]\n", i)
299 fmt.Printf("tuple existence assign: mpTi[apT[%d]]\n", i)
305 fmt.Printf("tuple existence decl: mipM[%d]\n", i)
309 fmt.Printf("tuple existence assign: mipM[%d]\n", i)
315 fmt.Printf("tuple existence decl: mit[%d]\n", i)
319 fmt.Printf("tuple existence assign: mit[%d]\n", i)
325 // fmt.Printf("tuple existence decl: mti[%d]\n", i)
329 // fmt.Printf("tuple existence assign: mti[%d]\n", i)
334 // test nonexistence with tuple check
335 // failed lookups yield a false value for the boolean.
336 for i := count; i < 2*count; i++ {
342 fmt.Printf("tuple nonexistence decl: mib[%d]", i)
346 fmt.Printf("tuple nonexistence assign: mib[%d]", i)
352 fmt.Printf("tuple nonexistence decl: mii[%d]", i)
356 fmt.Printf("tuple nonexistence assign: mii[%d]", i)
362 fmt.Printf("tuple nonexistence decl: mfi[%d]", i)
366 fmt.Printf("tuple nonexistence assign: mfi[%d]", i)
372 fmt.Printf("tuple nonexistence decl: mif[%d]", i)
376 fmt.Printf("tuple nonexistence assign: mif[%d]", i)
382 fmt.Printf("tuple nonexistence decl: mis[%d]", i)
386 fmt.Printf("tuple nonexistence assign: mis[%d]", i)
392 fmt.Printf("tuple nonexistence decl: msi[%d]", i)
396 fmt.Printf("tuple nonexistence assign: msi[%d]", i)
402 fmt.Printf("tuple nonexistence decl: mss[%d]", i)
406 fmt.Printf("tuple nonexistence assign: mss[%d]", i)
412 fmt.Printf("tuple nonexistence decl: mspa[%d]", i)
416 fmt.Printf("tuple nonexistence assign: mspa[%d]", i)
422 fmt.Printf("tuple nonexistence decl: mipT[%d]", i)
426 fmt.Printf("tuple nonexistence assign: mipT[%d]", i)
432 fmt.Printf("tuple nonexistence decl: mpTi[apt[%d]]", i)
436 fmt.Printf("tuple nonexistence assign: mpTi[apT[%d]]", i)
442 fmt.Printf("tuple nonexistence decl: mipM[%d]", i)
446 fmt.Printf("tuple nonexistence assign: mipM[%d]", i)
452 // fmt.Printf("tuple nonexistence decl: mti[%d]", i)
456 // fmt.Printf("tuple nonexistence assign: mti[%d]", i)
462 fmt.Printf("tuple nonexistence decl: mit[%d]", i)
466 fmt.Printf("tuple nonexistence assign: mit[%d]", i)
471 // tests for structured map element updates
472 for i := 0; i < count; i++ {
474 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])
480 if mipT[i].i != int64(i)+1 {
481 fmt.Printf("update mipT[%d].i = %d\n", i, mipT[i].i)
483 mipT[i].f = float32(i + 1)
484 if mipT[i].f != float32(i+1) {
485 fmt.Printf("update mipT[%d].f = %g\n", i, mipT[i].f)
489 if mipM[i][i] != (i+1)+1 {
490 fmt.Printf("update mipM[%d][%d] = %i\n", i, i, mipM[i][i])
494 // test range on nil map
495 var mnil map[string]int
496 for _, _ = range mnil {
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.
509 nz = math.Float32frombits(1 << 31)
510 nana = float32(math.NaN())
511 nanb = math.Float32frombits(math.Float32bits(nana) ^ 2)
514 m := map[float32]string{
520 fmt.Println("float32 map cannot read back m[+0]:", m[pz])
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")
528 fmt.Println("float32 map does not treat -0 and +0 as equal for write")
530 if _, ok := m[nana]; ok {
531 fmt.Println("float32 map allows NaN lookup (a)")
533 if _, ok := m[nanb]; ok {
534 fmt.Println("float32 map allows NaN lookup (b)")
537 fmt.Println("float32 map should have 3 entries:", m)
542 fmt.Println("float32 map should have 5 entries:", m)
549 nz = math.Float64frombits(1 << 63)
550 nana = float64(math.NaN())
551 nanb = math.Float64frombits(math.Float64bits(nana) ^ 2)
554 m := map[float64]string{
560 fmt.Println("float64 map does not treat -0 and +0 as equal for read")
564 fmt.Println("float64 map does not treat -0 and +0 as equal for write")
566 if _, ok := m[nana]; ok {
567 fmt.Println("float64 map allows NaN lookup (a)")
569 if _, ok := m[nanb]; ok {
570 fmt.Println("float64 map allows NaN lookup (b)")
573 fmt.Println("float64 map should have 3 entries:", m)
578 fmt.Println("float64 map should have 5 entries:", m)
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))
590 m := map[complex64]string{
596 fmt.Println("complex64 map does not treat -0 and +0 as equal for read")
600 fmt.Println("complex64 map does not treat -0 and +0 as equal for write")
602 if _, ok := m[nana]; ok {
603 fmt.Println("complex64 map allows NaN lookup (a)")
605 if _, ok := m[nanb]; ok {
606 fmt.Println("complex64 map allows NaN lookup (b)")
609 fmt.Println("complex64 map should have 3 entries:", m)
614 fmt.Println("complex64 map should have 5 entries:", m)
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))
626 m := map[complex128]string{
632 fmt.Println("complex128 map does not treat -0 and +0 as equal for read")
636 fmt.Println("complex128 map does not treat -0 and +0 as equal for write")
638 if _, ok := m[nana]; ok {
639 fmt.Println("complex128 map allows NaN lookup (a)")
641 if _, ok := m[nanb]; ok {
642 fmt.Println("complex128 map allows NaN lookup (b)")
645 fmt.Println("complex128 map should have 3 entries:", m)
650 fmt.Println("complex128 map should have 5 entries:", m)
656 // Test that NaNs in maps don't go quadratic.
657 t := func(n int) time.Duration {
659 m := map[float64]int{}
661 for i := 0; i < n; i++ {
665 panic("wrong size map after nan insertion")
667 return time.Since(t0)
670 // 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.
675 n := 30000 // 0.02 seconds on a MacBook Air
680 // should be 2x (linear); allow up to 3x
686 fmt.Printf("too slow: %d inserts: %v; %d inserts: %v\n", n, t1, 2*n, t2)