]> Sergey Matveev's repositories - btrtrc.git/blob - file_test.go
Add abstraction and tests for #387
[btrtrc.git] / file_test.go
1 package torrent
2
3 import (
4         "testing"
5
6         "github.com/anacrolix/missinggo/v2/bitmap"
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 bitmap.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:  2,
45                 firstPieceIndex: 1,
46                 endPieceIndex:   1,
47                 fileOffset:      1,
48                 fileLength:      1,
49                 expected:        1,
50         }.Run(t)
51
52         testFileBytesLeft{
53                 usualPieceSize:  3,
54                 firstPieceIndex: 0,
55                 endPieceIndex:   0,
56                 fileOffset:      1,
57                 fileLength:      1,
58                 expected:        1,
59                 name:            "FileInFirstPiece",
60         }.Run(t)
61
62         testFileBytesLeft{
63                 usualPieceSize:  3,
64                 firstPieceIndex: 0,
65                 endPieceIndex:   0,
66                 fileOffset:      1,
67                 fileLength:      1,
68                 expected:        1,
69                 name:            "LandLocked",
70         }.Run(t)
71 }