priorityq/queue/lib_test.go

147 lines
2.4 KiB
Go
Raw Normal View History

2023-03-04 04:09:51 +00:00
package queue_test
2023-03-01 04:33:22 +00:00
import (
"math/rand"
2023-03-01 04:33:22 +00:00
"testing"
2023-03-04 04:09:51 +00:00
"gogs.humancabbage.net/sam/priorityq/queue"
2023-03-01 04:33:22 +00:00
)
func TestRepeatPushPop(t *testing.T) {
t.Parallel()
2023-03-04 04:09:51 +00:00
q := queue.Make[int](4)
if q.Capacity() != 4 {
t.Errorf("wrong capacity")
}
2023-03-01 04:33:22 +00:00
for i := 0; i < 50; i++ {
2023-03-04 04:09:51 +00:00
q.PushBack(1)
q.PushBack(2)
q.PushBack(3)
q.PushBack(4)
if q.Len() != 4 {
t.Errorf("wrong length")
}
2023-03-01 04:33:22 +00:00
checkPop := func(n int) {
2023-03-04 04:09:51 +00:00
if v := q.PopFront(); v != n {
2023-03-01 04:33:22 +00:00
t.Errorf("popped %d, expected %d", v, n)
}
}
checkPop(1)
checkPop(2)
checkPop(3)
checkPop(4)
}
}
func TestInterleavedPushPop(t *testing.T) {
t.Parallel()
2023-03-04 04:09:51 +00:00
q := queue.Make[int](4)
2023-03-01 04:33:22 +00:00
checkPop := func(n int) {
2023-03-04 04:09:51 +00:00
if v := q.PopFront(); v != n {
2023-03-01 04:33:22 +00:00
t.Errorf("popped %d, expected %d", v, n)
}
}
2023-03-04 04:09:51 +00:00
q.PushBack(1)
q.PushBack(2)
q.PushBack(3)
2023-03-01 04:33:22 +00:00
checkPop(1)
2023-03-04 04:09:51 +00:00
q.PushBack(4)
q.PushBack(5)
2023-03-01 04:33:22 +00:00
checkPop(2)
}
func TestEmptyPopPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("popping empty buffer did not panic")
}
}()
t.Parallel()
2023-03-04 04:09:51 +00:00
q := queue.Make[int](4)
q.PopFront()
2023-03-01 04:33:22 +00:00
}
func TestFullPushPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("pushing full buffer did not panic")
}
}()
t.Parallel()
2023-03-04 04:09:51 +00:00
q := queue.Make[int](1)
q.PushBack(1)
q.PushBack(2)
2023-03-01 04:33:22 +00:00
}
2024-08-22 07:53:48 +00:00
func TestIter(t *testing.T) {
q := queue.Make[int](4)
q.PushBack(1)
q.PushBack(2)
q.PushBack(3)
q.PushBack(4)
i := 0
for v := range q.Iter() {
expected := i + 1
if v != expected {
t.Errorf("iter %d should have value %d, not %d",
i, expected, v)
}
i++
}
if q.Len() != 4 {
t.Errorf("wrong length")
}
}
func TestIterPop(t *testing.T) {
t.Parallel()
q := queue.Make[int](4)
q.PushBack(1)
q.PushBack(2)
q.PushBack(3)
q.PushBack(4)
i := 0
for v := range q.IterPop() {
expected := i + 1
if v != expected {
t.Errorf("iter %d should have value %d, not %d",
i, expected, v)
}
i++
// to test yield() returning false
if i == 4 {
break
}
}
if q.Len() != 0 {
t.Errorf("wrong length")
}
}
func BenchmarkPush(b *testing.B) {
2023-03-04 04:09:51 +00:00
q := queue.Make[int](b.N)
rs := rand.NewSource(0)
r := rand.New(rs)
items := make([]int, b.N)
for i := 0; i < b.N; i++ {
items[i] = r.Int()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
2023-03-04 04:09:51 +00:00
q.PushBack(items[i])
}
}
func BenchmarkPop(b *testing.B) {
2023-03-04 04:09:51 +00:00
q := queue.Make[int](b.N)
rs := rand.NewSource(0)
r := rand.New(rs)
for i := 0; i < b.N; i++ {
2023-03-04 04:09:51 +00:00
q.PushBack(r.Int())
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
2023-03-04 04:09:51 +00:00
q.PopFront()
}
}