]> Sergey Matveev's repositories - btrtrc.git/blob - file_test.go
Print peer ID in ASCII-only
[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         testFileBytesLeft{
44                 usualPieceSize:  3,
45                 firstPieceIndex: 1,
46                 endPieceIndex:   1,
47                 fileOffset:      1,
48                 fileLength:      0,
49                 expected:        0,
50                 name:            "ZeroLengthFile",
51         }.Run(t)
52
53         testFileBytesLeft{
54                 usualPieceSize:  2,
55                 firstPieceIndex: 1,
56                 endPieceIndex:   2,
57                 fileOffset:      1,
58                 fileLength:      1,
59                 expected:        1,
60                 name:            "EndOfSecondPiece",
61         }.Run(t)
62
63         testFileBytesLeft{
64                 usualPieceSize:  3,
65                 firstPieceIndex: 0,
66                 endPieceIndex:   1,
67                 fileOffset:      1,
68                 fileLength:      1,
69                 expected:        1,
70                 name:            "FileInFirstPiece",
71         }.Run(t)
72
73         testFileBytesLeft{
74                 usualPieceSize:  3,
75                 firstPieceIndex: 0,
76                 endPieceIndex:   1,
77                 fileOffset:      1,
78                 fileLength:      1,
79                 expected:        1,
80                 name:            "LandLocked",
81         }.Run(t)
82
83         testFileBytesLeft{
84                 usualPieceSize:  3,
85                 firstPieceIndex: 1,
86                 endPieceIndex:   3,
87                 fileOffset:      4,
88                 fileLength:      4,
89                 expected:        4,
90                 name:            "TwoPieces",
91         }.Run(t)
92
93         testFileBytesLeft{
94                 usualPieceSize:  3,
95                 firstPieceIndex: 1,
96                 endPieceIndex:   4,
97                 fileOffset:      5,
98                 fileLength:      7,
99                 expected:        7,
100                 name:            "ThreePieces",
101         }.Run(t)
102
103         testFileBytesLeft{
104                 usualPieceSize:  3,
105                 firstPieceIndex: 1,
106                 endPieceIndex:   4,
107                 fileOffset:      5,
108                 fileLength:      7,
109                 expected:        0,
110                 completedPieces: func() (ret roaring.Bitmap) {
111                         ret.AddRange(0, 5)
112                         return
113                 }(),
114                 name: "ThreePiecesCompletedAll",
115         }.Run(t)
116 }