diff --git a/safe.go b/safe.go index f6e915a..32b2f67 100644 --- a/safe.go +++ b/safe.go @@ -54,7 +54,6 @@ func (s *threadSafeSet) Remove(elems ...interface{}) { s.mu.Lock() defer s.mu.Unlock() s.unsafe.Remove(elems...) - } func (s *threadSafeSet) Copy() Set { @@ -63,7 +62,6 @@ func (s *threadSafeSet) Copy() Set { return &threadSafeSet{ unsafe: (s.unsafe.Copy()).(*set), } - } func (s *threadSafeSet) Len() int { @@ -95,11 +93,9 @@ func (s *threadSafeSet) ContainsAny(elems ...interface{}) bool { s.mu.RLock() defer s.mu.RUnlock() return s.unsafe.ContainsAny(elems...) - } func (s *threadSafeSet) Equal(b Set) bool { - safeb, ok := b.(*threadSafeSet) if ok { safeb.mu.RLock() @@ -122,7 +118,6 @@ func (s *threadSafeSet) IsSubsetOf(b Set) bool { defer s.mu.RUnlock() return s.unsafe.IsSubsetOf(b) - } func (s *threadSafeSet) IsSupersetOf(b Set) bool { @@ -135,7 +130,6 @@ func (s *threadSafeSet) IsSupersetOf(b Set) bool { defer s.mu.RUnlock() return s.unsafe.IsSupersetOf(b) - } func (s *threadSafeSet) String() string { @@ -151,7 +145,6 @@ func (s *threadSafeSet) ToThreadUnsafe() Set { func (s *threadSafeSet) ToThreadSafe() Set { return s - } func (s *threadSafeSet) Diff(b Set) Set { diff --git a/safe_test.go b/safe_test.go index a24bef7..07ebd59 100644 --- a/safe_test.go +++ b/safe_test.go @@ -126,13 +126,13 @@ func Test_threadSafeSet_Equal(t *testing.T) { }, } var wg sync.WaitGroup - for _, tt := range tests { + for i := range tests { + tt := tests[i] wg.Add(1) go func(tt test) { defer wg.Done() if got := s.Equal(tt.b); got != tt.want { t.Errorf("threadSafeSet.Equal() == %v, want %v", got, tt.b) - } }(tt) } @@ -163,19 +163,18 @@ func Test_threadSafeSet_IsSubsetOf(t *testing.T) { }, } var wg sync.WaitGroup - for _, tt := range tests { + for i := range tests { + tt := tests[i] wg.Add(1) go func(tt test) { defer wg.Done() if got := s.IsSubsetOf(tt.b); got != tt.want { t.Errorf("threadSafeSet.IsSubsetOf() == %v, want %v", got, tt.b) - } }(tt) } wg.Wait() - } func Test_threadSafeSet_IsSupersetOf(t *testing.T) { @@ -205,13 +204,13 @@ func Test_threadSafeSet_IsSupersetOf(t *testing.T) { }, } var wg sync.WaitGroup - for _, tt := range tests { + for i := range tests { + tt := tests[i] wg.Add(1) go func(tt test) { defer wg.Done() if got := s.IsSupersetOf(tt.b); got != tt.want { t.Errorf("threadSafeSet.IsSupersetOf() == %v, want %v", got, tt.want) - } }(tt) } @@ -231,7 +230,8 @@ func Test_threadSafeSet_ToThreadUnsafe_And_Safe(t *testing.T) { true, }, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { unsafe := tt.a.ToThreadUnsafe() if _, got := unsafe.(*set); got != tt.want { @@ -242,7 +242,6 @@ func Test_threadSafeSet_ToThreadUnsafe_And_Safe(t *testing.T) { t.Errorf("threadSafeSet.ToThreadUnsafe() = %v, want %v", got, tt.want) } }) - } } @@ -264,7 +263,8 @@ func Test_threadSafeSet_Diff(t *testing.T) { }, } var wg sync.WaitGroup - for _, tt := range tests { + for i := range tests { + tt := tests[i] wg.Add(1) go func(tt test) { defer wg.Done() @@ -275,7 +275,6 @@ func Test_threadSafeSet_Diff(t *testing.T) { } wg.Wait() - } func Test_threadSafeSet_SymmetricDiff(t *testing.T) { @@ -296,7 +295,8 @@ func Test_threadSafeSet_SymmetricDiff(t *testing.T) { }, } var wg sync.WaitGroup - for _, tt := range tests { + for i := range tests { + tt := tests[i] wg.Add(1) go func(tt test) { defer wg.Done() @@ -307,7 +307,6 @@ func Test_threadSafeSet_SymmetricDiff(t *testing.T) { } wg.Wait() - } func Test_threadSafeSet_Unite(t *testing.T) { @@ -328,7 +327,8 @@ func Test_threadSafeSet_Unite(t *testing.T) { }, } var wg sync.WaitGroup - for _, tt := range tests { + for i := range tests { + tt := tests[i] wg.Add(1) go func(tt test) { defer wg.Done() @@ -357,7 +357,8 @@ func Test_threadSafeSet_Intersect(t *testing.T) { }, } var wg sync.WaitGroup - for _, tt := range tests { + for i := range tests { + tt := tests[i] wg.Add(1) go func(tt test) { defer wg.Done() diff --git a/set.go b/set.go index e20afb6..808e27d 100644 --- a/set.go +++ b/set.go @@ -61,7 +61,7 @@ func (s *set) Extend(b interface{}) error { s2 := setb.ToThreadUnsafe().(*set) s2.Range(func(_ int, elem interface{}) bool { - s.Add(elem) + s.Add(elem) //nolint:errcheck return true }) diff --git a/set_test.go b/set_test.go index bec5cc7..b2cb91a 100644 --- a/set_test.go +++ b/set_test.go @@ -22,9 +22,11 @@ import ( func Test_set_Add(t *testing.T) { s := newSet() + //nolint type temp struct { test int } + //nolint type temp2 struct { test int m map[string]int @@ -45,7 +47,8 @@ func Test_set_Add(t *testing.T) { {"add slice error", []int{1, 2, 3}, true}, {"add map error", map[string]int{"1": 2}, true}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if err := s.Add(tt.elem); (err != nil) != tt.wantErr { t.Errorf("set.Add() error = %v, wantErr %v", err, tt.wantErr) @@ -70,7 +73,8 @@ func Test_set_Extend(t *testing.T) { {"extend err", "test", true}, {"extend nil", nil, false}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if err := s.Extend(tt.b); (err != nil) != tt.wantErr { t.Errorf("set.Extend() error = %v, wantErr %v", err, tt.wantErr) @@ -92,7 +96,8 @@ func Test_set_Remove(t *testing.T) { {"remove float", 3.0}, {"remove missing", 1}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { s.Remove(tt.elem) if s.Contains(tt.elem) != false { @@ -117,7 +122,8 @@ func Test_set_Contains(t *testing.T) { {"contains missing", 3, false}, {"contains missing", []int{1, 2, 3}, false}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := s.Contains(tt.elem); got != tt.want { t.Errorf("set.Contains() = %v, want %v", got, tt.want) @@ -127,7 +133,6 @@ func Test_set_Contains(t *testing.T) { } func Test_set_Equal(t *testing.T) { - tests := []struct { name string a *set @@ -147,7 +152,8 @@ func Test_set_Equal(t *testing.T) { false, }, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.a.Equal(tt.b); got != tt.want { t.Errorf("set.Equal() = %v, want %v", got, tt.want) @@ -157,7 +163,6 @@ func Test_set_Equal(t *testing.T) { } func Test_set_IsSubsetOf(t *testing.T) { - tests := []struct { name string a Set @@ -183,7 +188,8 @@ func Test_set_IsSubsetOf(t *testing.T) { true, }, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.a.IsSubsetOf(tt.b); got != tt.want { t.Errorf("set.IsSubsetOf() = %v, want %v", got, tt.want) @@ -193,7 +199,6 @@ func Test_set_IsSubsetOf(t *testing.T) { } func Test_set_IsSupersetOf(t *testing.T) { - tests := []struct { name string a Set @@ -219,7 +224,8 @@ func Test_set_IsSupersetOf(t *testing.T) { false, }, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.a.IsSupersetOf(tt.b); got != tt.want { t.Errorf("set.IsSupersetOf() = %v, want %v", got, tt.want) @@ -240,7 +246,8 @@ func Test_set_ToThreadUnsafe_And_Safe(t *testing.T) { true, }, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { unsafe := tt.a.ToThreadUnsafe() if _, got := unsafe.(*set); got != tt.want { @@ -280,7 +287,8 @@ func Test_set_Diff(t *testing.T) { newSet(), }, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.a.Diff(tt.b); !got.Equal(tt.want) { t.Errorf("set.Diff() = %v, want %v", got, tt.want) @@ -315,7 +323,8 @@ func Test_set_SymmetricDiff(t *testing.T) { newSet("2", 3, 4), }, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.a.SymmetricDiff(tt.b); !got.Equal(tt.want) { t.Errorf("set.SymmetricDiff() = %v, want %v", got, tt.want) @@ -350,7 +359,8 @@ func Test_set_Unite(t *testing.T) { newSet(1, "2", 3, 4), }, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.a.Unite(tt.b); !got.Equal(tt.want) { t.Errorf("set.Unite() = %v, want %v", got, tt.want) @@ -385,7 +395,8 @@ func Test_set_Intersect(t *testing.T) { newSet(1), }, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.a.Intersect(tt.b); !got.Equal(tt.want) { t.Errorf("set.Intersect() = %v, want %v", got, tt.want) @@ -404,7 +415,8 @@ func Test_set_Range(t *testing.T) { newSet(1, 2, 3, 4), }, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { seen := make([]interface{}, 0, tt.a.Len()) tt.a.Range(func(index int, elem interface{}) bool { diff --git a/set_to_slice_test.go b/set_to_slice_test.go index 44bb147..471f19b 100644 --- a/set_to_slice_test.go +++ b/set_to_slice_test.go @@ -30,7 +30,8 @@ func Test_set_ToStrings(t *testing.T) { }{ {"", newSet(1, 2, "str", "str1"), []string{"str", "str1"}}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { got := tt.s.ToStrings() sort.Strings(got) @@ -50,7 +51,8 @@ func Test_set_ToInts(t *testing.T) { }{ {"", newSet(1, 2, "str", "str1"), []int{1, 2}}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { got := tt.s.ToInts() sort.Ints(got) diff --git a/typed.go b/typed.go index 77f7e77..337152f 100644 --- a/typed.go +++ b/typed.go @@ -18,7 +18,6 @@ package goset import ( "fmt" - "sync" "sync/atomic" ) @@ -108,17 +107,17 @@ func typedAssert(elem interface{}) typed { return typedAny } -func concurrent(f func(typed)) { - wg := sync.WaitGroup{} - for _, t := range allTyped { - wg.Add(1) - go func(t typed) { - defer wg.Done() - f(t) - }(t) - } - wg.Wait() -} +// func concurrent(f func(typed)) { +// wg := sync.WaitGroup{} +// for _, t := range allTyped { +// wg.Add(1) +// go func(t typed) { +// defer wg.Done() +// f(t) +// }(t) +// } +// wg.Wait() +// } func synchronous(f func(typed)) { for _, t := range allTyped { @@ -136,7 +135,7 @@ func newTypedSetGroup(elems ...interface{}) typedSetGroup { s.store(typedString, newStrings()) s.store(typedAny, newAny()) - s.Add(elems...) + s.Add(elems...) //nolint:errcheck return s } @@ -145,8 +144,7 @@ func (s typedSetGroup) store(t typed, in typedSet) { } func (s typedSetGroup) load(t typed) typedSet { - v, _ := s[t] - return v.(typedSet) + return s[t].(typedSet) } func (s typedSetGroup) typedSetFor(elem interface{}) typedSet { diff --git a/typed_test.go b/typed_test.go index 1766aea..7e99b9d 100644 --- a/typed_test.go +++ b/typed_test.go @@ -36,7 +36,8 @@ func Test_typedAssert(t *testing.T) { {"", "str", typedString}, {"", Empty{}, typedAny}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := typedAssert(tt.args); got != tt.want { t.Errorf("typedAssert() = %v, want %v", got, tt.want) @@ -53,7 +54,8 @@ func Test_typedSet_Add(t *testing.T) { }{ {"", newTypedSetGroup(), interfaces{1, "str", 1.2, Empty{}}}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { tt.s.Add(tt.args...) if !tt.s.ContainsAll(tt.args...) { @@ -72,7 +74,8 @@ func Test_typedSet_Remove(t *testing.T) { }{ {"", newTypedSetGroup(1, 2, "str", 1.2, Empty{}), interfaces{2, "str", 1.2}, interfaces{1, Empty{}}}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { tt.s.Remove(tt.args...) if !tt.s.ContainsAll(tt.want...) { @@ -95,7 +98,8 @@ func Test_typedSet_Contains(t *testing.T) { {"", newTypedSetGroup(1, 2, "str", 1.2, Empty{}), 3, false}, {"", newTypedSetGroup(1, 2, "str", 1.2, Empty{}), "s", false}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.s.Contains(tt.args); got != tt.want { t.Errorf("typedSet.Contains() = %v, want %v", got, tt.want) @@ -118,7 +122,8 @@ func Test_typedSet_Contains_Any(t *testing.T) { {"", newTypedSetGroup(1, 2, "str", 1.2, Empty{}), interfaces{3}, false}, {"", newTypedSetGroup(1, 2, "str", 1.2, Empty{}), interfaces{2.1}, false}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.s.ContainsAny(tt.args...); got != tt.want { t.Errorf("typedSet.ContainsAny() = %v, want %v", got, tt.want) @@ -135,7 +140,8 @@ func Test_typedSet_Copy(t *testing.T) { }{ {"", newTypedSetGroup(1, 2, "str", 1.2, Empty{}), newTypedSetGroup(1, 2, "str", 1.2, Empty{})}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.s.Copy(); !reflect.DeepEqual(got, tt.want) { t.Errorf("typedSet.Copy() = %v, want %v", got, tt.want) @@ -152,7 +158,8 @@ func Test_typedSet_Len(t *testing.T) { }{ {"", newTypedSetGroup(1, 2, "str", 1.2, Empty{}), 5}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.s.Len(); got != tt.want { t.Errorf("typedSet.Len() = %v, want %v", got, tt.want) @@ -174,7 +181,8 @@ func Test_typedSet_Equal(t *testing.T) { // TODO: Add test cases. } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.s.Equal(tt.args); got != tt.want { t.Errorf("typedSet.Equal() = %v, want %v", got, tt.want) @@ -195,7 +203,8 @@ func Test_typedSet_IsSubsetOf(t *testing.T) { {"", newTypedSetGroup(1, "str", 1.2, Empty{}), newTypedSetGroup(1, 2, "str", 1.2, Empty{}), true}, {"", newTypedSetGroup(1, 2, "str", 1.2, Empty{}), newTypedSetGroup(1, "str", 1.2, Empty{}), false}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.s.IsSubsetOf(tt.args); got != tt.want { t.Errorf("typedSet.IsSubsetOf() = %v, want %v", got, tt.want) @@ -239,7 +248,6 @@ func Test_typedSet_Range(t *testing.T) { if !reflect.DeepEqual(got, want) { t.Errorf("typedSet.Range() = %v, want %v", got, want) } - } func Test_typedSet_Diff(t *testing.T) { @@ -253,7 +261,8 @@ func Test_typedSet_Diff(t *testing.T) { {"", newTypedSetGroup(1, 2, "str", "str2", 1.2, Empty{}), newTypedSetGroup(2, 3, "str2", Empty{}), newTypedSetGroup(1, "str", 1.2)}, {"", newTypedSetGroup(1, 2, "str", "str2", 1.2, Empty{}), newTypedSetGroup(), newTypedSetGroup(1, 2, "str", "str2", 1.2, Empty{})}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.s.Diff(tt.args); !reflect.DeepEqual(got, tt.want) { t.Errorf("typedSet.Diff() = %v, want %v", got, tt.want) @@ -273,7 +282,8 @@ func Test_typedSet_SymmetricDiff(t *testing.T) { {"", newTypedSetGroup(1, 2, "str", "str2", 1.2, Empty{}), newTypedSetGroup(2, 3, "str2", Empty{}), newTypedSetGroup(1, 3, "str", 1.2)}, {"", newTypedSetGroup(1, 2, "str", "str2", 1.2, Empty{}), newTypedSetGroup(), newTypedSetGroup(1, 2, "str", "str2", 1.2, Empty{})}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.s.SymmetricDiff(tt.args); !reflect.DeepEqual(got, tt.want) { t.Errorf("typedSet.SymmetricDiff() = %v, want %v", got, tt.want) @@ -293,7 +303,8 @@ func Test_typedSet_Unite(t *testing.T) { {"", newTypedSetGroup(1, "str", 1.2, Empty{}), newTypedSetGroup(2, 3, "str2", Empty{}), newTypedSetGroup(1, 2, 3, "str", "str2", 1.2, Empty{})}, {"", newTypedSetGroup(1, 2, "str", "str2", 1.2, Empty{}), newTypedSetGroup(), newTypedSetGroup(1, 2, "str", "str2", 1.2, Empty{})}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.s.Unite(tt.args); !reflect.DeepEqual(got, tt.want) { t.Errorf("typedSet.Unite() = %v, want %v", got, tt.want) @@ -313,7 +324,8 @@ func Test_typedSet_Intersect(t *testing.T) { {"", newTypedSetGroup(1, "str", 1.2, Empty{}), newTypedSetGroup(2, 3, "str2", Empty{}), newTypedSetGroup(Empty{})}, {"", newTypedSetGroup(1, 2, "str", "str2", 1.2, Empty{}), newTypedSetGroup(), newTypedSetGroup()}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.s.Intersect(tt.args); !reflect.DeepEqual(got, tt.want) { t.Errorf("typedSet.Intersect() = %v, want %v", got, tt.want) @@ -332,7 +344,8 @@ func Test_typedSet_Elements(t *testing.T) { {"", newTypedSetGroup(1, "str", 1.2, Empty{}), interfaces{1, "str", 1.2, Empty{}}}, {"", newTypedSetGroup(1, 2, "str", "str2", 1.2, Empty{}), interfaces{1, 2, "str", "str2", 1.2, Empty{}}}, } - for _, tt := range tests { + for i := range tests { + tt := tests[i] t.Run(tt.name, func(t *testing.T) { if got := tt.s.Elements(); !tt.s.ContainsAll(got...) { t.Errorf("typedSet.Elements() = %v, want %v", got, tt.want)