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