tinyqueue/tinyqueue_test.go

71 lines
1.4 KiB
Go
Raw Normal View History

2017-05-01 22:53:51 +03:00
package tinyqueue
import (
"math/rand"
2021-01-26 00:51:33 +03:00
"reflect"
2017-05-01 22:53:51 +03:00
"sort"
"testing"
"time"
)
type floatValue float64
2021-01-26 00:51:33 +03:00
func assertEqual(t *testing.T, a, b interface{}) {
if !reflect.DeepEqual(a, b) {
t.Fatalf("'%v' != '%v'", a, b)
}
}
2017-05-01 22:53:51 +03:00
func (a floatValue) Less(b Item) bool {
return a < b.(floatValue)
}
var data, sorted = func() ([]Item, []Item) {
rand.Seed(time.Now().UnixNano())
var data []Item
for i := 0; i < 100; i++ {
data = append(data, floatValue(rand.Float64()*100))
}
sorted := make([]Item, len(data))
copy(sorted, data)
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].Less(sorted[j])
})
return data, sorted
}()
func TestMaintainsPriorityQueue(t *testing.T) {
q := New(nil)
for i := 0; i < len(data); i++ {
q.Push(data[i])
}
2021-01-26 00:51:33 +03:00
assertEqual(t, q.Peek(), sorted[0])
2017-05-01 22:53:51 +03:00
var result []Item
for q.length > 0 {
result = append(result, q.Pop())
}
2021-01-26 00:51:33 +03:00
assertEqual(t, result, sorted)
2017-05-01 22:53:51 +03:00
}
func TestAcceptsDataInConstructor(t *testing.T) {
q := New(data)
var result []Item
for q.length > 0 {
result = append(result, q.Pop())
}
2021-01-26 00:51:33 +03:00
assertEqual(t, result, sorted)
2017-05-01 22:53:51 +03:00
}
func TestHandlesEdgeCasesWithFewElements(t *testing.T) {
q := New(nil)
q.Push(floatValue(2))
q.Push(floatValue(1))
q.Pop()
q.Pop()
q.Pop()
q.Push(floatValue(2))
q.Push(floatValue(1))
2021-01-26 00:51:33 +03:00
assertEqual(t, float64(q.Pop().(floatValue)), 1.0)
assertEqual(t, float64(q.Pop().(floatValue)), 2.0)
assertEqual(t, q.Pop(), nil)
2017-05-01 22:53:51 +03:00
}