From 8cf68aaa51ef0f7b84df48f15f1b64e066921b96 Mon Sep 17 00:00:00 2001 From: Ben Krieger Date: Thu, 24 Oct 2024 23:57:17 -0400 Subject: [PATCH] reflect: add TestClearMap --- src/reflect/value_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/reflect/value_test.go b/src/reflect/value_test.go index 9cc1f53c48..add40b3f82 100644 --- a/src/reflect/value_test.go +++ b/src/reflect/value_test.go @@ -765,6 +765,32 @@ func TestClearSlice(t *testing.T) { } } +func TestClearMap(t *testing.T) { + for _, test := range []struct { + m any + expect any + }{ + { + m: map[int]bool{1: true, 2: false, 3: true}, + expect: map[int]bool{}, + }, + { + m: map[string][]byte{"hello": []byte("world")}, + expect: map[string][]byte{}, + }, + } { + v := ValueOf(test.m) + + v.Clear() + if len := v.Len(); len != 0 { + t.Errorf("Clear(map) should set len to 0, got %d", len) + } + if !DeepEqual(test.m, test.expect) { + t.Errorf("Clear(map) got %v, expected %v", test.m, test.expect) + } + } +} + func TestIssue4040(t *testing.T) { var value interface{} = uint16(0)