summaryrefslogtreecommitdiffstats
path: root/test/mapclear.go
blob: a29f30da743e77cb43f92f9af58eb5def6e41756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// run

// Copyright 2018 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.

// Ensure that range loops over maps with delete statements
// have the requisite side-effects.

package main

import (
	"fmt"
	"os"
)

func checkcleared() {
	m := make(map[byte]int)
	m[1] = 1
	m[2] = 2
	for k := range m {
		delete(m, k)
	}
	l := len(m)
	if want := 0; l != want {
		fmt.Printf("len after map clear = %d want %d\n", l, want)
		os.Exit(1)
	}

	m[0] = 0 // To have non empty map and avoid internal map code fast paths.
	n := 0
	for range m {
		n++
	}
	if want := 1; n != want {
		fmt.Printf("number of keys found = %d want %d\n", n, want)
		os.Exit(1)
	}
}

func checkloopvars() {
	k := 0
	m := make(map[int]int)
	m[42] = 0
	for k = range m {
		delete(m, k)
	}
	if want := 42; k != want {
		fmt.Printf("var after range with side-effect = %d want %d\n", k, want)
		os.Exit(1)
	}
}

func checksideeffects() {
	var x int
	f := func() int {
		x++
		return 0
	}
	m := make(map[int]int)
	m[0] = 0
	m[1] = 1
	for k := range m {
		delete(m, k+f())
	}
	if want := 2; x != want {
		fmt.Printf("var after range with side-effect = %d want %d\n", x, want)
		os.Exit(1)
	}

	var n int
	m = make(map[int]int)
	m[0] = 0
	m[1] = 1
	for k := range m {
		delete(m, k)
		n++
	}
	if want := 2; n != want {
		fmt.Printf("counter for range with side-effect = %d want %d\n", n, want)
		os.Exit(1)
	}
}

func main() {
	checkcleared()
	checkloopvars()
	checksideeffects()
}