// run
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test maps, almost exhaustively.
// Complexity (linearity) test is in maplinear.go.
package main
import (
"fmt"
"math"
"strconv"
)
const count = 100
func P(a []string) string {
s := "{"
for i := 0; i < len(a); i++ {
if i > 0 {
s += ","
}
s += `"` + a[i] + `"`
}
s += "}"
return s
}
func main() {
testbasic()
testfloat()
testnan()
}
func testbasic() {
// Test a map literal.
mlit := map[string]int{"0": 0, "1": 1, "2": 2, "3": 3, "4": 4}
for i := 0; i < len(mlit); i++ {
s := string([]byte{byte(i) + '0'})
if mlit[s] != i {
panic(fmt.Sprintf("mlit[%s] = %d\n", s, mlit[s]))
}
}
mib := make(map[int]bool)
mii := make(map[int]int)
mfi := make(map[float32]int)
mif := make(map[int]float32)
msi := make(map[string]int)
mis := make(map[int]string)
mss := make(map[string]string)
mspa := make(map[string][]string)
// BUG need an interface map both ways too
type T struct {
i int64 // can't use string here; struct values are only compared at the top level
f float32
}
mipT := make(map[int]*T)
mpTi := make(map[*T]int)
mit := make(map[int]T)
// mti := make(map[T] int)
type M map[int]int
mipM := make(map[int]M)
var apT [2 * count]*T
for i := 0; i < count; i++ {
s := strconv.Itoa(i)
s10 := strconv.Itoa(i * 10)
f := float32(i)
t := T{int64(i), f}
apT[i] = new(T)
apT[i].i = int64(i)
apT[i].f = f
apT[2*i] = new(T) // need twice as many entries as we use, for the nonexistence check
apT[2*i].i = int64(i)
apT[2*i].f = f
m := M{i: i + 1}
mib[i] = (i != 0)
mii[i] = 10 * i
mfi[float32(i)] = 10 * i
mif[i] = 10.0 * f
mis[i] = s
msi[s] = i
mss[s] = s10
mss[s] = s10
as := make([]string, 2)
as[0] = s10
as[1] = s10
mspa[s] = as
mipT[i] = apT[i]
mpTi[apT[i]] = i
mipM[i] = m
mit[i] = t
// mti[t] = i
}
// test len
if len(mib) != count {
panic(fmt.Sprintf("len(mib) = %d\n", len(mib)))
}
if len(mii) != count {
panic(fmt.Sprintf("len(mii) = %d\n", len(mii)))
}
if len(mfi) != count {
panic(fmt.Sprintf("len(mfi) = %d\n", len(mfi)))
}
if len(mif) != count {
panic(fmt.Sprintf("len(mif) = %d\n", len(mif)))
}
if len(msi) != count {
panic(fmt.Sprintf("len(msi) = %d\n", len(msi)))
}
if len(mis) != count {
panic(fmt.Sprintf("len(mis) = %d\n", len(mis)))
}
if len(mss) != count {
panic(fmt.Sprintf("len(mss) = %d\n", len(mss)))
}
if len(mspa) != count {
panic(fmt.Sprintf("len(mspa) = %d\n", len(mspa)))
}
if len(mipT) != count {
panic(fmt.Sprintf("len(mipT) = %d\n", len(mipT)))
}
if len(mpTi) != count {
panic(fmt.Sprintf("len(mpTi) = %d\n", len(mpTi)))
}
// if len(mti) != count {
// panic(fmt.Sprintf("len(mti) = %d\n", len(mti)))
// }
if len(mipM) != count {
panic(fmt.Sprintf("len(mipM) = %d\n", len(mipM)))
}
// if len(mti) != count {
// panic(fmt.Sprintf("len(mti) = %d\n", len(mti)))
// }
if len(mit) != count {
panic(fmt.Sprintf("len(mit) = %d\n", len(mit)))
}
// test construction directly
for i := 0; i < count; i++