From 80fddad64d4c0150ae38eee4943113dac1da8e01 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 8 May 2020 21:08:06 +0800 Subject: [PATCH] fix issue in PopRight function for package garray; improve json.Marshal for package garray --- .example/other/test.go | 41 +++---- container/garray/garray_normal_any.go | 5 +- container/garray/garray_normal_int.go | 5 +- container/garray/garray_normal_str.go | 5 +- container/garray/garray_sorted_any.go | 5 +- container/garray/garray_sorted_int.go | 5 +- container/garray/garray_sorted_str.go | 5 +- .../garray_z_unit_normal_any_array_test.go | 98 ++++++++++++++++ .../garray_z_unit_normal_int_array_test.go | 97 +++++++++++++++ .../garray_z_unit_normal_str_array_test.go | 97 +++++++++++++++ .../garray_z_unit_sorted_any_array_test.go | 110 +++++++++++++++--- .../garray_z_unit_sorted_int_array_test.go | 71 +++++++++++ .../garray_z_unit_sorted_str_array_test.go | 74 ++++++++++++ 13 files changed, 573 insertions(+), 45 deletions(-) diff --git a/.example/other/test.go b/.example/other/test.go index 2fe3b9889..81e3c1ead 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -1,29 +1,30 @@ package main import ( - "encoding/hex" - "fmt" - "github.com/gogf/gf/encoding/gbase64" + "time" + + "github.com/gogf/gf/os/glog" "github.com/gogf/gf/util/gconv" - "github.com/gogf/gf/util/grand" + + "github.com/gogf/gf/container/garray" ) -// bytesToHexString converts binary content to hex string content. -func bytesToHexStr(b []byte) string { - dst := make([]byte, hex.EncodedLen(len(b))) - hex.Encode(dst, b) - return gconv.UnsafeBytesToStr(dst) -} - func main() { - b := make([]byte, 1024) - for i := 0; i < 1024; i++ { - b[i] = byte(grand.N(0, 255)) + // 创建对象 + var a = garray.NewStrArray() + // push 100个 字符串进去 + for i := 1; i <= 10; i++ { + a.PushLeft("a_" + gconv.String(i)) + } + // 死循环 Pop 取出 + for { + glog.Printf("a.Len() ---> %d", a.Len()) + if a.Len() == 0 { + break + } + if v, isFound := a.PopRight(); isFound { + glog.Printf("Pop -----> %s", v) + } + time.Sleep(50 * time.Millisecond) } - - fmt.Println(bytesToHexStr(b)) - fmt.Println(len(b)) - fmt.Println(len(bytesToHexStr(b))) - fmt.Println(gbase64.EncodeToString(b)) - fmt.Println(len(gbase64.EncodeToString(b))) } diff --git a/container/garray/garray_normal_any.go b/container/garray/garray_normal_any.go index 2a79fbf8d..292c14db1 100644 --- a/container/garray/garray_normal_any.go +++ b/container/garray/garray_normal_any.go @@ -288,7 +288,7 @@ func (a *Array) PopRight() (value interface{}, found bool) { a.mu.Lock() defer a.mu.Unlock() index := len(a.array) - 1 - if index <= 0 { + if index < 0 { return nil, false } value = a.array[index] @@ -718,7 +718,8 @@ func (a *Array) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (a *Array) MarshalJSON() ([]byte, error) { +// Note that do not use pointer as its receiver here. +func (a Array) MarshalJSON() ([]byte, error) { a.mu.RLock() defer a.mu.RUnlock() return json.Marshal(a.array) diff --git a/container/garray/garray_normal_int.go b/container/garray/garray_normal_int.go index 30ab8077c..94176aba4 100644 --- a/container/garray/garray_normal_int.go +++ b/container/garray/garray_normal_int.go @@ -264,7 +264,7 @@ func (a *IntArray) PopRight() (value int, found bool) { a.mu.Lock() defer a.mu.Unlock() index := len(a.array) - 1 - if index <= 0 { + if index < 0 { return 0, false } value = a.array[index] @@ -717,7 +717,8 @@ func (a *IntArray) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (a *IntArray) MarshalJSON() ([]byte, error) { +// Note that do not use pointer as its receiver here. +func (a IntArray) MarshalJSON() ([]byte, error) { a.mu.RLock() defer a.mu.RUnlock() return json.Marshal(a.array) diff --git a/container/garray/garray_normal_str.go b/container/garray/garray_normal_str.go index ba8ca1bfc..3f53358a3 100644 --- a/container/garray/garray_normal_str.go +++ b/container/garray/garray_normal_str.go @@ -252,7 +252,7 @@ func (a *StrArray) PopRight() (value string, found bool) { a.mu.Lock() defer a.mu.Unlock() index := len(a.array) - 1 - if index <= 0 { + if index < 0 { return "", false } value = a.array[index] @@ -732,7 +732,8 @@ func (a *StrArray) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (a *StrArray) MarshalJSON() ([]byte, error) { +// Note that do not use pointer as its receiver here. +func (a StrArray) MarshalJSON() ([]byte, error) { a.mu.RLock() defer a.mu.RUnlock() return json.Marshal(a.array) diff --git a/container/garray/garray_sorted_any.go b/container/garray/garray_sorted_any.go index 9a476f00e..ac7f6a6a2 100644 --- a/container/garray/garray_sorted_any.go +++ b/container/garray/garray_sorted_any.go @@ -224,7 +224,7 @@ func (a *SortedArray) PopRight() (value interface{}, found bool) { a.mu.Lock() defer a.mu.Unlock() index := len(a.array) - 1 - if index <= 0 { + if index < 0 { return nil, false } value = a.array[index] @@ -666,7 +666,8 @@ func (a *SortedArray) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (a *SortedArray) MarshalJSON() ([]byte, error) { +// Note that do not use pointer as its receiver here. +func (a SortedArray) MarshalJSON() ([]byte, error) { a.mu.RLock() defer a.mu.RUnlock() return json.Marshal(a.array) diff --git a/container/garray/garray_sorted_int.go b/container/garray/garray_sorted_int.go index ad8fab14c..45a09ca6f 100644 --- a/container/garray/garray_sorted_int.go +++ b/container/garray/garray_sorted_int.go @@ -209,7 +209,7 @@ func (a *SortedIntArray) PopRight() (value int, found bool) { a.mu.Lock() defer a.mu.Unlock() index := len(a.array) - 1 - if index <= 0 { + if index < 0 { return 0, false } value = a.array[index] @@ -640,7 +640,8 @@ func (a *SortedIntArray) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (a *SortedIntArray) MarshalJSON() ([]byte, error) { +// Note that do not use pointer as its receiver here. +func (a SortedIntArray) MarshalJSON() ([]byte, error) { a.mu.RLock() defer a.mu.RUnlock() return json.Marshal(a.array) diff --git a/container/garray/garray_sorted_str.go b/container/garray/garray_sorted_str.go index 5bc9b21d3..65d880ec3 100644 --- a/container/garray/garray_sorted_str.go +++ b/container/garray/garray_sorted_str.go @@ -195,7 +195,7 @@ func (a *SortedStrArray) PopRight() (value string, found bool) { a.mu.Lock() defer a.mu.Unlock() index := len(a.array) - 1 - if index <= 0 { + if index < 0 { return "", false } value = a.array[index] @@ -653,7 +653,8 @@ func (a *SortedStrArray) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (a *SortedStrArray) MarshalJSON() ([]byte, error) { +// Note that do not use pointer as its receiver here. +func (a SortedStrArray) MarshalJSON() ([]byte, error) { a.mu.RLock() defer a.mu.RUnlock() return json.Marshal(a.array) diff --git a/container/garray/garray_z_unit_normal_any_array_test.go b/container/garray/garray_z_unit_normal_any_array_test.go index 3cd7df5ff..d59584b53 100644 --- a/container/garray/garray_z_unit_normal_any_array_test.go +++ b/container/garray/garray_z_unit_normal_any_array_test.go @@ -137,6 +137,65 @@ func TestArray_PopRands(t *testing.T) { }) } +func TestArray_PopLeft(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + array := garray.NewFrom(g.Slice{1, 2, 3}) + v, ok := array.PopLeft() + t.Assert(v, 1) + t.Assert(ok, true) + t.Assert(array.Len(), 2) + v, ok = array.PopLeft() + t.Assert(v, 2) + t.Assert(ok, true) + t.Assert(array.Len(), 1) + v, ok = array.PopLeft() + t.Assert(v, 3) + t.Assert(ok, true) + t.Assert(array.Len(), 0) + }) +} + +func TestArray_PopRight(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + array := garray.NewFrom(g.Slice{1, 2, 3}) + + v, ok := array.PopRight() + t.Assert(v, 3) + t.Assert(ok, true) + t.Assert(array.Len(), 2) + + v, ok = array.PopRight() + t.Assert(v, 2) + t.Assert(ok, true) + t.Assert(array.Len(), 1) + + v, ok = array.PopRight() + t.Assert(v, 1) + t.Assert(ok, true) + t.Assert(array.Len(), 0) + }) +} + +func TestArray_PopLefts(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + array := garray.NewFrom(g.Slice{1, 2, 3}) + t.Assert(array.PopLefts(2), g.Slice{1, 2}) + t.Assert(array.Len(), 1) + t.Assert(array.PopLefts(2), g.Slice{3}) + t.Assert(array.Len(), 0) + }) +} + +func TestArray_PopRights(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + array := garray.NewFrom(g.Slice{1, 2, 3}) + t.Assert(array.PopRights(2), g.Slice{2, 3}) + t.Assert(array.Len(), 1) + t.Assert(array.PopLefts(2), g.Slice{1}) + t.Assert(array.Len(), 0) + }) +} + func TestArray_PopLeftsAndPopRights(t *testing.T) { gtest.C(t, func(t *gtest.T) { array := garray.New() @@ -501,6 +560,7 @@ func TestArray_RLockFunc(t *testing.T) { } func TestArray_Json(t *testing.T) { + // pointer gtest.C(t, func(t *gtest.T) { s1 := []interface{}{"a", "b", "d", "c"} a1 := garray.NewArrayFrom(s1) @@ -519,7 +579,26 @@ func TestArray_Json(t *testing.T) { t.Assert(err, nil) t.Assert(a3.Slice(), s1) }) + // value. + gtest.C(t, func(t *gtest.T) { + s1 := []interface{}{"a", "b", "d", "c"} + a1 := *garray.NewArrayFrom(s1) + b1, err1 := json.Marshal(a1) + b2, err2 := json.Marshal(s1) + t.Assert(b1, b2) + t.Assert(err1, err2) + a2 := garray.New() + err2 = json.Unmarshal(b2, &a2) + t.Assert(err2, nil) + t.Assert(a2.Slice(), s1) + + var a3 garray.Array + err := json.Unmarshal(b2, &a3) + t.Assert(err, nil) + t.Assert(a3.Slice(), s1) + }) + // pointer gtest.C(t, func(t *gtest.T) { type User struct { Name string @@ -532,6 +611,25 @@ func TestArray_Json(t *testing.T) { b, err := json.Marshal(data) t.Assert(err, nil) + user := new(User) + err = json.Unmarshal(b, user) + t.Assert(err, nil) + t.Assert(user.Name, data["Name"]) + t.Assert(user.Scores, data["Scores"]) + }) + // value + gtest.C(t, func(t *gtest.T) { + type User struct { + Name string + Scores garray.Array + } + data := g.Map{ + "Name": "john", + "Scores": []int{99, 100, 98}, + } + b, err := json.Marshal(data) + t.Assert(err, nil) + user := new(User) err = json.Unmarshal(b, user) t.Assert(err, nil) diff --git a/container/garray/garray_z_unit_normal_int_array_test.go b/container/garray/garray_z_unit_normal_int_array_test.go index 9ee47c380..66f1f2357 100644 --- a/container/garray/garray_z_unit_normal_int_array_test.go +++ b/container/garray/garray_z_unit_normal_int_array_test.go @@ -230,6 +230,65 @@ func TestIntArray_Fill(t *testing.T) { }) } +func TestIntArray_PopLeft(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + array := garray.NewIntArrayFrom(g.SliceInt{1, 2, 3}) + v, ok := array.PopLeft() + t.Assert(v, 1) + t.Assert(ok, true) + t.Assert(array.Len(), 2) + v, ok = array.PopLeft() + t.Assert(v, 2) + t.Assert(ok, true) + t.Assert(array.Len(), 1) + v, ok = array.PopLeft() + t.Assert(v, 3) + t.Assert(ok, true) + t.Assert(array.Len(), 0) + }) +} + +func TestIntArray_PopRight(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + array := garray.NewIntArrayFrom(g.SliceInt{1, 2, 3}) + + v, ok := array.PopRight() + t.Assert(v, 3) + t.Assert(ok, true) + t.Assert(array.Len(), 2) + + v, ok = array.PopRight() + t.Assert(v, 2) + t.Assert(ok, true) + t.Assert(array.Len(), 1) + + v, ok = array.PopRight() + t.Assert(v, 1) + t.Assert(ok, true) + t.Assert(array.Len(), 0) + }) +} + +func TestIntArray_PopLefts(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + array := garray.NewIntArrayFrom(g.SliceInt{1, 2, 3}) + t.Assert(array.PopLefts(2), g.Slice{1, 2}) + t.Assert(array.Len(), 1) + t.Assert(array.PopLefts(2), g.Slice{3}) + t.Assert(array.Len(), 0) + }) +} + +func TestIntArray_PopRights(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + array := garray.NewIntArrayFrom(g.SliceInt{1, 2, 3}) + t.Assert(array.PopRights(2), g.Slice{2, 3}) + t.Assert(array.Len(), 1) + t.Assert(array.PopLefts(2), g.Slice{1}) + t.Assert(array.Len(), 0) + }) +} + func TestIntArray_Chunk(t *testing.T) { gtest.C(t, func(t *gtest.T) { a1 := []int{1, 2, 3, 4, 5} @@ -546,6 +605,7 @@ func TestIntArray_RLockFunc(t *testing.T) { } func TestIntArray_Json(t *testing.T) { + // array pointer gtest.C(t, func(t *gtest.T) { s1 := []int{1, 4, 3, 2} a1 := garray.NewIntArrayFrom(s1) @@ -563,7 +623,25 @@ func TestIntArray_Json(t *testing.T) { t.Assert(err, nil) t.Assert(a3.Slice(), s1) }) + // array value + gtest.C(t, func(t *gtest.T) { + s1 := []int{1, 4, 3, 2} + a1 := *garray.NewIntArrayFrom(s1) + b1, err1 := json.Marshal(a1) + b2, err2 := json.Marshal(s1) + t.Assert(b1, b2) + t.Assert(err1, err2) + a2 := garray.NewIntArray() + err1 = json.Unmarshal(b2, &a2) + t.Assert(a2.Slice(), s1) + + var a3 garray.IntArray + err := json.Unmarshal(b2, &a3) + t.Assert(err, nil) + t.Assert(a3.Slice(), s1) + }) + // array pointer gtest.C(t, func(t *gtest.T) { type User struct { Name string @@ -576,6 +654,25 @@ func TestIntArray_Json(t *testing.T) { b, err := json.Marshal(data) t.Assert(err, nil) + user := new(User) + err = json.Unmarshal(b, user) + t.Assert(err, nil) + t.Assert(user.Name, data["Name"]) + t.Assert(user.Scores, data["Scores"]) + }) + // array value + gtest.C(t, func(t *gtest.T) { + type User struct { + Name string + Scores garray.IntArray + } + data := g.Map{ + "Name": "john", + "Scores": []int{99, 100, 98}, + } + b, err := json.Marshal(data) + t.Assert(err, nil) + user := new(User) err = json.Unmarshal(b, user) t.Assert(err, nil) diff --git a/container/garray/garray_z_unit_normal_str_array_test.go b/container/garray/garray_z_unit_normal_str_array_test.go index ac1101446..10866fbd0 100644 --- a/container/garray/garray_z_unit_normal_str_array_test.go +++ b/container/garray/garray_z_unit_normal_str_array_test.go @@ -129,6 +129,65 @@ func TestStrArray_PushAndPop(t *testing.T) { }) } +func TestStrArray_PopLeft(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + array := garray.NewStrArrayFrom(g.SliceStr{"1", "2", "3"}) + v, ok := array.PopLeft() + t.Assert(v, 1) + t.Assert(ok, true) + t.Assert(array.Len(), 2) + v, ok = array.PopLeft() + t.Assert(v, 2) + t.Assert(ok, true) + t.Assert(array.Len(), 1) + v, ok = array.PopLeft() + t.Assert(v, 3) + t.Assert(ok, true) + t.Assert(array.Len(), 0) + }) +} + +func TestStrArray_PopRight(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + array := garray.NewStrArrayFrom(g.SliceStr{"1", "2", "3"}) + + v, ok := array.PopRight() + t.Assert(v, 3) + t.Assert(ok, true) + t.Assert(array.Len(), 2) + + v, ok = array.PopRight() + t.Assert(v, 2) + t.Assert(ok, true) + t.Assert(array.Len(), 1) + + v, ok = array.PopRight() + t.Assert(v, 1) + t.Assert(ok, true) + t.Assert(array.Len(), 0) + }) +} + +func TestStrArray_PopLefts(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + array := garray.NewStrArrayFrom(g.SliceStr{"1", "2", "3"}) + t.Assert(array.PopLefts(2), g.Slice{"1", "2"}) + t.Assert(array.Len(), 1) + t.Assert(array.PopLefts(2), g.Slice{"3"}) + t.Assert(array.Len(), 0) + }) +} + +func TestStrArray_PopRights(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + array := garray.NewStrArrayFrom(g.SliceStr{"1", "2", "3"}) + t.Assert(array.PopRights(2), g.Slice{"2", "3"}) + t.Assert(array.Len(), 1) + t.Assert(array.PopLefts(2), g.Slice{"1"}) + t.Assert(array.Len(), 0) + }) +} + func TestStrArray_PopLeftsAndPopRights(t *testing.T) { gtest.C(t, func(t *gtest.T) { array := garray.NewStrArray() @@ -545,6 +604,7 @@ func TestStrArray_LockFunc(t *testing.T) { } func TestStrArray_Json(t *testing.T) { + // array pointer gtest.C(t, func(t *gtest.T) { s1 := []string{"a", "b", "d", "c"} a1 := garray.NewStrArrayFrom(s1) @@ -562,7 +622,25 @@ func TestStrArray_Json(t *testing.T) { t.Assert(err, nil) t.Assert(a3.Slice(), s1) }) + // array value + gtest.C(t, func(t *gtest.T) { + s1 := []string{"a", "b", "d", "c"} + a1 := *garray.NewStrArrayFrom(s1) + b1, err1 := json.Marshal(a1) + b2, err2 := json.Marshal(s1) + t.Assert(b1, b2) + t.Assert(err1, err2) + a2 := garray.NewStrArray() + err1 = json.Unmarshal(b2, &a2) + t.Assert(a2.Slice(), s1) + + var a3 garray.StrArray + err := json.Unmarshal(b2, &a3) + t.Assert(err, nil) + t.Assert(a3.Slice(), s1) + }) + // array pointer gtest.C(t, func(t *gtest.T) { type User struct { Name string @@ -575,6 +653,25 @@ func TestStrArray_Json(t *testing.T) { b, err := json.Marshal(data) t.Assert(err, nil) + user := new(User) + err = json.Unmarshal(b, user) + t.Assert(err, nil) + t.Assert(user.Name, data["Name"]) + t.Assert(user.Scores, data["Scores"]) + }) + // array value + gtest.C(t, func(t *gtest.T) { + type User struct { + Name string + Scores garray.StrArray + } + data := g.Map{ + "Name": "john", + "Scores": []string{"A+", "A", "A"}, + } + b, err := json.Marshal(data) + t.Assert(err, nil) + user := new(User) err = json.Unmarshal(b, user) t.Assert(err, nil) diff --git a/container/garray/garray_z_unit_sorted_any_array_test.go b/container/garray/garray_z_unit_sorted_any_array_test.go index 7003dd458..02f9562bd 100644 --- a/container/garray/garray_z_unit_sorted_any_array_test.go +++ b/container/garray/garray_z_unit_sorted_any_array_test.go @@ -148,34 +148,62 @@ func TestSortedArray_Remove(t *testing.T) { func TestSortedArray_PopLeft(t *testing.T) { gtest.C(t, func(t *gtest.T) { - 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) + array1 := garray.NewSortedArrayFrom( + []interface{}{"a", "d", "c", "b"}, + gutil.ComparatorString, + ) i1, ok := array1.PopLeft() t.Assert(ok, true) t.Assert(gconv.String(i1), "a") t.Assert(array1.Len(), 3) t.Assert(array1, []interface{}{"b", "c", "d"}) }) - + gtest.C(t, func(t *gtest.T) { + array := garray.NewSortedArrayFrom(g.Slice{1, 2, 3}, gutil.ComparatorInt) + v, ok := array.PopLeft() + t.Assert(v, 1) + t.Assert(ok, true) + t.Assert(array.Len(), 2) + v, ok = array.PopLeft() + t.Assert(v, 2) + t.Assert(ok, true) + t.Assert(array.Len(), 1) + v, ok = array.PopLeft() + t.Assert(v, 3) + t.Assert(ok, true) + t.Assert(array.Len(), 0) + }) } func TestSortedArray_PopRight(t *testing.T) { gtest.C(t, func(t *gtest.T) { - 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) + array1 := garray.NewSortedArrayFrom( + []interface{}{"a", "d", "c", "b"}, + gutil.ComparatorString, + ) i1, ok := array1.PopRight() t.Assert(ok, true) t.Assert(gconv.String(i1), "d") t.Assert(array1.Len(), 3) t.Assert(array1, []interface{}{"a", "b", "c"}) }) + gtest.C(t, func(t *gtest.T) { + array := garray.NewSortedArrayFrom(g.Slice{1, 2, 3}, gutil.ComparatorInt) + v, ok := array.PopRight() + t.Assert(v, 3) + t.Assert(ok, true) + t.Assert(array.Len(), 2) + v, ok = array.PopRight() + t.Assert(v, 2) + t.Assert(ok, true) + t.Assert(array.Len(), 1) + + v, ok = array.PopRight() + t.Assert(v, 1) + t.Assert(ok, true) + t.Assert(array.Len(), 0) + }) } func TestSortedArray_PopRand(t *testing.T) { @@ -249,7 +277,6 @@ func TestSortedArray_PopLefts(t *testing.T) { t.Assert(len(i2), 4) t.AssertIN(i1, []interface{}{"a", "d", "c", "b", "e", "f"}) t.Assert(array1.Len(), 0) - }) } @@ -267,7 +294,7 @@ func TestSortedArray_PopRights(t *testing.T) { i2 := array1.PopRights(10) t.Assert(len(i2), 4) - + t.Assert(array1.Len(), 0) }) } @@ -612,6 +639,7 @@ func TestSortedArray_Merge(t *testing.T) { } func TestSortedArray_Json(t *testing.T) { + // array pointer gtest.C(t, func(t *gtest.T) { s1 := []interface{}{"a", "b", "d", "c"} s2 := []interface{}{"a", "b", "c", "d"} @@ -631,7 +659,27 @@ func TestSortedArray_Json(t *testing.T) { t.Assert(a3.Slice(), s1) t.Assert(a3.Interfaces(), s1) }) + // array value + gtest.C(t, func(t *gtest.T) { + s1 := []interface{}{"a", "b", "d", "c"} + s2 := []interface{}{"a", "b", "c", "d"} + a1 := *garray.NewSortedArrayFrom(s1, gutil.ComparatorString) + b1, err1 := json.Marshal(a1) + b2, err2 := json.Marshal(s1) + t.Assert(b1, b2) + t.Assert(err1, err2) + a2 := garray.NewSortedArray(gutil.ComparatorString) + err1 = json.Unmarshal(b2, &a2) + t.Assert(a2.Slice(), s2) + + var a3 garray.SortedArray + err := json.Unmarshal(b2, &a3) + t.Assert(err, nil) + t.Assert(a3.Slice(), s1) + t.Assert(a3.Interfaces(), s1) + }) + // array pointer gtest.C(t, func(t *gtest.T) { type User struct { Name string @@ -663,6 +711,42 @@ func TestSortedArray_Json(t *testing.T) { t.AssertIN(v, data["Scores"]) t.Assert(ok, true) + v, ok = user.Scores.PopLeft() + t.Assert(v, nil) + t.Assert(ok, false) + }) + // array value + gtest.C(t, func(t *gtest.T) { + type User struct { + Name string + Scores garray.SortedArray + } + data := g.Map{ + "Name": "john", + "Scores": []int{99, 100, 98}, + } + b, err := json.Marshal(data) + t.Assert(err, nil) + + user := new(User) + err = json.Unmarshal(b, user) + t.Assert(err, nil) + t.Assert(user.Name, data["Name"]) + t.AssertNE(user.Scores, nil) + t.Assert(user.Scores.Len(), 3) + + v, ok := user.Scores.PopLeft() + t.AssertIN(v, data["Scores"]) + t.Assert(ok, true) + + v, ok = user.Scores.PopLeft() + t.AssertIN(v, data["Scores"]) + t.Assert(ok, true) + + v, ok = user.Scores.PopLeft() + t.AssertIN(v, data["Scores"]) + t.Assert(ok, true) + v, ok = user.Scores.PopLeft() t.Assert(v, nil) t.Assert(ok, false) diff --git a/container/garray/garray_z_unit_sorted_int_array_test.go b/container/garray/garray_z_unit_sorted_int_array_test.go index f195fcd63..3a44ea38b 100644 --- a/container/garray/garray_z_unit_sorted_int_array_test.go +++ b/container/garray/garray_z_unit_sorted_int_array_test.go @@ -133,6 +133,21 @@ func TestSortedIntArray_PopLeft(t *testing.T) { t.Assert(array1.Len(), 3) t.Assert(array1.Search(1), -1) }) + gtest.C(t, func(t *gtest.T) { + array := garray.NewSortedIntArrayFrom(g.SliceInt{1, 2, 3}) + v, ok := array.PopLeft() + t.Assert(v, 1) + t.Assert(ok, true) + t.Assert(array.Len(), 2) + v, ok = array.PopLeft() + t.Assert(v, 2) + t.Assert(ok, true) + t.Assert(array.Len(), 1) + v, ok = array.PopLeft() + t.Assert(v, 3) + t.Assert(ok, true) + t.Assert(array.Len(), 0) + }) } func TestSortedIntArray_PopRight(t *testing.T) { @@ -145,6 +160,23 @@ func TestSortedIntArray_PopRight(t *testing.T) { t.Assert(array1.Len(), 3) t.Assert(array1.Search(5), -1) }) + gtest.C(t, func(t *gtest.T) { + array := garray.NewSortedIntArrayFrom(g.SliceInt{1, 2, 3}) + v, ok := array.PopRight() + t.Assert(v, 3) + t.Assert(ok, true) + t.Assert(array.Len(), 2) + + v, ok = array.PopRight() + t.Assert(v, 2) + t.Assert(ok, true) + t.Assert(array.Len(), 1) + + v, ok = array.PopRight() + t.Assert(v, 1) + t.Assert(ok, true) + t.Assert(array.Len(), 0) + }) } func TestSortedIntArray_PopRand(t *testing.T) { @@ -504,6 +536,7 @@ func TestSortedIntArray_Merge(t *testing.T) { } func TestSortedIntArray_Json(t *testing.T) { + // array pointer gtest.C(t, func(t *gtest.T) { s1 := []int{1, 4, 3, 2} s2 := []int{1, 2, 3, 4} @@ -522,7 +555,26 @@ func TestSortedIntArray_Json(t *testing.T) { t.Assert(err, nil) t.Assert(a3.Slice(), s1) }) + // array value + gtest.C(t, func(t *gtest.T) { + s1 := []int{1, 4, 3, 2} + s2 := []int{1, 2, 3, 4} + a1 := *garray.NewSortedIntArrayFrom(s1) + b1, err1 := json.Marshal(a1) + b2, err2 := json.Marshal(s1) + t.Assert(b1, b2) + t.Assert(err1, err2) + a2 := garray.NewSortedIntArray() + err1 = json.Unmarshal(b2, &a2) + t.Assert(a2.Slice(), s2) + + var a3 garray.SortedIntArray + err := json.Unmarshal(b2, &a3) + t.Assert(err, nil) + t.Assert(a3.Slice(), s1) + }) + // array pointer gtest.C(t, func(t *gtest.T) { type User struct { Name string @@ -535,6 +587,25 @@ func TestSortedIntArray_Json(t *testing.T) { b, err := json.Marshal(data) t.Assert(err, nil) + user := new(User) + err = json.Unmarshal(b, user) + t.Assert(err, nil) + t.Assert(user.Name, data["Name"]) + t.Assert(user.Scores, []int{98, 99, 100}) + }) + // array value + gtest.C(t, func(t *gtest.T) { + type User struct { + Name string + Scores garray.SortedIntArray + } + data := g.Map{ + "Name": "john", + "Scores": []int{99, 100, 98}, + } + b, err := json.Marshal(data) + t.Assert(err, nil) + user := new(User) err = json.Unmarshal(b, user) t.Assert(err, nil) diff --git a/container/garray/garray_z_unit_sorted_str_array_test.go b/container/garray/garray_z_unit_sorted_str_array_test.go index 5b0e02bb2..9b60b1e3a 100644 --- a/container/garray/garray_z_unit_sorted_str_array_test.go +++ b/container/garray/garray_z_unit_sorted_str_array_test.go @@ -137,6 +137,21 @@ func TestSortedStrArray_PopLeft(t *testing.T) { t.Assert(array1.Len(), 4) t.Assert(array1.Contains("a"), false) }) + gtest.C(t, func(t *gtest.T) { + array := garray.NewSortedStrArrayFrom(g.SliceStr{"1", "2", "3"}) + v, ok := array.PopLeft() + t.Assert(v, 1) + t.Assert(ok, true) + t.Assert(array.Len(), 2) + v, ok = array.PopLeft() + t.Assert(v, 2) + t.Assert(ok, true) + t.Assert(array.Len(), 1) + v, ok = array.PopLeft() + t.Assert(v, 3) + t.Assert(ok, true) + t.Assert(array.Len(), 0) + }) } func TestSortedStrArray_PopRight(t *testing.T) { @@ -149,6 +164,23 @@ func TestSortedStrArray_PopRight(t *testing.T) { t.Assert(array1.Len(), 4) t.Assert(array1.Contains("e"), false) }) + gtest.C(t, func(t *gtest.T) { + array := garray.NewSortedStrArrayFrom(g.SliceStr{"1", "2", "3"}) + v, ok := array.PopRight() + t.Assert(v, 3) + t.Assert(ok, true) + t.Assert(array.Len(), 2) + + v, ok = array.PopRight() + t.Assert(v, 2) + t.Assert(ok, true) + t.Assert(array.Len(), 1) + + v, ok = array.PopRight() + t.Assert(v, 1) + t.Assert(ok, true) + t.Assert(array.Len(), 0) + }) } func TestSortedStrArray_PopRand(t *testing.T) { @@ -210,6 +242,7 @@ func TestSortedStrArray_PopLefts(t *testing.T) { s1 = array1.PopLefts(4) t.Assert(len(s1), 3) t.Assert(s1, []string{"c", "d", "e"}) + t.Assert(array1.Len(), 0) }) } @@ -523,6 +556,7 @@ func TestSortedStrArray_Merge(t *testing.T) { } func TestSortedStrArray_Json(t *testing.T) { + // array pointer gtest.C(t, func(t *gtest.T) { s1 := []string{"a", "b", "d", "c"} s2 := []string{"a", "b", "c", "d"} @@ -543,7 +577,28 @@ func TestSortedStrArray_Json(t *testing.T) { t.Assert(a3.Slice(), s1) t.Assert(a3.Interfaces(), s1) }) + // array value + gtest.C(t, func(t *gtest.T) { + s1 := []string{"a", "b", "d", "c"} + s2 := []string{"a", "b", "c", "d"} + a1 := *garray.NewSortedStrArrayFrom(s1) + b1, err1 := json.Marshal(a1) + b2, err2 := json.Marshal(s1) + t.Assert(b1, b2) + t.Assert(err1, err2) + a2 := garray.NewSortedStrArray() + err1 = json.Unmarshal(b2, &a2) + t.Assert(a2.Slice(), s2) + t.Assert(a2.Interfaces(), s2) + + var a3 garray.SortedStrArray + err := json.Unmarshal(b2, &a3) + t.Assert(err, nil) + t.Assert(a3.Slice(), s1) + t.Assert(a3.Interfaces(), s1) + }) + // array pointer gtest.C(t, func(t *gtest.T) { type User struct { Name string @@ -556,6 +611,25 @@ func TestSortedStrArray_Json(t *testing.T) { b, err := json.Marshal(data) t.Assert(err, nil) + user := new(User) + err = json.Unmarshal(b, user) + t.Assert(err, nil) + t.Assert(user.Name, data["Name"]) + t.Assert(user.Scores, []string{"A", "A", "A+"}) + }) + // array value + gtest.C(t, func(t *gtest.T) { + type User struct { + Name string + Scores garray.SortedStrArray + } + data := g.Map{ + "Name": "john", + "Scores": []string{"A+", "A", "A"}, + } + b, err := json.Marshal(data) + t.Assert(err, nil) + user := new(User) err = json.Unmarshal(b, user) t.Assert(err, nil)