1// run 2 3// Copyright 2023 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. 6 7package main 8 9import ( 10 "math" 11 "reflect" 12) 13 14func main() { 15 for i := 0; i < 100; i++ { 16 f() 17 g() 18 } 19} 20 21func f() { 22 // Allocate map. 23 m := map[float64]int{} 24 // Fill to just before a growth trigger. 25 const N = 13 << 4 // 6.5 * 2 * 2^k 26 for i := 0; i < N; i++ { 27 m[math.NaN()] = i 28 } 29 // Trigger growth. 30 m[math.NaN()] = N 31 32 // Iterate through map. 33 i := 0 34 for range m { 35 if i == 6 { 36 // Partway through iteration, clear the map. 37 clear(m) 38 } else if i > 6 { 39 // If we advance to the next iteration, that's a bug. 40 panic("BAD") 41 } 42 i++ 43 } 44 if len(m) != 0 { 45 panic("clear did not empty the map") 46 } 47} 48 49func g() { 50 // Allocate map. 51 m := map[float64]int{} 52 // Fill to just before a growth trigger. 53 const N = 13 << 4 // 6.5 * 2 * 2^k 54 for i := 0; i < N; i++ { 55 m[math.NaN()] = i 56 } 57 // Trigger growth. 58 m[math.NaN()] = N 59 60 // Iterate through map. 61 i := 0 62 v := reflect.ValueOf(m) 63 iter := v.MapRange() 64 for iter.Next() { 65 if i == 6 { 66 // Partway through iteration, clear the map. 67 v.Clear() 68 } else if i > 6 { 69 // If we advance to the next iteration, that's a bug. 70 panic("BAD") 71 } 72 i++ 73 } 74 if v.Len() != 0 { 75 panic("clear did not empty the map") 76 } 77} 78