1// +build darwin linux
2// run
3
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.
7
8// Test that NaNs in maps don't go quadratic.
9
10package main
11
12import (
13	"fmt"
14	"math"
15	"time"
16)
17
18func main() {
19
20	// Test that NaNs in maps don't go quadratic.
21	t := func(n int) time.Duration {
22		t1 := time.Now()
23		m := map[float64]int{}
24		nan := math.NaN()
25		for i := 0; i < n; i++ {
26			m[nan] = 1
27		}
28		if len(m) != n {
29			panic("wrong size map after nan insertion")
30		}
31		return time.Since(t1)
32	}
33
34	// Depending on the machine and OS, this test might be too fast
35	// to measure with accurate enough granularity. On failure,
36	// make it run longer, hoping that the timing granularity
37	// is eventually sufficient.
38
39	n := 30000 // ~8ms user time on a Mid 2011 MacBook Air (1.8 GHz Core i7)
40	fails := 0
41	for {
42		t1 := t(n)
43		t2 := t(2 * n)
44		// should be 2x (linear); allow up to 3x
45		if t2 < 3*t1 {
46			return
47		}
48		fails++
49		if fails == 6 {
50			panic(fmt.Sprintf("too slow: %d inserts: %v; %d inserts: %v\n", n, t1, 2*n, t2))
51		}
52		if fails < 4 {
53			n *= 2
54		}
55	}
56}
57