From 4596f4541b600c9967ba9d06f477754cf756ef47 Mon Sep 17 00:00:00 2001 From: xing Date: Fri, 3 Feb 2023 22:08:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=86=99=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- safety/safemap_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 safety/safemap_test.go diff --git a/safety/safemap_test.go b/safety/safemap_test.go new file mode 100644 index 0000000..9f0ca17 --- /dev/null +++ b/safety/safemap_test.go @@ -0,0 +1,63 @@ +package safety + +import ( + "fmt" + "github.com/fthvgb1/wp-go/helper/number" + "github.com/fthvgb1/wp-go/taskPools" + "reflect" + "testing" +) + +func TestMap_Load(t *testing.T) { + type args[K comparable] struct { + key K + } + m := NewMap[int, int]() + m.Store(1, 1) + type testCase[K comparable, V any] struct { + name string + m Map[K, V] + args args[K] + wantValue V + wantOk bool + } + tests := []testCase[int, int]{ + { + name: "t1", + m: m, + args: args[int]{1}, + wantValue: 1, + wantOk: true, + }, + } + p := taskPools.NewPools(10) + var a0, a1 []int + for i := 0; i < 5000; i++ { + v := number.Rand(0, 1) + if 1 == v { + a1 = append(a1, 1) + } else { + a0 = append(a0, 0) + } + p.Execute(func() { + if 1 == v { + m.Load(number.Rand(2, 1000)) + } else { + m.Store(number.Rand(2, 1000), number.Rand(1, 1000)) + } + }) + + } + fmt.Println(len(a0), len(a1), m.Len()) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotValue, gotOk := tt.m.Load(tt.args.key) + if !reflect.DeepEqual(gotValue, tt.wantValue) { + t.Errorf("Load() gotValue = %v, want %v", gotValue, tt.wantValue) + } + if gotOk != tt.wantOk { + t.Errorf("Load() gotOk = %v, want %v", gotOk, tt.wantOk) + } + }) + } +}