]> Sergey Matveev's repositories - btrtrc.git/blob - misc_test.go
Drop support for go 1.20
[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 BenchmarkIterBitmapsDistinct(t *testing.B) {
27         t.ReportAllocs()
28         for i := 0; i < t.N; i += 1 {
29                 var skip, first, second bitmap.Bitmap
30                 skip.Add(1)
31                 first.Add(1, 0, 3)
32                 second.Add(1, 2, 0)
33                 skipCopy := skip.Copy()
34                 t.StartTimer()
35                 output := iter.ToSlice(iterBitmapsDistinct(&skipCopy, first, second))
36                 t.StopTimer()
37                 assert.Equal(t, []interface{}{0, 3, 2}, output)
38                 assert.Equal(t, []bitmap.BitIndex{1}, skip.ToSortedSlice())
39         }
40 }
41
42 func TestSpewConnStats(t *testing.T) {
43         s := spew.Sdump(ConnStats{})
44         t.Logf("\n%s", s)
45         lines := strings.Count(s, "\n")
46         assert.EqualValues(t, 2+reflect.ValueOf(ConnStats{}).NumField(), lines)
47 }