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.
17 func P(a []string) string {
19 for i := 0; i < len(a); i++ {
30 // Test a map literal.
31 mlit := map[string]int{"0": 0, "1": 1, "2": 2, "3": 3, "4": 4}
32 for i := 0; i < len(mlit); i++ {
33 s := string([]byte{byte(i) + '0'})
35 fmt.Printf("mlit[%s] = %d\n", s, mlit[s])
39 mib := make(map[int]bool)
40 mii := make(map[int]int)
41 mfi := make(map[float32]int)
42 mif := make(map[int]float32)
43 msi := make(map[string]int)
44 mis := make(map[int]string)
45 mss := make(map[string]string)
46 mspa := make(map[string][]string)
47 // BUG need an interface map both ways too
50 i int64 // can't use string here; struct values are only compared at the top level
53 mipT := make(map[int]*T)
54 mpTi := make(map[*T]int)
55 mit := make(map[int]T)
56 // mti := make(map[T] int)
59 mipM := make(map[int]M)
63 for i := 0; i < count; i++ {
65 s10 := strconv.Itoa(i * 10)
71 apT[2*i] = new(T) // need twice as many entries as we use, for the nonexistence check
77 mfi[float32(i)] = 10 * i
83 as := make([]string, 2)
95 if len(mib) != count {
96 fmt.Printf("len(mib) = %d\n", len(mib))
98 if len(mii) != count {
99 fmt.Printf("len(mii) = %d\n", len(mii))
101 if len(mfi) != count {
102 fmt.Printf("len(mfi) = %d\n", len(mfi))
104 if len(mif) != count {
105 fmt.Printf("len(mif) = %d\n", len(mif))
107 if len(msi) != count {
108 fmt.Printf("len(msi) = %d\n", len(msi))
110 if len(mis) != count {
111 fmt.Printf("len(mis) = %d\n", len(mis))
113 if len(mss) != count {
114 fmt.Printf("len(mss) = %d\n", len(mss))
116 if len(mspa) != count {
117 fmt.Printf("len(mspa) = %d\n", len(mspa))
119 if len(mipT) != count {
120 fmt.Printf("len(mipT) = %d\n", len(mipT))
122 if len(mpTi) != count {
123 fmt.Printf("len(mpTi) = %d\n", len(mpTi))
125 // if len(mti) != count {
126 // fmt.Printf("len(mti) = %d\n", len(mti))
128 if len(mipM) != count {
129 fmt.Printf("len(mipM) = %d\n", len(mipM))
131 // if len(mti) != count {
132 // fmt.Printf("len(mti) = %d\n", len(mti))
134 if len(mit) != count {
135 fmt.Printf("len(mit) = %d\n", len(mit))
138 // test construction directly
139 for i := 0; i < count; i++ {
141 s10 := strconv.Itoa(i * 10)
143 // BUG m := M(i, i+1)
144 if mib[i] != (i != 0) {
145 fmt.Printf("mib[%d] = %t\n", i, mib[i])
148 fmt.Printf("mii[%d] = %d\n", i, mii[i])
151 fmt.Printf("mfi[%d] = %d\n", i, mfi[f])
153 if mif[i] != 10.0*f {
154 fmt.Printf("mif[%d] = %g\n", i, mif[i])
157 fmt.Printf("mis[%d] = %s\n", i, mis[i])
160 fmt.Printf("msi[%s] = %d\n", s, msi[s])
163 fmt.Printf("mss[%s] = %g\n", s, mss[s])
165 for j := 0; j < len(mspa[s]); j++ {
166 if mspa[s][j] != s10 {
167 fmt.Printf("mspa[%s][%d] = %s\n", s, j, mspa[s][j])
170 if mipT[i].i != int64(i) || mipT[i].f != f {
171 fmt.Printf("mipT[%d] = %v\n", i, mipT[i])
173 if mpTi[apT[i]] != i {
174 fmt.Printf("mpTi[apT[%d]] = %d\n", i, mpTi[apT[i]])
177 // fmt.Printf("mti[%s] = %s\n", s, mti[t])
179 if mipM[i][i] != i+1 {
180 fmt.Printf("mipM[%d][%d] = %d\n", i, i, mipM[i][i])
183 // fmt.Printf("mti[%v] = %d\n", t, mti[t])
185 if mit[i].i != int64(i) || mit[i].f != f {
186 fmt.Printf("mit[%d] = {%d %g}\n", i, mit[i].i, mit[i].f)
190 // test existence with tuple check
191 // failed lookups yield a false value for the boolean.
192 for i := 0; i < count; i++ {
198 fmt.Printf("tuple existence decl: mib[%d]\n", i)
202 fmt.Printf("tuple existence assign: mib[%d]\n", i)
208 fmt.Printf("tuple existence decl: mii[%d]\n", i)
212 fmt.Printf("tuple existence assign: mii[%d]\n", i)
218 fmt.Printf("tuple existence decl: mfi[%d]\n", i)
222 fmt.Printf("tuple existence assign: mfi[%d]\n", i)
228 fmt.Printf("tuple existence decl: mif[%d]\n", i)
232 fmt.Printf("tuple existence assign: mif[%d]\n", i)
238 fmt.Printf("tuple existence decl: mis[%d]\n", i)
242 fmt.Printf("tuple existence assign: mis[%d]\n", i)
248 fmt.Printf("tuple existence decl: msi[%d]\n", i)
252 fmt.Printf("tuple existence assign: msi[%d]\n", i)
258 fmt.Printf("tuple existence decl: mss[%d]\n", i)
262 fmt.Printf("tuple existence assign: mss[%d]\n", i)
268 fmt.Printf("tuple existence decl: mspa[%d]\n", i)
272 fmt.Printf("tuple existence assign: mspa[%d]\n", i)
278 fmt.Printf("tuple existence decl: mipT[%d]\n", i)
282 fmt.Printf("tuple existence assign: mipT[%d]\n", i)
288 fmt.Printf("tuple existence decl: mpTi[apT[%d]]\n", i)
292 fmt.Printf("tuple existence assign: mpTi[apT[%d]]\n", i)
298 fmt.Printf("tuple existence decl: mipM[%d]\n", i)
302 fmt.Printf("tuple existence assign: mipM[%d]\n", i)
308 fmt.Printf("tuple existence decl: mit[%d]\n", i)
312 fmt.Printf("tuple existence assign: mit[%d]\n", i)
318 // fmt.Printf("tuple existence decl: mti[%d]\n", i)
322 // fmt.Printf("tuple existence assign: mti[%d]\n", i)
327 // test nonexistence with tuple check
328 // failed lookups yield a false value for the boolean.
329 for i := count; i < 2*count; i++ {
335 fmt.Printf("tuple nonexistence decl: mib[%d]", i)
339 fmt.Printf("tuple nonexistence assign: mib[%d]", i)
345 fmt.Printf("tuple nonexistence decl: mii[%d]", i)
349 fmt.Printf("tuple nonexistence assign: mii[%d]", i)
355 fmt.Printf("tuple nonexistence decl: mfi[%d]", i)
359 fmt.Printf("tuple nonexistence assign: mfi[%d]", i)
365 fmt.Printf("tuple nonexistence decl: mif[%d]", i)
369 fmt.Printf("tuple nonexistence assign: mif[%d]", i)
375 fmt.Printf("tuple nonexistence decl: mis[%d]", i)
379 fmt.Printf("tuple nonexistence assign: mis[%d]", i)
385 fmt.Printf("tuple nonexistence decl: msi[%d]", i)
389 fmt.Printf("tuple nonexistence assign: msi[%d]", i)
395 fmt.Printf("tuple nonexistence decl: mss[%d]", i)
399 fmt.Printf("tuple nonexistence assign: mss[%d]", i)
405 fmt.Printf("tuple nonexistence decl: mspa[%d]", i)
409 fmt.Printf("tuple nonexistence assign: mspa[%d]", i)
415 fmt.Printf("tuple nonexistence decl: mipT[%d]", i)
419 fmt.Printf("tuple nonexistence assign: mipT[%d]", i)
425 fmt.Printf("tuple nonexistence decl: mpTi[apt[%d]]", i)
429 fmt.Printf("tuple nonexistence assign: mpTi[apT[%d]]", i)
435 fmt.Printf("tuple nonexistence decl: mipM[%d]", i)
439 fmt.Printf("tuple nonexistence assign: mipM[%d]", i)
445 // fmt.Printf("tuple nonexistence decl: mti[%d]", i)
449 // fmt.Printf("tuple nonexistence assign: mti[%d]", i)
455 fmt.Printf("tuple nonexistence decl: mit[%d]", i)
459 fmt.Printf("tuple nonexistence assign: mit[%d]", i)
464 // tests for structured map element updates
465 for i := 0; i < count; i++ {
467 mspa[s][i%2] = "deleted"
468 if mspa[s][i%2] != "deleted" {
469 fmt.Printf("update mspa[%s][%d] = %s\n", s, i%2, mspa[s][i%2])
473 if mipT[i].i != int64(i)+1 {
474 fmt.Printf("update mipT[%d].i = %d\n", i, mipT[i].i)
476 mipT[i].f = float32(i + 1)
477 if mipT[i].f != float32(i+1) {
478 fmt.Printf("update mipT[%d].f = %g\n", i, mipT[i].f)
482 if mipM[i][i] != (i+1)+1 {
483 fmt.Printf("update mipM[%d][%d] = %i\n", i, i, mipM[i][i])
487 // test range on nil map
488 var mnil map[string]int
489 for _, _ = range mnil {
497 // Test floating point numbers in maps.
498 // Two map keys refer to the same entry if the keys are ==.
499 // The special cases, then, are that +0 == -0 and that NaN != NaN.
504 nz = math.Float32frombits(1 << 31)
505 nana = float32(math.NaN())
506 nanb = math.Float32frombits(math.Float32bits(nana) ^ 2)
509 m := map[float32]string{
515 fmt.Println("float32 map cannot read back m[+0]:", m[pz])
518 fmt.Println("float32 map does not treat", pz, "and", nz, "as equal for read")
519 fmt.Println("float32 map does not treat -0 and +0 as equal for read")
523 fmt.Println("float32 map does not treat -0 and +0 as equal for write")
525 if _, ok := m[nana]; ok {
526 fmt.Println("float32 map allows NaN lookup (a)")
528 if _, ok := m[nanb]; ok {
529 fmt.Println("float32 map allows NaN lookup (b)")
532 fmt.Println("float32 map should have 3 entries:", m)
537 fmt.Println("float32 map should have 5 entries:", m)
544 nz = math.Float64frombits(1 << 63)
545 nana = float64(math.NaN())
546 nanb = math.Float64frombits(math.Float64bits(nana) ^ 2)
549 m := map[float64]string{
555 fmt.Println("float64 map does not treat -0 and +0 as equal for read")
559 fmt.Println("float64 map does not treat -0 and +0 as equal for write")
561 if _, ok := m[nana]; ok {
562 fmt.Println("float64 map allows NaN lookup (a)")
564 if _, ok := m[nanb]; ok {
565 fmt.Println("float64 map allows NaN lookup (b)")
568 fmt.Println("float64 map should have 3 entries:", m)
573 fmt.Println("float64 map should have 5 entries:", m)
580 nz = complex(0, math.Float32frombits(1<<31))
581 nana = complex(5, float32(math.NaN()))
582 nanb = complex(5, math.Float32frombits(math.Float32bits(float32(math.NaN()))^2))
585 m := map[complex64]string{
591 fmt.Println("complex64 map does not treat -0 and +0 as equal for read")
595 fmt.Println("complex64 map does not treat -0 and +0 as equal for write")
597 if _, ok := m[nana]; ok {
598 fmt.Println("complex64 map allows NaN lookup (a)")
600 if _, ok := m[nanb]; ok {
601 fmt.Println("complex64 map allows NaN lookup (b)")
604 fmt.Println("complex64 map should have 3 entries:", m)
609 fmt.Println("complex64 map should have 5 entries:", m)
616 nz = complex(0, math.Float64frombits(1<<63))
617 nana = complex(5, float64(math.NaN()))
618 nanb = complex(5, math.Float64frombits(math.Float64bits(float64(math.NaN()))^2))
621 m := map[complex128]string{
627 fmt.Println("complex128 map does not treat -0 and +0 as equal for read")
631 fmt.Println("complex128 map does not treat -0 and +0 as equal for write")
633 if _, ok := m[nana]; ok {
634 fmt.Println("complex128 map allows NaN lookup (a)")
636 if _, ok := m[nanb]; ok {
637 fmt.Println("complex128 map allows NaN lookup (b)")
640 fmt.Println("complex128 map should have 3 entries:", m)
645 fmt.Println("complex128 map should have 5 entries:", m)