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.
7 // Test maps, almost exhaustively.
8 // Complexity (linearity) test is in maplinear.go.
20 func P(a []string) string {
22 for i := 0; i < len(a); i++ {
39 // Test a map literal.
40 mlit := map[string]int{"0": 0, "1": 1, "2": 2, "3": 3, "4": 4}
41 for i := 0; i < len(mlit); i++ {
42 s := string([]byte{byte(i) + '0'})
44 panic(fmt.Sprintf("mlit[%s] = %d\n", s, mlit[s]))
48 mib := make(map[int]bool)
49 mii := make(map[int]int)
50 mfi := make(map[float32]int)
51 mif := make(map[int]float32)
52 msi := make(map[string]int)
53 mis := make(map[int]string)
54 mss := make(map[string]string)
55 mspa := make(map[string][]string)
56 // BUG need an interface map both ways too
59 i int64 // can't use string here; struct values are only compared at the top level
62 mipT := make(map[int]*T)
63 mpTi := make(map[*T]int)
64 mit := make(map[int]T)
65 // mti := make(map[T] int)
68 mipM := make(map[int]M)
72 for i := 0; i < count; i++ {
74 s10 := strconv.Itoa(i * 10)
80 apT[2*i] = new(T) // need twice as many entries as we use, for the nonexistence check
86 mfi[float32(i)] = 10 * i
92 as := make([]string, 2)
104 if len(mib) != count {
105 panic(fmt.Sprintf("len(mib) = %d\n", len(mib)))
107 if len(mii) != count {
108 panic(fmt.Sprintf("len(mii) = %d\n", len(mii)))
110 if len(mfi) != count {
111 panic(fmt.Sprintf("len(mfi) = %d\n", len(mfi)))
113 if len(mif) != count {
114 panic(fmt.Sprintf("len(mif) = %d\n", len(mif)))
116 if len(msi) != count {
117 panic(fmt.Sprintf("len(msi) = %d\n", len(msi)))
119 if len(mis) != count {
120 panic(fmt.Sprintf("len(mis) = %d\n", len(mis)))
122 if len(mss) != count {
123 panic(fmt.Sprintf("len(mss) = %d\n", len(mss)))
125 if len(mspa) != count {
126 panic(fmt.Sprintf("len(mspa) = %d\n", len(mspa)))
128 if len(mipT) != count {
129 panic(fmt.Sprintf("len(mipT) = %d\n", len(mipT)))
131 if len(mpTi) != count {
132 panic(fmt.Sprintf("len(mpTi) = %d\n", len(mpTi)))
134 // if len(mti) != count {
135 // panic(fmt.Sprintf("len(mti) = %d\n", len(mti)))
137 if len(mipM) != count {
138 panic(fmt.Sprintf("len(mipM) = %d\n", len(mipM)))
140 // if len(mti) != count {
141 // panic(fmt.Sprintf("len(mti) = %d\n", len(mti)))
143 if len(mit) != count {
144 panic(fmt.Sprintf("len(mit) = %d\n", len(mit)))
147 // test construction directly
148 for i := 0; i < count; i++ {
150 s10 := strconv.Itoa(i * 10)
152 // BUG m := M(i, i+1)
153 if mib[i] != (i != 0) {
154 panic(fmt.Sprintf("mib[%d] = %t\n", i, mib[i]))
157 panic(fmt.Sprintf("mii[%d] = %d\n", i, mii[i]))
160 panic(fmt.Sprintf("mfi[%d] = %d\n", i, mfi[f]))
162 if mif[i] != 10.0*f {
163 panic(fmt.Sprintf("mif[%d] = %g\n", i, mif[i]))
166 panic(fmt.Sprintf("mis[%d] = %s\n", i, mis[i]))
169 panic(fmt.Sprintf("msi[%s] = %d\n", s, msi[s]))
172 panic(fmt.Sprintf("mss[%s] = %g\n", s, mss[s]))
174 for j := 0; j < len(mspa[s]); j++ {
175 if mspa[s][j] != s10 {
176 panic(fmt.Sprintf("mspa[%s][%d] = %s\n", s, j, mspa[s][j]))
179 if mipT[i].i != int64(i) || mipT[i].f != f {
180 panic(fmt.Sprintf("mipT[%d] = %v\n", i, mipT[i]))
182 if mpTi[apT[i]] != i {
183 panic(fmt.Sprintf("mpTi[apT[%d]] = %d\n", i, mpTi[apT[i]]))
186 // panic(fmt.Sprintf("mti[%s] = %s\n", s, mti[t]))
188 if mipM[i][i] != i+1 {
189 panic(fmt.Sprintf("mipM[%d][%d] = %d\n", i, i, mipM[i][i]))
192 // panic(fmt.Sprintf("mti[%v] = %d\n", t, mti[t]))
194 if mit[i].i != int64(i) || mit[i].f != f {
195 panic(fmt.Sprintf("mit[%d] = {%d %g}\n", i, mit[i].i, mit[i].f))
199 // test existence with tuple check
200 // failed lookups yield a false value for the boolean.
201 for i := 0; i < count; i++ {
207 panic(fmt.Sprintf("tuple existence decl: mib[%d]\n", i))
211 panic(fmt.Sprintf("tuple existence assign: mib[%d]\n", i))
217 panic(fmt.Sprintf("tuple existence decl: mii[%d]\n", i))
221 panic(fmt.Sprintf("tuple existence assign: mii[%d]\n", i))
227 panic(fmt.Sprintf("tuple existence decl: mfi[%d]\n", i))
231 panic(fmt.Sprintf("tuple existence assign: mfi[%d]\n", i))
237 panic(fmt.Sprintf("tuple existence decl: mif[%d]\n", i))
241 panic(fmt.Sprintf("tuple existence assign: mif[%d]\n", i))
247 panic(fmt.Sprintf("tuple existence decl: mis[%d]\n", i))
251 panic(fmt.Sprintf("tuple existence assign: mis[%d]\n", i))
257 panic(fmt.Sprintf("tuple existence decl: msi[%d]\n", i))
261 panic(fmt.Sprintf("tuple existence assign: msi[%d]\n", i))
267 panic(fmt.Sprintf("tuple existence decl: mss[%d]\n", i))
271 panic(fmt.Sprintf("tuple existence assign: mss[%d]\n", i))
277 panic(fmt.Sprintf("tuple existence decl: mspa[%d]\n", i))
281 panic(fmt.Sprintf("tuple existence assign: mspa[%d]\n", i))
287 panic(fmt.Sprintf("tuple existence decl: mipT[%d]\n", i))
291 panic(fmt.Sprintf("tuple existence assign: mipT[%d]\n", i))
297 panic(fmt.Sprintf("tuple existence decl: mpTi[apT[%d]]\n", i))
301 panic(fmt.Sprintf("tuple existence assign: mpTi[apT[%d]]\n", i))
307 panic(fmt.Sprintf("tuple existence decl: mipM[%d]\n", i))
311 panic(fmt.Sprintf("tuple existence assign: mipM[%d]\n", i))
317 panic(fmt.Sprintf("tuple existence decl: mit[%d]\n", i))
321 panic(fmt.Sprintf("tuple existence assign: mit[%d]\n", i))
327 // panic(fmt.Sprintf("tuple existence decl: mti[%d]\n", i))
331 // panic(fmt.Sprintf("tuple existence assign: mti[%d]\n", i))
336 // test nonexistence with tuple check
337 // failed lookups yield a false value for the boolean.
338 for i := count; i < 2*count; i++ {
344 panic(fmt.Sprintf("tuple nonexistence decl: mib[%d]", i))
348 panic(fmt.Sprintf("tuple nonexistence assign: mib[%d]", i))
354 panic(fmt.Sprintf("tuple nonexistence decl: mii[%d]", i))
358 panic(fmt.Sprintf("tuple nonexistence assign: mii[%d]", i))
364 panic(fmt.Sprintf("tuple nonexistence decl: mfi[%d]", i))
368 panic(fmt.Sprintf("tuple nonexistence assign: mfi[%d]", i))
374 panic(fmt.Sprintf("tuple nonexistence decl: mif[%d]", i))
378 panic(fmt.Sprintf("tuple nonexistence assign: mif[%d]", i))
384 panic(fmt.Sprintf("tuple nonexistence decl: mis[%d]", i))
388 panic(fmt.Sprintf("tuple nonexistence assign: mis[%d]", i))
394 panic(fmt.Sprintf("tuple nonexistence decl: msi[%d]", i))
398 panic(fmt.Sprintf("tuple nonexistence assign: msi[%d]", i))
404 panic(fmt.Sprintf("tuple nonexistence decl: mss[%d]", i))
408 panic(fmt.Sprintf("tuple nonexistence assign: mss[%d]", i))
414 panic(fmt.Sprintf("tuple nonexistence decl: mspa[%d]", i))
418 panic(fmt.Sprintf("tuple nonexistence assign: mspa[%d]", i))
424 panic(fmt.Sprintf("tuple nonexistence decl: mipT[%d]", i))
428 panic(fmt.Sprintf("tuple nonexistence assign: mipT[%d]", i))
434 panic(fmt.Sprintf("tuple nonexistence decl: mpTi[apt[%d]]", i))
438 panic(fmt.Sprintf("tuple nonexistence assign: mpTi[apT[%d]]", i))
444 panic(fmt.Sprintf("tuple nonexistence decl: mipM[%d]", i))
448 panic(fmt.Sprintf("tuple nonexistence assign: mipM[%d]", i))
454 // panic(fmt.Sprintf("tuple nonexistence decl: mti[%d]", i))
458 // panic(fmt.Sprintf("tuple nonexistence assign: mti[%d]", i))
464 panic(fmt.Sprintf("tuple nonexistence decl: mit[%d]", i))
468 panic(fmt.Sprintf("tuple nonexistence assign: mit[%d]", i))
473 // tests for structured map element updates
474 for i := 0; i < count; i++ {
476 mspa[s][i%2] = "deleted"
477 if mspa[s][i%2] != "deleted" {
478 panic(fmt.Sprintf("update mspa[%s][%d] = %s\n", s, i%2, mspa[s][i%2]))
483 if mipT[i].i != int64(i)+1 {
484 panic(fmt.Sprintf("update mipT[%d].i = %d\n", i, mipT[i].i))
487 mipT[i].f = float32(i + 1)
488 if mipT[i].f != float32(i+1) {
489 panic(fmt.Sprintf("update mipT[%d].f = %g\n", i, mipT[i].f))
494 if mipM[i][i] != (i+1)+1 {
495 panic(fmt.Sprintf("update mipM[%d][%d] = %d\n", i, i, mipM[i][i]))
500 // test range on nil map
501 var mnil map[string]int
502 for _, _ = range mnil {
508 // Test floating point numbers in maps.
509 // Two map keys refer to the same entry if the keys are ==.
510 // The special cases, then, are that +0 == -0 and that NaN != NaN.
515 nz = math.Float32frombits(1 << 31)
516 nana = float32(math.NaN())
517 nanb = math.Float32frombits(math.Float32bits(nana) ^ 2)
520 m := map[float32]string{
526 panic(fmt.Sprintln("float32 map cannot read back m[+0]:", m[pz]))
529 fmt.Sprintln("float32 map does not treat", pz, "and", nz, "as equal for read")
530 panic(fmt.Sprintln("float32 map does not treat -0 and +0 as equal for read"))
534 panic(fmt.Sprintln("float32 map does not treat -0 and +0 as equal for write"))
536 if _, ok := m[nana]; ok {
537 panic(fmt.Sprintln("float32 map allows NaN lookup (a)"))
539 if _, ok := m[nanb]; ok {
540 panic(fmt.Sprintln("float32 map allows NaN lookup (b)"))
543 panic(fmt.Sprintln("float32 map should have 3 entries:", m))
548 panic(fmt.Sprintln("float32 map should have 5 entries:", m))
555 nz = math.Float64frombits(1 << 63)
556 nana = float64(math.NaN())
557 nanb = math.Float64frombits(math.Float64bits(nana) ^ 2)
560 m := map[float64]string{
566 panic(fmt.Sprintln("float64 map does not treat -0 and +0 as equal for read"))
570 panic(fmt.Sprintln("float64 map does not treat -0 and +0 as equal for write"))
572 if _, ok := m[nana]; ok {
573 panic(fmt.Sprintln("float64 map allows NaN lookup (a)"))
575 if _, ok := m[nanb]; ok {
576 panic(fmt.Sprintln("float64 map allows NaN lookup (b)"))
579 panic(fmt.Sprintln("float64 map should have 3 entries:", m))
584 panic(fmt.Sprintln("float64 map should have 5 entries:", m))
591 nz = complex(0, math.Float32frombits(1<<31))
592 nana = complex(5, float32(math.NaN()))
593 nanb = complex(5, math.Float32frombits(math.Float32bits(float32(math.NaN()))^2))
596 m := map[complex64]string{
602 panic(fmt.Sprintln("complex64 map does not treat -0 and +0 as equal for read"))
606 panic(fmt.Sprintln("complex64 map does not treat -0 and +0 as equal for write"))
608 if _, ok := m[nana]; ok {
609 panic(fmt.Sprintln("complex64 map allows NaN lookup (a)"))
611 if _, ok := m[nanb]; ok {
612 panic(fmt.Sprintln("complex64 map allows NaN lookup (b)"))
615 panic(fmt.Sprintln("complex64 map should have 3 entries:", m))
620 panic(fmt.Sprintln("complex64 map should have 5 entries:", m))
627 nz = complex(0, math.Float64frombits(1<<63))
628 nana = complex(5, float64(math.NaN()))
629 nanb = complex(5, math.Float64frombits(math.Float64bits(float64(math.NaN()))^2))
632 m := map[complex128]string{
638 panic(fmt.Sprintln("complex128 map does not treat -0 and +0 as equal for read"))
642 panic(fmt.Sprintln("complex128 map does not treat -0 and +0 as equal for write"))
644 if _, ok := m[nana]; ok {
645 panic(fmt.Sprintln("complex128 map allows NaN lookup (a)"))
647 if _, ok := m[nanb]; ok {
648 panic(fmt.Sprintln("complex128 map allows NaN lookup (b)"))
651 panic(fmt.Sprintln("complex128 map should have 3 entries:", m))
656 panic(fmt.Sprintln("complex128 map should have 5 entries:", m))
663 m := map[float64]int{}
665 for i := 0; i < n; i++ {
669 panic("wrong size map after nan insertion")
672 for k, v := range m {
682 panic("wrong number of nan range iters")