diff --git a/container/gset/gset_any_set.go b/container/gset/gset_any_set.go index 444745a9c..03807efbf 100644 --- a/container/gset/gset_any_set.go +++ b/container/gset/gset_any_set.go @@ -518,9 +518,9 @@ func (set *Set) DeepCopy() interface{} { } set.mu.RLock() defer set.mu.RUnlock() - data := make(map[interface{}]struct{}, len(set.data)) - for k, v := range set.data { - data[k] = v + data := make([]interface{}, 0) + for k, _ := range set.data { + data = append(data, k) } return NewFrom(data, set.mu.IsSafe()) } diff --git a/container/gset/gset_z_unit_any_test.go b/container/gset/gset_z_unit_any_test.go index f6e4835a3..f9ce95703 100644 --- a/container/gset/gset_z_unit_any_test.go +++ b/container/gset/gset_z_unit_any_test.go @@ -126,11 +126,16 @@ func TestSet_Equal(t *testing.T) { s1 := gset.NewSet() s2 := gset.NewSet() s3 := gset.NewSet() + s4 := gset.NewSet() s1.Add(1, 2, 3) s2.Add(1, 2, 3) s3.Add(1, 2, 3, 4) + s4.Add(4, 5, 6) t.Assert(s1.Equal(s2), true) t.Assert(s1.Equal(s3), false) + t.Assert(s1.Equal(s4), false) + s5 := s1 + t.Assert(s1.Equal(s5), true) }) } @@ -147,6 +152,9 @@ func TestSet_IsSubsetOf(t *testing.T) { t.Assert(s1.IsSubsetOf(s3), true) t.Assert(s2.IsSubsetOf(s1), false) t.Assert(s3.IsSubsetOf(s2), false) + + s4 := s1 + t.Assert(s1.IsSubsetOf(s4), true) }) } @@ -175,6 +183,13 @@ func TestSet_Diff(t *testing.T) { t.Assert(s3.Contains(2), true) t.Assert(s3.Contains(3), false) t.Assert(s3.Contains(4), false) + + s4 := s1 + s5 := s1.Diff(s2, s4) + t.Assert(s5.Contains(1), true) + t.Assert(s5.Contains(2), true) + t.Assert(s5.Contains(3), false) + t.Assert(s5.Contains(4), false) }) } @@ -247,6 +262,10 @@ func TestSet_Join(t *testing.T) { t.Assert(strings.Contains(str1, `\c`), true) t.Assert(strings.Contains(str1, `a`), true) }) + gtest.C(t, func(t *gtest.T) { + s1 := gset.Set{} + t.Assert(s1.Join(","), "") + }) } func TestSet_String(t *testing.T) { @@ -257,6 +276,13 @@ func TestSet_String(t *testing.T) { t.Assert(strings.Contains(str1, "["), true) t.Assert(strings.Contains(str1, "]"), true) t.Assert(strings.Contains(str1, "a2"), true) + + s1 = nil + t.Assert(s1.String(), "") + + s2 := gset.New() + s2.Add(1) + t.Assert(s2.String(), "[1]") }) } @@ -285,6 +311,7 @@ func TestSet_Sum(t *testing.T) { func TestSet_Pop(t *testing.T) { gtest.C(t, func(t *gtest.T) { s := gset.New(true) + t.Assert(s.Pop(), nil) s.Add(1, 2, 3, 4) t.Assert(s.Size(), 4) t.AssertIN(s.Pop(), []int{1, 2, 3, 4}) @@ -354,6 +381,7 @@ func TestSet_AddIfNotExist(t *testing.T) { t.Assert(s.AddIfNotExist(2), true) t.Assert(s.Contains(2), true) t.Assert(s.AddIfNotExist(2), false) + t.Assert(s.AddIfNotExist(nil), false) t.Assert(s.Contains(2), true) }) } @@ -370,6 +398,7 @@ func TestSet_AddIfNotExistFunc(t *testing.T) { t.Assert(s.Contains(2), true) t.Assert(s.AddIfNotExistFunc(2, func() bool { return true }), false) t.Assert(s.Contains(2), true) + t.Assert(s.AddIfNotExistFunc(nil, func() bool { return false }), false) }) gtest.C(t, func(t *gtest.T) { s := gset.New(true) @@ -386,6 +415,10 @@ func TestSet_AddIfNotExistFunc(t *testing.T) { s.Add(1) wg.Wait() }) + gtest.C(t, func(t *gtest.T) { + s := gset.Set{} + t.Assert(s.AddIfNotExistFunc(1, func() bool { return true }), true) + }) } func TestSet_Walk(t *testing.T) { @@ -424,6 +457,12 @@ func TestSet_AddIfNotExistFuncLock(t *testing.T) { }() wg.Wait() }) + gtest.C(t, func(t *gtest.T) { + s := gset.New(true) + t.Assert(s.AddIfNotExistFuncLock(nil, func() bool { return true }), false) + s1 := gset.Set{} + t.Assert(s1.AddIfNotExistFuncLock(1, func() bool { return true }), true) + }) } func TestSet_UnmarshalValue(t *testing.T) { @@ -462,3 +501,18 @@ func TestSet_UnmarshalValue(t *testing.T) { t.Assert(v.Set.Contains("k4"), false) }) } + +func TestSet_DeepCopy(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + set := gset.New() + set.Add(1, 2, 3) + + copySet := set.DeepCopy().(*gset.Set) + copySet.Add(4) + t.AssertNE(set.Size(), copySet.Size()) + t.AssertNE(set.String(), copySet.String()) + + set = nil + t.AssertNil(set.DeepCopy()) + }) +} diff --git a/container/gset/gset_z_unit_int_test.go b/container/gset/gset_z_unit_int_test.go index 8a7086a50..a077a0ad7 100644 --- a/container/gset/gset_z_unit_int_test.go +++ b/container/gset/gset_z_unit_int_test.go @@ -106,11 +106,16 @@ func TestIntSet_Equal(t *testing.T) { s1 := gset.NewIntSet() s2 := gset.NewIntSet() s3 := gset.NewIntSet() + s4 := gset.NewIntSet() s1.Add(1, 2, 3) s2.Add(1, 2, 3) s3.Add(1, 2, 3, 4) + s4.Add(4, 5, 6) t.Assert(s1.Equal(s2), true) t.Assert(s1.Equal(s3), false) + t.Assert(s1.Equal(s4), false) + s5 := s1 + t.Assert(s1.Equal(s5), true) }) } @@ -127,6 +132,9 @@ func TestIntSet_IsSubsetOf(t *testing.T) { t.Assert(s1.IsSubsetOf(s3), true) t.Assert(s2.IsSubsetOf(s1), false) t.Assert(s3.IsSubsetOf(s2), false) + + s4 := s1 + t.Assert(s1.IsSubsetOf(s4), true) }) } @@ -155,6 +163,13 @@ func TestIntSet_Diff(t *testing.T) { t.Assert(s3.Contains(2), true) t.Assert(s3.Contains(3), false) t.Assert(s3.Contains(4), false) + + s4 := s1 + s5 := s1.Diff(s2, s4) + t.Assert(s5.Contains(1), true) + t.Assert(s5.Contains(2), true) + t.Assert(s5.Contains(3), false) + t.Assert(s5.Contains(4), false) }) } @@ -212,6 +227,7 @@ func TestIntSet_Merge(t *testing.T) { func TestIntSet_Join(t *testing.T) { gtest.C(t, func(t *gtest.T) { s1 := gset.NewIntSet() + t.Assert(s1.Join(","), "") s1.Add(1, 2, 3) s3 := s1.Join(",") t.Assert(strings.Contains(s3, "1"), true) @@ -230,6 +246,8 @@ func TestIntSet_String(t *testing.T) { t.Assert(strings.Contains(s3, "1"), true) t.Assert(strings.Contains(s3, "2"), true) t.Assert(strings.Contains(s3, "3"), true) + s1 = nil + t.Assert(s1.String(), "") }) } @@ -248,6 +266,7 @@ func TestIntSet_Sum(t *testing.T) { func TestIntSet_Pop(t *testing.T) { gtest.C(t, func(t *gtest.T) { s := gset.NewIntSet() + t.Assert(s.Pop(), 0) s.Add(4, 2, 3) t.Assert(s.Size(), 3) t.AssertIN(s.Pop(), []int{4, 2, 3}) @@ -291,6 +310,10 @@ func TestIntSet_AddIfNotExist(t *testing.T) { t.Assert(s.AddIfNotExist(2), false) t.Assert(s.Contains(2), true) }) + gtest.C(t, func(t *gtest.T) { + s := gset.IntSet{} + t.Assert(s.AddIfNotExist(1), true) + }) } func TestIntSet_AddIfNotExistFunc(t *testing.T) { @@ -321,6 +344,10 @@ func TestIntSet_AddIfNotExistFunc(t *testing.T) { s.Add(1) wg.Wait() }) + gtest.C(t, func(t *gtest.T) { + s := gset.IntSet{} + t.Assert(s.AddIfNotExistFunc(1, func() bool { return true }), true) + }) } func TestIntSet_AddIfNotExistFuncLock(t *testing.T) { @@ -346,6 +373,10 @@ func TestIntSet_AddIfNotExistFuncLock(t *testing.T) { }() wg.Wait() }) + gtest.C(t, func(t *gtest.T) { + s := gset.IntSet{} + t.Assert(s.AddIfNotExistFuncLock(1, func() bool { return true }), true) + }) } func TestIntSet_Json(t *testing.T) { @@ -426,3 +457,18 @@ func TestIntSet_UnmarshalValue(t *testing.T) { t.Assert(v.Set.Contains(4), false) }) } + +func TestIntSet_DeepCopy(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + set := gset.NewIntSet() + set.Add(1, 2, 3) + + copySet := set.DeepCopy().(*gset.IntSet) + copySet.Add(4) + t.AssertNE(set.Size(), copySet.Size()) + t.AssertNE(set.String(), copySet.String()) + + set = nil + t.AssertNil(set.DeepCopy()) + }) +} diff --git a/container/gset/gset_z_unit_str_test.go b/container/gset/gset_z_unit_str_test.go index ed41308da..310788415 100644 --- a/container/gset/gset_z_unit_str_test.go +++ b/container/gset/gset_z_unit_str_test.go @@ -69,6 +69,7 @@ func TestStrSet_ContainsI(t *testing.T) { t.Assert(s.Contains("A"), false) t.Assert(s.Contains("a"), true) t.Assert(s.ContainsI("A"), true) + t.Assert(s.ContainsI("d"), false) }) } @@ -116,11 +117,16 @@ func TestStrSet_Equal(t *testing.T) { s1 := gset.NewStrSet() s2 := gset.NewStrSet() s3 := gset.NewStrSet() + s4 := gset.NewStrSet() s1.Add("1", "2", "3") s2.Add("1", "2", "3") s3.Add("1", "2", "3", "4") + s4.Add("4", "5", "6") t.Assert(s1.Equal(s2), true) t.Assert(s1.Equal(s3), false) + t.Assert(s1.Equal(s4), false) + s5 := s1 + t.Assert(s1.Equal(s5), true) }) } @@ -137,6 +143,9 @@ func TestStrSet_IsSubsetOf(t *testing.T) { t.Assert(s1.IsSubsetOf(s3), true) t.Assert(s2.IsSubsetOf(s1), false) t.Assert(s3.IsSubsetOf(s2), false) + + s4 := s1 + t.Assert(s1.IsSubsetOf(s4), true) }) } @@ -165,6 +174,13 @@ func TestStrSet_Diff(t *testing.T) { t.Assert(s3.Contains("2"), true) t.Assert(s3.Contains("3"), false) t.Assert(s3.Contains("4"), false) + + s4 := s1 + s5 := s1.Diff(s2, s4) + t.Assert(s5.Contains("1"), true) + t.Assert(s5.Contains("2"), true) + t.Assert(s5.Contains("3"), false) + t.Assert(s5.Contains("4"), false) }) } @@ -239,6 +255,7 @@ func TestStrSet_Join(t *testing.T) { gtest.C(t, func(t *gtest.T) { s1 := gset.NewStrSet() + t.Assert(s1.Join(","), "") s1.Add("a", `"b"`, `\c`) str1 := s1.Join(",") t.Assert(strings.Contains(str1, `"b"`), true) @@ -253,6 +270,8 @@ func TestStrSet_String(t *testing.T) { str1 := s1.String() t.Assert(strings.Contains(str1, "b"), true) t.Assert(strings.Contains(str1, "d"), false) + s1 = nil + t.Assert(s1.String(), "") }) gtest.C(t, func(t *gtest.T) { @@ -300,6 +319,9 @@ func TestStrSet_Pop(t *testing.T) { t.Assert(s.Size(), 3) t.AssertIN(s.Pop(), a) t.Assert(s.Size(), 2) + + s1 := gset.StrSet{} + t.Assert(s1.Pop(), "") }) } @@ -337,6 +359,10 @@ func TestStrSet_AddIfNotExist(t *testing.T) { t.Assert(s.AddIfNotExist("2"), false) t.Assert(s.Contains("2"), true) }) + gtest.C(t, func(t *gtest.T) { + s := gset.StrSet{} + t.Assert(s.AddIfNotExist("1"), true) + }) } func TestStrSet_AddIfNotExistFunc(t *testing.T) { @@ -367,6 +393,10 @@ func TestStrSet_AddIfNotExistFunc(t *testing.T) { s.Add("1") wg.Wait() }) + gtest.C(t, func(t *gtest.T) { + s := gset.StrSet{} + t.Assert(s.AddIfNotExistFunc("1", func() bool { return true }), true) + }) } func TestStrSet_AddIfNotExistFuncLock(t *testing.T) { @@ -392,6 +422,10 @@ func TestStrSet_AddIfNotExistFuncLock(t *testing.T) { }() wg.Wait() }) + gtest.C(t, func(t *gtest.T) { + s := gset.StrSet{} + t.Assert(s.AddIfNotExistFuncLock("1", func() bool { return true }), true) + }) } func TestStrSet_Json(t *testing.T) { @@ -477,3 +511,18 @@ func TestStrSet_UnmarshalValue(t *testing.T) { t.Assert(v.Set.Contains("4"), false) }) } + +func TestStrSet_DeepCopy(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + set := gset.NewStrSet() + set.Add("1", "2", "3") + + copySet := set.DeepCopy().(*gset.StrSet) + copySet.Add("4") + t.AssertNE(set.Size(), copySet.Size()) + t.AssertNE(set.String(), copySet.String()) + + set = nil + t.AssertNil(set.DeepCopy()) + }) +}