Add abstraction and tests for #387
This commit is contained in:
parent
378ccd0932
commit
3507ff1a69
20
file.go
20
file.go
|
@ -54,11 +54,9 @@ func (f *File) bytesCompleted() int64 {
|
||||||
return f.length - f.bytesLeft()
|
return f.length - f.bytesLeft()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *File) bytesLeft() (left int64) {
|
func fileBytesLeft(pieceSize int64, firstPieceIndex int, endPieceIndex int, fileOffset int64, fileLength int64, completedPieces bitmap.Bitmap) (left int64) {
|
||||||
pieceSize := int64(f.t.usualPieceSize())
|
endPieceIndex--
|
||||||
firstPieceIndex := f.firstPieceIndex()
|
bitmap.Flip(completedPieces, firstPieceIndex+1, endPieceIndex).IterTyped(func(piece int) bool {
|
||||||
endPieceIndex := f.endPieceIndex() - 1
|
|
||||||
bitmap.Flip(f.t._completedPieces, firstPieceIndex+1, endPieceIndex).IterTyped(func(piece int) bool {
|
|
||||||
if piece >= endPieceIndex {
|
if piece >= endPieceIndex {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -67,15 +65,19 @@ func (f *File) bytesLeft() (left int64) {
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
if !f.t.pieceComplete(firstPieceIndex) {
|
if !completedPieces.Get(firstPieceIndex) {
|
||||||
left += pieceSize - (f.offset % pieceSize)
|
left += pieceSize - (fileOffset % pieceSize)
|
||||||
}
|
}
|
||||||
if !f.t.pieceComplete(endPieceIndex) {
|
if !completedPieces.Get(endPieceIndex) {
|
||||||
left += (f.offset + f.length) % pieceSize
|
left += (fileOffset + fileLength) % pieceSize
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *File) bytesLeft() (left int64) {
|
||||||
|
return fileBytesLeft(int64(f.t.usualPieceSize()), f.firstPieceIndex(), f.endPieceIndex(), f.offset, f.length, f.t._completedPieces)
|
||||||
|
}
|
||||||
|
|
||||||
// The relative file path for a multi-file torrent, and the torrent name for a
|
// The relative file path for a multi-file torrent, and the torrent name for a
|
||||||
// single-file torrent.
|
// single-file torrent.
|
||||||
func (f *File) DisplayPath() string {
|
func (f *File) DisplayPath() string {
|
||||||
|
|
49
file_test.go
49
file_test.go
|
@ -3,6 +3,7 @@ package torrent
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/anacrolix/missinggo/v2/bitmap"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,3 +21,51 @@ func TestFileExclusivePieces(t *testing.T) {
|
||||||
assert.EqualValues(t, _case.end, end)
|
assert.EqualValues(t, _case.end, end)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type testFileBytesLeft struct {
|
||||||
|
usualPieceSize int64
|
||||||
|
firstPieceIndex int
|
||||||
|
endPieceIndex int
|
||||||
|
fileOffset int64
|
||||||
|
fileLength int64
|
||||||
|
completedPieces bitmap.Bitmap
|
||||||
|
expected int64
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me testFileBytesLeft) Run(t *testing.T) {
|
||||||
|
t.Run(me.name, func(t *testing.T) {
|
||||||
|
assert.EqualValues(t, me.expected, fileBytesLeft(me.usualPieceSize, me.firstPieceIndex, me.endPieceIndex, me.fileOffset, me.fileLength, me.completedPieces))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileBytesLeft(t *testing.T) {
|
||||||
|
testFileBytesLeft{
|
||||||
|
usualPieceSize: 2,
|
||||||
|
firstPieceIndex: 1,
|
||||||
|
endPieceIndex: 1,
|
||||||
|
fileOffset: 1,
|
||||||
|
fileLength: 1,
|
||||||
|
expected: 1,
|
||||||
|
}.Run(t)
|
||||||
|
|
||||||
|
testFileBytesLeft{
|
||||||
|
usualPieceSize: 3,
|
||||||
|
firstPieceIndex: 0,
|
||||||
|
endPieceIndex: 0,
|
||||||
|
fileOffset: 1,
|
||||||
|
fileLength: 1,
|
||||||
|
expected: 1,
|
||||||
|
name: "FileInFirstPiece",
|
||||||
|
}.Run(t)
|
||||||
|
|
||||||
|
testFileBytesLeft{
|
||||||
|
usualPieceSize: 3,
|
||||||
|
firstPieceIndex: 0,
|
||||||
|
endPieceIndex: 0,
|
||||||
|
fileOffset: 1,
|
||||||
|
fileLength: 1,
|
||||||
|
expected: 1,
|
||||||
|
name: "LandLocked",
|
||||||
|
}.Run(t)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue