]> Sergey Matveev's repositories - btrtrc.git/blob - file_test.go
Use roaring.Bitmap directly for completed pieces
[btrtrc.git] / file_test.go
1 package torrent
2
3 import (
4         "testing"
5
6         "github.com/RoaringBitmap/roaring"
7         "github.com/stretchr/testify/assert"
8 )
9
10 func TestFileExclusivePieces(t *testing.T) {
11         for _, _case := range []struct {
12                 off, size, pieceSize int64
13                 begin, end           int
14         }{
15                 {0, 2, 2, 0, 1},
16                 {1, 2, 2, 1, 1},
17                 {1, 4, 2, 1, 2},
18         } {
19                 begin, end := byteRegionExclusivePieces(_case.off, _case.size, _case.pieceSize)
20                 assert.EqualValues(t, _case.begin, begin)
21                 assert.EqualValues(t, _case.end, end)
22         }
23 }
24
25 type testFileBytesLeft struct {
26         usualPieceSize  int64
27         firstPieceIndex int
28         endPieceIndex   int
29         fileOffset      int64
30         fileLength      int64
31         completedPieces roaring.Bitmap
32         expected        int64
33         name            string
34 }
35
36 func (me testFileBytesLeft) Run(t *testing.T) {
37         t.Run(me.name, func(t *testing.T) {
38                 assert.EqualValues(t, me.expected, fileBytesLeft(me.usualPieceSize, me.firstPieceIndex, me.endPieceIndex, me.fileOffset, me.fileLength, &me.completedPieces))
39         })
40 }
41
42 func TestFileBytesLeft(t *testing.T) {
43
44         testFileBytesLeft{
45                 usualPieceSize:  3,
46                 firstPieceIndex: 1,
47                 endPieceIndex:   1,
48                 fileOffset:      1,
49                 fileLength:      0,
50                 expected:        0,
51                 name:            "ZeroLengthFile",
52         }.Run(t)
53
54         testFileBytesLeft{
55                 usualPieceSize:  2,
56                 firstPieceIndex: 1,
57                 endPieceIndex:   2,
58                 fileOffset:      1,
59                 fileLength:      1,
60                 expected:        1,
61                 name:            "EndOfSecondPiece",
62         }.Run(t)
63
64         testFileBytesLeft{
65                 usualPieceSize:  3,
66                 firstPieceIndex: 0,
67                 endPieceIndex:   1,
68                 fileOffset:      1,
69                 fileLength:      1,
70                 expected:        1,
71                 name:            "FileInFirstPiece",
72         }.Run(t)
73
74         testFileBytesLeft{
75                 usualPieceSize:  3,
76                 firstPieceIndex: 0,
77                 endPieceIndex:   1,
78                 fileOffset:      1,
79                 fileLength:      1,
80                 expected:        1,
81                 name:            "LandLocked",
82         }.Run(t)
83
84         testFileBytesLeft{
85                 usualPieceSize:  3,
86                 firstPieceIndex: 1,
87                 endPieceIndex:   3,
88                 fileOffset:      4,
89                 fileLength:      4,
90                 expected:        4,
91                 name:            "TwoPieces",
92         }.Run(t)
93
94         testFileBytesLeft{
95                 usualPieceSize:  3,
96                 firstPieceIndex: 1,
97                 endPieceIndex:   4,
98                 fileOffset:      5,
99                 fileLength:      7,
100                 expected:        7,
101                 name:            "ThreePieces",
102         }.Run(t)
103
104         testFileBytesLeft{
105                 usualPieceSize:  3,
106                 firstPieceIndex: 1,
107                 endPieceIndex:   4,
108                 fileOffset:      5,
109                 fileLength:      7,
110                 expected:        0,
111                 completedPieces: func() (ret roaring.Bitmap) {
112                         ret.AddRange(0, 5)
113                         return
114                 }(),
115                 name: "ThreePiecesCompletedAll",
116         }.Run(t)
117 }