]> Sergey Matveev's repositories - btrtrc.git/blob - torrent_test.go
Big visibility/doc clean-up, and extract mmap_span package
[btrtrc.git] / torrent_test.go
1 package torrent
2
3 import (
4         "bitbucket.org/anacrolix/go.torrent/peer_protocol"
5         "testing"
6 )
7
8 func r(i, b, l peer_protocol.Integer) Request {
9         return Request{i, chunkSpec{b, l}}
10 }
11
12 // Check the given Request is correct for various torrent offsets.
13 func TestTorrentRequest(t *testing.T) {
14         const s = 472183431 // Length of torrent.
15         for _, _case := range []struct {
16                 off int64   // An offset into the torrent.
17                 req Request // The expected Request. The zero value means !ok.
18         }{
19                 // Invalid offset.
20                 {-1, Request{}},
21                 {0, r(0, 0, 16384)},
22                 // One before the end of a piece.
23                 {1<<18 - 1, r(0, 1<<18-16384, 16384)},
24                 // Offset beyond torrent length.
25                 {472 * 1 << 20, Request{}},
26                 // One before the end of the torrent. Complicates the chunk length.
27                 {s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
28                 {1, r(0, 0, 16384)},
29                 // One before end of chunk.
30                 {16383, r(0, 0, 16384)},
31                 // Second chunk.
32                 {16384, r(0, 16384, 16384)},
33         } {
34                 req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
35                 if (_case.req == Request{}) == ok {
36                         t.Fatalf("expected %v, got %v", _case.req, req)
37                 }
38                 if req != _case.req {
39                         t.Fatalf("expected %v, got %v", _case.req, req)
40                 }
41         }
42 }