package torrent import ( "bitbucket.org/anacrolix/go.torrent/peer_protocol" "testing" ) func r(i, b, l peer_protocol.Integer) request { return request{i, chunkSpec{b, l}} } // Check the given Request is correct for various torrent offsets. func TestTorrentRequest(t *testing.T) { const s = 472183431 // Length of torrent. for _, _case := range []struct { off int64 // An offset into the torrent. req request // The expected Request. The zero value means !ok. }{ // Invalid offset. {-1, request{}}, {0, r(0, 0, 16384)}, // One before the end of a piece. {1<<18 - 1, r(0, 1<<18-16384, 16384)}, // Offset beyond torrent length. {472 * 1 << 20, request{}}, // One before the end of the torrent. Complicates the chunk length. {s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)}, {1, r(0, 0, 16384)}, // One before end of chunk. {16383, r(0, 0, 16384)}, // Second chunk. {16384, r(0, 16384, 16384)}, } { req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off) if (_case.req == request{}) == ok { t.Fatalf("expected %v, got %v", _case.req, req) } if req != _case.req { t.Fatalf("expected %v, got %v", _case.req, req) } } }