]> Sergey Matveev's repositories - btrtrc.git/blob - torrent_test.go
Merge pull request #72 from shoenig/magnetize
[btrtrc.git] / torrent_test.go
1 package torrent
2
3 import (
4         "testing"
5
6         "github.com/anacrolix/torrent/peer_protocol"
7 )
8
9 func r(i, b, l peer_protocol.Integer) request {
10         return request{i, chunkSpec{b, l}}
11 }
12
13 // Check the given Request is correct for various torrent offsets.
14 func TestTorrentRequest(t *testing.T) {
15         const s = 472183431 // Length of torrent.
16         for _, _case := range []struct {
17                 off int64   // An offset into the torrent.
18                 req request // The expected Request. The zero value means !ok.
19         }{
20                 // Invalid offset.
21                 {-1, request{}},
22                 {0, r(0, 0, 16384)},
23                 // One before the end of a piece.
24                 {1<<18 - 1, r(0, 1<<18-16384, 16384)},
25                 // Offset beyond torrent length.
26                 {472 * 1 << 20, request{}},
27                 // One before the end of the torrent. Complicates the chunk length.
28                 {s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
29                 {1, r(0, 0, 16384)},
30                 // One before end of chunk.
31                 {16383, r(0, 0, 16384)},
32                 // Second chunk.
33                 {16384, r(0, 16384, 16384)},
34         } {
35                 req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
36                 if (_case.req == request{}) == ok {
37                         t.Fatalf("expected %v, got %v", _case.req, req)
38                 }
39                 if req != _case.req {
40                         t.Fatalf("expected %v, got %v", _case.req, req)
41                 }
42         }
43 }
44
45 func TestAppendToCopySlice(t *testing.T) {
46         orig := []int{1, 2, 3}
47         dupe := append([]int{}, orig...)
48         dupe[0] = 4
49         if orig[0] != 1 {
50                 t.FailNow()
51         }
52 }
53
54 func TestTorrentString(t *testing.T) {
55         tor := &Torrent{}
56         s := tor.InfoHash().HexString()
57         if s != "0000000000000000000000000000000000000000" {
58                 t.FailNow()
59         }
60 }