From 199d3744fcce05c98b246c9c18545d9a32a009a0 Mon Sep 17 00:00:00 2001 From: zhangyunhao Date: Wed, 26 May 2021 17:28:32 +0800 Subject: [PATCH 1/2] fix: skipmap.LoadOrStore return input value --- collection/skipmap/skipmap.go | 2 +- collection/skipmap/skipmap_test.go | 44 ++++++++++++++++++++++++++++++ collection/skipmap/types.go | 38 +++++++++++++------------- 3 files changed, 64 insertions(+), 20 deletions(-) diff --git a/collection/skipmap/skipmap.go b/collection/skipmap/skipmap.go index 11481733..1e1749f0 100644 --- a/collection/skipmap/skipmap.go +++ b/collection/skipmap/skipmap.go @@ -281,7 +281,7 @@ func (s *Int64Map) LoadOrStore(key int64, value interface{}) (actual interface{} loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } diff --git a/collection/skipmap/skipmap_test.go b/collection/skipmap/skipmap_test.go index ab3ae9e9..447a4ee0 100644 --- a/collection/skipmap/skipmap_test.go +++ b/collection/skipmap/skipmap_test.go @@ -21,6 +21,8 @@ import ( "sync" "sync/atomic" "testing" + + "github.com/bytedance/gopkg/lang/fastrand" ) func TestSkipMap(t *testing.T) { @@ -131,6 +133,48 @@ func TestSkipMap(t *testing.T) { if m.Len() != 999 || int(count) != m.Len() { t.Fatal("fail") } + // Correctness 2. + var m1 sync.Map + m2 := NewUint32() + var v1, v2 interface{} + var ok1, ok2 bool + for i := 0; i < 100000; i++ { + rd := fastrand.Uint32n(10) + r1, r2 := fastrand.Uint32n(100), fastrand.Uint32n(100) + if rd == 0 { + m1.Store(r1, r2) + m2.Store(r1, r2) + } else if rd == 1 { + v1, ok1 = m1.LoadAndDelete(r1) + v2, ok2 = m2.LoadAndDelete(r1) + if ok1 != ok2 || v1 != v2 { + t.Fatal(rd, v1, ok1, v2, ok2) + } + } else if rd == 2 { + v1, ok1 = m1.LoadOrStore(r1, r2) + v2, ok2 = m2.LoadOrStore(r1, r2) + if ok1 != ok2 || v1 != v2 { + t.Fatal(rd, v1, ok1, v2, ok2, "input -> ", r1, r2) + } + } else if rd == 3 { + m1.Delete(r1) + m2.Delete(r1) + } else if rd == 4 { + m2.Range(func(key uint32, value interface{}) bool { + v, ok := m1.Load(key) + if !ok || v != value { + t.Fatal(v, ok, key, value) + } + return true + }) + } else { + v1, ok1 = m1.Load(r1) + v2, ok2 = m2.Load(r1) + if ok1 != ok2 || v1 != v2 { + t.Fatal(rd, v1, ok1, v2, ok2) + } + } + } } func TestSkipMapDesc(t *testing.T) { diff --git a/collection/skipmap/types.go b/collection/skipmap/types.go index d80a7dc6..0010dc92 100644 --- a/collection/skipmap/types.go +++ b/collection/skipmap/types.go @@ -281,7 +281,7 @@ func (s *Float32Map) LoadOrStore(key float32, value interface{}) (actual interfa loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -637,7 +637,7 @@ func (s *Float64Map) LoadOrStore(key float64, value interface{}) (actual interfa loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -993,7 +993,7 @@ func (s *Int32Map) LoadOrStore(key int32, value interface{}) (actual interface{} loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -1349,7 +1349,7 @@ func (s *Int16Map) LoadOrStore(key int16, value interface{}) (actual interface{} loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -1705,7 +1705,7 @@ func (s *IntMap) LoadOrStore(key int, value interface{}) (actual interface{}, lo loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -2061,7 +2061,7 @@ func (s *Uint64Map) LoadOrStore(key uint64, value interface{}) (actual interface loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -2417,7 +2417,7 @@ func (s *Uint32Map) LoadOrStore(key uint32, value interface{}) (actual interface loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -2773,7 +2773,7 @@ func (s *Uint16Map) LoadOrStore(key uint16, value interface{}) (actual interface loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -3129,7 +3129,7 @@ func (s *UintMap) LoadOrStore(key uint, value interface{}) (actual interface{}, loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -3485,7 +3485,7 @@ func (s *Float32MapDesc) LoadOrStore(key float32, value interface{}) (actual int loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -3841,7 +3841,7 @@ func (s *Float64MapDesc) LoadOrStore(key float64, value interface{}) (actual int loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -4197,7 +4197,7 @@ func (s *Int32MapDesc) LoadOrStore(key int32, value interface{}) (actual interfa loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -4553,7 +4553,7 @@ func (s *Int16MapDesc) LoadOrStore(key int16, value interface{}) (actual interfa loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -4909,7 +4909,7 @@ func (s *IntMapDesc) LoadOrStore(key int, value interface{}) (actual interface{} loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -5265,7 +5265,7 @@ func (s *Uint64MapDesc) LoadOrStore(key uint64, value interface{}) (actual inter loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -5621,7 +5621,7 @@ func (s *Uint32MapDesc) LoadOrStore(key uint32, value interface{}) (actual inter loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -5977,7 +5977,7 @@ func (s *Uint16MapDesc) LoadOrStore(key uint16, value interface{}) (actual inter loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -6333,7 +6333,7 @@ func (s *UintMapDesc) LoadOrStore(key uint, value interface{}) (actual interface loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } @@ -6689,7 +6689,7 @@ func (s *Int64MapDesc) LoadOrStore(key int64, value interface{}) (actual interfa loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true } From f07715e29067e860dbcd3218ebf37f54d1f13455 Mon Sep 17 00:00:00 2001 From: zhangyunhao Date: Wed, 26 May 2021 17:43:49 +0800 Subject: [PATCH 2/2] fix str map --- collection/skipmap/skipmap_str.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collection/skipmap/skipmap_str.go b/collection/skipmap/skipmap_str.go index 5a375664..af3a5dd9 100644 --- a/collection/skipmap/skipmap_str.go +++ b/collection/skipmap/skipmap_str.go @@ -294,7 +294,7 @@ func (s *StringMap) LoadOrStore(key string, value interface{}) (actual interface loadedval, ok := s.Load(key) if !ok { s.Store(key, value) - return nil, false + return value, false } return loadedval, true }