4 // Copyright 2013 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
8 // Test that maps don't go quadratic for NaNs and other values.
18 // checkLinear asserts that the running time of f(n) is in O(n).
19 // tries is the initial number of iterations.
20 func checkLinear(typ string, tries int, f func(n int)) {
21 // Depending on the machine and OS, this test might be too fast
22 // to measure with accurate enough granularity. On failure,
23 // make it run longer, hoping that the timing granularity
24 // is eventually sufficient.
26 timeF := func(n int) time.Duration {
40 // should be 2x (linear); allow up to 3x
43 fmt.Println(typ, "\t", time.Since(t0))
49 panic(fmt.Sprintf("%s: too slow: %d inserts: %v; %d inserts: %v\n",
67 // NaNs. ~31ms on a 1.6GHz Zeon.
68 checkLinear("NaN", 30000, func(n int) {
69 m := map[float64]int{}
71 for i := 0; i < n; i++ {
75 panic("wrong size map after nan insertion")
79 // ~6ms on a 1.6GHz Zeon.
80 checkLinear("eface", 10000, func(n int) {
81 m := map[interface{}]int{}
82 for i := 0; i < n; i++ {
87 // ~7ms on a 1.6GHz Zeon.
88 // Regression test for CL 119360043.
89 checkLinear("iface", 10000, func(n int) {
91 for i := 0; i < n; i++ {
96 // ~6ms on a 1.6GHz Zeon.
97 checkLinear("int", 10000, func(n int) {
99 for i := 0; i < n; i++ {
104 // ~18ms on a 1.6GHz Zeon.
105 checkLinear("string", 10000, func(n int) {
106 m := map[string]int{}
107 for i := 0; i < n; i++ {
112 // ~6ms on a 1.6GHz Zeon.
113 checkLinear("float32", 10000, func(n int) {
114 m := map[float32]int{}
115 for i := 0; i < n; i++ {
120 // ~6ms on a 1.6GHz Zeon.
121 checkLinear("float64", 10000, func(n int) {
122 m := map[float64]int{}
123 for i := 0; i < n; i++ {
128 // ~22ms on a 1.6GHz Zeon.
129 checkLinear("complex64", 10000, func(n int) {
130 m := map[complex64]int{}
131 for i := 0; i < n; i++ {
132 m[complex(float32(i), float32(i))] = 1
136 // ~32ms on a 1.6GHz Zeon.
137 checkLinear("complex128", 10000, func(n int) {
138 m := map[complex128]int{}
139 for i := 0; i < n; i++ {
140 m[complex(float64(i), float64(i))] = 1