2015-04-07 16:20:01 +00:00
|
|
|
package torrent
|
|
|
|
|
2015-08-03 06:23:05 +00:00
|
|
|
import (
|
2018-06-12 12:40:04 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2015-08-03 06:23:05 +00:00
|
|
|
"testing"
|
2015-04-07 16:20:01 +00:00
|
|
|
|
2017-09-23 05:25:47 +00:00
|
|
|
"github.com/anacrolix/missinggo/iter"
|
2020-01-10 04:09:21 +00:00
|
|
|
"github.com/anacrolix/missinggo/v2/bitmap"
|
2018-06-12 12:40:04 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2015-08-03 06:23:05 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTorrentOffsetRequest(t *testing.T) {
|
2021-01-28 03:23:22 +00:00
|
|
|
check := func(tl, ps, off int64, expected Request, ok bool) {
|
2015-07-15 05:31:18 +00:00
|
|
|
req, _ok := torrentOffsetRequest(tl, ps, defaultChunkSize, off)
|
2015-08-03 06:23:05 +00:00
|
|
|
assert.Equal(t, _ok, ok)
|
|
|
|
assert.Equal(t, req, expected)
|
2015-04-07 16:20:01 +00: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)
|
2021-01-28 03:23:22 +00:00
|
|
|
check(13, 5, 13, Request{}, false)
|
2015-04-07 16:20:01 +00:00
|
|
|
}
|
2017-09-23 05:25:47 +00:00
|
|
|
|
2021-05-08 00:35:23 +00:00
|
|
|
func BenchmarkIterBitmapsDistinct(t *testing.B) {
|
|
|
|
t.ReportAllocs()
|
2021-09-14 03:46:50 +00:00
|
|
|
for i := 0; i < t.N; i += 1 {
|
2021-05-08 00:35:23 +00:00
|
|
|
var skip, first, second bitmap.Bitmap
|
|
|
|
skip.Add(1)
|
|
|
|
first.Add(1, 0, 3)
|
|
|
|
second.Add(1, 2, 0)
|
|
|
|
skipCopy := skip.Copy()
|
|
|
|
t.StartTimer()
|
|
|
|
output := iter.ToSlice(iterBitmapsDistinct(&skipCopy, first, second))
|
|
|
|
t.StopTimer()
|
|
|
|
assert.Equal(t, []interface{}{0, 3, 2}, output)
|
2021-05-21 04:01:41 +00:00
|
|
|
assert.Equal(t, []bitmap.BitIndex{1}, skip.ToSortedSlice())
|
2021-05-08 00:35:23 +00:00
|
|
|
}
|
2017-09-23 05:25:47 +00:00
|
|
|
}
|
2018-06-12 12:40:04 +00:00
|
|
|
|
|
|
|
func TestSpewConnStats(t *testing.T) {
|
|
|
|
s := spew.Sdump(ConnStats{})
|
2018-06-13 00:56:09 +00:00
|
|
|
t.Logf("\n%s", s)
|
2018-06-12 12:40:04 +00:00
|
|
|
lines := strings.Count(s, "\n")
|
|
|
|
assert.EqualValues(t, 2+reflect.ValueOf(ConnStats{}).NumField(), lines)
|
|
|
|
}
|