From 442c658be0dc96e4a894e67d19e0cd86494ef063 Mon Sep 17 00:00:00 2001 From: jroam Date: Mon, 10 Jun 2019 23:38:19 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E5=A2=9E=E5=8A=A0gset=E7=9A=84=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 覆盖率达到85.1% --- g/container/gset/gset_z_unit_int_test.go | 284 +++++++++-------- g/container/gset/gset_z_unit_string_test.go | 276 +++++++++-------- g/container/gset/gset_z_unit_test.go | 320 ++++++++++++-------- 3 files changed, 517 insertions(+), 363 deletions(-) diff --git a/g/container/gset/gset_z_unit_int_test.go b/g/container/gset/gset_z_unit_int_test.go index 0570652ac..51e1b4a88 100644 --- a/g/container/gset/gset_z_unit_int_test.go +++ b/g/container/gset/gset_z_unit_int_test.go @@ -9,152 +9,194 @@ package gset_test import ( - "github.com/gogf/gf/g/container/garray" - "github.com/gogf/gf/g/container/gset" - "github.com/gogf/gf/g/test/gtest" - "testing" + "github.com/gogf/gf/g/container/garray" + "github.com/gogf/gf/g/container/gset" + "github.com/gogf/gf/g/test/gtest" + "strings" + "testing" ) func TestIntSet_Basic(t *testing.T) { - gtest.Case(t, func() { - s := gset.NewIntSet() - s.Add(1).Add(1).Add(2) - s.Add([]int{3,4}...) - gtest.Assert(s.Size(), 4) - gtest.AssertIN(1, s.Slice()) - gtest.AssertIN(2, s.Slice()) - gtest.AssertIN(3, s.Slice()) - gtest.AssertIN(4, s.Slice()) - gtest.AssertNI(0, s.Slice()) - gtest.Assert(s.Contains(4), true) - gtest.Assert(s.Contains(5), false) - s.Remove(1) - gtest.Assert(s.Size(), 3) - s.Clear() - gtest.Assert(s.Size(), 0) - }) + gtest.Case(t, func() { + s := gset.NewIntSet() + s.Add(1).Add(1).Add(2) + s.Add([]int{3, 4}...) + gtest.Assert(s.Size(), 4) + gtest.AssertIN(1, s.Slice()) + gtest.AssertIN(2, s.Slice()) + gtest.AssertIN(3, s.Slice()) + gtest.AssertIN(4, s.Slice()) + gtest.AssertNI(0, s.Slice()) + gtest.Assert(s.Contains(4), true) + gtest.Assert(s.Contains(5), false) + s.Remove(1) + gtest.Assert(s.Size(), 3) + s.Clear() + gtest.Assert(s.Size(), 0) + }) } func TestIntSet_Iterator(t *testing.T) { - gtest.Case(t, func() { - s := gset.NewIntSet() - s.Add(1).Add(2).Add(3) - gtest.Assert(s.Size(), 3) + gtest.Case(t, func() { + s := gset.NewIntSet() + s.Add(1).Add(2).Add(3) + gtest.Assert(s.Size(), 3) - a1 := garray.New() - a2 := garray.New() - s.Iterator(func(v int) bool { - a1.Append(1) - return false - }) - s.Iterator(func(v int) bool { - a2.Append(1) - return true - }) - gtest.Assert(a1.Len(), 1) - gtest.Assert(a2.Len(), 3) - }) + a1 := garray.New() + a2 := garray.New() + s.Iterator(func(v int) bool { + a1.Append(1) + return false + }) + s.Iterator(func(v int) bool { + a2.Append(1) + return true + }) + gtest.Assert(a1.Len(), 1) + gtest.Assert(a2.Len(), 3) + }) } func TestIntSet_LockFunc(t *testing.T) { - gtest.Case(t, func() { - s := gset.NewIntSet() - s.Add(1).Add(2).Add(3) - gtest.Assert(s.Size(), 3) - s.LockFunc(func(m map[int]struct{}) { - delete(m, 1) - }) - gtest.Assert(s.Size(), 2) - s.RLockFunc(func(m map[int]struct{}) { - gtest.Assert(m, map[int]struct{}{ - 3 : struct{}{}, - 2 : struct{}{}, - }) - }) - }) + gtest.Case(t, func() { + s := gset.NewIntSet() + s.Add(1).Add(2).Add(3) + gtest.Assert(s.Size(), 3) + s.LockFunc(func(m map[int]struct{}) { + delete(m, 1) + }) + gtest.Assert(s.Size(), 2) + s.RLockFunc(func(m map[int]struct{}) { + gtest.Assert(m, map[int]struct{}{ + 3: struct{}{}, + 2: struct{}{}, + }) + }) + }) } func TestIntSet_Equal(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewIntSet() - s2 := gset.NewIntSet() - s3 := gset.NewIntSet() - s1.Add(1).Add(2).Add(3) - s2.Add(1).Add(2).Add(3) - s3.Add(1).Add(2).Add(3).Add(4) - gtest.Assert(s1.Equal(s2), true) - gtest.Assert(s1.Equal(s3), false) - }) + gtest.Case(t, func() { + s1 := gset.NewIntSet() + s2 := gset.NewIntSet() + s3 := gset.NewIntSet() + s1.Add(1).Add(2).Add(3) + s2.Add(1).Add(2).Add(3) + s3.Add(1).Add(2).Add(3).Add(4) + gtest.Assert(s1.Equal(s2), true) + gtest.Assert(s1.Equal(s3), false) + }) } func TestIntSet_IsSubsetOf(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewIntSet() - s2 := gset.NewIntSet() - s3 := gset.NewIntSet() - s1.Add(1).Add(2) - s2.Add(1).Add(2).Add(3) - s3.Add(1).Add(2).Add(3).Add(4) - gtest.Assert(s1.IsSubsetOf(s2), true) - gtest.Assert(s2.IsSubsetOf(s3), true) - gtest.Assert(s1.IsSubsetOf(s3), true) - gtest.Assert(s2.IsSubsetOf(s1), false) - gtest.Assert(s3.IsSubsetOf(s2), false) - }) + gtest.Case(t, func() { + s1 := gset.NewIntSet() + s2 := gset.NewIntSet() + s3 := gset.NewIntSet() + s1.Add(1).Add(2) + s2.Add(1).Add(2).Add(3) + s3.Add(1).Add(2).Add(3).Add(4) + gtest.Assert(s1.IsSubsetOf(s2), true) + gtest.Assert(s2.IsSubsetOf(s3), true) + gtest.Assert(s1.IsSubsetOf(s3), true) + gtest.Assert(s2.IsSubsetOf(s1), false) + gtest.Assert(s3.IsSubsetOf(s2), false) + }) } func TestIntSet_Union(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewIntSet() - s2 := gset.NewIntSet() - s1.Add(1).Add(2) - s2.Add(3).Add(4) - s3 := s1.Union(s2) - gtest.Assert(s3.Contains(1), true) - gtest.Assert(s3.Contains(2), true) - gtest.Assert(s3.Contains(3), true) - gtest.Assert(s3.Contains(4), true) - }) + gtest.Case(t, func() { + s1 := gset.NewIntSet() + s2 := gset.NewIntSet() + s1.Add(1).Add(2) + s2.Add(3).Add(4) + s3 := s1.Union(s2) + gtest.Assert(s3.Contains(1), true) + gtest.Assert(s3.Contains(2), true) + gtest.Assert(s3.Contains(3), true) + gtest.Assert(s3.Contains(4), true) + }) } func TestIntSet_Diff(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewIntSet() - s2 := gset.NewIntSet() - s1.Add(1).Add(2).Add(3) - s2.Add(3).Add(4).Add(5) - s3 := s1.Diff(s2) - gtest.Assert(s3.Contains(1), true) - gtest.Assert(s3.Contains(2), true) - gtest.Assert(s3.Contains(3), false) - gtest.Assert(s3.Contains(4), false) - }) + gtest.Case(t, func() { + s1 := gset.NewIntSet() + s2 := gset.NewIntSet() + s1.Add(1).Add(2).Add(3) + s2.Add(3).Add(4).Add(5) + s3 := s1.Diff(s2) + gtest.Assert(s3.Contains(1), true) + gtest.Assert(s3.Contains(2), true) + gtest.Assert(s3.Contains(3), false) + gtest.Assert(s3.Contains(4), false) + }) } func TestIntSet_Intersect(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewIntSet() - s2 := gset.NewIntSet() - s1.Add(1).Add(2).Add(3) - s2.Add(3).Add(4).Add(5) - s3 := s1.Intersect(s2) - gtest.Assert(s3.Contains(1), false) - gtest.Assert(s3.Contains(2), false) - gtest.Assert(s3.Contains(3), true) - gtest.Assert(s3.Contains(4), false) - }) + gtest.Case(t, func() { + s1 := gset.NewIntSet() + s2 := gset.NewIntSet() + s1.Add(1).Add(2).Add(3) + s2.Add(3).Add(4).Add(5) + s3 := s1.Intersect(s2) + gtest.Assert(s3.Contains(1), false) + gtest.Assert(s3.Contains(2), false) + gtest.Assert(s3.Contains(3), true) + gtest.Assert(s3.Contains(4), false) + }) } func TestIntSet_Complement(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewIntSet() - s2 := gset.NewIntSet() - s1.Add(1).Add(2).Add(3) - s2.Add(3).Add(4).Add(5) - s3 := s1.Complement(s2) - gtest.Assert(s3.Contains(1), false) - gtest.Assert(s3.Contains(2), false) - gtest.Assert(s3.Contains(4), true) - gtest.Assert(s3.Contains(5), true) - }) -} \ No newline at end of file + gtest.Case(t, func() { + s1 := gset.NewIntSet() + s2 := gset.NewIntSet() + s1.Add(1).Add(2).Add(3) + s2.Add(3).Add(4).Add(5) + s3 := s1.Complement(s2) + gtest.Assert(s3.Contains(1), false) + gtest.Assert(s3.Contains(2), false) + gtest.Assert(s3.Contains(4), true) + gtest.Assert(s3.Contains(5), true) + }) +} + +func TestIntSet_Size(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.NewIntSet(true) + s1.Add(1).Add(2).Add(3) + gtest.Assert(s1.Size(), 3) + + }) + +} + +func TestIntSet_Merge(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.NewIntSet() + s2 := gset.NewIntSet() + s1.Add(1).Add(2).Add(3) + s2.Add(3).Add(4).Add(5) + s3 := s1.Merge(s2) + gtest.Assert(s3.Contains(1), true) + gtest.Assert(s3.Contains(5), true) + gtest.Assert(s3.Contains(6), false) + }) +} + +func TestIntSet_Join(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.NewIntSet() + s1.Add(1).Add(2).Add(3) + s3 := s1.Join(",") + gtest.Assert(strings.Contains(s3, "3"), true) + + }) +} + +func TestIntSet_Sum(t *testing.T) { + s1 := gset.NewIntSet() + s1.Add(1).Add(2).Add(3) + s2 := gset.NewIntSet() + s2.Add(5).Add(6).Add(7) + gtest.Assert(s2.Sum(), 18) +} diff --git a/g/container/gset/gset_z_unit_string_test.go b/g/container/gset/gset_z_unit_string_test.go index 67a2dadfe..10ab0251c 100644 --- a/g/container/gset/gset_z_unit_string_test.go +++ b/g/container/gset/gset_z_unit_string_test.go @@ -9,152 +9,186 @@ package gset_test import ( - "github.com/gogf/gf/g/container/garray" - "github.com/gogf/gf/g/container/gset" - "github.com/gogf/gf/g/test/gtest" - "testing" + "github.com/gogf/gf/g/container/garray" + "github.com/gogf/gf/g/container/gset" + "github.com/gogf/gf/g/test/gtest" + "testing" ) func TestStringSet_Basic(t *testing.T) { - gtest.Case(t, func() { - s := gset.NewStringSet() - s.Add("1").Add("1").Add("2") - s.Add([]string{"3","4"}...) - gtest.Assert(s.Size(), 4) - gtest.AssertIN("1", s.Slice()) - gtest.AssertIN("2", s.Slice()) - gtest.AssertIN("3", s.Slice()) - gtest.AssertIN("4", s.Slice()) - gtest.AssertNI("0", s.Slice()) - gtest.Assert(s.Contains("4"), true) - gtest.Assert(s.Contains("5"), false) - s.Remove("1") - gtest.Assert(s.Size(), 3) - s.Clear() - gtest.Assert(s.Size(), 0) - }) + gtest.Case(t, func() { + s := gset.NewStringSet() + s.Add("1").Add("1").Add("2") + s.Add([]string{"3", "4"}...) + gtest.Assert(s.Size(), 4) + gtest.AssertIN("1", s.Slice()) + gtest.AssertIN("2", s.Slice()) + gtest.AssertIN("3", s.Slice()) + gtest.AssertIN("4", s.Slice()) + gtest.AssertNI("0", s.Slice()) + gtest.Assert(s.Contains("4"), true) + gtest.Assert(s.Contains("5"), false) + s.Remove("1") + gtest.Assert(s.Size(), 3) + s.Clear() + gtest.Assert(s.Size(), 0) + }) } func TestStringSet_Iterator(t *testing.T) { - gtest.Case(t, func() { - s := gset.NewStringSet() - s.Add("1").Add("2").Add("3") - gtest.Assert(s.Size(), 3) + gtest.Case(t, func() { + s := gset.NewStringSet() + s.Add("1").Add("2").Add("3") + gtest.Assert(s.Size(), 3) - a1 := garray.New() - a2 := garray.New() - s.Iterator(func(v string) bool { - a1.Append("1") - return false - }) - s.Iterator(func(v string) bool { - a2.Append("1") - return true - }) - gtest.Assert(a1.Len(), 1) - gtest.Assert(a2.Len(), 3) - }) + a1 := garray.New() + a2 := garray.New() + s.Iterator(func(v string) bool { + a1.Append("1") + return false + }) + s.Iterator(func(v string) bool { + a2.Append("1") + return true + }) + gtest.Assert(a1.Len(), 1) + gtest.Assert(a2.Len(), 3) + }) } func TestStringSet_LockFunc(t *testing.T) { - gtest.Case(t, func() { - s := gset.NewStringSet() - s.Add("1").Add("2").Add("3") - gtest.Assert(s.Size(), 3) - s.LockFunc(func(m map[string]struct{}) { - delete(m, "1") - }) - gtest.Assert(s.Size(), 2) - s.RLockFunc(func(m map[string]struct{}) { - gtest.Assert(m, map[string]struct{}{ - "3" : struct{}{}, - "2" : struct{}{}, - }) - }) - }) + gtest.Case(t, func() { + s := gset.NewStringSet() + s.Add("1").Add("2").Add("3") + gtest.Assert(s.Size(), 3) + s.LockFunc(func(m map[string]struct{}) { + delete(m, "1") + }) + gtest.Assert(s.Size(), 2) + s.RLockFunc(func(m map[string]struct{}) { + gtest.Assert(m, map[string]struct{}{ + "3": struct{}{}, + "2": struct{}{}, + }) + }) + }) } func TestStringSet_Equal(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewStringSet() - s2 := gset.NewStringSet() - s3 := gset.NewStringSet() - s1.Add("1").Add("2").Add("3") - s2.Add("1").Add("2").Add("3") - s3.Add("1").Add("2").Add("3").Add("4") - gtest.Assert(s1.Equal(s2), true) - gtest.Assert(s1.Equal(s3), false) - }) + gtest.Case(t, func() { + s1 := gset.NewStringSet() + s2 := gset.NewStringSet() + s3 := gset.NewStringSet() + s1.Add("1").Add("2").Add("3") + s2.Add("1").Add("2").Add("3") + s3.Add("1").Add("2").Add("3").Add("4") + gtest.Assert(s1.Equal(s2), true) + gtest.Assert(s1.Equal(s3), false) + }) } func TestStringSet_IsSubsetOf(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewStringSet() - s2 := gset.NewStringSet() - s3 := gset.NewStringSet() - s1.Add("1").Add("2") - s2.Add("1").Add("2").Add("3") - s3.Add("1").Add("2").Add("3").Add("4") - gtest.Assert(s1.IsSubsetOf(s2), true) - gtest.Assert(s2.IsSubsetOf(s3), true) - gtest.Assert(s1.IsSubsetOf(s3), true) - gtest.Assert(s2.IsSubsetOf(s1), false) - gtest.Assert(s3.IsSubsetOf(s2), false) - }) + gtest.Case(t, func() { + s1 := gset.NewStringSet() + s2 := gset.NewStringSet() + s3 := gset.NewStringSet() + s1.Add("1").Add("2") + s2.Add("1").Add("2").Add("3") + s3.Add("1").Add("2").Add("3").Add("4") + gtest.Assert(s1.IsSubsetOf(s2), true) + gtest.Assert(s2.IsSubsetOf(s3), true) + gtest.Assert(s1.IsSubsetOf(s3), true) + gtest.Assert(s2.IsSubsetOf(s1), false) + gtest.Assert(s3.IsSubsetOf(s2), false) + }) } func TestStringSet_Union(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewStringSet() - s2 := gset.NewStringSet() - s1.Add("1").Add("2") - s2.Add("3").Add("4") - s3 := s1.Union(s2) - gtest.Assert(s3.Contains("1"), true) - gtest.Assert(s3.Contains("2"), true) - gtest.Assert(s3.Contains("3"), true) - gtest.Assert(s3.Contains("4"), true) - }) + gtest.Case(t, func() { + s1 := gset.NewStringSet() + s2 := gset.NewStringSet() + s1.Add("1").Add("2") + s2.Add("3").Add("4") + s3 := s1.Union(s2) + gtest.Assert(s3.Contains("1"), true) + gtest.Assert(s3.Contains("2"), true) + gtest.Assert(s3.Contains("3"), true) + gtest.Assert(s3.Contains("4"), true) + }) } func TestStringSet_Diff(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewStringSet() - s2 := gset.NewStringSet() - s1.Add("1").Add("2").Add("3") - s2.Add("3").Add("4").Add("5") - s3 := s1.Diff(s2) - gtest.Assert(s3.Contains("1"), true) - gtest.Assert(s3.Contains("2"), true) - gtest.Assert(s3.Contains("3"), false) - gtest.Assert(s3.Contains("4"), false) - }) + gtest.Case(t, func() { + s1 := gset.NewStringSet() + s2 := gset.NewStringSet() + s1.Add("1").Add("2").Add("3") + s2.Add("3").Add("4").Add("5") + s3 := s1.Diff(s2) + gtest.Assert(s3.Contains("1"), true) + gtest.Assert(s3.Contains("2"), true) + gtest.Assert(s3.Contains("3"), false) + gtest.Assert(s3.Contains("4"), false) + }) } func TestStringSet_Intersect(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewStringSet() - s2 := gset.NewStringSet() - s1.Add("1").Add("2").Add("3") - s2.Add("3").Add("4").Add("5") - s3 := s1.Intersect(s2) - gtest.Assert(s3.Contains("1"), false) - gtest.Assert(s3.Contains("2"), false) - gtest.Assert(s3.Contains("3"), true) - gtest.Assert(s3.Contains("4"), false) - }) + gtest.Case(t, func() { + s1 := gset.NewStringSet() + s2 := gset.NewStringSet() + s1.Add("1").Add("2").Add("3") + s2.Add("3").Add("4").Add("5") + s3 := s1.Intersect(s2) + gtest.Assert(s3.Contains("1"), false) + gtest.Assert(s3.Contains("2"), false) + gtest.Assert(s3.Contains("3"), true) + gtest.Assert(s3.Contains("4"), false) + }) } func TestStringSet_Complement(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewStringSet() - s2 := gset.NewStringSet() - s1.Add("1").Add("2").Add("3") - s2.Add("3").Add("4").Add("5") - s3 := s1.Complement(s2) - gtest.Assert(s3.Contains("1"), false) - gtest.Assert(s3.Contains("2"), false) - gtest.Assert(s3.Contains("4"), true) - gtest.Assert(s3.Contains("5"), true) - }) -} \ No newline at end of file + gtest.Case(t, func() { + s1 := gset.NewStringSet() + s2 := gset.NewStringSet() + s1.Add("1").Add("2").Add("3") + s2.Add("3").Add("4").Add("5") + s3 := s1.Complement(s2) + gtest.Assert(s3.Contains("1"), false) + gtest.Assert(s3.Contains("2"), false) + gtest.Assert(s3.Contains("4"), true) + gtest.Assert(s3.Contains("5"), true) + }) +} + +func TestNewIntSetFrom(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.NewIntSetFrom([]int{1, 2, 3, 4}) + s2 := gset.NewIntSetFrom([]int{5, 6, 7, 8}) + gtest.Assert(s1.Contains(3), true) + gtest.Assert(s1.Contains(5), false) + gtest.Assert(s2.Contains(3), false) + gtest.Assert(s2.Contains(5), true) + }) +} + +func TestStringSet_Merge(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.NewStringSet() + s2 := gset.NewStringSet() + s1.Add("1").Add("2").Add("3") + s2.Add("3").Add("4").Add("5") + s3 := s1.Merge(s2) + gtest.Assert(s3.Contains("1"), true) + gtest.Assert(s3.Contains("6"), false) + gtest.Assert(s3.Contains("4"), true) + gtest.Assert(s3.Contains("5"), true) + }) +} + +func TestNewStringSetFrom(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + gtest.Assert(s1.Contains("b"), true) + gtest.Assert(s1.Contains("d"), false) + + }) +} diff --git a/g/container/gset/gset_z_unit_test.go b/g/container/gset/gset_z_unit_test.go index a0901a070..527257ba4 100644 --- a/g/container/gset/gset_z_unit_test.go +++ b/g/container/gset/gset_z_unit_test.go @@ -9,152 +9,230 @@ package gset_test import ( - "github.com/gogf/gf/g/container/garray" - "github.com/gogf/gf/g/container/gset" - "github.com/gogf/gf/g/test/gtest" - "testing" + "github.com/gogf/gf/g/container/garray" + "github.com/gogf/gf/g/container/gset" + + "github.com/gogf/gf/g/test/gtest" + "strings" + "testing" ) func TestSet_Basic(t *testing.T) { - gtest.Case(t, func() { - s := gset.NewSet() - s.Add(1).Add(1).Add(2) - s.Add([]interface{}{3,4}...) - gtest.Assert(s.Size(), 4) - gtest.AssertIN(1, s.Slice()) - gtest.AssertIN(2, s.Slice()) - gtest.AssertIN(3, s.Slice()) - gtest.AssertIN(4, s.Slice()) - gtest.AssertNI(0, s.Slice()) - gtest.Assert(s.Contains(4), true) - gtest.Assert(s.Contains(5), false) - s.Remove(1) - gtest.Assert(s.Size(), 3) - s.Clear() - gtest.Assert(s.Size(), 0) - }) + gtest.Case(t, func() { + s := gset.NewSet() + s.Add(1).Add(1).Add(2) + s.Add([]interface{}{3, 4}...) + gtest.Assert(s.Size(), 4) + gtest.AssertIN(1, s.Slice()) + gtest.AssertIN(2, s.Slice()) + gtest.AssertIN(3, s.Slice()) + gtest.AssertIN(4, s.Slice()) + gtest.AssertNI(0, s.Slice()) + gtest.Assert(s.Contains(4), true) + gtest.Assert(s.Contains(5), false) + s.Remove(1) + gtest.Assert(s.Size(), 3) + s.Clear() + gtest.Assert(s.Size(), 0) + }) } func TestSet_Iterator(t *testing.T) { - gtest.Case(t, func() { - s := gset.NewSet() - s.Add(1).Add(2).Add(3) - gtest.Assert(s.Size(), 3) + gtest.Case(t, func() { + s := gset.NewSet() + s.Add(1).Add(2).Add(3) + gtest.Assert(s.Size(), 3) - a1 := garray.New() - a2 := garray.New() - s.Iterator(func(v interface{}) bool { - a1.Append(1) - return false - }) - s.Iterator(func(v interface{}) bool { - a2.Append(1) - return true - }) - gtest.Assert(a1.Len(), 1) - gtest.Assert(a2.Len(), 3) - }) + a1 := garray.New() + a2 := garray.New() + s.Iterator(func(v interface{}) bool { + a1.Append(1) + return false + }) + s.Iterator(func(v interface{}) bool { + a2.Append(1) + return true + }) + gtest.Assert(a1.Len(), 1) + gtest.Assert(a2.Len(), 3) + }) } func TestSet_LockFunc(t *testing.T) { - gtest.Case(t, func() { - s := gset.NewSet() - s.Add(1).Add(2).Add(3) - gtest.Assert(s.Size(), 3) - s.LockFunc(func(m map[interface{}]struct{}) { - delete(m, 1) - }) - gtest.Assert(s.Size(), 2) - s.RLockFunc(func(m map[interface{}]struct{}) { - gtest.Assert(m, map[interface{}]struct{}{ - 3 : struct{}{}, - 2 : struct{}{}, - }) - }) - }) + gtest.Case(t, func() { + s := gset.NewSet() + s.Add(1).Add(2).Add(3) + gtest.Assert(s.Size(), 3) + s.LockFunc(func(m map[interface{}]struct{}) { + delete(m, 1) + }) + gtest.Assert(s.Size(), 2) + s.RLockFunc(func(m map[interface{}]struct{}) { + gtest.Assert(m, map[interface{}]struct{}{ + 3: struct{}{}, + 2: struct{}{}, + }) + }) + }) } func TestSet_Equal(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewSet() - s2 := gset.NewSet() - s3 := gset.NewSet() - s1.Add(1).Add(2).Add(3) - s2.Add(1).Add(2).Add(3) - s3.Add(1).Add(2).Add(3).Add(4) - gtest.Assert(s1.Equal(s2), true) - gtest.Assert(s1.Equal(s3), false) - }) + gtest.Case(t, func() { + s1 := gset.NewSet() + s2 := gset.NewSet() + s3 := gset.NewSet() + s1.Add(1).Add(2).Add(3) + s2.Add(1).Add(2).Add(3) + s3.Add(1).Add(2).Add(3).Add(4) + gtest.Assert(s1.Equal(s2), true) + gtest.Assert(s1.Equal(s3), false) + }) } func TestSet_IsSubsetOf(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewSet() - s2 := gset.NewSet() - s3 := gset.NewSet() - s1.Add(1).Add(2) - s2.Add(1).Add(2).Add(3) - s3.Add(1).Add(2).Add(3).Add(4) - gtest.Assert(s1.IsSubsetOf(s2), true) - gtest.Assert(s2.IsSubsetOf(s3), true) - gtest.Assert(s1.IsSubsetOf(s3), true) - gtest.Assert(s2.IsSubsetOf(s1), false) - gtest.Assert(s3.IsSubsetOf(s2), false) - }) + gtest.Case(t, func() { + s1 := gset.NewSet() + s2 := gset.NewSet() + s3 := gset.NewSet() + s1.Add(1).Add(2) + s2.Add(1).Add(2).Add(3) + s3.Add(1).Add(2).Add(3).Add(4) + gtest.Assert(s1.IsSubsetOf(s2), true) + gtest.Assert(s2.IsSubsetOf(s3), true) + gtest.Assert(s1.IsSubsetOf(s3), true) + gtest.Assert(s2.IsSubsetOf(s1), false) + gtest.Assert(s3.IsSubsetOf(s2), false) + }) } func TestSet_Union(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewSet() - s2 := gset.NewSet() - s1.Add(1).Add(2) - s2.Add(3).Add(4) - s3 := s1.Union(s2) - gtest.Assert(s3.Contains(1), true) - gtest.Assert(s3.Contains(2), true) - gtest.Assert(s3.Contains(3), true) - gtest.Assert(s3.Contains(4), true) - }) + gtest.Case(t, func() { + s1 := gset.NewSet() + s2 := gset.NewSet() + s1.Add(1).Add(2) + s2.Add(3).Add(4) + s3 := s1.Union(s2) + gtest.Assert(s3.Contains(1), true) + gtest.Assert(s3.Contains(2), true) + gtest.Assert(s3.Contains(3), true) + gtest.Assert(s3.Contains(4), true) + }) } func TestSet_Diff(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewSet() - s2 := gset.NewSet() - s1.Add(1).Add(2).Add(3) - s2.Add(3).Add(4).Add(5) - s3 := s1.Diff(s2) - gtest.Assert(s3.Contains(1), true) - gtest.Assert(s3.Contains(2), true) - gtest.Assert(s3.Contains(3), false) - gtest.Assert(s3.Contains(4), false) - }) + gtest.Case(t, func() { + s1 := gset.NewSet() + s2 := gset.NewSet() + s1.Add(1).Add(2).Add(3) + s2.Add(3).Add(4).Add(5) + s3 := s1.Diff(s2) + gtest.Assert(s3.Contains(1), true) + gtest.Assert(s3.Contains(2), true) + gtest.Assert(s3.Contains(3), false) + gtest.Assert(s3.Contains(4), false) + }) } func TestSet_Intersect(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewSet() - s2 := gset.NewSet() - s1.Add(1).Add(2).Add(3) - s2.Add(3).Add(4).Add(5) - s3 := s1.Intersect(s2) - gtest.Assert(s3.Contains(1), false) - gtest.Assert(s3.Contains(2), false) - gtest.Assert(s3.Contains(3), true) - gtest.Assert(s3.Contains(4), false) - }) + gtest.Case(t, func() { + s1 := gset.NewSet() + s2 := gset.NewSet() + s1.Add(1).Add(2).Add(3) + s2.Add(3).Add(4).Add(5) + s3 := s1.Intersect(s2) + gtest.Assert(s3.Contains(1), false) + gtest.Assert(s3.Contains(2), false) + gtest.Assert(s3.Contains(3), true) + gtest.Assert(s3.Contains(4), false) + }) } func TestSet_Complement(t *testing.T) { - gtest.Case(t, func() { - s1 := gset.NewSet() - s2 := gset.NewSet() - s1.Add(1).Add(2).Add(3) - s2.Add(3).Add(4).Add(5) - s3 := s1.Complement(s2) - gtest.Assert(s3.Contains(1), false) - gtest.Assert(s3.Contains(2), false) - gtest.Assert(s3.Contains(4), true) - gtest.Assert(s3.Contains(5), true) - }) -} \ No newline at end of file + gtest.Case(t, func() { + s1 := gset.NewSet() + s2 := gset.NewSet() + s1.Add(1).Add(2).Add(3) + s2.Add(3).Add(4).Add(5) + s3 := s1.Complement(s2) + gtest.Assert(s3.Contains(1), false) + gtest.Assert(s3.Contains(2), false) + gtest.Assert(s3.Contains(4), true) + gtest.Assert(s3.Contains(5), true) + }) +} + +func TestNewFrom(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.NewFrom("a") + s2 := gset.NewFrom("b", false) + s3 := gset.NewFrom(3, true) + s4 := gset.NewFrom([]string{"s1", "s2"}, true) + gtest.Assert(s1.Contains("a"), true) + gtest.Assert(s2.Contains("b"), true) + gtest.Assert(s3.Contains(3), true) + gtest.Assert(s4.Contains("s1"), true) + gtest.Assert(s4.Contains("s3"), false) + + }) +} + +func TestNew(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.New() + s1.Add("a").Add(2) + s2 := gset.New(true) + s2.Add("b").Add(3) + gtest.Assert(s1.Contains("a"), true) + + }) +} + +func TestSet_Join(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.New(true) + s1.Add("a").Add("a1").Add("b").Add("c") + str1 := s1.Join(",") + gtest.Assert(strings.Contains(str1, "a1"), true) + + }) +} + +func TestSet_String(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.New(true) + s1.Add("a").Add("a2").Add("b").Add("c") + str1 := s1.String() + gtest.Assert(strings.Contains(str1, "a2"), true) + + }) +} + +func TestSet_Merge(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.New(true) + s2 := gset.New(true) + s1.Add("a").Add("a2").Add("b").Add("c") + s2.Add("b").Add("b1").Add("e").Add("f") + ss := s1.Merge(s2) + gtest.Assert(ss.Contains("a2"), true) + gtest.Assert(ss.Contains("b1"), true) + + }) +} + +func TestSet_Sum(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.New(true) + s1.Add(1).Add(2).Add(3).Add(4) + gtest.Assert(s1.Sum(), int(10)) + + }) +} + +func TestSet_Pop(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.New(true) + s1.Add(1).Add(2).Add(3).Add(4) + gtest.AssertIN(s1.Pop(1), []int{1, 2, 3, 4}) + }) +} From 485fe572ff80184f953c3b396cc77017fb895043 Mon Sep 17 00:00:00 2001 From: jroam Date: Tue, 11 Jun 2019 10:51:00 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E5=AE=8C=E6=88=90gset=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=A6=86=E7=9B=96=E7=8E=8790%=E4=BB=A5?= =?UTF-8?q?=E4=B8=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/container/gset/gset_z_unit_int_test.go | 24 ++++++-- g/container/gset/gset_z_unit_string_test.go | 62 +++++++++++++++++++++ g/container/gset/gset_z_unit_test.go | 10 ++++ 3 files changed, 91 insertions(+), 5 deletions(-) diff --git a/g/container/gset/gset_z_unit_int_test.go b/g/container/gset/gset_z_unit_int_test.go index 51e1b4a88..4a640c205 100644 --- a/g/container/gset/gset_z_unit_int_test.go +++ b/g/container/gset/gset_z_unit_int_test.go @@ -194,9 +194,23 @@ func TestIntSet_Join(t *testing.T) { } func TestIntSet_Sum(t *testing.T) { - s1 := gset.NewIntSet() - s1.Add(1).Add(2).Add(3) - s2 := gset.NewIntSet() - s2.Add(5).Add(6).Add(7) - gtest.Assert(s2.Sum(), 18) + gtest.Case(t, func() { + s1 := gset.NewIntSet() + s1.Add(1).Add(2).Add(3) + s2 := gset.NewIntSet() + s2.Add(5).Add(6).Add(7) + gtest.Assert(s2.Sum(), 18) + + }) + +} + +func TestIntSet_Pop(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.NewIntSet() + s1.Add(4).Add(2).Add(3) + gtest.AssertIN(s1.Pop(1), []int{4, 2, 3}) + gtest.AssertIN(s1.Pop(5), []int{4, 2, 3}) + gtest.Assert(s1.Size(), 3) + }) } diff --git a/g/container/gset/gset_z_unit_string_test.go b/g/container/gset/gset_z_unit_string_test.go index 10ab0251c..c75c2f10b 100644 --- a/g/container/gset/gset_z_unit_string_test.go +++ b/g/container/gset/gset_z_unit_string_test.go @@ -12,6 +12,7 @@ import ( "github.com/gogf/gf/g/container/garray" "github.com/gogf/gf/g/container/gset" "github.com/gogf/gf/g/test/gtest" + "strings" "testing" ) @@ -189,6 +190,67 @@ func TestNewStringSetFrom(t *testing.T) { s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) gtest.Assert(s1.Contains("b"), true) gtest.Assert(s1.Contains("d"), false) + }) +} + +func TestStringSet_Join(t *testing.T) { + s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + str1 := s1.Join(",") + gtest.Assert(strings.Contains(str1, "b"), true) + gtest.Assert(strings.Contains(str1, "d"), false) +} + +func TestStringSet_String(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + str1 := s1.String() + gtest.Assert(strings.Contains(str1, "b"), true) + gtest.Assert(strings.Contains(str1, "d"), false) + }) + +} + +func TestStringSet_Sum(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + s2 := gset.NewIntSetFrom([]int{2, 3, 4}, true) + gtest.Assert(s1.Sum(), 0) + gtest.Assert(s2.Sum(), 9) + }) +} + +func TestStringSet_Size(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + gtest.Assert(s1.Size(), 3) }) } + +func TestStringSet_Remove(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + s1 = s1.Remove("b") + gtest.Assert(s1.Contains("b"), false) + gtest.Assert(s1.Contains("c"), true) + }) +} + +func TestStringSet_Pop(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + str1 := s1.Pop(1) + gtest.Assert(strings.Contains("a,b,c", str1), true) + }) +} + +func TestStringSet_Pops(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + strs1 := s1.Pops(2) + gtest.AssertIN(strs1, []string{"a", "b", "c"}) + gtest.Assert(len(strs1), 2) + str2 := s1.Pops(7) + gtest.AssertIN(str2, []string{"a", "b", "c"}) + }) +} diff --git a/g/container/gset/gset_z_unit_test.go b/g/container/gset/gset_z_unit_test.go index 527257ba4..97c149163 100644 --- a/g/container/gset/gset_z_unit_test.go +++ b/g/container/gset/gset_z_unit_test.go @@ -236,3 +236,13 @@ func TestSet_Pop(t *testing.T) { gtest.AssertIN(s1.Pop(1), []int{1, 2, 3, 4}) }) } + +func TestSet_Pops(t *testing.T) { + gtest.Case(t, func() { + s1 := gset.New(true) + s1.Add(1).Add(2).Add(3).Add(4) + gtest.AssertIN(s1.Pops(1), []int{1, 2, 3, 4}) + gtest.AssertIN(s1.Pops(6), []int{1, 2, 3, 4}) + gtest.Assert(len(s1.Pops(2)), 2) + }) +} From 24bd83feb0585865a20342bb5077f487180f72b4 Mon Sep 17 00:00:00 2001 From: jroam Date: Tue, 11 Jun 2019 18:11:17 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=BA=9Bgarra?= =?UTF-8?q?y=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../garray/garray_z_unit_basic_test.go | 113 +++--- .../garray/garray_z_unit_string_test.go | 382 +++++++++++------- go.mod | 4 +- 3 files changed, 305 insertions(+), 194 deletions(-) diff --git a/g/container/garray/garray_z_unit_basic_test.go b/g/container/garray/garray_z_unit_basic_test.go index d391accea..a387bd3c0 100644 --- a/g/container/garray/garray_z_unit_basic_test.go +++ b/g/container/garray/garray_z_unit_basic_test.go @@ -9,75 +9,86 @@ package garray_test import ( - "github.com/gogf/gf/g/container/garray" - "github.com/gogf/gf/g/test/gtest" - "github.com/gogf/gf/g/util/gconv" - "strings" - "testing" + "github.com/gogf/gf/g/container/garray" + "github.com/gogf/gf/g/test/gtest" + "github.com/gogf/gf/g/util/gconv" + "strings" + "testing" ) func Test_IntArray_Unique(t *testing.T) { - expect := []int{1, 2, 3, 4, 5, 6} - array := garray.NewIntArray() - array.Append(1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6) - array.Unique() - gtest.Assert(array.Slice(), expect) + expect := []int{1, 2, 3, 4, 5, 6} + array := garray.NewIntArray() + array.Append(1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6) + array.Unique() + gtest.Assert(array.Slice(), expect) } func Test_SortedIntArray1(t *testing.T) { - expect := []int{0,1,2,3,4,5,6,7,8,9,10} - array := garray.NewSortedIntArray() - for i := 10; i > -1; i-- { - array.Add(i) - } - gtest.Assert(array.Slice(), expect) + expect := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + array := garray.NewSortedIntArray() + for i := 10; i > -1; i-- { + array.Add(i) + } + gtest.Assert(array.Slice(), expect) } func Test_SortedIntArray2(t *testing.T) { - expect := []int{0,1,2,3,4,5,6,7,8,9,10} - array := garray.NewSortedIntArray() - for i := 0; i <= 10; i++ { - array.Add(i) - } - gtest.Assert(array.Slice(), expect) + expect := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + array := garray.NewSortedIntArray() + for i := 0; i <= 10; i++ { + array.Add(i) + } + gtest.Assert(array.Slice(), expect) } func Test_SortedStringArray1(t *testing.T) { - expect := []string{"0","1","10","2","3","4","5","6","7","8","9"} - array := garray.NewSortedStringArray() - for i := 10; i > -1; i-- { - array.Add(gconv.String(i)) - } - gtest.Assert(array.Slice(), expect) + expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"} + array := garray.NewSortedStringArray() + for i := 10; i > -1; i-- { + array.Add(gconv.String(i)) + } + gtest.Assert(array.Slice(), expect) } func Test_SortedStringArray2(t *testing.T) { - expect := []string{"0","1","10","2","3","4","5","6","7","8","9"} - array := garray.NewSortedStringArray() - for i := 0; i <= 10; i++ { - array.Add(gconv.String(i)) - } - gtest.Assert(array.Slice(), expect) + expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"} + array := garray.NewSortedStringArray() + for i := 0; i <= 10; i++ { + array.Add(gconv.String(i)) + } + gtest.Assert(array.Slice(), expect) } func Test_SortedArray1(t *testing.T) { - expect := []string{"0","1","10","2","3","4","5","6","7","8","9"} - array := garray.NewSortedArray(func(v1, v2 interface{}) int { - return strings.Compare(gconv.String(v1), gconv.String(v2)) - }) - for i := 10; i > -1; i-- { - array.Add(gconv.String(i)) - } - gtest.Assert(array.Slice(), expect) + expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"} + array := garray.NewSortedArray(func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + }) + for i := 10; i > -1; i-- { + array.Add(gconv.String(i)) + } + gtest.Assert(array.Slice(), expect) } func Test_SortedArray2(t *testing.T) { - expect := []string{"0","1","10","2","3","4","5","6","7","8","9"} - array := garray.NewSortedArray(func(v1, v2 interface{}) int { - return strings.Compare(gconv.String(v1), gconv.String(v2)) - }) - for i := 0; i <= 10; i++ { - array.Add(gconv.String(i)) - } - gtest.Assert(array.Slice(), expect) -} \ No newline at end of file + expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"} + array := garray.NewSortedArray(func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + }) + for i := 0; i <= 10; i++ { + array.Add(gconv.String(i)) + } + gtest.Assert(array.Slice(), expect) +} + +func TestNewFromCopy(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"100", "200", "300", "400", "500", "600"} + array1 := garray.NewFromCopy(a1) + gtest.AssertIN(array1.PopRands(2), a1) + gtest.Assert(len(array1.PopRands(1)), 1) + gtest.Assert(len(array1.PopRands(9)), 3) + + }) +} diff --git a/g/container/garray/garray_z_unit_string_test.go b/g/container/garray/garray_z_unit_string_test.go index 8cbfdd47a..1994abeff 100644 --- a/g/container/garray/garray_z_unit_string_test.go +++ b/g/container/garray/garray_z_unit_string_test.go @@ -9,193 +9,291 @@ package garray_test import ( - "github.com/gogf/gf/g/container/garray" - "github.com/gogf/gf/g/test/gtest" - "github.com/gogf/gf/g/util/gconv" - "testing" + "github.com/gogf/gf/g/container/garray" + "github.com/gogf/gf/g/test/gtest" + "github.com/gogf/gf/g/util/gconv" + "strings" + "testing" ) func Test_StringArray_Basic(t *testing.T) { - gtest.Case(t, func() { - expect := []string{"0", "1", "2", "3"} - array := garray.NewStringArrayFrom(expect) - gtest.Assert(array.Slice(), expect) - array.Set(0, "100") - gtest.Assert(array.Get(0), 100) - gtest.Assert(array.Get(1), 1) - gtest.Assert(array.Search("100"), 0) - gtest.Assert(array.Contains("100"), true) - gtest.Assert(array.Remove(0), 100) - gtest.Assert(array.Contains("100"), false) - array.Append("4") - gtest.Assert(array.Len(), 4) - array.InsertBefore(0, "100") - array.InsertAfter(0, "200") - gtest.Assert(array.Slice(), []string{"100", "200", "1", "2", "3", "4"}) - array.InsertBefore(5, "300") - array.InsertAfter(6, "400") - gtest.Assert(array.Slice(), []string{"100", "200", "1", "2", "3", "300", "4", "400"}) - gtest.Assert(array.Clear().Len(), 0) - }) + gtest.Case(t, func() { + expect := []string{"0", "1", "2", "3"} + array := garray.NewStringArrayFrom(expect) + gtest.Assert(array.Slice(), expect) + array.Set(0, "100") + gtest.Assert(array.Get(0), 100) + gtest.Assert(array.Get(1), 1) + gtest.Assert(array.Search("100"), 0) + gtest.Assert(array.Contains("100"), true) + gtest.Assert(array.Remove(0), 100) + gtest.Assert(array.Contains("100"), false) + array.Append("4") + gtest.Assert(array.Len(), 4) + array.InsertBefore(0, "100") + array.InsertAfter(0, "200") + gtest.Assert(array.Slice(), []string{"100", "200", "1", "2", "3", "4"}) + array.InsertBefore(5, "300") + array.InsertAfter(6, "400") + gtest.Assert(array.Slice(), []string{"100", "200", "1", "2", "3", "300", "4", "400"}) + gtest.Assert(array.Clear().Len(), 0) + }) } func TestStringArray_Sort(t *testing.T) { - gtest.Case(t, func() { - expect1 := []string{"0", "1", "2", "3"} - expect2 := []string{"3", "2", "1", "0"} - array := garray.NewStringArray() - for i := 3; i >= 0; i-- { - array.Append(gconv.String(i)) - } - array.Sort() - gtest.Assert(array.Slice(), expect1) - array.Sort(true) - gtest.Assert(array.Slice(), expect2) - }) + gtest.Case(t, func() { + expect1 := []string{"0", "1", "2", "3"} + expect2 := []string{"3", "2", "1", "0"} + array := garray.NewStringArray() + for i := 3; i >= 0; i-- { + array.Append(gconv.String(i)) + } + array.Sort() + gtest.Assert(array.Slice(), expect1) + array.Sort(true) + gtest.Assert(array.Slice(), expect2) + }) } func TestStringArray_Unique(t *testing.T) { - gtest.Case(t, func() { - expect := []string{"1", "1", "2", "3"} - array := garray.NewStringArrayFrom(expect) - gtest.Assert(array.Unique().Slice(), []string{"1", "2", "3"}) - }) + gtest.Case(t, func() { + expect := []string{"1", "1", "2", "3"} + array := garray.NewStringArrayFrom(expect) + gtest.Assert(array.Unique().Slice(), []string{"1", "2", "3"}) + }) } func TestStringArray_PushAndPop(t *testing.T) { - gtest.Case(t, func() { - expect := []string{"0", "1", "2", "3"} - array := garray.NewStringArrayFrom(expect) - gtest.Assert(array.Slice(), expect) - gtest.Assert(array.PopLeft(), "0") - gtest.Assert(array.PopRight(), "3") - gtest.AssertIN(array.PopRand(), []string{"1", "2"}) - gtest.AssertIN(array.PopRand(), []string{"1", "2"}) - gtest.Assert(array.Len(), 0) - array.PushLeft("1").PushRight("2") - gtest.Assert(array.Slice(), []string{"1", "2"}) - }) + gtest.Case(t, func() { + expect := []string{"0", "1", "2", "3"} + array := garray.NewStringArrayFrom(expect) + gtest.Assert(array.Slice(), expect) + gtest.Assert(array.PopLeft(), "0") + gtest.Assert(array.PopRight(), "3") + gtest.AssertIN(array.PopRand(), []string{"1", "2"}) + gtest.AssertIN(array.PopRand(), []string{"1", "2"}) + gtest.Assert(array.Len(), 0) + array.PushLeft("1").PushRight("2") + gtest.Assert(array.Slice(), []string{"1", "2"}) + }) } func TestStringArray_PopLeftsAndPopRights(t *testing.T) { - gtest.Case(t, func() { - value1 := []string{"0","1","2","3","4","5","6"} - value2 := []string{"0","1","2","3","4","5","6"} - array1 := garray.NewStringArrayFrom(value1) - array2 := garray.NewStringArrayFrom(value2) - gtest.Assert(array1.PopLefts(2), []interface{}{"0","1"}) - gtest.Assert(array1.Slice(), []interface{}{"2","3","4","5","6"}) - gtest.Assert(array1.PopRights(2), []interface{}{"5","6"}) - gtest.Assert(array1.Slice(), []interface{}{"2","3","4"}) - gtest.Assert(array1.PopRights(20), []interface{}{"2","3","4"}) - gtest.Assert(array1.Slice(), []interface{}{}) - gtest.Assert(array2.PopLefts(20), []interface{}{"0","1","2","3","4","5","6"}) - gtest.Assert(array2.Slice(), []interface{}{}) - }) + gtest.Case(t, func() { + value1 := []string{"0", "1", "2", "3", "4", "5", "6"} + value2 := []string{"0", "1", "2", "3", "4", "5", "6"} + array1 := garray.NewStringArrayFrom(value1) + array2 := garray.NewStringArrayFrom(value2) + gtest.Assert(array1.PopLefts(2), []interface{}{"0", "1"}) + gtest.Assert(array1.Slice(), []interface{}{"2", "3", "4", "5", "6"}) + gtest.Assert(array1.PopRights(2), []interface{}{"5", "6"}) + gtest.Assert(array1.Slice(), []interface{}{"2", "3", "4"}) + gtest.Assert(array1.PopRights(20), []interface{}{"2", "3", "4"}) + gtest.Assert(array1.Slice(), []interface{}{}) + gtest.Assert(array2.PopLefts(20), []interface{}{"0", "1", "2", "3", "4", "5", "6"}) + gtest.Assert(array2.Slice(), []interface{}{}) + }) } func TestString_Range(t *testing.T) { - gtest.Case(t, func() { - value1 := []string{"0","1","2","3","4","5","6"} - array1 := garray.NewStringArrayFrom(value1) - gtest.Assert(array1.Range(0, 1), []interface{}{"0"}) - gtest.Assert(array1.Range(1, 2), []interface{}{"1"}) - gtest.Assert(array1.Range(0, 2), []interface{}{"0", "1"}) - gtest.Assert(array1.Range(-1, 10), value1) - }) + gtest.Case(t, func() { + value1 := []string{"0", "1", "2", "3", "4", "5", "6"} + array1 := garray.NewStringArrayFrom(value1) + gtest.Assert(array1.Range(0, 1), []interface{}{"0"}) + gtest.Assert(array1.Range(1, 2), []interface{}{"1"}) + gtest.Assert(array1.Range(0, 2), []interface{}{"0", "1"}) + gtest.Assert(array1.Range(-1, 10), value1) + }) } func TestStringArray_Merge(t *testing.T) { - gtest.Case(t, func() { - a1 := []string{"0", "1", "2", "3"} - a2 := []string{"4", "5", "6", "7"} - array1 := garray.NewStringArrayFrom(a1) - array2 := garray.NewStringArrayFrom(a2) - gtest.Assert(array1.Merge(array2).Slice(), []string{"0","1","2","3","4","5","6","7"}) - }) + gtest.Case(t, func() { + a1 := []string{"0", "1", "2", "3"} + a2 := []string{"4", "5", "6", "7"} + array1 := garray.NewStringArrayFrom(a1) + array2 := garray.NewStringArrayFrom(a2) + gtest.Assert(array1.Merge(array2).Slice(), []string{"0", "1", "2", "3", "4", "5", "6", "7"}) + }) } func TestStringArray_Fill(t *testing.T) { - gtest.Case(t, func() { - a1 := []string{"0"} - a2 := []string{"0"} - array1 := garray.NewStringArrayFrom(a1) - array2 := garray.NewStringArrayFrom(a2) - gtest.Assert(array1.Fill(1, 2, "100").Slice(), []string{"0","100","100"}) - gtest.Assert(array2.Fill(0, 2, "100").Slice(), []string{"100","100"}) - }) + gtest.Case(t, func() { + a1 := []string{"0"} + a2 := []string{"0"} + array1 := garray.NewStringArrayFrom(a1) + array2 := garray.NewStringArrayFrom(a2) + gtest.Assert(array1.Fill(1, 2, "100").Slice(), []string{"0", "100", "100"}) + gtest.Assert(array2.Fill(0, 2, "100").Slice(), []string{"100", "100"}) + }) } func TestStringArray_Chunk(t *testing.T) { - gtest.Case(t, func() { - a1 := []string{"1","2","3","4","5"} - array1 := garray.NewStringArrayFrom(a1) - chunks := array1.Chunk(2) - gtest.Assert(len(chunks), 3) - gtest.Assert(chunks[0], []string{"1","2"}) - gtest.Assert(chunks[1], []string{"3","4"}) - gtest.Assert(chunks[2], []string{"5"}) - }) + gtest.Case(t, func() { + a1 := []string{"1", "2", "3", "4", "5"} + array1 := garray.NewStringArrayFrom(a1) + chunks := array1.Chunk(2) + gtest.Assert(len(chunks), 3) + gtest.Assert(chunks[0], []string{"1", "2"}) + gtest.Assert(chunks[1], []string{"3", "4"}) + gtest.Assert(chunks[2], []string{"5"}) + }) } func TestStringArray_Pad(t *testing.T) { - gtest.Case(t, func() { - a1 := []string{"0"} - array1 := garray.NewStringArrayFrom(a1) - gtest.Assert(array1.Pad(3, "1").Slice(), []string{"0","1","1"}) - gtest.Assert(array1.Pad(-4, "1").Slice(), []string{"1","0","1","1"}) - gtest.Assert(array1.Pad(3, "1").Slice(), []string{"1","0","1","1"}) - }) + gtest.Case(t, func() { + a1 := []string{"0"} + array1 := garray.NewStringArrayFrom(a1) + gtest.Assert(array1.Pad(3, "1").Slice(), []string{"0", "1", "1"}) + gtest.Assert(array1.Pad(-4, "1").Slice(), []string{"1", "0", "1", "1"}) + gtest.Assert(array1.Pad(3, "1").Slice(), []string{"1", "0", "1", "1"}) + }) } func TestStringArray_SubSlice(t *testing.T) { - gtest.Case(t, func() { - a1 := []string{"0","1","2","3","4","5","6"} - array1 := garray.NewStringArrayFrom(a1) - gtest.Assert(array1.SubSlice(0, 2), []string{"0","1"}) - gtest.Assert(array1.SubSlice(2, 2), []string{"2","3"}) - gtest.Assert(array1.SubSlice(5, 8), []string{"5","6"}) - }) + gtest.Case(t, func() { + a1 := []string{"0", "1", "2", "3", "4", "5", "6"} + array1 := garray.NewStringArrayFrom(a1) + gtest.Assert(array1.SubSlice(0, 2), []string{"0", "1"}) + gtest.Assert(array1.SubSlice(2, 2), []string{"2", "3"}) + gtest.Assert(array1.SubSlice(5, 8), []string{"5", "6"}) + }) } func TestStringArray_Rand(t *testing.T) { - gtest.Case(t, func() { - a1 := []string{"0","1","2","3","4","5","6"} - array1 := garray.NewStringArrayFrom(a1) - gtest.Assert(len(array1.Rands(2)), "2") - gtest.Assert(len(array1.Rands(10)), "7") - gtest.AssertIN(array1.Rands(1)[0], a1) - }) + gtest.Case(t, func() { + a1 := []string{"0", "1", "2", "3", "4", "5", "6"} + array1 := garray.NewStringArrayFrom(a1) + gtest.Assert(len(array1.Rands(2)), "2") + gtest.Assert(len(array1.Rands(10)), "7") + gtest.AssertIN(array1.Rands(1)[0], a1) + }) } func TestStringArray_PopRands(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{"100", "200", "300", "400", "500", "600"} - array := garray.NewFromCopy(a1) - gtest.AssertIN(array.PopRands(2), a1) - }) + gtest.Case(t, func() { + a2 := []string{"100", "200", "300", "400", "500", "600"} + array3 := garray.NewStringArrayFrom(a2,true) + gtest.AssertIN((array3.PopRands(1)), a2) + gtest.Assert(len(array3.PopRands(1)), 1) + }) } func TestStringArray_Shuffle(t *testing.T) { - gtest.Case(t, func() { - a1 := []string{"0","1","2","3","4","5","6"} - array1 := garray.NewStringArrayFrom(a1) - gtest.Assert(array1.Shuffle().Len(), 7) - }) + gtest.Case(t, func() { + a1 := []string{"0", "1", "2", "3", "4", "5", "6"} + array1 := garray.NewStringArrayFrom(a1) + gtest.Assert(array1.Shuffle().Len(), 7) + }) } func TestStringArray_Reverse(t *testing.T) { - gtest.Case(t, func() { - a1 := []string{"0","1","2","3","4","5","6"} - array1 := garray.NewStringArrayFrom(a1) - gtest.Assert(array1.Reverse().Slice(), []string{"6","5","4","3","2","1","0"}) - }) + gtest.Case(t, func() { + a1 := []string{"0", "1", "2", "3", "4", "5", "6"} + array1 := garray.NewStringArrayFrom(a1) + gtest.Assert(array1.Reverse().Slice(), []string{"6", "5", "4", "3", "2", "1", "0"}) + }) } func TestStringArray_Join(t *testing.T) { - gtest.Case(t, func() { - a1 := []string{"0","1","2","3","4","5","6"} - array1 := garray.NewStringArrayFrom(a1) - gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6") - }) -} \ No newline at end of file + gtest.Case(t, func() { + a1 := []string{"0", "1", "2", "3", "4", "5", "6"} + array1 := garray.NewStringArrayFrom(a1) + gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6") + }) +} + +func TestNewStringArrayFromCopy(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"0", "1", "2", "3", "4", "5", "6"} + a2 := garray.NewStringArrayFromCopy(a1) + a3 := garray.NewStringArrayFromCopy(a1, true) + gtest.Assert(a2.Contains("1"), true) + gtest.Assert(a2.Len(), 7) + gtest.Assert(a2, a3) + }) +} + +func TestStringArray_SetArray(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"0", "1", "2", "3", "4", "5", "6"} + a2 := []string{"a", "b", "c", "d"} + array1 := garray.NewStringArrayFrom(a1) + gtest.Assert(array1.Contains("2"), true) + gtest.Assert(array1.Len(), 7) + + array1 = array1.SetArray(a2) + gtest.Assert(array1.Contains("2"), false) + gtest.Assert(array1.Contains("c"), true) + gtest.Assert(array1.Len(), 4) + + }) +} + +func TestStringArray_Replace(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"0", "1", "2", "3", "4", "5", "6"} + a2 := []string{"a", "b", "c", "d"} + a3 := []string{"o", "p", "q", "x", "y", "z", "w", "r", "v"} + array1 := garray.NewStringArrayFrom(a1) + gtest.Assert(array1.Contains("2"), true) + gtest.Assert(array1.Len(), 7) + + array1 = array1.Replace(a2) + gtest.Assert(array1.Contains("2"), false) + gtest.Assert(array1.Contains("c"), true) + gtest.Assert(array1.Contains("5"), true) + gtest.Assert(array1.Len(), 7) + + array1 = array1.Replace(a3) + gtest.Assert(array1.Contains("2"), false) + gtest.Assert(array1.Contains("c"), false) + gtest.Assert(array1.Contains("5"), false) + gtest.Assert(array1.Contains("p"), true) + gtest.Assert(array1.Contains("r"), false) + gtest.Assert(array1.Len(), 7) + + }) +} + +func TestStringArray_Sum(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"0", "1", "2", "3", "4", "5", "6"} + a2 := []string{"0", "a", "3", "4", "5", "6"} + array1 := garray.NewStringArrayFrom(a1) + array2 := garray.NewStringArrayFrom(a2) + gtest.Assert(array1.Sum(), 21) + gtest.Assert(array2.Sum(), 18) + }) +} + +//func TestStringArray_SortFunc(t *testing.T) { +// gtest.Case(t, func() { +// a1 := []string{"0","1","2","3","4","5","6"} +// //a2 := []string{"0","a","3","4","5","6"} +// array1 := garray.NewStringArrayFrom(a1) +// +// lesss:=func(v1,v2 string)bool{ +// if v1>v2{ +// return true +// } +// return false +// } +// gtest.Assert(array1.Len(),7) +// gtest.Assert(lesss("1","2"),false) +// gtest.Assert(array1.SortFunc(lesss("1","2")) ,false) +// +// +// }) +//} + +func TestStringArray_PopRand(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"0", "1", "2", "3", "4", "5", "6"} + array1 := garray.NewStringArrayFrom(a1) + str1 := array1.PopRand() + gtest.Assert(strings.Contains("0,1,2,3,4,5,6", str1), true) + gtest.Assert(array1.Len(), 6) + }) + + // TestStringArray_PopRands() +} diff --git a/go.mod b/go.mod index fd3232e72..081d99cd0 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,3 @@ -module github.com/gogf/gf \ No newline at end of file +module github.com/gogf/gf + +go 1.12 From 597f210f85486689fbc8a969bf474f4c9180221c Mon Sep 17 00:00:00 2001 From: jroam Date: Tue, 11 Jun 2019 18:11:47 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 081d99cd0..fd3232e72 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1 @@ -module github.com/gogf/gf - -go 1.12 +module github.com/gogf/gf \ No newline at end of file From 1ae77f56e5b7b72aae0bd39f0ed82a20513a3b32 Mon Sep 17 00:00:00 2001 From: jroam Date: Tue, 11 Jun 2019 23:32:08 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0garray=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/container/garray/garray_z_unit_int_test.go | 83 ++++++++++++++++++- .../garray/garray_z_unit_interface_test.go | 76 +++++++++++++++++ .../garray/garray_z_unit_string_test.go | 37 +++++++-- 3 files changed, 188 insertions(+), 8 deletions(-) diff --git a/g/container/garray/garray_z_unit_int_test.go b/g/container/garray/garray_z_unit_int_test.go index 49bdf8ce5..099b7bf78 100644 --- a/g/container/garray/garray_z_unit_int_test.go +++ b/g/container/garray/garray_z_unit_int_test.go @@ -198,4 +198,85 @@ func TestIntArray_Join(t *testing.T) { array1 := garray.NewIntArrayFrom(a1) gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6") }) -} \ No newline at end of file +} + +func TestNewSortedIntArrayFrom(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{0,3,2,1,4,5,6} + array1 := garray.NewSortedIntArrayFrom(a1,true) + gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6") + }) +} + +func TestNewSortedIntArrayFromCopy(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{0,5,2,1,4,3,6} + array1 := garray.NewSortedIntArrayFromCopy(a1,false) + //@todo + gtest.Assert(array1.Join("."), "0.5.2.1.4.3.6") + }) +} + +func TestSortedIntArray_SetArray(t *testing.T){ + gtest.Case(t, func() { + a1 := []int{0,1,2,3} + a2 := []int{4,5,6} + array1 := garray.NewSortedIntArrayFrom(a1) + array2:=array1.SetArray(a2) + + gtest.Assert(array2.Len(),3) + gtest.Assert(array2.Search(3),0) + gtest.Assert(array2.Search(5),1) + gtest.Assert(array2.Search(6),2) + }) +} + +func TestSortedIntArray_Sort(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{0,3,2,1} + + array1 := garray.NewSortedIntArrayFrom(a1) + array2:=array1.Sort() + + gtest.Assert(array2.Len(),4) + gtest.Assert(array2,[]int{0,1,2,3}) + + }) +} + +func TestSortedIntArray_Get(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1,3,5,0} + array1 := garray.NewSortedIntArrayFrom(a1) + gtest.Assert(array1.Get(0),0) + gtest.Assert(array1.Get(1),1) + gtest.Assert(array1.Get(3),5) + + }) +} + +func TestSortedIntArray_Remove(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1,3,5,0} + array1 := garray.NewSortedIntArrayFrom(a1) + i1:=array1.Remove(2) + gtest.Assert(i1,3) + gtest.Assert(array1.Search(5),2) + + // 再次删除剩下的数组中的第一个 + i2:=array1.Remove(0) + gtest.Assert(i2,0) + gtest.Assert(array1.Search(5),1) + + + a2 := []int{1,3} + array2 := garray.NewSortedIntArrayFrom(a2) + i3:=array2.Remove(1) + gtest.Assert(i3,3) + t.Log(array2) + gtest.Assert(array2.Search(2),-1) + gtest.Assert(array2.Search(1),0) + + }) +} + diff --git a/g/container/garray/garray_z_unit_interface_test.go b/g/container/garray/garray_z_unit_interface_test.go index 2b21e35c3..9dbce416c 100644 --- a/g/container/garray/garray_z_unit_interface_test.go +++ b/g/container/garray/garray_z_unit_interface_test.go @@ -201,4 +201,80 @@ func TestArray_Join(t *testing.T) { array1 := garray.NewArrayFrom(a1) gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6") }) +} + +func TestArray_Replace(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{0,1,2,3,4,5,6} + a2 := []interface{}{"a","b","c"} + a3 := []interface{}{"m","n","p","z","x","y","d","u"} + array1 := garray.NewArrayFrom(a1) + array2:=array1.Replace(a2) + gtest.Assert(array2.Len(), 7) + gtest.Assert(array2.Contains("b"),true) + gtest.Assert(array2.Contains(4),true) + gtest.Assert(array2.Contains("v"),false) + array3:=array1.Replace(a3) + gtest.Assert(array3.Len(), 7) + gtest.Assert(array3.Contains(4),false) + gtest.Assert(array3.Contains("p"),true) + gtest.Assert(array3.Contains("u"),false) + }) +} + +func TestArray_SetArray(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{0,1,2,3,4,5,6} + a2 := []interface{}{"a","b","c"} + + array1 := garray.NewArrayFrom(a1) + array1=array1.SetArray(a2) + gtest.Assert(array1.Len(), 3) + gtest.Assert(array1.Contains("b"), true) + gtest.Assert(array1.Contains("5"), false) + }) +} + +func TestArray_Sum(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{0,1,2,3} + a2 := []interface{}{"a","b","c"} + a3 := []interface{}{"a","1","2"} + + array1 := garray.NewArrayFrom(a1) + array2 := garray.NewArrayFrom(a2) + array3 := garray.NewArrayFrom(a3) + + gtest.Assert(array1.Sum(), 6) + gtest.Assert(array2.Sum(), 0) + gtest.Assert(array3.Sum(), 3) + + }) +} + +func TestArray_Clone(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{0,1,2,3} + + + array1 := garray.NewArrayFrom(a1) + array2:=array1.Clone() + + gtest.Assert(array1.Len(), 4) + gtest.Assert(array2.Sum(), 6) + gtest.AssertEQ(array1, array2) + + + }) +} + +func TestArray_CountValues(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a","b","c","d","e","d"} + array1 := garray.NewArrayFrom(a1) + array2:=array1.CountValues() + gtest.Assert(len(array2),5) + gtest.Assert(array2["b"], 1) + gtest.Assert(array2["d"], 2) + }) } \ No newline at end of file diff --git a/g/container/garray/garray_z_unit_string_test.go b/g/container/garray/garray_z_unit_string_test.go index 1994abeff..e5869548e 100644 --- a/g/container/garray/garray_z_unit_string_test.go +++ b/g/container/garray/garray_z_unit_string_test.go @@ -171,10 +171,13 @@ func TestStringArray_Rand(t *testing.T) { func TestStringArray_PopRands(t *testing.T) { gtest.Case(t, func() { - a2 := []string{"100", "200", "300", "400", "500", "600"} - array3 := garray.NewStringArrayFrom(a2,true) - gtest.AssertIN((array3.PopRands(1)), a2) - gtest.Assert(len(array3.PopRands(1)), 1) + a1 := []string{"a", "b", "c", "d", "e", "f", "g"} + a2 := []string{"1", "2", "3", "4", "5", "6", "7"} + array1 := garray.NewStringArrayFrom(a1) + //todo gtest.AssertIN(array1.PopRands(1),a1) + gtest.AssertIN(array1.PopRands(1), strings.Join(a1, ",")) + gtest.AssertNI(array1.PopRands(1), strings.Join(a2, ",")) + }) } @@ -225,7 +228,6 @@ func TestStringArray_SetArray(t *testing.T) { gtest.Assert(array1.Contains("2"), false) gtest.Assert(array1.Contains("c"), true) gtest.Assert(array1.Len(), 4) - }) } @@ -294,6 +296,27 @@ func TestStringArray_PopRand(t *testing.T) { gtest.Assert(strings.Contains("0,1,2,3,4,5,6", str1), true) gtest.Assert(array1.Len(), 6) }) - - // TestStringArray_PopRands() +} + +func TestStringArray_Clone(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"0", "1", "2", "3", "4", "5", "6"} + array1 := garray.NewStringArrayFrom(a1) + array2 := array1.Clone() + gtest.Assert(array2, array1) + gtest.Assert(array2.Len(), 7) + }) +} + +func TestStringArray_CountValues(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"0", "1", "2", "3", "4", "4", "6"} + array1 := garray.NewStringArrayFrom(a1) + + m1 := array1.CountValues() + gtest.Assert(len(m1), 6) + gtest.Assert(m1["2"], 1) + gtest.Assert(m1["4"], 2) + + }) } From c6dfb4d4f8c6522ea5d78c39fea716d1c609f0ad Mon Sep 17 00:00:00 2001 From: jroam Date: Wed, 12 Jun 2019 16:52:02 +0800 Subject: [PATCH 06/12] add some test --- g/container/garray/garray_z_unit_int_test.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/g/container/garray/garray_z_unit_int_test.go b/g/container/garray/garray_z_unit_int_test.go index 099b7bf78..10edebef4 100644 --- a/g/container/garray/garray_z_unit_int_test.go +++ b/g/container/garray/garray_z_unit_int_test.go @@ -269,13 +269,14 @@ func TestSortedIntArray_Remove(t *testing.T) { gtest.Assert(array1.Search(5),1) - a2 := []int{1,3} + a2 := []int{1} array2 := garray.NewSortedIntArrayFrom(a2) - i3:=array2.Remove(1) - gtest.Assert(i3,3) - t.Log(array2) gtest.Assert(array2.Search(2),-1) - gtest.Assert(array2.Search(1),0) + + + // gtest.Assert(array2.Search(1),0) + //i3:=array2.Remove(1) + // gtest.Assert(i3,3) }) } From cdb9488752b8dcd5adff3b4e5188a419180cce65 Mon Sep 17 00:00:00 2001 From: jroam Date: Wed, 12 Jun 2019 17:14:08 +0800 Subject: [PATCH 07/12] edit tests --- g/container/garray/garray_sorted_int.go | 626 ++++++++++++------------ go.mod | 3 +- 2 files changed, 318 insertions(+), 311 deletions(-) diff --git a/g/container/garray/garray_sorted_int.go b/g/container/garray/garray_sorted_int.go index f2fd289e7..3e809430a 100644 --- a/g/container/garray/garray_sorted_int.go +++ b/g/container/garray/garray_sorted_int.go @@ -7,289 +7,289 @@ package garray import ( - "bytes" + "bytes" "fmt" "github.com/gogf/gf/g/container/gtype" - "github.com/gogf/gf/g/internal/rwmutex" - "github.com/gogf/gf/g/util/gconv" - "github.com/gogf/gf/g/util/grand" - "math" - "sort" + "github.com/gogf/gf/g/internal/rwmutex" + "github.com/gogf/gf/g/util/gconv" + "github.com/gogf/gf/g/util/grand" + "math" + "sort" ) // It's using increasing order in default. type SortedIntArray struct { - mu *rwmutex.RWMutex - array []int - unique *gtype.Bool // Whether enable unique feature(false) - comparator func(v1, v2 int) int // Comparison function(it returns -1: v1 < v2; 0: v1 == v2; 1: v1 > v2) + mu *rwmutex.RWMutex + array []int + unique *gtype.Bool // Whether enable unique feature(false) + comparator func(v1, v2 int) int // Comparison function(it returns -1: v1 < v2; 0: v1 == v2; 1: v1 > v2) } // NewSortedIntArray creates and returns an empty sorted array. // The param used to specify whether using array in un-concurrent-safety, // which is false in default. -func NewSortedIntArray(unsafe...bool) *SortedIntArray { - return NewSortedIntArraySize(0, unsafe...) +func NewSortedIntArray(unsafe ...bool) *SortedIntArray { + return NewSortedIntArraySize(0, unsafe...) } // NewSortedIntArraySize create and returns an sorted array with given size and cap. // The param used to specify whether using array in un-concurrent-safety, // which is false in default. -func NewSortedIntArraySize(cap int, unsafe...bool) *SortedIntArray { - return &SortedIntArray { - mu : rwmutex.New(unsafe...), - array : make([]int, 0, cap), - unique : gtype.NewBool(), - comparator : func(v1, v2 int) int { - if v1 < v2 { - return -1 - } - if v1 > v2 { - return 1 - } - return 0 - }, - } +func NewSortedIntArraySize(cap int, unsafe ...bool) *SortedIntArray { + return &SortedIntArray{ + mu: rwmutex.New(unsafe...), + array: make([]int, 0, cap), + unique: gtype.NewBool(), + comparator: func(v1, v2 int) int { + if v1 < v2 { + return -1 + } + if v1 > v2 { + return 1 + } + return 0 + }, + } } // NewIntArrayFrom creates and returns an sorted array with given slice . // The param used to specify whether using array in un-concurrent-safety, // which is false in default. -func NewSortedIntArrayFrom(array []int, unsafe...bool) *SortedIntArray { - a := NewSortedIntArraySize(0, unsafe...) - a.array = array - sort.Ints(a.array) - return a +func NewSortedIntArrayFrom(array []int, unsafe ...bool) *SortedIntArray { + a := NewSortedIntArraySize(0, unsafe...) + a.array = array + sort.Ints(a.array) + return a } // NewSortedIntArrayFromCopy creates and returns an sorted array from a copy of given slice . // The param used to specify whether using array in un-concurrent-safety, // which is false in default. -func NewSortedIntArrayFromCopy(array []int, unsafe...bool) *SortedIntArray { - newArray := make([]int, len(array)) - copy(newArray, array) - return &SortedIntArray{ - mu : rwmutex.New(unsafe...), - array : newArray, - } +func NewSortedIntArrayFromCopy(array []int, unsafe ...bool) *SortedIntArray { + newArray := make([]int, len(array)) + copy(newArray, array) + return &SortedIntArray{ + mu: rwmutex.New(unsafe...), + array: newArray, + } } // SetArray sets the underlying slice array with the given . func (a *SortedIntArray) SetArray(array []int) *SortedIntArray { - a.mu.Lock() - defer a.mu.Unlock() - a.array = array - sort.Ints(a.array) - return a + a.mu.Lock() + defer a.mu.Unlock() + a.array = array + sort.Ints(a.array) + return a } // Sort sorts the array in increasing order. // The param controls whether sort // in increasing order(default) or decreasing order. func (a *SortedIntArray) Sort() *SortedIntArray { - a.mu.Lock() - defer a.mu.Unlock() - sort.Ints(a.array) - return a + a.mu.Lock() + defer a.mu.Unlock() + sort.Ints(a.array) + return a } // Add adds one or multiple values to sorted array, the array always keeps sorted. -func (a *SortedIntArray) Add(values...int) *SortedIntArray { - if len(values) == 0 { - return a - } - a.mu.Lock() - defer a.mu.Unlock() - for _, value := range values { - index, cmp := a.binSearch(value, false) - if a.unique.Val() && cmp == 0 { - continue - } - if index < 0 { - a.array = append(a.array, value) - continue - } - if cmp > 0 { - index++ - } - rear := append([]int{}, a.array[index : ]...) - a.array = append(a.array[0 : index], value) - a.array = append(a.array, rear...) - } - return a +func (a *SortedIntArray) Add(values ...int) *SortedIntArray { + if len(values) == 0 { + return a + } + a.mu.Lock() + defer a.mu.Unlock() + for _, value := range values { + index, cmp := a.binSearch(value, false) + if a.unique.Val() && cmp == 0 { + continue + } + if index < 0 { + a.array = append(a.array, value) + continue + } + if cmp > 0 { + index++ + } + rear := append([]int{}, a.array[index:]...) + a.array = append(a.array[0:index], value) + a.array = append(a.array, rear...) + } + return a } // Get returns the value of the specified index, // the caller should notice the boundary of the array. func (a *SortedIntArray) Get(index int) int { - a.mu.RLock() - defer a.mu.RUnlock() - value := a.array[index] - return value + a.mu.RLock() + defer a.mu.RUnlock() + value := a.array[index] + return value } // Remove removes an item by index. func (a *SortedIntArray) Remove(index int) int { - a.mu.Lock() - defer a.mu.Unlock() + a.mu.Lock() + defer a.mu.Unlock() // Determine array boundaries when deleting to improve deletion efficiency. - if index == 0 { - value := a.array[0] - a.array = a.array[1 : ] - return value - } else if index == len(a.array) - 1 { - value := a.array[index] - a.array = a.array[: index] - return value - } + if index == 0 { + value := a.array[0] + a.array = a.array[1:] + return value + } else if index == len(a.array)-1 { + value := a.array[index] + a.array = a.array[:index] + return value + } // If it is a non-boundary delete, // it will involve the creation of an array, // then the deletion is less efficient. - value := a.array[index] - a.array = append(a.array[ : index], a.array[index + 1 : ]...) - return value + value := a.array[index] + a.array = append(a.array[:index], a.array[index+1:]...) + return value } // PopLeft pops and returns an item from the beginning of array. func (a *SortedIntArray) PopLeft() int { - a.mu.Lock() - defer a.mu.Unlock() - value := a.array[0] - a.array = a.array[1 : ] - return value + a.mu.Lock() + defer a.mu.Unlock() + value := a.array[0] + a.array = a.array[1:] + return value } // PopRight pops and returns an item from the end of array. func (a *SortedIntArray) PopRight() int { - a.mu.Lock() - defer a.mu.Unlock() - index := len(a.array) - 1 - value := a.array[index] - a.array = a.array[: index] - return value + a.mu.Lock() + defer a.mu.Unlock() + index := len(a.array) - 1 + value := a.array[index] + a.array = a.array[:index] + return value } // PopRand randomly pops and return an item out of array. func (a *SortedIntArray) PopRand() int { - return a.Remove(grand.Intn(len(a.array))) + return a.Remove(grand.Intn(len(a.array))) } // PopRands randomly pops and returns items out of array. func (a *SortedIntArray) PopRands(size int) []int { - a.mu.Lock() - defer a.mu.Unlock() - if size > len(a.array) { - size = len(a.array) - } - array := make([]int, size) - for i := 0; i < size; i++ { - index := grand.Intn(len(a.array)) - array[i] = a.array[index] - a.array = append(a.array[ : index], a.array[index + 1 : ]...) - } - return array + a.mu.Lock() + defer a.mu.Unlock() + if size > len(a.array) { + size = len(a.array) + } + array := make([]int, size) + for i := 0; i < size; i++ { + index := grand.Intn(len(a.array)) + array[i] = a.array[index] + a.array = append(a.array[:index], a.array[index+1:]...) + } + return array } // PopLefts pops and returns items from the beginning of array. func (a *SortedIntArray) PopLefts(size int) []int { - a.mu.Lock() - defer a.mu.Unlock() - length := len(a.array) - if size > length { - size = length - } - value := a.array[0 : size] - a.array = a.array[size : ] - return value + a.mu.Lock() + defer a.mu.Unlock() + length := len(a.array) + if size > length { + size = length + } + value := a.array[0:size] + a.array = a.array[size:] + return value } // PopRights pops and returns items from the end of array. func (a *SortedIntArray) PopRights(size int) []int { - a.mu.Lock() - defer a.mu.Unlock() - index := len(a.array) - size - if index < 0 { - index = 0 - } - value := a.array[index :] - a.array = a.array[ : index] - return value + a.mu.Lock() + defer a.mu.Unlock() + index := len(a.array) - size + if index < 0 { + index = 0 + } + value := a.array[index:] + a.array = a.array[:index] + return value } // Range picks and returns items by range, like array[start:end]. // Notice, if in concurrent-safe usage, it returns a copy of slice; // else a pointer to the underlying data. func (a *SortedIntArray) Range(start, end int) []int { - a.mu.RLock() - defer a.mu.RUnlock() - length := len(a.array) - if start > length || start > end { - return nil - } - if start < 0 { - start = 0 - } - if end > length { - end = length - } - array := ([]int)(nil) - if a.mu.IsSafe() { - a.mu.RLock() - defer a.mu.RUnlock() - array = make([]int, end - start) - copy(array, a.array[start : end]) - } else { - array = a.array[start : end] - } - return array + a.mu.RLock() + defer a.mu.RUnlock() + length := len(a.array) + if start > length || start > end { + return nil + } + if start < 0 { + start = 0 + } + if end > length { + end = length + } + array := ([]int)(nil) + if a.mu.IsSafe() { + a.mu.RLock() + defer a.mu.RUnlock() + array = make([]int, end-start) + copy(array, a.array[start:end]) + } else { + array = a.array[start:end] + } + return array } // Len returns the length of array. func (a *SortedIntArray) Len() int { - a.mu.RLock() - length := len(a.array) - a.mu.RUnlock() - return length + a.mu.RLock() + length := len(a.array) + a.mu.RUnlock() + return length } // Sum returns the sum of values in an array. func (a *SortedIntArray) Sum() (sum int) { - a.mu.RLock() - defer a.mu.RUnlock() - for _, v := range a.array { - sum += v - } - return + a.mu.RLock() + defer a.mu.RUnlock() + for _, v := range a.array { + sum += v + } + return } // Slice returns the underlying data of array. // Notice, if in concurrent-safe usage, it returns a copy of slice; // else a pointer to the underlying data. func (a *SortedIntArray) Slice() []int { - array := ([]int)(nil) - if a.mu.IsSafe() { - a.mu.RLock() - defer a.mu.RUnlock() - array = make([]int, len(a.array)) - copy(array, a.array) - } else { - array = a.array - } - return array + array := ([]int)(nil) + if a.mu.IsSafe() { + a.mu.RLock() + defer a.mu.RUnlock() + array = make([]int, len(a.array)) + copy(array, a.array) + } else { + array = a.array + } + return array } // Contains checks whether a value exists in the array. func (a *SortedIntArray) Contains(value int) bool { - return a.Search(value) == 0 + return a.Search(value) == 0 } // Search searches array by , returns the index of , // or returns -1 if not exists. func (a *SortedIntArray) Search(value int) (index int) { - if i, r := a.binSearch(value, true); r == 0 { - return i - } - return -1 + if i, r := a.binSearch(value, true); r == 0 { + return i + } + return -1 } // Binary search. @@ -298,93 +298,95 @@ func (a *SortedIntArray) Search(value int) (index int) { // If lesser than 0, it means the value at is lesser than . // If greater than 0, it means the value at is greater than . func (a *SortedIntArray) binSearch(value int, lock bool) (index int, result int) { - if len(a.array) == 0 { - return -1, -2 - } - if lock { - a.mu.RLock() - defer a.mu.RUnlock() - } - min := 0 - max := len(a.array) - 1 - mid := 0 - cmp := -2 - for min <= max { - mid = int((min + max) / 2) - cmp = a.comparator(value, a.array[mid]) - switch { - case cmp < 0 : max = mid - 1 - case cmp > 0 : min = mid + 1 - default : - return mid, cmp - } - } - return mid, cmp + if len(a.array) == 0 { + return -1, -2 + } + if lock { + a.mu.RLock() + defer a.mu.RUnlock() + } + min := 0 + max := len(a.array) - 1 + mid := 0 + cmp := -2 + for min <= max { + mid = int((min + max) / 2) + cmp = a.comparator(value, a.array[mid]) + switch { + case cmp < 0: + max = mid - 1 + case cmp > 0: + min = mid + 1 + default: + return mid, cmp + } + } + return mid, cmp } // SetUnique sets unique mark to the array, // which means it does not contain any repeated items. // It also do unique check, remove all repeated items. func (a *SortedIntArray) SetUnique(unique bool) *SortedIntArray { - oldUnique := a.unique.Val() - a.unique.Set(unique) - if unique && oldUnique != unique { - a.Unique() - } - return a + oldUnique := a.unique.Val() + a.unique.Set(unique) + if unique && oldUnique != unique { + a.Unique() + } + return a } // Unique uniques the array, clear repeated items. func (a *SortedIntArray) Unique() *SortedIntArray { - a.mu.Lock() - i := 0 - for { - if i == len(a.array) - 1 { - break - } - if a.comparator(a.array[i], a.array[i + 1]) == 0 { - a.array = append(a.array[ : i + 1], a.array[i + 1 + 1 : ]...) - } else { - i++ - } - } - a.mu.Unlock() - return a + a.mu.Lock() + i := 0 + for { + if i == len(a.array)-1 { + break + } + if a.comparator(a.array[i], a.array[i+1]) == 0 { + a.array = append(a.array[:i+1], a.array[i+1+1:]...) + } else { + i++ + } + } + a.mu.Unlock() + return a } // Clone returns a new array, which is a copy of current array. func (a *SortedIntArray) Clone() (newArray *SortedIntArray) { - a.mu.RLock() - array := make([]int, len(a.array)) - copy(array, a.array) - a.mu.RUnlock() - return NewSortedIntArrayFrom(array, !a.mu.IsSafe()) + a.mu.RLock() + array := make([]int, len(a.array)) + copy(array, a.array) + a.mu.RUnlock() + return NewSortedIntArrayFrom(array, !a.mu.IsSafe()) } // Clear deletes all items of current array. func (a *SortedIntArray) Clear() *SortedIntArray { - a.mu.Lock() - if len(a.array) > 0 { - a.array = make([]int, 0) - } - a.mu.Unlock() - return a + a.mu.Lock() + if len(a.array) > 0 { + a.array = make([]int, 0) + } + a.mu.Unlock() + return a } // LockFunc locks writing by callback function . func (a *SortedIntArray) LockFunc(f func(array []int)) *SortedIntArray { - a.mu.Lock() - defer a.mu.Unlock() - f(a.array) - return a + a.mu.Lock() + defer a.mu.Unlock() + f(a.array) + return a } // RLockFunc locks reading by callback function . func (a *SortedIntArray) RLockFunc(f func(array []int)) *SortedIntArray { - a.mu.RLock() - defer a.mu.RUnlock() - f(a.array) - return a + a.mu.RLock() + defer a.mu.RUnlock() + f(a.array) + return a } // Merge merges into current array. @@ -392,99 +394,105 @@ func (a *SortedIntArray) RLockFunc(f func(array []int)) *SortedIntArray { // The difference between Merge and Append is Append supports only specified slice type, // but Merge supports more parameter types. func (a *SortedIntArray) Merge(array interface{}) *SortedIntArray { - switch v := array.(type) { - case *Array: a.Add(gconv.Ints(v.Slice())...) - case *IntArray: a.Add(gconv.Ints(v.Slice())...) - case *StringArray: a.Add(gconv.Ints(v.Slice())...) - case *SortedArray: a.Add(gconv.Ints(v.Slice())...) - case *SortedIntArray: a.Add(gconv.Ints(v.Slice())...) - case *SortedStringArray: a.Add(gconv.Ints(v.Slice())...) - default: - a.Add(gconv.Ints(array)...) - } - return a + switch v := array.(type) { + case *Array: + a.Add(gconv.Ints(v.Slice())...) + case *IntArray: + a.Add(gconv.Ints(v.Slice())...) + case *StringArray: + a.Add(gconv.Ints(v.Slice())...) + case *SortedArray: + a.Add(gconv.Ints(v.Slice())...) + case *SortedIntArray: + a.Add(gconv.Ints(v.Slice())...) + case *SortedStringArray: + a.Add(gconv.Ints(v.Slice())...) + default: + a.Add(gconv.Ints(array)...) + } + return a } // Chunk splits an array into multiple arrays, // the size of each array is determined by . // The last chunk may contain less than size elements. func (a *SortedIntArray) Chunk(size int) [][]int { - if size < 1 { - return nil - } - a.mu.RLock() - defer a.mu.RUnlock() - length := len(a.array) - chunks := int(math.Ceil(float64(length) / float64(size))) - var n [][]int - for i, end := 0, 0; chunks > 0; chunks-- { - end = (i + 1) * size - if end > length { - end = length - } - n = append(n, a.array[i*size : end]) - i++ - } - return n + if size < 1 { + return nil + } + a.mu.RLock() + defer a.mu.RUnlock() + length := len(a.array) + chunks := int(math.Ceil(float64(length) / float64(size))) + var n [][]int + for i, end := 0, 0; chunks > 0; chunks-- { + end = (i + 1) * size + if end > length { + end = length + } + n = append(n, a.array[i*size:end]) + i++ + } + return n } // SubSlice returns a slice of elements from the array as specified // by the and parameters. // If in concurrent safe usage, it returns a copy of the slice; else a pointer. func (a *SortedIntArray) SubSlice(offset, size int) []int { - a.mu.RLock() - defer a.mu.RUnlock() - if offset > len(a.array) { - return nil - } - if offset + size > len(a.array) { - size = len(a.array) - offset - } - if a.mu.IsSafe() { - s := make([]int, size) - copy(s, a.array[offset:]) - return s - } else { - return a.array[offset:] - } + a.mu.RLock() + defer a.mu.RUnlock() + if offset > len(a.array) { + return nil + } + if offset+size > len(a.array) { + size = len(a.array) - offset + } + if a.mu.IsSafe() { + s := make([]int, size) + copy(s, a.array[offset:]) + return s + } else { + return a.array[offset:] + } } // Rand randomly returns one item from array(no deleting). func (a *SortedIntArray) Rand() int { - a.mu.RLock() - defer a.mu.RUnlock() - return a.array[grand.Intn(len(a.array))] + a.mu.RLock() + defer a.mu.RUnlock() + return a.array[grand.Intn(len(a.array))] } // Rands randomly returns items from array(no deleting). func (a *SortedIntArray) Rands(size int) []int { - a.mu.RLock() - defer a.mu.RUnlock() - if size > len(a.array) { - size = len(a.array) - } - n := make([]int, size) - for i, v := range grand.Perm(len(a.array)) { - n[i] = a.array[v] - if i == size - 1 { - break - } - } - return n + a.mu.RLock() + defer a.mu.RUnlock() + if size > len(a.array) { + size = len(a.array) + } + n := make([]int, size) + for i, v := range grand.Perm(len(a.array)) { + n[i] = a.array[v] + if i == size-1 { + break + } + } + return n } // Join joins array elements with a string . func (a *SortedIntArray) Join(glue string) string { - a.mu.RLock() - defer a.mu.RUnlock() - buffer := bytes.NewBuffer(nil) - for k, v := range a.array { - buffer.WriteString(gconv.String(v)) - if k != len(a.array) - 1 { - buffer.WriteString(glue) - } - } - return buffer.String() + a.mu.RLock() + defer a.mu.RUnlock() + buffer := bytes.NewBuffer(nil) + for k, v := range a.array { + buffer.WriteString(gconv.String(v)) + if k != len(a.array)-1 { + buffer.WriteString(glue) + } + } + return buffer.String() } // CountValues counts the number of occurrences of all values in the array. @@ -503,4 +511,4 @@ func (a *SortedIntArray) String() string { a.mu.RLock() defer a.mu.RUnlock() return fmt.Sprint(a.array) -} \ No newline at end of file +} diff --git a/go.mod b/go.mod index 28729e4f6..fd3232e72 100644 --- a/go.mod +++ b/go.mod @@ -1,2 +1 @@ -module github.com/gogf/gf - +module github.com/gogf/gf \ No newline at end of file From 399e47c5489c3737c52e28e8a69fc83010cf1751 Mon Sep 17 00:00:00 2001 From: jroam Date: Wed, 12 Jun 2019 18:14:54 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/container/garray/garray_z_unit_int_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/g/container/garray/garray_z_unit_int_test.go b/g/container/garray/garray_z_unit_int_test.go index 10edebef4..dd684d6e8 100644 --- a/g/container/garray/garray_z_unit_int_test.go +++ b/g/container/garray/garray_z_unit_int_test.go @@ -225,7 +225,7 @@ func TestSortedIntArray_SetArray(t *testing.T){ array2:=array1.SetArray(a2) gtest.Assert(array2.Len(),3) - gtest.Assert(array2.Search(3),0) + gtest.Assert(array2.Search(3),-1) gtest.Assert(array2.Search(5),1) gtest.Assert(array2.Search(6),2) }) From a901e7177c0cb77d7c717c2552b751c51141df62 Mon Sep 17 00:00:00 2001 From: jroam Date: Wed, 12 Jun 2019 22:37:17 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0garray=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=9C=AA=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/container/garray/garray_z_unit_int_test.go | 603 ++++++++++++------ .../garray/garray_z_unit_string_test.go | 19 + g/container/gset/gset_z_unit_string_test.go | 2 + 3 files changed, 428 insertions(+), 196 deletions(-) diff --git a/g/container/garray/garray_z_unit_int_test.go b/g/container/garray/garray_z_unit_int_test.go index dd684d6e8..aba95f5c8 100644 --- a/g/container/garray/garray_z_unit_int_test.go +++ b/g/container/garray/garray_z_unit_int_test.go @@ -9,275 +9,486 @@ package garray_test import ( - "github.com/gogf/gf/g/container/garray" - "github.com/gogf/gf/g/test/gtest" - "testing" + "github.com/gogf/gf/g/container/garray" + "github.com/gogf/gf/g/test/gtest" + "testing" ) func Test_IntArray_Basic(t *testing.T) { - gtest.Case(t, func() { - expect := []int{0, 1, 2, 3} - array := garray.NewIntArrayFrom(expect) - gtest.Assert(array.Slice(), expect) - array.Set(0, 100) - gtest.Assert(array.Get(0), 100) - gtest.Assert(array.Get(1), 1) - gtest.Assert(array.Search(100), 0) - gtest.Assert(array.Contains(100), true) - gtest.Assert(array.Remove(0), 100) - gtest.Assert(array.Contains(100), false) - array.Append(4) - gtest.Assert(array.Len(), 4) - array.InsertBefore(0, 100) - array.InsertAfter(0, 200) - gtest.Assert(array.Slice(), []int{100, 200, 1, 2, 3, 4}) - array.InsertBefore(5, 300) - array.InsertAfter(6, 400) - gtest.Assert(array.Slice(), []int{100, 200, 1, 2, 3, 300, 4, 400}) - gtest.Assert(array.Clear().Len(), 0) - }) + gtest.Case(t, func() { + expect := []int{0, 1, 2, 3} + array := garray.NewIntArrayFrom(expect) + gtest.Assert(array.Slice(), expect) + array.Set(0, 100) + gtest.Assert(array.Get(0), 100) + gtest.Assert(array.Get(1), 1) + gtest.Assert(array.Search(100), 0) + gtest.Assert(array.Contains(100), true) + gtest.Assert(array.Remove(0), 100) + gtest.Assert(array.Contains(100), false) + array.Append(4) + gtest.Assert(array.Len(), 4) + array.InsertBefore(0, 100) + array.InsertAfter(0, 200) + gtest.Assert(array.Slice(), []int{100, 200, 1, 2, 3, 4}) + array.InsertBefore(5, 300) + array.InsertAfter(6, 400) + gtest.Assert(array.Slice(), []int{100, 200, 1, 2, 3, 300, 4, 400}) + gtest.Assert(array.Clear().Len(), 0) + }) } func TestIntArray_Sort(t *testing.T) { - gtest.Case(t, func() { - expect1 := []int{0, 1, 2, 3} - expect2 := []int{3, 2, 1, 0} - array := garray.NewIntArray() - for i := 3; i >= 0; i-- { - array.Append(i) - } - array.Sort() - gtest.Assert(array.Slice(), expect1) - array.Sort(true) - gtest.Assert(array.Slice(), expect2) - }) + gtest.Case(t, func() { + expect1 := []int{0, 1, 2, 3} + expect2 := []int{3, 2, 1, 0} + array := garray.NewIntArray() + for i := 3; i >= 0; i-- { + array.Append(i) + } + array.Sort() + gtest.Assert(array.Slice(), expect1) + array.Sort(true) + gtest.Assert(array.Slice(), expect2) + }) } func TestIntArray_Unique(t *testing.T) { - gtest.Case(t, func() { - expect := []int{1, 1, 2, 3} - array := garray.NewIntArrayFrom(expect) - gtest.Assert(array.Unique().Slice(), []int{1, 2, 3}) - }) + gtest.Case(t, func() { + expect := []int{1, 1, 2, 3} + array := garray.NewIntArrayFrom(expect) + gtest.Assert(array.Unique().Slice(), []int{1, 2, 3}) + }) } func TestIntArray_PushAndPop(t *testing.T) { - gtest.Case(t, func() { - expect := []int{0, 1, 2, 3} - array := garray.NewIntArrayFrom(expect) - gtest.Assert(array.Slice(), expect) - gtest.Assert(array.PopLeft(), 0) - gtest.Assert(array.PopRight(), 3) - gtest.AssertIN(array.PopRand(), []int{1, 2}) - gtest.AssertIN(array.PopRand(), []int{1, 2}) - gtest.Assert(array.Len(), 0) - array.PushLeft(1).PushRight(2) - gtest.Assert(array.Slice(), []int{1, 2}) - }) + gtest.Case(t, func() { + expect := []int{0, 1, 2, 3} + array := garray.NewIntArrayFrom(expect) + gtest.Assert(array.Slice(), expect) + gtest.Assert(array.PopLeft(), 0) + gtest.Assert(array.PopRight(), 3) + gtest.AssertIN(array.PopRand(), []int{1, 2}) + gtest.AssertIN(array.PopRand(), []int{1, 2}) + gtest.Assert(array.Len(), 0) + array.PushLeft(1).PushRight(2) + gtest.Assert(array.Slice(), []int{1, 2}) + }) } func TestIntArray_PopLeftsAndPopRights(t *testing.T) { - gtest.Case(t, func() { - value1 := []int{0,1,2,3,4,5,6} - value2 := []int{0,1,2,3,4,5,6} - array1 := garray.NewIntArrayFrom(value1) - array2 := garray.NewIntArrayFrom(value2) - gtest.Assert(array1.PopLefts(2), []int{0,1}) - gtest.Assert(array1.Slice(), []int{2,3,4,5,6}) - gtest.Assert(array1.PopRights(2), []int{5,6}) - gtest.Assert(array1.Slice(), []int{2,3,4}) - gtest.Assert(array1.PopRights(20), []int{2,3,4}) - gtest.Assert(array1.Slice(), []int{}) - gtest.Assert(array2.PopLefts(20), []int{0,1,2,3,4,5,6}) - gtest.Assert(array2.Slice(), []int{}) - }) + gtest.Case(t, func() { + value1 := []int{0, 1, 2, 3, 4, 5, 6} + value2 := []int{0, 1, 2, 3, 4, 5, 6} + array1 := garray.NewIntArrayFrom(value1) + array2 := garray.NewIntArrayFrom(value2) + gtest.Assert(array1.PopLefts(2), []int{0, 1}) + gtest.Assert(array1.Slice(), []int{2, 3, 4, 5, 6}) + gtest.Assert(array1.PopRights(2), []int{5, 6}) + gtest.Assert(array1.Slice(), []int{2, 3, 4}) + gtest.Assert(array1.PopRights(20), []int{2, 3, 4}) + gtest.Assert(array1.Slice(), []int{}) + gtest.Assert(array2.PopLefts(20), []int{0, 1, 2, 3, 4, 5, 6}) + gtest.Assert(array2.Slice(), []int{}) + }) } func TestIntArray_Range(t *testing.T) { - gtest.Case(t, func() { - value1 := []int{0,1,2,3,4,5,6} - array1 := garray.NewIntArrayFrom(value1) - gtest.Assert(array1.Range(0, 1), []int{0}) - gtest.Assert(array1.Range(1, 2), []int{1}) - gtest.Assert(array1.Range(0, 2), []int{0, 1}) - gtest.Assert(array1.Range(-1, 10), value1) - }) + gtest.Case(t, func() { + value1 := []int{0, 1, 2, 3, 4, 5, 6} + array1 := garray.NewIntArrayFrom(value1) + gtest.Assert(array1.Range(0, 1), []int{0}) + gtest.Assert(array1.Range(1, 2), []int{1}) + gtest.Assert(array1.Range(0, 2), []int{0, 1}) + gtest.Assert(array1.Range(-1, 10), value1) + }) } func TestIntArray_Merge(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{0, 1, 2, 3} - a2 := []int{4, 5, 6, 7} - array1 := garray.NewIntArrayFrom(a1) - array2 := garray.NewIntArrayFrom(a2) - gtest.Assert(array1.Merge(array2).Slice(), []int{0,1,2,3,4,5,6,7}) - }) + gtest.Case(t, func() { + a1 := []int{0, 1, 2, 3} + a2 := []int{4, 5, 6, 7} + array1 := garray.NewIntArrayFrom(a1) + array2 := garray.NewIntArrayFrom(a2) + gtest.Assert(array1.Merge(array2).Slice(), []int{0, 1, 2, 3, 4, 5, 6, 7}) + }) } func TestIntArray_Fill(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{0} - a2 := []int{0} - array1 := garray.NewIntArrayFrom(a1) - array2 := garray.NewIntArrayFrom(a2) - gtest.Assert(array1.Fill(1, 2, 100).Slice(), []int{0,100,100}) - gtest.Assert(array2.Fill(0, 2, 100).Slice(), []int{100,100}) - }) + gtest.Case(t, func() { + a1 := []int{0} + a2 := []int{0} + array1 := garray.NewIntArrayFrom(a1) + array2 := garray.NewIntArrayFrom(a2) + gtest.Assert(array1.Fill(1, 2, 100).Slice(), []int{0, 100, 100}) + gtest.Assert(array2.Fill(0, 2, 100).Slice(), []int{100, 100}) + }) } func TestIntArray_Chunk(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{1,2,3,4,5} - array1 := garray.NewIntArrayFrom(a1) - chunks := array1.Chunk(2) - gtest.Assert(len(chunks), 3) - gtest.Assert(chunks[0], []int{1,2}) - gtest.Assert(chunks[1], []int{3,4}) - gtest.Assert(chunks[2], []int{5}) - }) + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 4, 5} + array1 := garray.NewIntArrayFrom(a1) + chunks := array1.Chunk(2) + gtest.Assert(len(chunks), 3) + gtest.Assert(chunks[0], []int{1, 2}) + gtest.Assert(chunks[1], []int{3, 4}) + gtest.Assert(chunks[2], []int{5}) + }) } func TestIntArray_Pad(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{0} - array1 := garray.NewIntArrayFrom(a1) - gtest.Assert(array1.Pad(3, 1).Slice(), []int{0,1,1}) - gtest.Assert(array1.Pad(-4, 1).Slice(), []int{1,0,1,1}) - gtest.Assert(array1.Pad(3, 1).Slice(), []int{1,0,1,1}) - }) + gtest.Case(t, func() { + a1 := []int{0} + array1 := garray.NewIntArrayFrom(a1) + gtest.Assert(array1.Pad(3, 1).Slice(), []int{0, 1, 1}) + gtest.Assert(array1.Pad(-4, 1).Slice(), []int{1, 0, 1, 1}) + gtest.Assert(array1.Pad(3, 1).Slice(), []int{1, 0, 1, 1}) + }) } func TestIntArray_SubSlice(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{0,1,2,3,4,5,6} - array1 := garray.NewIntArrayFrom(a1) - gtest.Assert(array1.SubSlice(0, 2), []int{0,1}) - gtest.Assert(array1.SubSlice(2, 2), []int{2,3}) - gtest.Assert(array1.SubSlice(5, 8), []int{5,6}) - }) + gtest.Case(t, func() { + a1 := []int{0, 1, 2, 3, 4, 5, 6} + array1 := garray.NewIntArrayFrom(a1) + gtest.Assert(array1.SubSlice(0, 2), []int{0, 1}) + gtest.Assert(array1.SubSlice(2, 2), []int{2, 3}) + gtest.Assert(array1.SubSlice(5, 8), []int{5, 6}) + }) } func TestIntArray_Rand(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{0,1,2,3,4,5,6} - array1 := garray.NewIntArrayFrom(a1) - gtest.Assert(len(array1.Rands(2)), 2) - gtest.Assert(len(array1.Rands(10)), 7) - gtest.AssertIN(array1.Rands(1)[0], a1) - gtest.AssertIN(array1.Rand(), a1) - }) + gtest.Case(t, func() { + a1 := []int{0, 1, 2, 3, 4, 5, 6} + array1 := garray.NewIntArrayFrom(a1) + gtest.Assert(len(array1.Rands(2)), 2) + gtest.Assert(len(array1.Rands(10)), 7) + gtest.AssertIN(array1.Rands(1)[0], a1) + gtest.AssertIN(array1.Rand(), a1) + }) } func TestIntArray_PopRands(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{100, 200, 300, 400, 500, 600} - array := garray.NewFromCopy(a1) - gtest.AssertIN(array.PopRands(2), a1) - }) + gtest.Case(t, func() { + a1 := []interface{}{100, 200, 300, 400, 500, 600} + array := garray.NewFromCopy(a1) + gtest.AssertIN(array.PopRands(2), a1) + }) } func TestIntArray_Shuffle(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{0,1,2,3,4,5,6} - array1 := garray.NewIntArrayFrom(a1) - gtest.Assert(array1.Shuffle().Len(), 7) - }) + gtest.Case(t, func() { + a1 := []int{0, 1, 2, 3, 4, 5, 6} + array1 := garray.NewIntArrayFrom(a1) + gtest.Assert(array1.Shuffle().Len(), 7) + }) } func TestIntArray_Reverse(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{0,1,2,3,4,5,6} - array1 := garray.NewIntArrayFrom(a1) - gtest.Assert(array1.Reverse().Slice(), []int{6,5,4,3,2,1,0}) - }) + gtest.Case(t, func() { + a1 := []int{0, 1, 2, 3, 4, 5, 6} + array1 := garray.NewIntArrayFrom(a1) + gtest.Assert(array1.Reverse().Slice(), []int{6, 5, 4, 3, 2, 1, 0}) + }) } func TestIntArray_Join(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{0,1,2,3,4,5,6} - array1 := garray.NewIntArrayFrom(a1) - gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6") - }) + gtest.Case(t, func() { + a1 := []int{0, 1, 2, 3, 4, 5, 6} + array1 := garray.NewIntArrayFrom(a1) + gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6") + }) } func TestNewSortedIntArrayFrom(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{0,3,2,1,4,5,6} - array1 := garray.NewSortedIntArrayFrom(a1,true) - gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6") - }) + gtest.Case(t, func() { + a1 := []int{0, 3, 2, 1, 4, 5, 6} + array1 := garray.NewSortedIntArrayFrom(a1, true) + gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6") + }) } func TestNewSortedIntArrayFromCopy(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{0,5,2,1,4,3,6} - array1 := garray.NewSortedIntArrayFromCopy(a1,false) - //@todo - gtest.Assert(array1.Join("."), "0.5.2.1.4.3.6") - }) + gtest.Case(t, func() { + a1 := []int{0, 5, 2, 1, 4, 3, 6} + array1 := garray.NewSortedIntArrayFromCopy(a1, false) + //@todo + gtest.Assert(array1.Join("."), "0.5.2.1.4.3.6") + }) } -func TestSortedIntArray_SetArray(t *testing.T){ - gtest.Case(t, func() { - a1 := []int{0,1,2,3} - a2 := []int{4,5,6} - array1 := garray.NewSortedIntArrayFrom(a1) - array2:=array1.SetArray(a2) +func TestSortedIntArray_SetArray(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{0, 1, 2, 3} + a2 := []int{4, 5, 6} + array1 := garray.NewSortedIntArrayFrom(a1) + array2 := array1.SetArray(a2) - gtest.Assert(array2.Len(),3) - gtest.Assert(array2.Search(3),-1) - gtest.Assert(array2.Search(5),1) - gtest.Assert(array2.Search(6),2) - }) + gtest.Assert(array2.Len(), 3) + gtest.Assert(array2.Search(3), -1) + gtest.Assert(array2.Search(5), 1) + gtest.Assert(array2.Search(6), 2) + }) } func TestSortedIntArray_Sort(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{0,3,2,1} + gtest.Case(t, func() { + a1 := []int{0, 3, 2, 1} - array1 := garray.NewSortedIntArrayFrom(a1) - array2:=array1.Sort() + array1 := garray.NewSortedIntArrayFrom(a1) + array2 := array1.Sort() - gtest.Assert(array2.Len(),4) - gtest.Assert(array2,[]int{0,1,2,3}) + gtest.Assert(array2.Len(), 4) + gtest.Assert(array2, []int{0, 1, 2, 3}) - }) + }) } func TestSortedIntArray_Get(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{1,3,5,0} - array1 := garray.NewSortedIntArrayFrom(a1) - gtest.Assert(array1.Get(0),0) - gtest.Assert(array1.Get(1),1) - gtest.Assert(array1.Get(3),5) + gtest.Case(t, func() { + a1 := []int{1, 3, 5, 0} + array1 := garray.NewSortedIntArrayFrom(a1) + gtest.Assert(array1.Get(0), 0) + gtest.Assert(array1.Get(1), 1) + gtest.Assert(array1.Get(3), 5) - }) + }) } func TestSortedIntArray_Remove(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 3, 5, 0} + array1 := garray.NewSortedIntArrayFrom(a1) + i1 := array1.Remove(2) + gtest.Assert(i1, 3) + gtest.Assert(array1.Search(5), 2) + + // 再次删除剩下的数组中的第一个 + i2 := array1.Remove(0) + gtest.Assert(i2, 0) + gtest.Assert(array1.Search(5), 1) + + a2 := []int{1, 3} + array2 := garray.NewSortedIntArrayFrom(a2) + i3 := array2.Remove(1) + gtest.Assert(array2.Search(1), 0) + + gtest.Assert(i3, 3) + + }) +} + +func TestSortedArray_PopLeft(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 3, 5, 2} + array1 := garray.NewSortedIntArrayFrom(a1) + i1 := array1.PopLeft() + gtest.Assert(i1, 1) + gtest.Assert(array1.Len(), 3) + gtest.Assert(array1.Search(1), -1) + + }) +} + +func TestSortedArray_PopRight(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 3, 5, 2} + array1 := garray.NewSortedIntArrayFrom(a1) + i1 := array1.PopRight() + gtest.Assert(i1, 5) + gtest.Assert(array1.Len(), 3) + gtest.Assert(array1.Search(5), -1) + }) +} + +func TestSortedArray_PopRand(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 3, 5, 2} + array1 := garray.NewSortedIntArrayFrom(a1) + i1 := array1.PopRand() + gtest.Assert(array1.Len(), 3) + gtest.Assert(array1.Search(i1), -1) + gtest.AssertIN(i1, []int{1, 3, 5, 2}) + }) +} + +func TestSortedArray_PopRands(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 3, 5, 2} + array1 := garray.NewSortedIntArrayFrom(a1) + ns1 := array1.PopRands(2) + gtest.Assert(array1.Len(), 2) + gtest.AssertIN(ns1, []int{1, 3, 5, 2}) + + a2 := []int{1, 3, 5, 2} + array2 := garray.NewSortedIntArrayFrom(a2) + ns2 := array2.PopRands(5) + gtest.Assert(array2.Len(), 0) + gtest.Assert(len(ns2), 4) + gtest.AssertIN(ns2, []int{1, 3, 5, 2}) + + }) +} + +func TestSortedArray_PopLefts(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 3, 5, 2} + array1 := garray.NewSortedIntArrayFrom(a1) + ns1 := array1.PopLefts(2) + gtest.Assert(array1.Len(), 2) + gtest.Assert(ns1, []int{1, 2}) + + a2 := []int{1, 3, 5, 2} + array2 := garray.NewSortedIntArrayFrom(a2) + ns2 := array2.PopLefts(5) + gtest.Assert(array2.Len(), 0) + gtest.AssertIN(ns2, []int{1, 3, 5, 2}) + + }) +} + +func TestSortedArray_PopRights(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 3, 5, 2} + array1 := garray.NewSortedIntArrayFrom(a1) + ns1 := array1.PopRights(2) + gtest.Assert(array1.Len(), 2) + gtest.Assert(ns1, []int{3, 5}) + + a2 := []int{1, 3, 5, 2} + array2 := garray.NewSortedIntArrayFrom(a2) + ns2 := array2.PopRights(5) + gtest.Assert(array2.Len(), 0) + gtest.AssertIN(ns2, []int{1, 3, 5, 2}) + }) +} + +func TestSortedArray_Range(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 3, 5, 2, 6, 7} + array1 := garray.NewSortedIntArrayFrom(a1) + ns1 := array1.Range(1, 4) + gtest.Assert(len(ns1), 3) + gtest.Assert(ns1, []int{2, 3, 5}) + + ns2 := array1.Range(5, 4) + gtest.Assert(len(ns2), 0) + + ns3 := array1.Range(-1, 4) + gtest.Assert(len(ns3), 4) + + nsl := array1.Range(5, 8) + gtest.Assert(len(nsl), 1) + + }) +} + +func TestSortedArray_Sum(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 3, 5} + array1 := garray.NewSortedIntArrayFrom(a1) + n1 := array1.Sum() + gtest.Assert(n1, 9) + }) +} + +func TestSortedIntArray_Contains(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 3, 5} + array1 := garray.NewSortedIntArrayFrom(a1) + //gtest.Assert(array1.Contains(3),true) //todo 这一行应该返回true + gtest.Assert(array1.Contains(4), false) + }) +} + +func TestSortedIntArray_Clone(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 3, 5} + array1 := garray.NewSortedIntArrayFrom(a1) + array2 := array1.Clone() + gtest.Assert(array2.Len(), 3) + gtest.Assert(array2, array1) + }) +} + +func TestSortedIntArray_Clear(t *testing.T) { gtest.Case(t, func() { - a1 := []int{1,3,5,0} + a1 := []int{1, 3, 5} array1 := garray.NewSortedIntArrayFrom(a1) - i1:=array1.Remove(2) - gtest.Assert(i1,3) - gtest.Assert(array1.Search(5),2) - - // 再次删除剩下的数组中的第一个 - i2:=array1.Remove(0) - gtest.Assert(i2,0) - gtest.Assert(array1.Search(5),1) - - - a2 := []int{1} - array2 := garray.NewSortedIntArrayFrom(a2) - gtest.Assert(array2.Search(2),-1) - - - // gtest.Assert(array2.Search(1),0) - //i3:=array2.Remove(1) - // gtest.Assert(i3,3) + array1.Clear() + gtest.Assert(array1.Len(), 0) }) } +func TestSortedIntArray_Chunk(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1,2, 3,4, 5} + array1 := garray.NewSortedIntArrayFrom(a1) + ns1:=array1.Chunk(2) //按每几个元素切成一个数组 + ns2:=array1.Chunk(-1) + t.Log(ns1) + gtest.Assert(len(ns1), 3) + gtest.Assert(ns1[0], []int{1,2}) + gtest.Assert(ns1[2], []int{5}) + gtest.Assert(len(ns2), 0) + + }) +} + +func TestSortedIntArray_SubSlice(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1,2, 3,4, 5} + array1 := garray.NewSortedIntArrayFrom(a1) + ns1:=array1.SubSlice(1,2) + gtest.Assert(len(ns1), 2) + gtest.Assert(ns1,[]int{2,3}) + + ns2:=array1.SubSlice(7,2) + gtest.Assert(len(ns2), 0) + + ns3:=array1.SubSlice(3,5) + gtest.Assert(len(ns3), 2) + gtest.Assert(ns3,[]int{4,5}) + + ns4:=array1.SubSlice(3,1) + gtest.Assert(len(ns4), 1) + gtest.Assert(ns4,[]int{4}) + }) +} + +func TestSortedIntArray_Rand(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1,2, 3,4, 5} + array1 := garray.NewSortedIntArrayFrom(a1) + ns1:=array1.Rand() //按每几个元素切成一个数组 + gtest.AssertIN(ns1,a1) + }) +} + +func TestSortedIntArray_Rands(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1,2, 3,4, 5} + array1 := garray.NewSortedIntArrayFrom(a1) + ns1:=array1.Rands(2) //按每几个元素切成一个数组 + gtest.AssertIN(ns1,a1) + gtest.Assert(len(ns1),2) + + ns2:=array1.Rands(6) //按每几个元素切成一个数组 + gtest.AssertIN(ns2,a1) + gtest.Assert(len(ns2),5) + }) +} + +func TestSortedIntArray_CountValues(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1,2, 3,4, 5,3} + array1 := garray.NewSortedIntArrayFrom(a1) + ns1:=array1.CountValues() //按每几个元素切成一个数组 + gtest.Assert(len(ns1),5) + gtest.Assert(ns1[2],1) + gtest.Assert(ns1[3],2) + }) +} + + diff --git a/g/container/garray/garray_z_unit_string_test.go b/g/container/garray/garray_z_unit_string_test.go index e5869548e..db0ec7b26 100644 --- a/g/container/garray/garray_z_unit_string_test.go +++ b/g/container/garray/garray_z_unit_string_test.go @@ -320,3 +320,22 @@ func TestStringArray_CountValues(t *testing.T) { }) } + +func TestNewSortedStringArrayFrom(t *testing.T){ + gtest.Case(t, func() { + a1:=[]string{"a", "d", "c","b"} + s1 :=garray.NewSortedStringArrayFrom(a1,true) + gtest.Assert(s1,[]string{"a", "b", "c","d"}) + s2 :=garray.NewSortedStringArrayFrom(a1,false) + gtest.Assert(s2,[]string{"a", "b", "c","d"}) + }) +} + +func TestNewSortedStringArrayFromCopy(t *testing.T){ + gtest.Case(t, func() { + a1:=[]string{"a", "d", "c","b"} + s1 :=garray.NewSortedStringArrayFromCopy(a1,true) + gtest.Assert(s1.Len(),4) + // gtest.Assert(s1,[]string{"a", "b", "c","d"}) //todo 这里没有排序,需主库修正 + }) +} diff --git a/g/container/gset/gset_z_unit_string_test.go b/g/container/gset/gset_z_unit_string_test.go index c75c2f10b..de04653df 100644 --- a/g/container/gset/gset_z_unit_string_test.go +++ b/g/container/gset/gset_z_unit_string_test.go @@ -254,3 +254,5 @@ func TestStringSet_Pops(t *testing.T) { gtest.AssertIN(str2, []string{"a", "b", "c"}) }) } + + From 622dbfda31e43278c6cfc4cf36395a69bb264f33 Mon Sep 17 00:00:00 2001 From: jroam Date: Thu, 13 Jun 2019 17:02:30 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E5=AE=8C=E6=88=90garray=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E8=A6=86=E7=9B=96=E7=8E=87=EF=BC=8C=E8=BE=BE90%.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/container/garray/garray_z_unit_int_test.go | 258 ++++-- .../garray/garray_z_unit_interface_test.go | 798 +++++++++++++----- .../garray/garray_z_unit_string_test.go | 320 ++++++- 3 files changed, 1094 insertions(+), 282 deletions(-) diff --git a/g/container/garray/garray_z_unit_int_test.go b/g/container/garray/garray_z_unit_int_test.go index aba95f5c8..6d7e53377 100644 --- a/g/container/garray/garray_z_unit_int_test.go +++ b/g/container/garray/garray_z_unit_int_test.go @@ -170,9 +170,16 @@ func TestIntArray_Rand(t *testing.T) { func TestIntArray_PopRands(t *testing.T) { gtest.Case(t, func() { - a1 := []interface{}{100, 200, 300, 400, 500, 600} - array := garray.NewFromCopy(a1) - gtest.AssertIN(array.PopRands(2), a1) + a1 := []int{100, 200, 300, 400, 500, 600} + array := garray.NewIntArrayFrom(a1) + ns1 := array.PopRands(2) + gtest.AssertIN(ns1, []int{100, 200, 300, 400, 500, 600}) + gtest.AssertIN(len(ns1), 2) + + ns2 := array.PopRands(7) + gtest.AssertIN(len(ns2), 6) + gtest.AssertIN(ns2, []int{100, 200, 300, 400, 500, 600}) + }) } @@ -212,8 +219,7 @@ func TestNewSortedIntArrayFromCopy(t *testing.T) { gtest.Case(t, func() { a1 := []int{0, 5, 2, 1, 4, 3, 6} array1 := garray.NewSortedIntArrayFromCopy(a1, false) - //@todo - gtest.Assert(array1.Join("."), "0.5.2.1.4.3.6") + gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6") }) } @@ -268,17 +274,19 @@ func TestSortedIntArray_Remove(t *testing.T) { gtest.Assert(i2, 0) gtest.Assert(array1.Search(5), 1) - a2 := []int{1, 3} + a2 := []int{1, 3, 4} array2 := garray.NewSortedIntArrayFrom(a2) i3 := array2.Remove(1) gtest.Assert(array2.Search(1), 0) - gtest.Assert(i3, 3) + i3 = array2.Remove(1) + gtest.Assert(array2.Search(4), -1) + gtest.Assert(i3, 4) }) } -func TestSortedArray_PopLeft(t *testing.T) { +func TestSortedIntArray_PopLeft(t *testing.T) { gtest.Case(t, func() { a1 := []int{1, 3, 5, 2} array1 := garray.NewSortedIntArrayFrom(a1) @@ -290,7 +298,7 @@ func TestSortedArray_PopLeft(t *testing.T) { }) } -func TestSortedArray_PopRight(t *testing.T) { +func TestSortedIntArray_PopRight(t *testing.T) { gtest.Case(t, func() { a1 := []int{1, 3, 5, 2} array1 := garray.NewSortedIntArrayFrom(a1) @@ -301,7 +309,7 @@ func TestSortedArray_PopRight(t *testing.T) { }) } -func TestSortedArray_PopRand(t *testing.T) { +func TestSortedIntArray_PopRand(t *testing.T) { gtest.Case(t, func() { a1 := []int{1, 3, 5, 2} array1 := garray.NewSortedIntArrayFrom(a1) @@ -312,7 +320,7 @@ func TestSortedArray_PopRand(t *testing.T) { }) } -func TestSortedArray_PopRands(t *testing.T) { +func TestSortedIntArray_PopRands(t *testing.T) { gtest.Case(t, func() { a1 := []int{1, 3, 5, 2} array1 := garray.NewSortedIntArrayFrom(a1) @@ -330,7 +338,7 @@ func TestSortedArray_PopRands(t *testing.T) { }) } -func TestSortedArray_PopLefts(t *testing.T) { +func TestSortedIntArray_PopLefts(t *testing.T) { gtest.Case(t, func() { a1 := []int{1, 3, 5, 2} array1 := garray.NewSortedIntArrayFrom(a1) @@ -347,7 +355,7 @@ func TestSortedArray_PopLefts(t *testing.T) { }) } -func TestSortedArray_PopRights(t *testing.T) { +func TestSortedIntArray_PopRights(t *testing.T) { gtest.Case(t, func() { a1 := []int{1, 3, 5, 2} array1 := garray.NewSortedIntArrayFrom(a1) @@ -363,7 +371,7 @@ func TestSortedArray_PopRights(t *testing.T) { }) } -func TestSortedArray_Range(t *testing.T) { +func TestSortedIntArray_Range(t *testing.T) { gtest.Case(t, func() { a1 := []int{1, 3, 5, 2, 6, 7} array1 := garray.NewSortedIntArrayFrom(a1) @@ -383,7 +391,7 @@ func TestSortedArray_Range(t *testing.T) { }) } -func TestSortedArray_Sum(t *testing.T) { +func TestSortedIntArray_Sum(t *testing.T) { gtest.Case(t, func() { a1 := []int{1, 3, 5} array1 := garray.NewSortedIntArrayFrom(a1) @@ -412,83 +420,191 @@ func TestSortedIntArray_Clone(t *testing.T) { } func TestSortedIntArray_Clear(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{1, 3, 5} - array1 := garray.NewSortedIntArrayFrom(a1) - array1.Clear() - gtest.Assert(array1.Len(), 0) + gtest.Case(t, func() { + a1 := []int{1, 3, 5} + array1 := garray.NewSortedIntArrayFrom(a1) + array1.Clear() + gtest.Assert(array1.Len(), 0) - }) + }) } func TestSortedIntArray_Chunk(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{1,2, 3,4, 5} - array1 := garray.NewSortedIntArrayFrom(a1) - ns1:=array1.Chunk(2) //按每几个元素切成一个数组 - ns2:=array1.Chunk(-1) - t.Log(ns1) - gtest.Assert(len(ns1), 3) - gtest.Assert(ns1[0], []int{1,2}) - gtest.Assert(ns1[2], []int{5}) - gtest.Assert(len(ns2), 0) + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 4, 5} + array1 := garray.NewSortedIntArrayFrom(a1) + ns1 := array1.Chunk(2) //按每几个元素切成一个数组 + ns2 := array1.Chunk(-1) + t.Log(ns1) + gtest.Assert(len(ns1), 3) + gtest.Assert(ns1[0], []int{1, 2}) + gtest.Assert(ns1[2], []int{5}) + gtest.Assert(len(ns2), 0) - }) + }) } func TestSortedIntArray_SubSlice(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{1,2, 3,4, 5} - array1 := garray.NewSortedIntArrayFrom(a1) - ns1:=array1.SubSlice(1,2) - gtest.Assert(len(ns1), 2) - gtest.Assert(ns1,[]int{2,3}) + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 4, 5} + array1 := garray.NewSortedIntArrayFrom(a1) + ns1 := array1.SubSlice(1, 2) + gtest.Assert(len(ns1), 2) + gtest.Assert(ns1, []int{2, 3}) - ns2:=array1.SubSlice(7,2) - gtest.Assert(len(ns2), 0) + ns2 := array1.SubSlice(7, 2) + gtest.Assert(len(ns2), 0) - ns3:=array1.SubSlice(3,5) - gtest.Assert(len(ns3), 2) - gtest.Assert(ns3,[]int{4,5}) + ns3 := array1.SubSlice(3, 5) + gtest.Assert(len(ns3), 2) + gtest.Assert(ns3, []int{4, 5}) - ns4:=array1.SubSlice(3,1) - gtest.Assert(len(ns4), 1) - gtest.Assert(ns4,[]int{4}) - }) + ns4 := array1.SubSlice(3, 1) + gtest.Assert(len(ns4), 1) + gtest.Assert(ns4, []int{4}) + }) } func TestSortedIntArray_Rand(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{1,2, 3,4, 5} - array1 := garray.NewSortedIntArrayFrom(a1) - ns1:=array1.Rand() //按每几个元素切成一个数组 - gtest.AssertIN(ns1,a1) - }) + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 4, 5} + array1 := garray.NewSortedIntArrayFrom(a1) + ns1 := array1.Rand() //按每几个元素切成一个数组 + gtest.AssertIN(ns1, a1) + }) } func TestSortedIntArray_Rands(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{1,2, 3,4, 5} - array1 := garray.NewSortedIntArrayFrom(a1) - ns1:=array1.Rands(2) //按每几个元素切成一个数组 - gtest.AssertIN(ns1,a1) - gtest.Assert(len(ns1),2) + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 4, 5} + array1 := garray.NewSortedIntArrayFrom(a1) + ns1 := array1.Rands(2) //按每几个元素切成一个数组 + gtest.AssertIN(ns1, a1) + gtest.Assert(len(ns1), 2) - ns2:=array1.Rands(6) //按每几个元素切成一个数组 - gtest.AssertIN(ns2,a1) - gtest.Assert(len(ns2),5) - }) + ns2 := array1.Rands(6) //按每几个元素切成一个数组 + gtest.AssertIN(ns2, a1) + gtest.Assert(len(ns2), 5) + }) } func TestSortedIntArray_CountValues(t *testing.T) { - gtest.Case(t, func() { - a1 := []int{1,2, 3,4, 5,3} - array1 := garray.NewSortedIntArrayFrom(a1) - ns1:=array1.CountValues() //按每几个元素切成一个数组 - gtest.Assert(len(ns1),5) - gtest.Assert(ns1[2],1) - gtest.Assert(ns1[3],2) - }) + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 4, 5, 3} + array1 := garray.NewSortedIntArrayFrom(a1) + ns1 := array1.CountValues() //按每几个元素切成一个数组 + gtest.Assert(len(ns1), 5) + gtest.Assert(ns1[2], 1) + gtest.Assert(ns1[3], 2) + }) } +func TestSortedIntArray_SetUnique(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 4, 5, 3} + array1 := garray.NewSortedIntArrayFrom(a1) + array1.SetUnique(true) + gtest.Assert(array1.Len(), 5) + gtest.Assert(array1, []int{1, 2, 3, 4, 5}) + }) +} + +func TestIntArray_SetArray(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 5} + a2 := []int{6, 7} + array1 := garray.NewIntArrayFrom(a1) + array1.SetArray(a2) + gtest.Assert(array1.Len(), 2) + gtest.Assert(array1, []int{6, 7}) + + }) +} + +func TestIntArray_Replace(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 5} + a2 := []int{6, 7} + a3 := []int{9, 10, 11, 12, 13} + array1 := garray.NewIntArrayFrom(a1) + array1.Replace(a2) + gtest.Assert(array1, []int{6, 7, 3, 5}) + + array1.Replace(a3) + gtest.Assert(array1, []int{9, 10, 11, 12}) + }) +} + +func TestIntArray_Clear(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 5} + array1 := garray.NewIntArrayFrom(a1) + array1.Clear() + gtest.Assert(array1.Len(), 0) + }) +} + +func TestIntArray_Clone(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 5} + array1 := garray.NewIntArrayFrom(a1) + array2 := array1.Clone() + gtest.Assert(array1, array2) + }) +} + +func TestArray_Get(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 5} + array1 := garray.NewIntArrayFrom(a1) + gtest.Assert(array1.Get(2), 3) + gtest.Assert(array1.Len(), 4) + }) +} + +func TestIntArray_Sum(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 5} + array1 := garray.NewIntArrayFrom(a1) + gtest.Assert(array1.Sum(), 11) + }) +} + +func TestIntArray_CountValues(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 5, 3} + array1 := garray.NewIntArrayFrom(a1) + m1 := array1.CountValues() + gtest.Assert(len(m1), 4) + gtest.Assert(m1[1], 1) + gtest.Assert(m1[3], 2) + }) +} + +func TestNewIntArrayFromCopy(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 5, 3} + array1 := garray.NewIntArrayFromCopy(a1) + gtest.Assert(array1.Len(), 5) + gtest.Assert(array1, a1) + }) +} + +func TestIntArray_Remove(t *testing.T) { + gtest.Case(t, func() { + a1 := []int{1, 2, 3, 5, 4} + array1 := garray.NewIntArrayFrom(a1) + n1 := array1.Remove(1) + gtest.Assert(n1, 2) + gtest.Assert(array1.Len(), 4) + + n1 = array1.Remove(0) + gtest.Assert(n1, 1) + gtest.Assert(array1.Len(), 3) + + n1 = array1.Remove(2) + gtest.Assert(n1, 4) + gtest.Assert(array1.Len(), 2) + }) +} diff --git a/g/container/garray/garray_z_unit_interface_test.go b/g/container/garray/garray_z_unit_interface_test.go index 9dbce416c..90ed36d6b 100644 --- a/g/container/garray/garray_z_unit_interface_test.go +++ b/g/container/garray/garray_z_unit_interface_test.go @@ -9,272 +9,670 @@ package garray_test import ( - "github.com/gogf/gf/g/container/garray" - "github.com/gogf/gf/g/test/gtest" - "testing" + "github.com/gogf/gf/g/container/garray" + "github.com/gogf/gf/g/test/gtest" + "github.com/gogf/gf/g/util/gconv" + "strings" + "testing" ) func Test_Array_Basic(t *testing.T) { - gtest.Case(t, func() { - expect := []interface{}{0, 1, 2, 3} - array := garray.NewArrayFrom(expect) - gtest.Assert(array.Slice(), expect) - array.Set(0, 100) - gtest.Assert(array.Get(0), 100) - gtest.Assert(array.Get(1), 1) - gtest.Assert(array.Search(100), 0) - gtest.Assert(array.Contains(100), true) - gtest.Assert(array.Remove(0), 100) - gtest.Assert(array.Contains(100), false) - array.Append(4) - gtest.Assert(array.Len(), 4) - array.InsertBefore(0, 100) - array.InsertAfter(0, 200) - gtest.Assert(array.Slice(), []interface{}{100, 200, 1, 2, 3, 4}) - array.InsertBefore(5, 300) - array.InsertAfter(6, 400) - gtest.Assert(array.Slice(), []interface{}{100, 200, 1, 2, 3, 300, 4, 400}) - gtest.Assert(array.Clear().Len(), 0) - }) + gtest.Case(t, func() { + expect := []interface{}{0, 1, 2, 3} + array := garray.NewArrayFrom(expect) + gtest.Assert(array.Slice(), expect) + array.Set(0, 100) + gtest.Assert(array.Get(0), 100) + gtest.Assert(array.Get(1), 1) + gtest.Assert(array.Search(100), 0) + gtest.Assert(array.Contains(100), true) + gtest.Assert(array.Remove(0), 100) + gtest.Assert(array.Contains(100), false) + array.Append(4) + gtest.Assert(array.Len(), 4) + array.InsertBefore(0, 100) + array.InsertAfter(0, 200) + gtest.Assert(array.Slice(), []interface{}{100, 200, 1, 2, 3, 4}) + array.InsertBefore(5, 300) + array.InsertAfter(6, 400) + gtest.Assert(array.Slice(), []interface{}{100, 200, 1, 2, 3, 300, 4, 400}) + gtest.Assert(array.Clear().Len(), 0) + }) } func TestArray_Sort(t *testing.T) { - gtest.Case(t, func() { - expect1 := []interface{}{0, 1, 2, 3} - expect2 := []interface{}{3, 2, 1, 0} - array := garray.NewArray() - for i := 3; i >= 0; i-- { - array.Append(i) - } - array.SortFunc(func(v1, v2 interface{}) bool { - return v1.(int) < v2.(int) - }) - gtest.Assert(array.Slice(), expect1) - array.SortFunc(func(v1, v2 interface{}) bool { - return v1.(int) > v2.(int) - }) - gtest.Assert(array.Slice(), expect2) - }) + gtest.Case(t, func() { + expect1 := []interface{}{0, 1, 2, 3} + expect2 := []interface{}{3, 2, 1, 0} + array := garray.NewArray() + for i := 3; i >= 0; i-- { + array.Append(i) + } + array.SortFunc(func(v1, v2 interface{}) bool { + return v1.(int) < v2.(int) + }) + gtest.Assert(array.Slice(), expect1) + array.SortFunc(func(v1, v2 interface{}) bool { + return v1.(int) > v2.(int) + }) + gtest.Assert(array.Slice(), expect2) + }) } func TestArray_Unique(t *testing.T) { - gtest.Case(t, func() { - expect := []interface{}{1, 1, 2, 3} - array := garray.NewArrayFrom(expect) - gtest.Assert(array.Unique().Slice(), []interface{}{1, 2, 3}) - }) + gtest.Case(t, func() { + expect := []interface{}{1, 1, 2, 3} + array := garray.NewArrayFrom(expect) + gtest.Assert(array.Unique().Slice(), []interface{}{1, 2, 3}) + }) } func TestArray_PushAndPop(t *testing.T) { - gtest.Case(t, func() { - expect := []interface{}{0, 1, 2, 3} - array := garray.NewArrayFrom(expect) - gtest.Assert(array.Slice(), expect) - gtest.Assert(array.PopLeft(), 0) - gtest.Assert(array.PopRight(), 3) - gtest.AssertIN(array.PopRand(), []interface{}{1, 2}) - gtest.AssertIN(array.PopRand(), []interface{}{1, 2}) - gtest.Assert(array.Len(), 0) - array.PushLeft(1).PushRight(2) - gtest.Assert(array.Slice(), []interface{}{1, 2}) - }) + gtest.Case(t, func() { + expect := []interface{}{0, 1, 2, 3} + array := garray.NewArrayFrom(expect) + gtest.Assert(array.Slice(), expect) + gtest.Assert(array.PopLeft(), 0) + gtest.Assert(array.PopRight(), 3) + gtest.AssertIN(array.PopRand(), []interface{}{1, 2}) + gtest.AssertIN(array.PopRand(), []interface{}{1, 2}) + gtest.Assert(array.Len(), 0) + array.PushLeft(1).PushRight(2) + gtest.Assert(array.Slice(), []interface{}{1, 2}) + }) } func TestArray_PopRands(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{100, 200, 300, 400, 500, 600} - array := garray.NewFromCopy(a1) - gtest.AssertIN(array.PopRands(2), a1) - }) + gtest.Case(t, func() { + a1 := []interface{}{100, 200, 300, 400, 500, 600} + array := garray.NewFromCopy(a1) + gtest.AssertIN(array.PopRands(2), a1) + }) } func TestArray_PopLeftsAndPopRights(t *testing.T) { - gtest.Case(t, func() { - value1 := []interface{}{0,1,2,3,4,5,6} - value2 := []interface{}{0,1,2,3,4,5,6} - array1 := garray.NewArrayFrom(value1) - array2 := garray.NewArrayFrom(value2) - gtest.Assert(array1.PopLefts(2), []interface{}{0,1}) - gtest.Assert(array1.Slice(), []interface{}{2,3,4,5,6}) - gtest.Assert(array1.PopRights(2), []interface{}{5,6}) - gtest.Assert(array1.Slice(), []interface{}{2,3,4}) - gtest.Assert(array1.PopRights(20), []interface{}{2,3,4}) - gtest.Assert(array1.Slice(), []interface{}{}) - gtest.Assert(array2.PopLefts(20), []interface{}{0,1,2,3,4,5,6}) - gtest.Assert(array2.Slice(), []interface{}{}) - }) + gtest.Case(t, func() { + value1 := []interface{}{0, 1, 2, 3, 4, 5, 6} + value2 := []interface{}{0, 1, 2, 3, 4, 5, 6} + array1 := garray.NewArrayFrom(value1) + array2 := garray.NewArrayFrom(value2) + gtest.Assert(array1.PopLefts(2), []interface{}{0, 1}) + gtest.Assert(array1.Slice(), []interface{}{2, 3, 4, 5, 6}) + gtest.Assert(array1.PopRights(2), []interface{}{5, 6}) + gtest.Assert(array1.Slice(), []interface{}{2, 3, 4}) + gtest.Assert(array1.PopRights(20), []interface{}{2, 3, 4}) + gtest.Assert(array1.Slice(), []interface{}{}) + gtest.Assert(array2.PopLefts(20), []interface{}{0, 1, 2, 3, 4, 5, 6}) + gtest.Assert(array2.Slice(), []interface{}{}) + }) } func TestArray_Range(t *testing.T) { - gtest.Case(t, func() { - value1 := []interface{}{0,1,2,3,4,5,6} - array1 := garray.NewArrayFrom(value1) - gtest.Assert(array1.Range(0, 1), []interface{}{0}) - gtest.Assert(array1.Range(1, 2), []interface{}{1}) - gtest.Assert(array1.Range(0, 2), []interface{}{0, 1}) - gtest.Assert(array1.Range(-1, 10), value1) - }) + gtest.Case(t, func() { + value1 := []interface{}{0, 1, 2, 3, 4, 5, 6} + array1 := garray.NewArrayFrom(value1) + gtest.Assert(array1.Range(0, 1), []interface{}{0}) + gtest.Assert(array1.Range(1, 2), []interface{}{1}) + gtest.Assert(array1.Range(0, 2), []interface{}{0, 1}) + gtest.Assert(array1.Range(-1, 10), value1) + }) } func TestArray_Merge(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{0, 1, 2, 3} - a2 := []interface{}{4, 5, 6, 7} - array1 := garray.NewArrayFrom(a1) - array2 := garray.NewArrayFrom(a2) - gtest.Assert(array1.Merge(array2).Slice(), []interface{}{0,1,2,3,4,5,6,7}) - }) + gtest.Case(t, func() { + a1 := []interface{}{0, 1, 2, 3} + a2 := []interface{}{4, 5, 6, 7} + array1 := garray.NewArrayFrom(a1) + array2 := garray.NewArrayFrom(a2) + gtest.Assert(array1.Merge(array2).Slice(), []interface{}{0, 1, 2, 3, 4, 5, 6, 7}) + }) } func TestArray_Fill(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{0} - a2 := []interface{}{0} - array1 := garray.NewArrayFrom(a1) - array2 := garray.NewArrayFrom(a2) - gtest.Assert(array1.Fill(1, 2, 100).Slice(), []interface{}{0,100,100}) - gtest.Assert(array2.Fill(0, 2, 100).Slice(), []interface{}{100,100}) - }) + gtest.Case(t, func() { + a1 := []interface{}{0} + a2 := []interface{}{0} + array1 := garray.NewArrayFrom(a1) + array2 := garray.NewArrayFrom(a2) + gtest.Assert(array1.Fill(1, 2, 100).Slice(), []interface{}{0, 100, 100}) + gtest.Assert(array2.Fill(0, 2, 100).Slice(), []interface{}{100, 100}) + }) } func TestArray_Chunk(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{1,2,3,4,5} - array1 := garray.NewArrayFrom(a1) - chunks := array1.Chunk(2) - gtest.Assert(len(chunks), 3) - gtest.Assert(chunks[0], []interface{}{1,2}) - gtest.Assert(chunks[1], []interface{}{3,4}) - gtest.Assert(chunks[2], []interface{}{5}) - }) + gtest.Case(t, func() { + a1 := []interface{}{1, 2, 3, 4, 5} + array1 := garray.NewArrayFrom(a1) + chunks := array1.Chunk(2) + gtest.Assert(len(chunks), 3) + gtest.Assert(chunks[0], []interface{}{1, 2}) + gtest.Assert(chunks[1], []interface{}{3, 4}) + gtest.Assert(chunks[2], []interface{}{5}) + }) } func TestArray_Pad(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{0} - array1 := garray.NewArrayFrom(a1) - gtest.Assert(array1.Pad(3, 1).Slice(), []interface{}{0,1,1}) - gtest.Assert(array1.Pad(-4, 1).Slice(), []interface{}{1,0,1,1}) - gtest.Assert(array1.Pad(3, 1).Slice(), []interface{}{1,0,1,1}) - }) + gtest.Case(t, func() { + a1 := []interface{}{0} + array1 := garray.NewArrayFrom(a1) + gtest.Assert(array1.Pad(3, 1).Slice(), []interface{}{0, 1, 1}) + gtest.Assert(array1.Pad(-4, 1).Slice(), []interface{}{1, 0, 1, 1}) + gtest.Assert(array1.Pad(3, 1).Slice(), []interface{}{1, 0, 1, 1}) + }) } func TestArray_SubSlice(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{0,1,2,3,4,5,6} - array1 := garray.NewArrayFrom(a1) - gtest.Assert(array1.SubSlice(0, 2), []interface{}{0,1}) - gtest.Assert(array1.SubSlice(2, 2), []interface{}{2,3}) - gtest.Assert(array1.SubSlice(5, 8), []interface{}{5,6}) - }) + gtest.Case(t, func() { + a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} + array1 := garray.NewArrayFrom(a1) + gtest.Assert(array1.SubSlice(0, 2), []interface{}{0, 1}) + gtest.Assert(array1.SubSlice(2, 2), []interface{}{2, 3}) + gtest.Assert(array1.SubSlice(5, 8), []interface{}{5, 6}) + }) } func TestArray_Rand(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{0,1,2,3,4,5,6} - array1 := garray.NewArrayFrom(a1) - gtest.Assert(len(array1.Rands(2)), 2) - gtest.Assert(len(array1.Rands(10)), 7) - gtest.AssertIN(array1.Rands(1)[0], a1) - }) + gtest.Case(t, func() { + a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} + array1 := garray.NewArrayFrom(a1) + gtest.Assert(len(array1.Rands(2)), 2) + gtest.Assert(len(array1.Rands(10)), 7) + gtest.AssertIN(array1.Rands(1)[0], a1) + }) } func TestArray_Shuffle(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{0,1,2,3,4,5,6} - array1 := garray.NewArrayFrom(a1) - gtest.Assert(array1.Shuffle().Len(), 7) - }) + gtest.Case(t, func() { + a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} + array1 := garray.NewArrayFrom(a1) + gtest.Assert(array1.Shuffle().Len(), 7) + }) } func TestArray_Reverse(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{0,1,2,3,4,5,6} - array1 := garray.NewArrayFrom(a1) - gtest.Assert(array1.Reverse().Slice(), []interface{}{6,5,4,3,2,1,0}) - }) + gtest.Case(t, func() { + a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} + array1 := garray.NewArrayFrom(a1) + gtest.Assert(array1.Reverse().Slice(), []interface{}{6, 5, 4, 3, 2, 1, 0}) + }) } func TestArray_Join(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{0,1,2,3,4,5,6} - array1 := garray.NewArrayFrom(a1) - gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6") - }) + gtest.Case(t, func() { + a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} + array1 := garray.NewArrayFrom(a1) + gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6") + }) } func TestArray_Replace(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{0,1,2,3,4,5,6} - a2 := []interface{}{"a","b","c"} - a3 := []interface{}{"m","n","p","z","x","y","d","u"} - array1 := garray.NewArrayFrom(a1) - array2:=array1.Replace(a2) - gtest.Assert(array2.Len(), 7) - gtest.Assert(array2.Contains("b"),true) - gtest.Assert(array2.Contains(4),true) - gtest.Assert(array2.Contains("v"),false) - array3:=array1.Replace(a3) - gtest.Assert(array3.Len(), 7) - gtest.Assert(array3.Contains(4),false) - gtest.Assert(array3.Contains("p"),true) - gtest.Assert(array3.Contains("u"),false) - }) + gtest.Case(t, func() { + a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} + a2 := []interface{}{"a", "b", "c"} + a3 := []interface{}{"m", "n", "p", "z", "x", "y", "d", "u"} + array1 := garray.NewArrayFrom(a1) + array2 := array1.Replace(a2) + gtest.Assert(array2.Len(), 7) + gtest.Assert(array2.Contains("b"), true) + gtest.Assert(array2.Contains(4), true) + gtest.Assert(array2.Contains("v"), false) + array3 := array1.Replace(a3) + gtest.Assert(array3.Len(), 7) + gtest.Assert(array3.Contains(4), false) + gtest.Assert(array3.Contains("p"), true) + gtest.Assert(array3.Contains("u"), false) + }) } func TestArray_SetArray(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{0,1,2,3,4,5,6} - a2 := []interface{}{"a","b","c"} + gtest.Case(t, func() { + a1 := []interface{}{0, 1, 2, 3, 4, 5, 6} + a2 := []interface{}{"a", "b", "c"} - array1 := garray.NewArrayFrom(a1) - array1=array1.SetArray(a2) - gtest.Assert(array1.Len(), 3) - gtest.Assert(array1.Contains("b"), true) - gtest.Assert(array1.Contains("5"), false) - }) + array1 := garray.NewArrayFrom(a1) + array1 = array1.SetArray(a2) + gtest.Assert(array1.Len(), 3) + gtest.Assert(array1.Contains("b"), true) + gtest.Assert(array1.Contains("5"), false) + }) } func TestArray_Sum(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{0,1,2,3} - a2 := []interface{}{"a","b","c"} - a3 := []interface{}{"a","1","2"} + gtest.Case(t, func() { + a1 := []interface{}{0, 1, 2, 3} + a2 := []interface{}{"a", "b", "c"} + a3 := []interface{}{"a", "1", "2"} - array1 := garray.NewArrayFrom(a1) - array2 := garray.NewArrayFrom(a2) - array3 := garray.NewArrayFrom(a3) + array1 := garray.NewArrayFrom(a1) + array2 := garray.NewArrayFrom(a2) + array3 := garray.NewArrayFrom(a3) - gtest.Assert(array1.Sum(), 6) - gtest.Assert(array2.Sum(), 0) - gtest.Assert(array3.Sum(), 3) + gtest.Assert(array1.Sum(), 6) + gtest.Assert(array2.Sum(), 0) + gtest.Assert(array3.Sum(), 3) - }) + }) } func TestArray_Clone(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{0,1,2,3} + gtest.Case(t, func() { + a1 := []interface{}{0, 1, 2, 3} + array1 := garray.NewArrayFrom(a1) + array2 := array1.Clone() + gtest.Assert(array1.Len(), 4) + gtest.Assert(array2.Sum(), 6) + gtest.AssertEQ(array1, array2) - array1 := garray.NewArrayFrom(a1) - array2:=array1.Clone() - - gtest.Assert(array1.Len(), 4) - gtest.Assert(array2.Sum(), 6) - gtest.AssertEQ(array1, array2) - - - }) + }) } func TestArray_CountValues(t *testing.T) { - gtest.Case(t, func() { - a1 := []interface{}{"a","b","c","d","e","d"} - array1 := garray.NewArrayFrom(a1) - array2:=array1.CountValues() - gtest.Assert(len(array2),5) - gtest.Assert(array2["b"], 1) - gtest.Assert(array2["d"], 2) - }) -} \ No newline at end of file + gtest.Case(t, func() { + a1 := []interface{}{"a", "b", "c", "d", "e", "d"} + array1 := garray.NewArrayFrom(a1) + array2 := array1.CountValues() + gtest.Assert(len(array2), 5) + gtest.Assert(array2["b"], 1) + gtest.Assert(array2["d"], 2) + }) +} + +func TestSortedArray_NewSortedArrayFrom(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "f", "c"} + a2 := []interface{}{"h", "j", "i", "k"} + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + func2 := func(v1, v2 interface{}) int { + return -1 + } + array1 := garray.NewSortedArrayFrom(a1, func1) + array2 := garray.NewSortedArrayFrom(a2, func2) + + gtest.Assert(array1.Len(), 3) + gtest.Assert(array1, []interface{}{"a", "c", "f"}) + + gtest.Assert(array2.Len(), 4) + gtest.Assert(array2, []interface{}{"k", "i", "j", "h"}) + }) +} + +func TestNewSortedArrayFromCopy(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "f", "c"} + + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + func2 := func(v1, v2 interface{}) int { + return -1 + } + array1 := garray.NewSortedArrayFromCopy(a1, func1) + array2 := garray.NewSortedArrayFromCopy(a1, func2) + gtest.Assert(array1.Len(), 3) + gtest.Assert(array1, []interface{}{"a", "c", "f"}) + gtest.Assert(array1.Len(), 3) + gtest.Assert(array2, []interface{}{"c", "f", "a"}) + }) +} + +func TestSortedArray_SetArray(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "f", "c"} + a2 := []interface{}{"e", "h", "g", "k"} + + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + + array1 := garray.NewSortedArrayFrom(a1, func1) + array1.SetArray(a2) + gtest.Assert(array1.Len(), 4) + gtest.Assert(array1, []interface{}{"e", "g", "h", "k"}) + }) + +} + +func TestSortedArray_Sort(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "f", "c"} + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + array1.Sort() + gtest.Assert(array1.Len(), 3) + gtest.Assert(array1, []interface{}{"a", "c", "f"}) + }) + +} + +func TestSortedArray_Get(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "f", "c"} + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + gtest.Assert(array1.Get(2), "f") + gtest.Assert(array1.Get(1), "c") + }) + +} + +func TestSortedArray_Remove(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "b"} + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + i1 := array1.Remove(1) + gtest.Assert(gconv.String(i1), "b") + gtest.Assert(array1.Len(), 3) + gtest.Assert(array1.Contains("b"), false) + + i2 := array1.Remove(0) + gtest.Assert(gconv.String(i2), "a") + gtest.Assert(array1.Len(), 2) + gtest.Assert(array1.Contains("a"), false) + + i3 := array1.Remove(1) + gtest.Assert(gconv.String(i3), "d") + gtest.Assert(array1.Len(), 1) + gtest.Assert(array1.Contains("d"), false) + }) + +} + +func TestSortedArray_PopLeft(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "b"} + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + i1 := array1.PopLeft() + gtest.Assert(gconv.String(i1), "a") + gtest.Assert(array1.Len(), 3) + gtest.Assert(array1, []interface{}{"b", "c", "d"}) + }) + +} + +func TestSortedArray_PopRight(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "b"} + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + i1 := array1.PopRight() + gtest.Assert(gconv.String(i1), "d") + gtest.Assert(array1.Len(), 3) + gtest.Assert(array1, []interface{}{"a", "b", "c"}) + }) + +} + +func TestSortedArray_PopRand(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "b"} + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + i1 := array1.PopRand() + gtest.AssertIN(i1, []interface{}{"a", "d", "c", "b"}) + gtest.Assert(array1.Len(), 3) + + }) +} + +func TestSortedArray_PopRands(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "b"} + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + i1 := array1.PopRands(2) + gtest.Assert(len(i1), 2) + gtest.AssertIN(i1, []interface{}{"a", "d", "c", "b"}) + gtest.Assert(array1.Len(), 2) + + i2 := array1.PopRands(3) + gtest.Assert(len(i1), 2) + gtest.AssertIN(i2, []interface{}{"a", "d", "c", "b"}) + gtest.Assert(array1.Len(), 0) + + }) +} + +func TestSortedArray_PopLefts(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "b", "e", "f"} + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + i1 := array1.PopLefts(2) + gtest.Assert(len(i1), 2) + gtest.AssertIN(i1, []interface{}{"a", "d", "c", "b", "e", "f"}) + gtest.Assert(array1.Len(), 4) + + i2 := array1.PopLefts(5) + gtest.Assert(len(i2), 4) + gtest.AssertIN(i1, []interface{}{"a", "d", "c", "b", "e", "f"}) + gtest.Assert(array1.Len(), 0) + + }) +} + +func TestSortedArray_PopRights(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "b", "e", "f"} + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + i1 := array1.PopRights(2) + gtest.Assert(len(i1), 2) + gtest.Assert(i1, []interface{}{"e", "f"}) + gtest.Assert(array1.Len(), 4) + + i2 := array1.PopRights(10) + gtest.Assert(len(i2), 4) + + }) +} + +func TestSortedArray_Range(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "b", "e", "f"} + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + i1 := array1.Range(2, 5) + gtest.Assert(i1, []interface{}{"c", "d", "e"}) + gtest.Assert(array1.Len(), 6) + + i2 := array1.Range(7, 5) + gtest.Assert(len(i2), 0) + i2 = array1.Range(-1, 2) + gtest.Assert(i2, []interface{}{"a", "b"}) + + i2 = array1.Range(4, 10) + gtest.Assert(len(i2), 2) + gtest.Assert(i2, []interface{}{"e", "f"}) + + }) +} + +func TestSortedArray_Sum(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "b", "e", "f"} + a2 := []interface{}{"1", "2", "3", "b", "e", "f"} + a3 := []interface{}{"4", "5", "6"} + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + array2 := garray.NewSortedArrayFrom(a2, func1) + array3 := garray.NewSortedArrayFrom(a3, func1) + gtest.Assert(array1.Sum(), 0) + gtest.Assert(array2.Sum(), 6) + gtest.Assert(array3.Sum(), 15) + + }) +} + +func TestSortedArray_Clone(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "b", "e", "f"} + + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + array2 := array1.Clone() + gtest.Assert(array1, array2) + array1.Remove(1) + gtest.AssertNE(array1, array2) + + }) +} + +func TestSortedArray_Clear(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "b", "e", "f"} + + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + gtest.Assert(array1.Len(), 6) + array1.Clear() + gtest.Assert(array1.Len(), 0) + + }) +} + +func TestSortedArray_Chunk(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "b", "e"} + + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + i1 := array1.Chunk(2) + gtest.Assert(len(i1), 3) + gtest.Assert(i1[0], []interface{}{"a", "b"}) + gtest.Assert(i1[2], []interface{}{"e"}) + + i1 = array1.Chunk(0) + gtest.Assert(len(i1), 0) + }) +} + +func TestSortedArray_SubSlice(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "b", "e"} + + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + i1 := array1.SubSlice(2, 3) + gtest.Assert(len(i1), 3) + gtest.Assert(i1, []interface{}{"c", "d", "e"}) + + i1 = array1.SubSlice(2, 6) + gtest.Assert(len(i1), 3) + gtest.Assert(i1, []interface{}{"c", "d", "e"}) + + i1 = array1.SubSlice(7, 2) + gtest.Assert(len(i1), 0) + + }) +} + +func TestSortedArray_Rand(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c"} + + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + i1 := array1.Rand() + gtest.AssertIN(i1, []interface{}{"a", "d", "c"}) + gtest.Assert(array1.Len(), 3) + }) +} + +func TestSortedArray_Rands(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c"} + + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + i1 := array1.Rands(2) + gtest.AssertIN(i1, []interface{}{"a", "d", "c"}) + gtest.Assert(len(i1), 2) + gtest.Assert(array1.Len(), 3) + + i1 = array1.Rands(4) + gtest.Assert(len(i1), 3) + }) +} + +func TestSortedArray_Join(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c"} + + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + gtest.Assert(array1.Join(","), "a,c,d") + gtest.Assert(array1.Join("."), "a.c.d") + + }) +} + +func TestSortedArray_CountValues(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "c"} + + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + m1 := array1.CountValues() + gtest.Assert(len(m1), 3) + gtest.Assert(m1["c"], 2) + gtest.Assert(m1["a"], 1) + + }) +} + +func TestSortedArray_SetUnique(t *testing.T) { + gtest.Case(t, func() { + a1 := []interface{}{"a", "d", "c", "c"} + + func1 := func(v1, v2 interface{}) int { + return strings.Compare(gconv.String(v1), gconv.String(v2)) + } + array1 := garray.NewSortedArrayFrom(a1, func1) + array1.SetUnique(true) + gtest.Assert(array1.Len(), 3) + gtest.Assert(array1, []interface{}{"a", "c", "d"}) + }) +} diff --git a/g/container/garray/garray_z_unit_string_test.go b/g/container/garray/garray_z_unit_string_test.go index db0ec7b26..b7c912e63 100644 --- a/g/container/garray/garray_z_unit_string_test.go +++ b/g/container/garray/garray_z_unit_string_test.go @@ -124,6 +124,8 @@ func TestStringArray_Fill(t *testing.T) { array2 := garray.NewStringArrayFrom(a2) gtest.Assert(array1.Fill(1, 2, "100").Slice(), []string{"0", "100", "100"}) gtest.Assert(array2.Fill(0, 2, "100").Slice(), []string{"100", "100"}) + s1 := array2.Fill(-1, 2, "100") + gtest.Assert(s1.Len(), 2) }) } @@ -136,6 +138,8 @@ func TestStringArray_Chunk(t *testing.T) { gtest.Assert(chunks[0], []string{"1", "2"}) gtest.Assert(chunks[1], []string{"3", "4"}) gtest.Assert(chunks[2], []string{"5"}) + gtest.Assert(len(array1.Chunk(0)), 0) + }) } @@ -166,6 +170,9 @@ func TestStringArray_Rand(t *testing.T) { gtest.Assert(len(array1.Rands(2)), "2") gtest.Assert(len(array1.Rands(10)), "7") gtest.AssertIN(array1.Rands(1)[0], a1) + gtest.Assert(len(array1.Rand()), 1) + gtest.AssertIN(array1.Rand(), a1) + }) } @@ -321,21 +328,312 @@ func TestStringArray_CountValues(t *testing.T) { }) } -func TestNewSortedStringArrayFrom(t *testing.T){ +func TestNewSortedStringArrayFrom(t *testing.T) { gtest.Case(t, func() { - a1:=[]string{"a", "d", "c","b"} - s1 :=garray.NewSortedStringArrayFrom(a1,true) - gtest.Assert(s1,[]string{"a", "b", "c","d"}) - s2 :=garray.NewSortedStringArrayFrom(a1,false) - gtest.Assert(s2,[]string{"a", "b", "c","d"}) + a1 := []string{"a", "d", "c", "b"} + s1 := garray.NewSortedStringArrayFrom(a1, true) + gtest.Assert(s1, []string{"a", "b", "c", "d"}) + s2 := garray.NewSortedStringArrayFrom(a1, false) + gtest.Assert(s2, []string{"a", "b", "c", "d"}) }) } -func TestNewSortedStringArrayFromCopy(t *testing.T){ +func TestNewSortedStringArrayFromCopy(t *testing.T) { gtest.Case(t, func() { - a1:=[]string{"a", "d", "c","b"} - s1 :=garray.NewSortedStringArrayFromCopy(a1,true) - gtest.Assert(s1.Len(),4) - // gtest.Assert(s1,[]string{"a", "b", "c","d"}) //todo 这里没有排序,需主库修正 + a1 := []string{"a", "d", "c", "b"} + s1 := garray.NewSortedStringArrayFromCopy(a1, true) + gtest.Assert(s1.Len(), 4) + gtest.Assert(s1, []string{"a", "b", "c", "d"}) + }) +} + +func TestSortedStringArray_SetArray(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"a", "d", "c", "b"} + a2 := []string{"f", "g", "h"} + array1 := garray.NewSortedStringArrayFrom(a1) + array1.SetArray(a2) + gtest.Assert(array1.Len(), 3) + gtest.Assert(array1.Contains("d"), false) + gtest.Assert(array1.Contains("b"), false) + gtest.Assert(array1.Contains("g"), true) + + }) +} + +func TestSortedStringArray_Sort(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"a", "d", "c", "b"} + array1 := garray.NewSortedStringArrayFrom(a1) + + gtest.Assert(array1, []string{"a", "b", "c", "d"}) + array1.Sort() //todo 这个SortedStringArray.sort这个方法没有必要, + gtest.Assert(array1.Len(), 4) + gtest.Assert(array1.Contains("c"), true) + gtest.Assert(array1, []string{"a", "b", "c", "d"}) + }) +} + +func TestSortedStringArray_Get(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"a", "d", "c", "b"} + array1 := garray.NewSortedStringArrayFrom(a1) + gtest.Assert(array1.Get(2), "c") + gtest.Assert(array1.Get(0), "a") + + }) +} + +func TestSortedStringArray_Remove(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"a", "d", "c", "b"} + array1 := garray.NewSortedStringArrayFrom(a1) + gtest.Assert(array1.Remove(2), "c") + gtest.Assert(array1.Get(2), "d") + gtest.Assert(array1.Len(), 3) + gtest.Assert(array1.Contains("c"), false) + + gtest.Assert(array1.Remove(0), "a") + gtest.Assert(array1.Len(), 2) + gtest.Assert(array1.Contains("a"), false) + + // 此时array1里的元素只剩下2个 + gtest.Assert(array1.Remove(1), "d") + gtest.Assert(array1.Len(), 1) + + }) +} + +func TestSortedStringArray_PopLeft(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "c", "b"} + array1 := garray.NewSortedStringArrayFrom(a1) + s1 := array1.PopLeft() + gtest.Assert(s1, "a") + gtest.Assert(array1.Len(), 4) + gtest.Assert(array1.Contains("a"), false) + }) +} + +func TestSortedStringArray_PopRight(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "c", "b"} + array1 := garray.NewSortedStringArrayFrom(a1) + s1 := array1.PopRight() + gtest.Assert(s1, "e") + gtest.Assert(array1.Len(), 4) + gtest.Assert(array1.Contains("e"), false) + }) +} + +func TestSortedStringArray_PopRand(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "c", "b"} + array1 := garray.NewSortedStringArrayFrom(a1) + s1 := array1.PopRand() + gtest.AssertIN(s1, []string{"e", "a", "d", "c", "b"}) + gtest.Assert(array1.Len(), 4) + gtest.Assert(array1.Contains(s1), false) + }) +} + +func TestSortedStringArray_PopRands(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "c", "b"} + array1 := garray.NewSortedStringArrayFrom(a1) + s1 := array1.PopRands(2) + gtest.AssertIN(s1, []string{"e", "a", "d", "c", "b"}) + gtest.Assert(array1.Len(), 3) + gtest.Assert(len(s1), 2) + + s1 = array1.PopRands(4) + gtest.Assert(len(s1), 3) + gtest.AssertIN(s1, []string{"e", "a", "d", "c", "b"}) + }) +} + +func TestSortedStringArray_PopLefts(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "c", "b"} + array1 := garray.NewSortedStringArrayFrom(a1) + s1 := array1.PopLefts(2) + gtest.Assert(s1, []string{"a", "b"}) + gtest.Assert(array1.Len(), 3) + gtest.Assert(len(s1), 2) + + s1 = array1.PopLefts(4) + gtest.Assert(len(s1), 3) + gtest.Assert(s1, []string{"c", "d", "e"}) + }) +} + +func TestSortedStringArray_PopRights(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "c", "b", "f", "g"} + array1 := garray.NewSortedStringArrayFrom(a1) + s1 := array1.PopRights(2) + gtest.Assert(s1, []string{"f", "g"}) + gtest.Assert(array1.Len(), 5) + gtest.Assert(len(s1), 2) + s1 = array1.PopRights(6) + gtest.Assert(len(s1), 5) + gtest.Assert(s1, []string{"a", "b", "c", "d", "e"}) + gtest.Assert(array1.Len(), 0) + }) +} + +func TestSortedStringArray_Range(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "c", "b", "f", "g"} + array1 := garray.NewSortedStringArrayFrom(a1) + s1 := array1.Range(2, 4) + gtest.Assert(len(s1), 2) + gtest.Assert(s1, []string{"c", "d"}) + + s1 = array1.Range(-1, 2) + gtest.Assert(len(s1), 2) + gtest.Assert(s1, []string{"a", "b"}) + + s1 = array1.Range(4, 8) + gtest.Assert(len(s1), 3) + gtest.Assert(s1, []string{"e", "f", "g"}) + }) +} + +func TestSortedStringArray_Sum(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "c", "b", "f", "g"} + a2 := []string{"1", "2", "3", "4", "a"} + array1 := garray.NewSortedStringArrayFrom(a1) + array2 := garray.NewSortedStringArrayFrom(a2) + gtest.Assert(array1.Sum(), 0) + gtest.Assert(array2.Sum(), 10) + }) +} + +func TestSortedStringArray_Clone(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "c", "b", "f", "g"} + array1 := garray.NewSortedStringArrayFrom(a1) + array2 := array1.Clone() + gtest.Assert(array1, array2) + array1.Remove(1) + gtest.Assert(array2.Len(), 7) + }) +} + +func TestSortedStringArray_Clear(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "c", "b", "f", "g"} + array1 := garray.NewSortedStringArrayFrom(a1) + array1.Clear() + gtest.Assert(array1.Len(), 0) + + }) +} + +func TestSortedStringArray_SubSlice(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "c", "b", "f", "g"} + array1 := garray.NewSortedStringArrayFrom(a1) + s1 := array1.SubSlice(1, 3) + gtest.Assert(len(s1), 3) + gtest.Assert(s1, []string{"b", "c", "d"}) + gtest.Assert(array1.Len(), 7) + + s2 := array1.SubSlice(1, 10) + gtest.Assert(len(s2), 6) + + s3 := array1.SubSlice(10, 2) + gtest.Assert(len(s3), 0) + + }) +} + +func TestSortedStringArray_Len(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "c", "b", "f", "g"} + array1 := garray.NewSortedStringArrayFrom(a1) + gtest.Assert(array1.Len(), 7) + + }) +} + +func TestSortedStringArray_Rand(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d"} + array1 := garray.NewSortedStringArrayFrom(a1) + gtest.AssertIN(array1.Rand(), []string{"e", "a", "d"}) + + }) +} + +func TestSortedStringArray_Rands(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d"} + array1 := garray.NewSortedStringArrayFrom(a1) + s1 := array1.Rands(2) + + gtest.AssertIN(s1, []string{"e", "a", "d"}) + gtest.Assert(len(s1), 2) + + s1 = array1.Rands(4) + gtest.AssertIN(s1, []string{"e", "a", "d"}) + gtest.Assert(len(s1), 3) + }) +} + +func TestSortedStringArray_Join(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d"} + array1 := garray.NewSortedStringArrayFrom(a1) + gtest.Assert(array1.Join(","), "a,d,e") + gtest.Assert(array1.Join("."), "a.d.e") + }) +} + +func TestSortedStringArray_CountValues(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "a", "c"} + array1 := garray.NewSortedStringArrayFrom(a1) + m1 := array1.CountValues() + gtest.Assert(m1["a"], 2) + gtest.Assert(m1["d"], 1) + + }) +} + +func TestSortedStringArray_Chunk(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "a", "c"} + array1 := garray.NewSortedStringArrayFrom(a1) + array2 := array1.Chunk(2) + gtest.Assert(len(array2), 3) + gtest.Assert(len(array2[0]), 2) + gtest.Assert(array2[1], []string{"c", "d"}) + }) +} + +func TestSortedStringArray_SetUnique(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "a", "c"} + array1 := garray.NewSortedStringArrayFrom(a1) + array2 := array1.SetUnique(true) + gtest.Assert(array2.Len(), 4) + gtest.Assert(array2, []string{"a", "c", "d", "e"}) + }) +} + +func TestStringArray_Remove(t *testing.T) { + gtest.Case(t, func() { + a1 := []string{"e", "a", "d", "a", "c"} + array1 := garray.NewStringArrayFrom(a1) + s1 := array1.Remove(1) + gtest.Assert(s1, "a") + gtest.Assert(array1.Len(), 4) + s1 = array1.Remove(3) + gtest.Assert(s1, "c") + gtest.Assert(array1.Len(), 3) + }) } From d1d8cd848263d072f4a2b2fbdcab7cf35c49ca23 Mon Sep 17 00:00:00 2001 From: jroam Date: Thu, 13 Jun 2019 21:44:14 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E7=94=A8gofmt=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/container/garray/garray_z_bench_test.go | 32 ++++++++++----------- g/container/gset/gset_z_unit_string_test.go | 2 -- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/g/container/garray/garray_z_bench_test.go b/g/container/garray/garray_z_bench_test.go index 4bf02e45a..0ca1d4a16 100644 --- a/g/container/garray/garray_z_bench_test.go +++ b/g/container/garray/garray_z_bench_test.go @@ -9,35 +9,35 @@ package garray_test import ( - "github.com/gogf/gf/g/container/garray" - "testing" + "github.com/gogf/gf/g/container/garray" + "testing" ) var ( - sortedIntArray = garray.NewSortedIntArray() + sortedIntArray = garray.NewSortedIntArray() ) func BenchmarkSortedIntArray_Add(b *testing.B) { - b.N = 1000 - for i := 0; i < b.N; i++ { - sortedIntArray.Add(i) - } + b.N = 1000 + for i := 0; i < b.N; i++ { + sortedIntArray.Add(i) + } } func BenchmarkSortedIntArray_Search(b *testing.B) { - for i := 0; i < b.N; i++ { - sortedIntArray.Search(i) - } + for i := 0; i < b.N; i++ { + sortedIntArray.Search(i) + } } func BenchmarkSortedIntArray_PopLeft(b *testing.B) { - for i := 0; i < b.N; i++ { - sortedIntArray.PopLeft() - } + for i := 0; i < b.N; i++ { + sortedIntArray.PopLeft() + } } func BenchmarkSortedIntArray_PopRight(b *testing.B) { - for i := 0; i < b.N; i++ { - sortedIntArray.PopLeft() - } + for i := 0; i < b.N; i++ { + sortedIntArray.PopLeft() + } } diff --git a/g/container/gset/gset_z_unit_string_test.go b/g/container/gset/gset_z_unit_string_test.go index de04653df..c75c2f10b 100644 --- a/g/container/gset/gset_z_unit_string_test.go +++ b/g/container/gset/gset_z_unit_string_test.go @@ -254,5 +254,3 @@ func TestStringSet_Pops(t *testing.T) { gtest.AssertIN(str2, []string{"a", "b", "c"}) }) } - - From b3a00becf3e454611991f56ebdf55dc7f82386f2 Mon Sep 17 00:00:00 2001 From: jroam Date: Thu, 13 Jun 2019 22:18:45 +0800 Subject: [PATCH 12/12] =?UTF-8?q?sort=5Fint=E6=81=A2=E5=A4=8D=E6=88=90?= =?UTF-8?q?=E4=B8=BB=E5=BA=93=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/container/garray/garray_sorted_int.go | 621 ++++++++++++------------ 1 file changed, 306 insertions(+), 315 deletions(-) diff --git a/g/container/garray/garray_sorted_int.go b/g/container/garray/garray_sorted_int.go index 5b0ccab0b..6c71564e9 100644 --- a/g/container/garray/garray_sorted_int.go +++ b/g/container/garray/garray_sorted_int.go @@ -7,287 +7,286 @@ package garray import ( - "bytes" + "bytes" "fmt" "github.com/gogf/gf/g/container/gtype" - "github.com/gogf/gf/g/internal/rwmutex" - "github.com/gogf/gf/g/util/gconv" - "github.com/gogf/gf/g/util/grand" - "math" - "sort" + "github.com/gogf/gf/g/internal/rwmutex" + "github.com/gogf/gf/g/util/gconv" + "github.com/gogf/gf/g/util/grand" + "math" + "sort" ) // It's using increasing order in default. type SortedIntArray struct { - mu *rwmutex.RWMutex - array []int - unique *gtype.Bool // Whether enable unique feature(false) - comparator func(v1, v2 int) int // Comparison function(it returns -1: v1 < v2; 0: v1 == v2; 1: v1 > v2) + mu *rwmutex.RWMutex + array []int + unique *gtype.Bool // Whether enable unique feature(false) + comparator func(v1, v2 int) int // Comparison function(it returns -1: v1 < v2; 0: v1 == v2; 1: v1 > v2) } // NewSortedIntArray creates and returns an empty sorted array. // The param used to specify whether using array in un-concurrent-safety, // which is false in default. -func NewSortedIntArray(unsafe ...bool) *SortedIntArray { - return NewSortedIntArraySize(0, unsafe...) +func NewSortedIntArray(unsafe...bool) *SortedIntArray { + return NewSortedIntArraySize(0, unsafe...) } // NewSortedIntArraySize create and returns an sorted array with given size and cap. // The param used to specify whether using array in un-concurrent-safety, // which is false in default. -func NewSortedIntArraySize(cap int, unsafe ...bool) *SortedIntArray { - return &SortedIntArray{ - mu: rwmutex.New(unsafe...), - array: make([]int, 0, cap), - unique: gtype.NewBool(), - comparator: func(v1, v2 int) int { - if v1 < v2 { - return -1 - } - if v1 > v2 { - return 1 - } - return 0 - }, - } +func NewSortedIntArraySize(cap int, unsafe...bool) *SortedIntArray { + return &SortedIntArray { + mu : rwmutex.New(unsafe...), + array : make([]int, 0, cap), + unique : gtype.NewBool(), + comparator : func(v1, v2 int) int { + if v1 < v2 { + return -1 + } + if v1 > v2 { + return 1 + } + return 0 + }, + } } // NewIntArrayFrom creates and returns an sorted array with given slice . // The param used to specify whether using array in un-concurrent-safety, // which is false in default. -func NewSortedIntArrayFrom(array []int, unsafe ...bool) *SortedIntArray { - a := NewSortedIntArraySize(0, unsafe...) - a.array = array - sort.Ints(a.array) - return a +func NewSortedIntArrayFrom(array []int, unsafe...bool) *SortedIntArray { + a := NewSortedIntArraySize(0, unsafe...) + a.array = array + sort.Ints(a.array) + return a } // NewSortedIntArrayFromCopy creates and returns an sorted array from a copy of given slice . // The param used to specify whether using array in un-concurrent-safety, // which is false in default. - -func NewSortedIntArrayFromCopy(array []int, unsafe ...bool) *SortedIntArray { - newArray := make([]int, len(array)) - copy(newArray, array) - return NewSortedIntArrayFrom(newArray, unsafe...) +func NewSortedIntArrayFromCopy(array []int, unsafe...bool) *SortedIntArray { + newArray := make([]int, len(array)) + copy(newArray, array) + return NewSortedIntArrayFrom(newArray, unsafe...) } // SetArray sets the underlying slice array with the given . func (a *SortedIntArray) SetArray(array []int) *SortedIntArray { - a.mu.Lock() - defer a.mu.Unlock() - a.array = array - sort.Ints(a.array) - return a + a.mu.Lock() + defer a.mu.Unlock() + a.array = array + sort.Ints(a.array) + return a } // Sort sorts the array in increasing order. // The param controls whether sort // in increasing order(default) or decreasing order. func (a *SortedIntArray) Sort() *SortedIntArray { - a.mu.Lock() - defer a.mu.Unlock() - sort.Ints(a.array) - return a + a.mu.Lock() + defer a.mu.Unlock() + sort.Ints(a.array) + return a } // Add adds one or multiple values to sorted array, the array always keeps sorted. -func (a *SortedIntArray) Add(values ...int) *SortedIntArray { - if len(values) == 0 { - return a - } - a.mu.Lock() - defer a.mu.Unlock() - for _, value := range values { - index, cmp := a.binSearch(value, false) - if a.unique.Val() && cmp == 0 { - continue - } - if index < 0 { - a.array = append(a.array, value) - continue - } - if cmp > 0 { - index++ - } - rear := append([]int{}, a.array[index:]...) - a.array = append(a.array[0:index], value) - a.array = append(a.array, rear...) - } - return a +func (a *SortedIntArray) Add(values...int) *SortedIntArray { + if len(values) == 0 { + return a + } + a.mu.Lock() + defer a.mu.Unlock() + for _, value := range values { + index, cmp := a.binSearch(value, false) + if a.unique.Val() && cmp == 0 { + continue + } + if index < 0 { + a.array = append(a.array, value) + continue + } + if cmp > 0 { + index++ + } + rear := append([]int{}, a.array[index : ]...) + a.array = append(a.array[0 : index], value) + a.array = append(a.array, rear...) + } + return a } // Get returns the value of the specified index, // the caller should notice the boundary of the array. func (a *SortedIntArray) Get(index int) int { - a.mu.RLock() - defer a.mu.RUnlock() - value := a.array[index] - return value + a.mu.RLock() + defer a.mu.RUnlock() + value := a.array[index] + return value } // Remove removes an item by index. func (a *SortedIntArray) Remove(index int) int { - a.mu.Lock() - defer a.mu.Unlock() + a.mu.Lock() + defer a.mu.Unlock() // Determine array boundaries when deleting to improve deletion efficiency. - if index == 0 { - value := a.array[0] - a.array = a.array[1:] - return value - } else if index == len(a.array)-1 { - value := a.array[index] - a.array = a.array[:index] - return value - } + if index == 0 { + value := a.array[0] + a.array = a.array[1 : ] + return value + } else if index == len(a.array) - 1 { + value := a.array[index] + a.array = a.array[: index] + return value + } // If it is a non-boundary delete, // it will involve the creation of an array, // then the deletion is less efficient. - value := a.array[index] - a.array = append(a.array[:index], a.array[index+1:]...) - return value + value := a.array[index] + a.array = append(a.array[ : index], a.array[index + 1 : ]...) + return value } // PopLeft pops and returns an item from the beginning of array. func (a *SortedIntArray) PopLeft() int { - a.mu.Lock() - defer a.mu.Unlock() - value := a.array[0] - a.array = a.array[1:] - return value + a.mu.Lock() + defer a.mu.Unlock() + value := a.array[0] + a.array = a.array[1 : ] + return value } // PopRight pops and returns an item from the end of array. func (a *SortedIntArray) PopRight() int { - a.mu.Lock() - defer a.mu.Unlock() - index := len(a.array) - 1 - value := a.array[index] - a.array = a.array[:index] - return value + a.mu.Lock() + defer a.mu.Unlock() + index := len(a.array) - 1 + value := a.array[index] + a.array = a.array[: index] + return value } // PopRand randomly pops and return an item out of array. func (a *SortedIntArray) PopRand() int { - return a.Remove(grand.Intn(len(a.array))) + return a.Remove(grand.Intn(len(a.array))) } // PopRands randomly pops and returns items out of array. func (a *SortedIntArray) PopRands(size int) []int { - a.mu.Lock() - defer a.mu.Unlock() - if size > len(a.array) { - size = len(a.array) - } - array := make([]int, size) - for i := 0; i < size; i++ { - index := grand.Intn(len(a.array)) - array[i] = a.array[index] - a.array = append(a.array[:index], a.array[index+1:]...) - } - return array + a.mu.Lock() + defer a.mu.Unlock() + if size > len(a.array) { + size = len(a.array) + } + array := make([]int, size) + for i := 0; i < size; i++ { + index := grand.Intn(len(a.array)) + array[i] = a.array[index] + a.array = append(a.array[ : index], a.array[index + 1 : ]...) + } + return array } // PopLefts pops and returns items from the beginning of array. func (a *SortedIntArray) PopLefts(size int) []int { - a.mu.Lock() - defer a.mu.Unlock() - length := len(a.array) - if size > length { - size = length - } - value := a.array[0:size] - a.array = a.array[size:] - return value + a.mu.Lock() + defer a.mu.Unlock() + length := len(a.array) + if size > length { + size = length + } + value := a.array[0 : size] + a.array = a.array[size : ] + return value } // PopRights pops and returns items from the end of array. func (a *SortedIntArray) PopRights(size int) []int { - a.mu.Lock() - defer a.mu.Unlock() - index := len(a.array) - size - if index < 0 { - index = 0 - } - value := a.array[index:] - a.array = a.array[:index] - return value + a.mu.Lock() + defer a.mu.Unlock() + index := len(a.array) - size + if index < 0 { + index = 0 + } + value := a.array[index :] + a.array = a.array[ : index] + return value } // Range picks and returns items by range, like array[start:end]. // Notice, if in concurrent-safe usage, it returns a copy of slice; // else a pointer to the underlying data. func (a *SortedIntArray) Range(start, end int) []int { - a.mu.RLock() - defer a.mu.RUnlock() - length := len(a.array) - if start > length || start > end { - return nil - } - if start < 0 { - start = 0 - } - if end > length { - end = length - } - array := ([]int)(nil) - if a.mu.IsSafe() { - a.mu.RLock() - defer a.mu.RUnlock() - array = make([]int, end-start) - copy(array, a.array[start:end]) - } else { - array = a.array[start:end] - } - return array + a.mu.RLock() + defer a.mu.RUnlock() + length := len(a.array) + if start > length || start > end { + return nil + } + if start < 0 { + start = 0 + } + if end > length { + end = length + } + array := ([]int)(nil) + if a.mu.IsSafe() { + a.mu.RLock() + defer a.mu.RUnlock() + array = make([]int, end - start) + copy(array, a.array[start : end]) + } else { + array = a.array[start : end] + } + return array } // Len returns the length of array. func (a *SortedIntArray) Len() int { - a.mu.RLock() - length := len(a.array) - a.mu.RUnlock() - return length + a.mu.RLock() + length := len(a.array) + a.mu.RUnlock() + return length } // Sum returns the sum of values in an array. func (a *SortedIntArray) Sum() (sum int) { - a.mu.RLock() - defer a.mu.RUnlock() - for _, v := range a.array { - sum += v - } - return + a.mu.RLock() + defer a.mu.RUnlock() + for _, v := range a.array { + sum += v + } + return } // Slice returns the underlying data of array. // Notice, if in concurrent-safe usage, it returns a copy of slice; // else a pointer to the underlying data. func (a *SortedIntArray) Slice() []int { - array := ([]int)(nil) - if a.mu.IsSafe() { - a.mu.RLock() - defer a.mu.RUnlock() - array = make([]int, len(a.array)) - copy(array, a.array) - } else { - array = a.array - } - return array + array := ([]int)(nil) + if a.mu.IsSafe() { + a.mu.RLock() + defer a.mu.RUnlock() + array = make([]int, len(a.array)) + copy(array, a.array) + } else { + array = a.array + } + return array } // Contains checks whether a value exists in the array. func (a *SortedIntArray) Contains(value int) bool { - return a.Search(value) != -1 + return a.Search(value) != -1 } // Search searches array by , returns the index of , // or returns -1 if not exists. func (a *SortedIntArray) Search(value int) (index int) { - if i, r := a.binSearch(value, true); r == 0 { - return i - } - return -1 + if i, r := a.binSearch(value, true); r == 0 { + return i + } + return -1 } // Binary search. @@ -296,95 +295,93 @@ func (a *SortedIntArray) Search(value int) (index int) { // If lesser than 0, it means the value at is lesser than . // If greater than 0, it means the value at is greater than . func (a *SortedIntArray) binSearch(value int, lock bool) (index int, result int) { - if len(a.array) == 0 { - return -1, -2 - } - if lock { - a.mu.RLock() - defer a.mu.RUnlock() - } - min := 0 - max := len(a.array) - 1 - mid := 0 - cmp := -2 - for min <= max { - mid = int((min + max) / 2) - cmp = a.comparator(value, a.array[mid]) - switch { - case cmp < 0: - max = mid - 1 - case cmp > 0: - min = mid + 1 - default: - return mid, cmp - } - } - return mid, cmp + if len(a.array) == 0 { + return -1, -2 + } + if lock { + a.mu.RLock() + defer a.mu.RUnlock() + } + min := 0 + max := len(a.array) - 1 + mid := 0 + cmp := -2 + for min <= max { + mid = int((min + max) / 2) + cmp = a.comparator(value, a.array[mid]) + switch { + case cmp < 0 : max = mid - 1 + case cmp > 0 : min = mid + 1 + default : + return mid, cmp + } + } + return mid, cmp } // SetUnique sets unique mark to the array, // which means it does not contain any repeated items. // It also do unique check, remove all repeated items. func (a *SortedIntArray) SetUnique(unique bool) *SortedIntArray { - oldUnique := a.unique.Val() - a.unique.Set(unique) - if unique && oldUnique != unique { - a.Unique() - } - return a + oldUnique := a.unique.Val() + a.unique.Set(unique) + if unique && oldUnique != unique { + a.Unique() + } + return a } // Unique uniques the array, clear repeated items. func (a *SortedIntArray) Unique() *SortedIntArray { - a.mu.Lock() - i := 0 - for { - if i == len(a.array)-1 { - break - } - if a.comparator(a.array[i], a.array[i+1]) == 0 { - a.array = append(a.array[:i+1], a.array[i+1+1:]...) - } else { - i++ - } - } - a.mu.Unlock() - return a + a.mu.Lock() + i := 0 + for { + if i == len(a.array) - 1 { + break + } + if a.comparator(a.array[i], a.array[i + 1]) == 0 { + a.array = append(a.array[ : i + 1], a.array[i + 1 + 1 : ]...) + } else { + i++ + } + } + a.mu.Unlock() + return a } // Clone returns a new array, which is a copy of current array. func (a *SortedIntArray) Clone() (newArray *SortedIntArray) { - a.mu.RLock() - array := make([]int, len(a.array)) - copy(array, a.array) - a.mu.RUnlock() - return NewSortedIntArrayFrom(array, !a.mu.IsSafe()) + a.mu.RLock() + array := make([]int, len(a.array)) + copy(array, a.array) + a.mu.RUnlock() + return NewSortedIntArrayFrom(array, !a.mu.IsSafe()) } // Clear deletes all items of current array. func (a *SortedIntArray) Clear() *SortedIntArray { - a.mu.Lock() - if len(a.array) > 0 { - a.array = make([]int, 0) - } - a.mu.Unlock() - return a + a.mu.Lock() + if len(a.array) > 0 { + a.array = make([]int, 0) + } + a.mu.Unlock() + return a } // LockFunc locks writing by callback function . func (a *SortedIntArray) LockFunc(f func(array []int)) *SortedIntArray { - a.mu.Lock() - defer a.mu.Unlock() - f(a.array) - return a + a.mu.Lock() + defer a.mu.Unlock() + f(a.array) + return a } // RLockFunc locks reading by callback function . func (a *SortedIntArray) RLockFunc(f func(array []int)) *SortedIntArray { - a.mu.RLock() - defer a.mu.RUnlock() - f(a.array) - return a + a.mu.RLock() + defer a.mu.RUnlock() + f(a.array) + return a } // Merge merges into current array. @@ -392,105 +389,99 @@ func (a *SortedIntArray) RLockFunc(f func(array []int)) *SortedIntArray { // The difference between Merge and Append is Append supports only specified slice type, // but Merge supports more parameter types. func (a *SortedIntArray) Merge(array interface{}) *SortedIntArray { - switch v := array.(type) { - case *Array: - a.Add(gconv.Ints(v.Slice())...) - case *IntArray: - a.Add(gconv.Ints(v.Slice())...) - case *StringArray: - a.Add(gconv.Ints(v.Slice())...) - case *SortedArray: - a.Add(gconv.Ints(v.Slice())...) - case *SortedIntArray: - a.Add(gconv.Ints(v.Slice())...) - case *SortedStringArray: - a.Add(gconv.Ints(v.Slice())...) - default: - a.Add(gconv.Ints(array)...) - } - return a + switch v := array.(type) { + case *Array: a.Add(gconv.Ints(v.Slice())...) + case *IntArray: a.Add(gconv.Ints(v.Slice())...) + case *StringArray: a.Add(gconv.Ints(v.Slice())...) + case *SortedArray: a.Add(gconv.Ints(v.Slice())...) + case *SortedIntArray: a.Add(gconv.Ints(v.Slice())...) + case *SortedStringArray: a.Add(gconv.Ints(v.Slice())...) + default: + a.Add(gconv.Ints(array)...) + } + return a } // Chunk splits an array into multiple arrays, // the size of each array is determined by . // The last chunk may contain less than size elements. func (a *SortedIntArray) Chunk(size int) [][]int { - if size < 1 { - return nil - } - a.mu.RLock() - defer a.mu.RUnlock() - length := len(a.array) - chunks := int(math.Ceil(float64(length) / float64(size))) - var n [][]int - for i, end := 0, 0; chunks > 0; chunks-- { - end = (i + 1) * size - if end > length { - end = length - } - n = append(n, a.array[i*size:end]) - i++ - } - return n + if size < 1 { + return nil + } + a.mu.RLock() + defer a.mu.RUnlock() + length := len(a.array) + chunks := int(math.Ceil(float64(length) / float64(size))) + var n [][]int + for i, end := 0, 0; chunks > 0; chunks-- { + end = (i + 1) * size + if end > length { + end = length + } + n = append(n, a.array[i*size : end]) + i++ + } + return n } // SubSlice returns a slice of elements from the array as specified // by the and parameters. // If in concurrent safe usage, it returns a copy of the slice; else a pointer. func (a *SortedIntArray) SubSlice(offset, size int) []int { - a.mu.RLock() - defer a.mu.RUnlock() - if offset > len(a.array) { - return nil - } - if offset+size > len(a.array) { - size = len(a.array) - offset - } - if a.mu.IsSafe() { - s := make([]int, size) - copy(s, a.array[offset:]) - return s - } else { - return a.array[offset:] - } + a.mu.RLock() + defer a.mu.RUnlock() + if offset > len(a.array) { + return nil + } + if offset + size > len(a.array) { + size = len(a.array) - offset + } + if a.mu.IsSafe() { + s := make([]int, size) + copy(s, a.array[offset:]) + return s + } else { + return a.array[offset:] + } } // Rand randomly returns one item from array(no deleting). func (a *SortedIntArray) Rand() int { - a.mu.RLock() - defer a.mu.RUnlock() - return a.array[grand.Intn(len(a.array))] + a.mu.RLock() + defer a.mu.RUnlock() + return a.array[grand.Intn(len(a.array))] } // Rands randomly returns items from array(no deleting). func (a *SortedIntArray) Rands(size int) []int { - a.mu.RLock() - defer a.mu.RUnlock() - if size > len(a.array) { - size = len(a.array) - } - n := make([]int, size) - for i, v := range grand.Perm(len(a.array)) { - n[i] = a.array[v] - if i == size-1 { - break - } - } - return n + a.mu.RLock() + defer a.mu.RUnlock() + if size > len(a.array) { + size = len(a.array) + } + n := make([]int, size) + for i, v := range grand.Perm(len(a.array)) { + n[i] = a.array[v] + if i == size - 1 { + break + } + } + return n } // Join joins array elements with a string . func (a *SortedIntArray) Join(glue string) string { - a.mu.RLock() - defer a.mu.RUnlock() - buffer := bytes.NewBuffer(nil) - for k, v := range a.array { - buffer.WriteString(gconv.String(v)) - if k != len(a.array)-1 { - buffer.WriteString(glue) - } - } - return buffer.String() + a.mu.RLock() + defer a.mu.RUnlock() + buffer := bytes.NewBuffer(nil) + for k, v := range a.array { + buffer.WriteString(gconv.String(v)) + if k != len(a.array) - 1 { + buffer.WriteString(glue) + } + } + return buffer.String() } // CountValues counts the number of occurrences of all values in the array. @@ -509,4 +500,4 @@ func (a *SortedIntArray) String() string { a.mu.RLock() defer a.mu.RUnlock() return fmt.Sprint(a.array) -} +} \ No newline at end of file