github.com/gogf/gf/v2@v2.7.4/container/garray/garray_z_unit_all_basic_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // go test *.go 8 9 package garray_test 10 11 import ( 12 "strings" 13 "testing" 14 15 "github.com/gogf/gf/v2/container/garray" 16 "github.com/gogf/gf/v2/test/gtest" 17 "github.com/gogf/gf/v2/util/gconv" 18 "github.com/gogf/gf/v2/util/gutil" 19 ) 20 21 func Test_Array_Var(t *testing.T) { 22 gtest.C(t, func(t *gtest.T) { 23 var array garray.Array 24 expect := []int{2, 3, 1} 25 array.Append(2, 3, 1) 26 t.Assert(array.Slice(), expect) 27 }) 28 gtest.C(t, func(t *gtest.T) { 29 var array garray.IntArray 30 expect := []int{2, 3, 1} 31 array.Append(2, 3, 1) 32 t.Assert(array.Slice(), expect) 33 }) 34 gtest.C(t, func(t *gtest.T) { 35 var array garray.StrArray 36 expect := []string{"b", "a"} 37 array.Append("b", "a") 38 t.Assert(array.Slice(), expect) 39 }) 40 gtest.C(t, func(t *gtest.T) { 41 var array garray.SortedArray 42 array.SetComparator(gutil.ComparatorInt) 43 expect := []int{1, 2, 3} 44 array.Add(2, 3, 1) 45 t.Assert(array.Slice(), expect) 46 }) 47 gtest.C(t, func(t *gtest.T) { 48 var array garray.SortedIntArray 49 expect := []int{1, 2, 3} 50 array.Add(2, 3, 1) 51 t.Assert(array.Slice(), expect) 52 }) 53 gtest.C(t, func(t *gtest.T) { 54 var array garray.SortedStrArray 55 expect := []string{"a", "b", "c"} 56 array.Add("c", "a", "b") 57 t.Assert(array.Slice(), expect) 58 }) 59 } 60 61 func Test_SortedIntArray_Var(t *testing.T) { 62 gtest.C(t, func(t *gtest.T) { 63 var array garray.SortedIntArray 64 expect := []int{1, 2, 3} 65 array.Add(2, 3, 1) 66 t.Assert(array.Slice(), expect) 67 }) 68 } 69 70 func Test_IntArray_Unique(t *testing.T) { 71 gtest.C(t, func(t *gtest.T) { 72 expect := []int{1, 2, 3, 4, 5, 6} 73 array := garray.NewIntArray() 74 array.Append(1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6) 75 array.Unique() 76 t.Assert(array.Slice(), expect) 77 }) 78 } 79 80 func Test_SortedIntArray1(t *testing.T) { 81 gtest.C(t, func(t *gtest.T) { 82 expect := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 83 array := garray.NewSortedIntArray() 84 for i := 10; i > -1; i-- { 85 array.Add(i) 86 } 87 t.Assert(array.Slice(), expect) 88 t.Assert(array.Add().Slice(), expect) 89 }) 90 } 91 92 func Test_SortedIntArray2(t *testing.T) { 93 gtest.C(t, func(t *gtest.T) { 94 expect := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 95 array := garray.NewSortedIntArray() 96 for i := 0; i <= 10; i++ { 97 array.Add(i) 98 } 99 t.Assert(array.Slice(), expect) 100 }) 101 } 102 103 func Test_SortedStrArray1(t *testing.T) { 104 gtest.C(t, func(t *gtest.T) { 105 expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"} 106 array1 := garray.NewSortedStrArray() 107 array2 := garray.NewSortedStrArray(true) 108 for i := 10; i > -1; i-- { 109 array1.Add(gconv.String(i)) 110 array2.Add(gconv.String(i)) 111 } 112 t.Assert(array1.Slice(), expect) 113 t.Assert(array2.Slice(), expect) 114 }) 115 116 } 117 118 func Test_SortedStrArray2(t *testing.T) { 119 gtest.C(t, func(t *gtest.T) { 120 expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"} 121 array := garray.NewSortedStrArray() 122 for i := 0; i <= 10; i++ { 123 array.Add(gconv.String(i)) 124 } 125 t.Assert(array.Slice(), expect) 126 array.Add() 127 t.Assert(array.Slice(), expect) 128 }) 129 } 130 131 func Test_SortedArray1(t *testing.T) { 132 gtest.C(t, func(t *gtest.T) { 133 expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"} 134 array := garray.NewSortedArray(func(v1, v2 interface{}) int { 135 return strings.Compare(gconv.String(v1), gconv.String(v2)) 136 }) 137 for i := 10; i > -1; i-- { 138 array.Add(gconv.String(i)) 139 } 140 t.Assert(array.Slice(), expect) 141 }) 142 } 143 144 func Test_SortedArray2(t *testing.T) { 145 gtest.C(t, func(t *gtest.T) { 146 expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"} 147 func1 := func(v1, v2 interface{}) int { 148 return strings.Compare(gconv.String(v1), gconv.String(v2)) 149 } 150 array := garray.NewSortedArray(func1) 151 array2 := garray.NewSortedArray(func1, true) 152 for i := 0; i <= 10; i++ { 153 array.Add(gconv.String(i)) 154 array2.Add(gconv.String(i)) 155 } 156 t.Assert(array.Slice(), expect) 157 t.Assert(array.Add().Slice(), expect) 158 t.Assert(array2.Slice(), expect) 159 }) 160 } 161 162 func TestNewFromCopy(t *testing.T) { 163 gtest.C(t, func(t *gtest.T) { 164 a1 := []interface{}{"100", "200", "300", "400", "500", "600"} 165 array1 := garray.NewFromCopy(a1) 166 t.AssertIN(array1.PopRands(2), a1) 167 t.Assert(len(array1.PopRands(1)), 1) 168 t.Assert(len(array1.PopRands(9)), 3) 169 }) 170 }