Files
gf/container/gqueue/gqueue_z_unit_test.go

109 lines
2.0 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-06-17 19:54:00 +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.
// go test *.go -bench=".*" -benchmem
2019-06-16 22:55:07 +08:00
package gqueue_test
import (
"testing"
2019-10-25 17:32:03 +08:00
"time"
2019-07-29 21:01:19 +08:00
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/container/gqueue"
"github.com/gogf/gf/v2/test/gtest"
2019-06-16 22:55:07 +08:00
)
func TestQueue_Len(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
2023-03-21 22:10:41 +08:00
var (
maxNum = 100
maxTries = 100
)
for n := 10; n < maxTries; n++ {
q1 := gqueue.New(maxNum)
for i := 0; i < maxNum; i++ {
2020-03-19 23:53:03 +08:00
q1.Push(i)
}
2023-03-21 22:10:41 +08:00
t.Assert(q1.Len(), maxNum)
t.Assert(q1.Size(), maxNum)
}
})
gtest.C(t, func(t *gtest.T) {
var (
maxNum = 100
maxTries = 100
)
for n := 10; n < maxTries; n++ {
q1 := gqueue.New()
for i := 0; i < maxNum; i++ {
q1.Push(i)
}
t.AssertLE(q1.Len(), maxNum)
t.AssertLE(q1.Size(), maxNum)
2019-06-16 23:24:03 +08:00
}
2020-03-19 23:53:03 +08:00
})
2019-06-17 19:54:00 +08:00
}
func TestQueue_Basic(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
q := gqueue.New()
for i := 0; i < 100; i++ {
q.Push(i)
}
t.Assert(q.Pop(), 0)
t.Assert(q.Pop(), 1)
})
2019-06-16 22:55:07 +08:00
}
func TestQueue_Pop(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
q1 := gqueue.New()
q1.Push(1)
q1.Push(2)
q1.Push(3)
q1.Push(4)
i1 := q1.Pop()
t.Assert(i1, 1)
})
2019-06-16 22:55:07 +08:00
}
func TestQueue_Close(t *testing.T) {
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
q1 := gqueue.New()
q1.Push(1)
q1.Push(2)
// wait sync to channel
time.Sleep(10 * time.Millisecond)
2020-03-19 23:53:03 +08:00
t.Assert(q1.Len(), 2)
q1.Close()
})
gtest.C(t, func(t *gtest.T) {
q1 := gqueue.New(2)
q1.Push(1)
q1.Push(2)
// wait sync to channel
time.Sleep(10 * time.Millisecond)
t.Assert(q1.Len(), 2)
q1.Close()
})
2019-06-16 22:55:07 +08:00
}
2023-03-21 22:10:41 +08:00
func Test_Issue2509(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
q := gqueue.New()
q.Push(1)
q.Push(2)
q.Push(3)
t.AssertLE(q.Len(), 3)
2023-03-21 22:10:41 +08:00
t.Assert(<-q.C, 1)
t.AssertLE(q.Len(), 2)
2023-03-21 22:10:41 +08:00
t.Assert(<-q.C, 2)
t.AssertLE(q.Len(), 1)
2023-03-21 22:10:41 +08:00
t.Assert(<-q.C, 3)
t.Assert(q.Len(), 0)
})
}