2015-04-08 00:20:01 +08:00
|
|
|
package torrent
|
|
|
|
|
2015-08-03 14:23:05 +08:00
|
|
|
import (
|
|
|
|
"testing"
|
2015-04-08 00:20:01 +08:00
|
|
|
|
2017-09-23 13:25:47 +08:00
|
|
|
"github.com/anacrolix/missinggo/bitmap"
|
|
|
|
"github.com/anacrolix/missinggo/iter"
|
2015-08-03 14:23:05 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTorrentOffsetRequest(t *testing.T) {
|
2015-04-08 00:20:01 +08:00
|
|
|
check := func(tl, ps, off int64, expected request, ok bool) {
|
2015-07-15 13:31:18 +08:00
|
|
|
req, _ok := torrentOffsetRequest(tl, ps, defaultChunkSize, off)
|
2015-08-03 14:23:05 +08:00
|
|
|
assert.Equal(t, _ok, ok)
|
|
|
|
assert.Equal(t, req, expected)
|
2015-04-08 00:20:01 +08:00
|
|
|
}
|
|
|
|
check(13, 5, 0, newRequest(0, 0, 5), true)
|
|
|
|
check(13, 5, 3, newRequest(0, 0, 5), true)
|
|
|
|
check(13, 5, 11, newRequest(2, 0, 3), true)
|
|
|
|
check(13, 5, 13, request{}, false)
|
|
|
|
}
|
2017-09-23 13:25:47 +08:00
|
|
|
|
|
|
|
func TestIterBitmapsDistinct(t *testing.T) {
|
|
|
|
var skip, first, second bitmap.Bitmap
|
|
|
|
skip.Add(1)
|
|
|
|
first.Add(1, 0, 3)
|
|
|
|
second.Add(1, 2, 0)
|
|
|
|
assert.Equal(t, []interface{}{0, 3, 2}, iter.ToSlice(iterBitmapsDistinct(skip.Copy(), first, second)))
|
|
|
|
assert.Equal(t, []int{1}, skip.ToSortedSlice())
|
|
|
|
}
|