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