]> Sergey Matveev's repositories - btrtrc.git/blob - misc_test.go
Expose more callbacks and Request and ChunkSpec
[btrtrc.git] / misc_test.go
1 package torrent
2
3 import (
4         "reflect"
5         "strings"
6         "testing"
7
8         "github.com/anacrolix/missinggo/iter"
9         "github.com/anacrolix/missinggo/v2/bitmap"
10         "github.com/davecgh/go-spew/spew"
11         "github.com/stretchr/testify/assert"
12 )
13
14 func TestTorrentOffsetRequest(t *testing.T) {
15         check := func(tl, ps, off int64, expected Request, ok bool) {
16                 req, _ok := torrentOffsetRequest(tl, ps, defaultChunkSize, off)
17                 assert.Equal(t, _ok, ok)
18                 assert.Equal(t, req, expected)
19         }
20         check(13, 5, 0, newRequest(0, 0, 5), true)
21         check(13, 5, 3, newRequest(0, 0, 5), true)
22         check(13, 5, 11, newRequest(2, 0, 3), true)
23         check(13, 5, 13, Request{}, false)
24 }
25
26 func TestIterBitmapsDistinct(t *testing.T) {
27         var skip, first, second bitmap.Bitmap
28         skip.Add(1)
29         first.Add(1, 0, 3)
30         second.Add(1, 2, 0)
31         skipCopy := skip.Copy()
32         assert.Equal(t, []interface{}{0, 3, 2}, iter.ToSlice(iterBitmapsDistinct(&skipCopy, first, second)))
33         assert.Equal(t, []int{1}, skip.ToSortedSlice())
34 }
35
36 func TestSpewConnStats(t *testing.T) {
37         s := spew.Sdump(ConnStats{})
38         t.Logf("\n%s", s)
39         lines := strings.Count(s, "\n")
40         assert.EqualValues(t, 2+reflect.ValueOf(ConnStats{}).NumField(), lines)
41 }