aboutsummaryrefslogtreecommitdiff
path: root/test/clear.go
diff options
context:
space:
mode:
authorCuong Manh Le <cuong.manhle.vn@gmail.com>2022-11-25 16:11:45 +0700
committerGopher Robot <gobot@golang.org>2023-01-31 19:43:07 +0000
commitb89a840d6572c97a80ac78462b03122b83bc84e9 (patch)
tree041d893fb10b864eeaa68b394b69d16e08fee8b7 /test/clear.go
parentbb4ea80befa5f91aa50ccb170e0abdcbc0d01627 (diff)
downloadgo-b89a840d6572c97a80ac78462b03122b83bc84e9.tar.gz
go-b89a840d6572c97a80ac78462b03122b83bc84e9.zip
cmd/compile: add clear(x) builtin
To clear map, and zero content of slice. Updates #56351 Change-Id: I5f81dfbc465500f5acadaf2c6beb9b5f0d2c4045 Reviewed-on: https://go-review.googlesource.com/c/go/+/453395 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Keith Randall <khr@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Keith Randall <khr@google.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Diffstat (limited to 'test/clear.go')
-rw-r--r--test/clear.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/test/clear.go b/test/clear.go
new file mode 100644
index 0000000000..60ee4ecf85
--- /dev/null
+++ b/test/clear.go
@@ -0,0 +1,47 @@
+// run
+
+// Copyright 2022 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.
+
+package main
+
+import "math"
+
+func checkClearSlice() {
+ s := []int{1, 2, 3}
+ clear(s)
+ for i := range s {
+ if s[i] != 0 {
+ panic("clear not zeroing slice elem")
+ }
+ }
+
+ clear([]int{})
+}
+
+func checkClearMap() {
+ m1 := make(map[int]int)
+ m1[0] = 0
+ m1[1] = 1
+ clear(m1)
+ if len(m1) != 0 {
+ panic("m1 is not cleared")
+ }
+
+ // map contains NaN keys is also cleared.
+ m2 := make(map[float64]int)
+ m2[math.NaN()] = 1
+ m2[math.NaN()] = 1
+ clear(m2)
+ if len(m2) != 0 {
+ panic("m2 is not cleared")
+ }
+
+ clear(map[int]int{})
+}
+
+func main() {
+ checkClearSlice()
+ checkClearMap()
+}