github.com/wangyougui/gf/v2@v2.6.5/container/gring/gring_z_unit_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/wangyougui/gf. 6 7 package gring_test 8 9 import ( 10 "container/ring" 11 "testing" 12 13 "github.com/wangyougui/gf/v2/container/gring" 14 "github.com/wangyougui/gf/v2/frame/g" 15 "github.com/wangyougui/gf/v2/test/gtest" 16 ) 17 18 type Student struct { 19 position int 20 name string 21 upgrade bool 22 } 23 24 func TestRing_Val(t *testing.T) { 25 gtest.C(t, func(t *gtest.T) { 26 //定义cap 为3的ring类型数据 27 r := gring.New(3, true) 28 //分别给3个元素初始化赋值 29 r.Put(&Student{1, "jimmy", true}) 30 r.Put(&Student{2, "tom", true}) 31 r.Put(&Student{3, "alon", false}) 32 33 //元素取值并判断和预设值是否相等 34 t.Assert(r.Val().(*Student).name, "jimmy") 35 //从当前位置往后移两个元素 36 r.Move(2) 37 t.Assert(r.Val().(*Student).name, "alon") 38 //更新元素值 39 //测试 value == nil 40 r.Set(nil) 41 t.Assert(r.Val(), nil) 42 //测试value != nil 43 r.Set(&Student{3, "jack", true}) 44 }) 45 } 46 47 func TestRing_CapLen(t *testing.T) { 48 gtest.C(t, func(t *gtest.T) { 49 r := gring.New(10) 50 t.Assert(r.Cap(), 10) 51 t.Assert(r.Len(), 0) 52 }) 53 gtest.C(t, func(t *gtest.T) { 54 r := gring.New(10) 55 r.Put("goframe") 56 //cap长度 10 57 t.Assert(r.Cap(), 10) 58 //已有数据项 1 59 t.Assert(r.Len(), 1) 60 }) 61 } 62 63 func TestRing_Position(t *testing.T) { 64 gtest.C(t, func(t *gtest.T) { 65 r := gring.New(2) 66 r.Put(1) 67 r.Put(2) 68 //往后移动1个元素 69 r.Next() 70 t.Assert(r.Val(), 2) 71 //往前移动1个元素 72 r.Prev() 73 t.Assert(r.Val(), 1) 74 75 }) 76 } 77 78 func TestRing_Link(t *testing.T) { 79 gtest.C(t, func(t *gtest.T) { 80 r := gring.New(3) 81 r.Put(1) 82 r.Put(2) 83 r.Put(3) 84 s := gring.New(2) 85 s.Put("a") 86 s.Put("b") 87 88 rs := r.Link(s) 89 t.Assert(rs.Move(2).Val(), "b") 90 }) 91 } 92 93 func TestRing_Unlink(t *testing.T) { 94 gtest.C(t, func(t *gtest.T) { 95 r := gring.New(5) 96 for i := 1; i <= 5; i++ { 97 r.Put(i) 98 } 99 t.Assert(r.Val(), 1) 100 // 1 2 3 4 101 // 删除当前位置往后的2个数据,返回被删除的数据 102 // 重新计算s len 103 s := r.Unlink(2) // 2 3 104 t.Assert(s.Val(), 2) 105 t.Assert(s.Len(), 2) 106 }) 107 } 108 109 func TestRing_Slice(t *testing.T) { 110 gtest.C(t, func(t *gtest.T) { 111 ringLen := 5 112 r := gring.New(ringLen) 113 for i := 0; i < ringLen; i++ { 114 r.Put(i + 1) 115 } 116 r.Move(2) // 3 117 array := r.SliceNext() // [3 4 5 1 2] 118 t.Assert(array[0], 3) 119 t.Assert(len(array), 5) 120 121 //判断array是否等于[3 4 5 1 2] 122 ra := []int{3, 4, 5, 1, 2} 123 t.Assert(ra, array) 124 125 //第3个元素设为nil 126 r.Set(nil) 127 array2 := r.SliceNext() //[4 5 1 2] 128 //返回当前位置往后不为空的元素数组,长度为4 129 t.Assert(array2, g.Slice{nil, 4, 5, 1, 2}) 130 131 array3 := r.SlicePrev() //[2 1 5 4] 132 t.Assert(array3, g.Slice{nil, 2, 1, 5, 4}) 133 134 s := gring.New(ringLen) 135 for i := 0; i < ringLen; i++ { 136 s.Put(i + 1) 137 } 138 array4 := s.SlicePrev() // [] 139 t.Assert(array4, g.Slice{1, 5, 4, 3, 2}) 140 }) 141 } 142 143 // https://github.com/wangyougui/gf/issues/1394 144 func Test_Issue1394(t *testing.T) { 145 gtest.C(t, func(t *gtest.T) { 146 // gring. 147 gRing := gring.New(10) 148 for i := 0; i < 10; i++ { 149 gRing.Put(i) 150 } 151 t.Logf("the length:%d", gRing.Len()) 152 gRingResult := gRing.Unlink(6) 153 for i := 0; i < 10; i++ { 154 t.Log(gRing.Val()) 155 gRing = gRing.Next() 156 } 157 t.Logf("the ring length:%d", gRing.Len()) 158 t.Logf("the result length:%d", gRingResult.Len()) 159 160 // stdring 161 stdRing := ring.New(10) 162 for i := 0; i < 10; i++ { 163 stdRing.Value = i 164 stdRing = stdRing.Next() 165 } 166 t.Logf("the length:%d", stdRing.Len()) 167 stdRingResult := stdRing.Unlink(6) 168 for i := 0; i < 10; i++ { 169 t.Log(stdRing.Value) 170 stdRing = stdRing.Next() 171 } 172 t.Logf("the ring length:%d", stdRing.Len()) 173 t.Logf("the result length:%d", stdRingResult.Len()) 174 175 // Assertion. 176 t.Assert(gRing.Len(), stdRing.Len()) 177 t.Assert(gRingResult.Len(), stdRingResult.Len()) 178 179 for i := 0; i < 10; i++ { 180 t.Assert(stdRing.Value, gRing.Val()) 181 stdRing = stdRing.Next() 182 gRing = gRing.Next() 183 } 184 }) 185 186 } 187 188 func TestRing_RLockIteratorNext(t *testing.T) { 189 gtest.C(t, func(t *gtest.T) { 190 r := gring.New(10) 191 for i := 0; i < 10; i++ { 192 r.Set(i).Next() 193 } 194 195 iterVal := 0 196 r.RLockIteratorNext(func(value interface{}) bool { 197 if value.(int) == 0 { 198 iterVal = value.(int) 199 return false 200 } 201 return true 202 }) 203 204 t.Assert(iterVal, 0) 205 }) 206 } 207 208 func TestRing_RLockIteratorPrev(t *testing.T) { 209 gtest.C(t, func(t *gtest.T) { 210 r := gring.New(10) 211 for i := 0; i < 10; i++ { 212 r.Set(i).Next() 213 } 214 215 iterVal := 0 216 r.RLockIteratorPrev(func(value interface{}) bool { 217 if value.(int) == 0 { 218 iterVal = value.(int) 219 return false 220 } 221 return true 222 }) 223 224 t.Assert(iterVal, 0) 225 }) 226 }