2021-01-17 21:46:25 +08:00
|
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-04-07 20:58:58 +08:00
|
|
|
|
//
|
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
2019-04-09 17:27:58 +08:00
|
|
|
|
package gring_test
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2022-02-14 16:08:26 +08:00
|
|
|
|
"container/ring"
|
2019-04-09 17:27:58 +08:00
|
|
|
|
"testing"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
|
"github.com/gogf/gf/v2/container/gring"
|
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2019-04-09 17:27:58 +08:00
|
|
|
|
)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
|
2019-04-09 17:27:58 +08:00
|
|
|
|
type Student struct {
|
|
|
|
|
|
position int
|
2019-06-19 09:06:52 +08:00
|
|
|
|
name string
|
|
|
|
|
|
upgrade bool
|
2019-04-09 17:27:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestRing_Val(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-04-09 17:27:58 +08:00
|
|
|
|
//定义cap 为3的ring类型数据
|
|
|
|
|
|
r := gring.New(3, true)
|
|
|
|
|
|
//分别给3个元素初始化赋值
|
2019-06-19 09:06:52 +08:00
|
|
|
|
r.Put(&Student{1, "jimmy", true})
|
|
|
|
|
|
r.Put(&Student{2, "tom", true})
|
|
|
|
|
|
r.Put(&Student{3, "alon", false})
|
2019-04-09 17:27:58 +08:00
|
|
|
|
|
|
|
|
|
|
//元素取值并判断和预设值是否相等
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(r.Val().(*Student).name, "jimmy")
|
2019-04-09 17:27:58 +08:00
|
|
|
|
//从当前位置往后移两个元素
|
|
|
|
|
|
r.Move(2)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(r.Val().(*Student).name, "alon")
|
2019-04-10 00:22:10 +08:00
|
|
|
|
//更新元素值
|
|
|
|
|
|
//测试 value == nil
|
|
|
|
|
|
r.Set(nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(r.Val(), nil)
|
2019-04-10 00:22:10 +08:00
|
|
|
|
//测试value != nil
|
|
|
|
|
|
r.Set(&Student{3, "jack", true})
|
2019-04-09 17:27:58 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2022-11-01 20:12:21 +08:00
|
|
|
|
|
2019-04-09 17:27:58 +08:00
|
|
|
|
func TestRing_CapLen(t *testing.T) {
|
2021-09-14 20:15:21 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
r := gring.New(10)
|
|
|
|
|
|
t.Assert(r.Cap(), 10)
|
|
|
|
|
|
t.Assert(r.Len(), 0)
|
|
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-04-09 17:27:58 +08:00
|
|
|
|
r := gring.New(10)
|
|
|
|
|
|
r.Put("goframe")
|
|
|
|
|
|
//cap长度 10
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(r.Cap(), 10)
|
2019-04-09 17:27:58 +08:00
|
|
|
|
//已有数据项 1
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(r.Len(), 1)
|
2019-04-09 17:27:58 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestRing_Position(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-04-09 17:27:58 +08:00
|
|
|
|
r := gring.New(2)
|
|
|
|
|
|
r.Put(1)
|
|
|
|
|
|
r.Put(2)
|
|
|
|
|
|
//往后移动1个元素
|
|
|
|
|
|
r.Next()
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(r.Val(), 2)
|
2019-04-09 17:27:58 +08:00
|
|
|
|
//往前移动1个元素
|
|
|
|
|
|
r.Prev()
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(r.Val(), 1)
|
2019-04-09 17:27:58 +08:00
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestRing_Link(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-04-09 17:27:58 +08:00
|
|
|
|
r := gring.New(3)
|
|
|
|
|
|
r.Put(1)
|
|
|
|
|
|
r.Put(2)
|
|
|
|
|
|
r.Put(3)
|
|
|
|
|
|
s := gring.New(2)
|
|
|
|
|
|
s.Put("a")
|
|
|
|
|
|
s.Put("b")
|
|
|
|
|
|
|
|
|
|
|
|
rs := r.Link(s)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(rs.Move(2).Val(), "b")
|
2019-04-09 17:27:58 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestRing_Unlink(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-04-09 17:27:58 +08:00
|
|
|
|
r := gring.New(5)
|
2021-09-14 20:15:21 +08:00
|
|
|
|
for i := 1; i <= 5; i++ {
|
|
|
|
|
|
r.Put(i)
|
2019-04-09 17:27:58 +08:00
|
|
|
|
}
|
2021-09-14 20:15:21 +08:00
|
|
|
|
t.Assert(r.Val(), 1)
|
2019-04-10 10:44:40 +08:00
|
|
|
|
// 1 2 3 4
|
2019-04-09 17:27:58 +08:00
|
|
|
|
// 删除当前位置往后的2个数据,返回被删除的数据
|
2019-04-10 10:13:56 +08:00
|
|
|
|
// 重新计算s len
|
2019-06-19 09:06:52 +08:00
|
|
|
|
s := r.Unlink(2) // 2 3
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(s.Val(), 2)
|
2021-09-14 20:15:21 +08:00
|
|
|
|
t.Assert(s.Len(), 2)
|
2019-04-09 17:27:58 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestRing_Slice(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-04-09 17:27:58 +08:00
|
|
|
|
ringLen := 5
|
|
|
|
|
|
r := gring.New(ringLen)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
for i := 0; i < ringLen; i++ {
|
|
|
|
|
|
r.Put(i + 1)
|
2019-04-09 17:27:58 +08:00
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
|
r.Move(2) // 3
|
|
|
|
|
|
array := r.SliceNext() // [3 4 5 1 2]
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array[0], 3)
|
|
|
|
|
|
t.Assert(len(array), 5)
|
2019-04-09 17:27:58 +08:00
|
|
|
|
|
|
|
|
|
|
//判断array是否等于[3 4 5 1 2]
|
2019-06-19 09:06:52 +08:00
|
|
|
|
ra := []int{3, 4, 5, 1, 2}
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(ra, array)
|
2019-04-09 17:27:58 +08:00
|
|
|
|
|
|
|
|
|
|
//第3个元素设为nil
|
|
|
|
|
|
r.Set(nil)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
array2 := r.SliceNext() //[4 5 1 2]
|
2019-04-09 17:27:58 +08:00
|
|
|
|
//返回当前位置往后不为空的元素数组,长度为4
|
2021-09-14 20:15:21 +08:00
|
|
|
|
t.Assert(array2, g.Slice{nil, 4, 5, 1, 2})
|
2019-04-09 17:27:58 +08:00
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
|
array3 := r.SlicePrev() //[2 1 5 4]
|
2021-09-14 20:15:21 +08:00
|
|
|
|
t.Assert(array3, g.Slice{nil, 2, 1, 5, 4})
|
2019-04-10 00:22:10 +08:00
|
|
|
|
|
|
|
|
|
|
s := gring.New(ringLen)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
for i := 0; i < ringLen; i++ {
|
|
|
|
|
|
s.Put(i + 1)
|
2019-04-10 10:44:40 +08:00
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
|
array4 := s.SlicePrev() // []
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array4, g.Slice{1, 5, 4, 3, 2})
|
2019-04-09 17:27:58 +08:00
|
|
|
|
})
|
2019-06-19 09:06:52 +08:00
|
|
|
|
}
|
2022-02-14 16:08:26 +08:00
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/1394
|
|
|
|
|
|
func Test_Issue1394(t *testing.T) {
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
// gring.
|
|
|
|
|
|
gRing := gring.New(10)
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
|
gRing.Put(i)
|
|
|
|
|
|
}
|
|
|
|
|
|
t.Logf("the length:%d", gRing.Len())
|
|
|
|
|
|
gRingResult := gRing.Unlink(6)
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
|
t.Log(gRing.Val())
|
|
|
|
|
|
gRing = gRing.Next()
|
|
|
|
|
|
}
|
|
|
|
|
|
t.Logf("the ring length:%d", gRing.Len())
|
|
|
|
|
|
t.Logf("the result length:%d", gRingResult.Len())
|
|
|
|
|
|
|
|
|
|
|
|
// stdring
|
|
|
|
|
|
stdRing := ring.New(10)
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
|
stdRing.Value = i
|
|
|
|
|
|
stdRing = stdRing.Next()
|
|
|
|
|
|
}
|
|
|
|
|
|
t.Logf("the length:%d", stdRing.Len())
|
|
|
|
|
|
stdRingResult := stdRing.Unlink(6)
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
|
t.Log(stdRing.Value)
|
|
|
|
|
|
stdRing = stdRing.Next()
|
|
|
|
|
|
}
|
|
|
|
|
|
t.Logf("the ring length:%d", stdRing.Len())
|
|
|
|
|
|
t.Logf("the result length:%d", stdRingResult.Len())
|
|
|
|
|
|
|
|
|
|
|
|
// Assertion.
|
|
|
|
|
|
t.Assert(gRing.Len(), stdRing.Len())
|
|
|
|
|
|
t.Assert(gRingResult.Len(), stdRingResult.Len())
|
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
|
t.Assert(stdRing.Value, gRing.Val())
|
|
|
|
|
|
stdRing = stdRing.Next()
|
|
|
|
|
|
gRing = gRing.Next()
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2022-07-11 19:34:40 +08:00
|
|
|
|
|
|
|
|
|
|
func TestRing_RLockIteratorNext(t *testing.T) {
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
r := gring.New(10)
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
|
r.Set(i).Next()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
iterVal := 0
|
|
|
|
|
|
r.RLockIteratorNext(func(value interface{}) bool {
|
|
|
|
|
|
if value.(int) == 0 {
|
|
|
|
|
|
iterVal = value.(int)
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
return true
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
t.Assert(iterVal, 0)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestRing_RLockIteratorPrev(t *testing.T) {
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
r := gring.New(10)
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
|
r.Set(i).Next()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
iterVal := 0
|
|
|
|
|
|
r.RLockIteratorPrev(func(value interface{}) bool {
|
|
|
|
|
|
if value.(int) == 0 {
|
|
|
|
|
|
iterVal = value.(int)
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
return true
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
t.Assert(iterVal, 0)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|