torrent/request-strategy/order_test.go

73 lines
1.5 KiB
Go
Raw Normal View History

package request_strategy
2021-05-13 01:26:22 +00:00
import (
"encoding/gob"
2021-05-13 01:26:22 +00:00
"testing"
2021-09-19 05:16:37 +00:00
"github.com/RoaringBitmap/roaring"
2021-05-13 01:26:22 +00:00
qt "github.com/frankban/quicktest"
2021-09-19 05:16:37 +00:00
"github.com/google/go-cmp/cmp"
2021-05-13 01:26:22 +00:00
)
func init() {
gob.Register(chunkIterRange(0))
gob.Register(sliceChunksIter{})
}
type chunkIterRange ChunkIndex
func (me chunkIterRange) Iter(f func(ChunkIndex)) {
for offset := ChunkIndex(0); offset < ChunkIndex(me); offset += 1 {
f(offset)
}
}
type sliceChunksIter []ChunkIndex
2021-09-19 05:16:37 +00:00
func chunkIter(offsets ...ChunkIndex) ChunksIter {
return sliceChunksIter(offsets)
}
func (offsets sliceChunksIter) Iter(f func(ChunkIndex)) {
for _, offset := range offsets {
f(offset)
2021-05-13 01:26:22 +00:00
}
}
2021-09-19 05:16:37 +00:00
func requestSetFromSlice(rs ...RequestIndex) (ret roaring.Bitmap) {
ret.AddMany(rs)
2021-05-13 01:26:22 +00:00
return
}
func init() {
gob.Register(intPeerId(0))
}
2021-05-13 01:26:22 +00:00
type intPeerId int
func (i intPeerId) Uintptr() uintptr {
return uintptr(i)
}
var hasAllRequests = func() (all roaring.Bitmap) {
all.AddRange(0, roaring.MaxRange)
return
}()
2021-09-19 05:16:37 +00:00
func checkNumRequestsAndInterest(c *qt.C, next PeerNextRequestState, num uint64, interest bool) {
addressableBm := next.Requests
c.Check(addressableBm.GetCardinality(), qt.ContentEquals, num)
2021-05-14 00:23:18 +00:00
c.Check(next.Interested, qt.Equals, interest)
}
2021-09-19 05:16:37 +00:00
func checkResultsRequestsLen(t *testing.T, reqs roaring.Bitmap, l uint64) {
qt.Check(t, reqs.GetCardinality(), qt.Equals, l)
}
var peerNextRequestStateChecker = qt.CmpEquals(
cmp.Transformer(
"bitmap",
func(bm roaring.Bitmap) []uint32 {
return bm.ToArray()
}))