]> Sergey Matveev's repositories - btrtrc.git/blob - request-strategy/order_test.go
Provide mapping from infohash to Torrent in Input
[btrtrc.git] / request-strategy / order_test.go
1 package request_strategy
2
3 import (
4         "encoding/gob"
5         "testing"
6
7         "github.com/RoaringBitmap/roaring"
8         qt "github.com/frankban/quicktest"
9         "github.com/google/go-cmp/cmp"
10 )
11
12 func init() {
13         gob.Register(chunkIterRange(0))
14         gob.Register(sliceChunksIter{})
15 }
16
17 type chunkIterRange ChunkIndex
18
19 func (me chunkIterRange) Iter(f func(ChunkIndex)) {
20         for offset := ChunkIndex(0); offset < ChunkIndex(me); offset += 1 {
21                 f(offset)
22         }
23 }
24
25 type sliceChunksIter []ChunkIndex
26
27 func chunkIter(offsets ...ChunkIndex) ChunksIter {
28         return sliceChunksIter(offsets)
29 }
30
31 func (offsets sliceChunksIter) Iter(f func(ChunkIndex)) {
32         for _, offset := range offsets {
33                 f(offset)
34         }
35 }
36
37 func requestSetFromSlice(rs ...RequestIndex) (ret roaring.Bitmap) {
38         ret.AddMany(rs)
39         return
40 }
41
42 func init() {
43         gob.Register(intPeerId(0))
44 }
45
46 type intPeerId int
47
48 func (i intPeerId) Uintptr() uintptr {
49         return uintptr(i)
50 }
51
52 var hasAllRequests = func() (all roaring.Bitmap) {
53         all.AddRange(0, roaring.MaxRange)
54         return
55 }()
56
57 func checkNumRequestsAndInterest(c *qt.C, next PeerNextRequestState, num uint64, interest bool) {
58         addressableBm := next.Requests
59         c.Check(addressableBm.GetCardinality(), qt.ContentEquals, num)
60         c.Check(next.Interested, qt.Equals, interest)
61 }
62
63 func checkResultsRequestsLen(t *testing.T, reqs roaring.Bitmap, l uint64) {
64         qt.Check(t, reqs.GetCardinality(), qt.Equals, l)
65 }
66
67 var peerNextRequestStateChecker = qt.CmpEquals(
68         cmp.Transformer(
69                 "bitmap",
70                 func(bm roaring.Bitmap) []uint32 {
71                         return bm.ToArray()
72                 }))