FedP2P/misc_test.go

42 lines
1.1 KiB
Go
Raw Normal View History

package torrent
import (
2018-06-12 20:40:04 +08:00
"reflect"
"strings"
"testing"
"github.com/anacrolix/missinggo/bitmap"
"github.com/anacrolix/missinggo/iter"
2018-06-12 20:40:04 +08:00
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
)
func TestTorrentOffsetRequest(t *testing.T) {
check := func(tl, ps, off int64, expected request, ok bool) {
req, _ok := torrentOffsetRequest(tl, ps, defaultChunkSize, off)
assert.Equal(t, _ok, ok)
assert.Equal(t, req, expected)
}
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)
}
func TestIterBitmapsDistinct(t *testing.T) {
var skip, first, second bitmap.Bitmap
skip.Add(1)
first.Add(1, 0, 3)
second.Add(1, 2, 0)
skipCopy := skip.Copy()
assert.Equal(t, []interface{}{0, 3, 2}, iter.ToSlice(iterBitmapsDistinct(&skipCopy, first, second)))
assert.Equal(t, []int{1}, skip.ToSortedSlice())
}
2018-06-12 20:40:04 +08:00
func TestSpewConnStats(t *testing.T) {
s := spew.Sdump(ConnStats{})
t.Logf("\n%s\n", s)
lines := strings.Count(s, "\n")
assert.EqualValues(t, 2+reflect.ValueOf(ConnStats{}).NumField(), lines)
}