|
10 | 10 | ) |
11 | 11 |
|
12 | 12 |
|
13 | | -def test_encode_str(): |
14 | | - assert bencode("WWWWWW") == b'6:WWWWWW' |
| 13 | +def test_encode_str(benchmark): |
| 14 | + assert benchmark(bencode, "WWWWWW") == b'6:WWWWWW' |
15 | 15 |
|
16 | 16 |
|
17 | | -def test_encode_int(): |
18 | | - assert bencode(233) == b'i233e' |
| 17 | +def test_encode_int(benchmark): |
| 18 | + assert benchmark(bencode, 233) == b'i233e' |
19 | 19 |
|
20 | 20 |
|
21 | | -def test_encode_large_int(): |
| 21 | +def test_encode_large_int(benchmark): |
22 | 22 | assert bencode(1455189890) == b'i1455189890e' |
23 | 23 | assert bencode(25735241490) == b'i25735241490e' |
24 | | - assert bencode(sys.maxsize) == ('i%de' % sys.maxsize).encode() |
| 24 | + assert benchmark(bencode, sys.maxsize) == ('i%de' % sys.maxsize).encode() |
25 | 25 |
|
26 | 26 |
|
27 | | -def test_encode_bytes(): |
| 27 | +def test_encode_bytes(benchmark): |
28 | 28 | b = b"TheseAreSomeBytes" |
29 | | - coded = bencode(b) |
| 29 | + coded = benchmark(bencode, b) |
30 | 30 | l = str(len(b)).encode() |
31 | 31 | assert coded == l + b':' + b |
32 | 32 |
|
33 | 33 |
|
34 | | -def test_encode_list(): |
35 | | - assert bencode(['a', 'b', 3]) == b'l1:a1:bi3ee' |
| 34 | +def test_encode_list(benchmark): |
| 35 | + assert benchmark(bencode, ['a', 'b', 3]) == b'l1:a1:bi3ee' |
36 | 36 |
|
37 | 37 |
|
38 | | -def test_encode_tuple(): |
39 | | - assert bencode(('a', 'b', 3)) == b'l1:a1:bi3ee' |
| 38 | +def test_encode_tuple(benchmark): |
| 39 | + assert benchmark(bencode, ('a', 'b', 3)) == b'l1:a1:bi3ee' |
40 | 40 |
|
41 | 41 |
|
42 | | -def test_encode_bool(): |
43 | | - assert bencode(True) == bencode(1) |
44 | | - assert bencode(False) == bencode(0) |
| 42 | +def test_encode_true(benchmark): |
| 43 | + assert benchmark(bencode, True) == bencode(1) |
45 | 44 |
|
46 | 45 |
|
47 | | -def test_encode_dict(): |
| 46 | +def test_encode_false(benchmark): |
| 47 | + assert benchmark(bencode, False) == bencode(0) |
| 48 | + |
| 49 | + |
| 50 | +def test_encode_dict(benchmark): |
48 | 51 | od = dict() |
49 | 52 | od['ka'] = 'va' |
50 | 53 | od['kb'] = 2 |
51 | | - assert bencode(od) == b'd2:ka2:va2:kbi2ee' |
| 54 | + assert benchmark(bencode, od) == b'd2:ka2:va2:kbi2ee' |
52 | 55 |
|
53 | 56 |
|
54 | | -def test_encode_dict_subclass(): |
| 57 | +def test_encode_dict_subclass(benchmark): |
55 | 58 | class AAA(dict): |
56 | 59 | pass |
57 | 60 |
|
58 | 61 | od = dict() |
59 | 62 | od['ka'] = 'va' |
60 | 63 | od['kb'] = 2 |
61 | | - assert bencode(od) == b'd2:ka2:va2:kbi2ee' |
| 64 | + assert benchmark(bencode, od) == b'd2:ka2:va2:kbi2ee' |
62 | 65 |
|
63 | 66 |
|
64 | | -def test_encode_complex(): |
| 67 | +def test_encode_complex(benchmark): |
65 | 68 | od = dict() |
66 | 69 | od['KeyA'] = ['listitemA', {'k': 'v'}, 3] |
67 | 70 | od['KeyB'] = {'k': 'v'} |
68 | 71 | od['KeyC'] = 3 |
69 | 72 | od['KeyD'] = 'AString' |
70 | 73 | expected_result = b'd4:KeyAl9:listitemAd1:k1:vei3ee4:KeyBd1:k1:ve4:KeyCi3e4:KeyD7:AStringe' |
71 | | - assert bencode(od) == expected_result |
| 74 | + assert benchmark(bencode, od) == expected_result |
72 | 75 |
|
73 | 76 |
|
74 | | -def test_infohash(): |
| 77 | +def test_infohash(benchmark): |
75 | 78 | import hashlib |
76 | 79 | with open(TORRENT_PATH, "rb") as f: |
77 | 80 | torrent = bdecode(f.read()) |
78 | | - infohash = hashlib.sha1(bencode(torrent[b'info'])).hexdigest() |
| 81 | + infohash = hashlib.sha1(benchmark(bencode, torrent[b'info'])).hexdigest() |
79 | 82 | assert infohash == "4194e473d6c49630e1c6247d6716076809bb96ae" |
0 commit comments