]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Move torrentOffsetRequest and torrentRequestOffset, fixing a bug in former, and test it
authorMatt Joiner <anacrolix@gmail.com>
Tue, 7 Apr 2015 16:20:01 +0000 (02:20 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 7 Apr 2015 16:20:01 +0000 (02:20 +1000)
misc.go
misc_test.go [new file with mode: 0644]
torrent.go

diff --git a/misc.go b/misc.go
index 56d1f9c9e6dee1d27584828a143b0b9e35d47071..c567f68d5631d320732873890a7c8372d4d9c486 100644 (file)
--- a/misc.go
+++ b/misc.go
@@ -78,3 +78,32 @@ func super(child interface{}) (parent interface{}, ok bool) {
        ok = parent != nil
        return
 }
+
+// Return the request that would include the given offset into the torrent data.
+func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
+       r request, ok bool) {
+       if offset < 0 || offset >= torrentLength {
+               return
+       }
+       r.Index = pp.Integer(offset / pieceSize)
+       r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
+       r.Length = pp.Integer(chunkSize)
+       pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
+       if r.Length > pieceLeft {
+               r.Length = pieceLeft
+       }
+       torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
+       if int64(r.Length) > torrentLeft {
+               r.Length = pp.Integer(torrentLeft)
+       }
+       ok = true
+       return
+}
+
+func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
+       off = int64(r.Index)*pieceSize + int64(r.Begin)
+       if off < 0 || off >= torrentLength {
+               panic("invalid request")
+       }
+       return
+}
diff --git a/misc_test.go b/misc_test.go
new file mode 100644 (file)
index 0000000..a8bd10f
--- /dev/null
@@ -0,0 +1,15 @@
+package torrent
+
+import . "gopkg.in/check.v1"
+
+func (suite) TestTorrentOffsetRequest(c *C) {
+       check := func(tl, ps, off int64, expected request, ok bool) {
+               req, _ok := torrentOffsetRequest(tl, ps, chunkSize, off)
+               c.Check(_ok, Equals, ok)
+               c.Check(req, Equals, expected)
+       }
+       check(13, 5, 0, newRequest(0, 0, 5), true)
+       check(13, 5, 3, newRequest(0, 0, 5), true)
+       check(13, 5, 11, newRequest(2, 0, 3), true)
+       check(13, 5, 13, request{}, false)
+}
index 32fa54d7d27d95c9b6b5e900bd7914287cdf9596..8b5dbb715a7eee3bcf154833a66eb7212721b8f3 100644 (file)
@@ -630,37 +630,12 @@ func (t *torrent) close() (err error) {
        return
 }
 
-// Return the request that would include the given offset into the torrent data.
-func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
-       r request, ok bool) {
-       if offset < 0 || offset >= torrentLength {
-               return
-       }
-       r.Index = pp.Integer(offset / pieceSize)
-       r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
-       left := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
-       if chunkSize < left {
-               r.Length = pp.Integer(chunkSize)
-       } else {
-               r.Length = pp.Integer(left)
-       }
-       ok = true
-       return
-}
-
-func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
-       off = int64(r.Index)*pieceSize + int64(r.Begin)
-       if off < 0 || off >= torrentLength {
-               panic("invalid request")
-       }
-       return
-}
-
 func (t *torrent) requestOffset(r request) int64 {
        return torrentRequestOffset(t.Length(), int64(t.usualPieceSize()), r)
 }
 
-// Return the request that would include the given offset into the torrent data.
+// Return the request that would include the given offset into the torrent
+// data. Returns !ok if there is no such request.
 func (t *torrent) offsetRequest(off int64) (req request, ok bool) {
        return torrentOffsetRequest(t.Length(), t.Info.PieceLength, chunkSize, off)
 }