~ubuntu-branches/debian/sid/pyrlp/sid

« back to all changes in this revision

Viewing changes to tests/test_big_endian.py

  • Committer: Package Import Robot
  • Author(s): Ben Finney
  • Date: 2017-07-15 05:25:42 UTC
  • Revision ID: package-import@ubuntu.com-20170715052542-a20zzsypt1qfecq1
Tags: upstream-0.5.1
ImportĀ upstreamĀ versionĀ 0.5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import unicode_literals
 
2
 
 
3
import pytest
 
4
from rlp import SerializationError, utils
 
5
from rlp.sedes import big_endian_int, BigEndianInt
 
6
from rlp.utils import int_to_big_endian
 
7
 
 
8
valid_data = (
 
9
    (256, b'\x01\x00'),
 
10
    (1024, b'\x04\x00'),
 
11
    (65535, b'\xff\xff'),
 
12
)
 
13
 
 
14
single_bytes = ((n, utils.ascii_chr(n)) for n in range(1, 256))
 
15
 
 
16
random_integers = (256, 257, 4839, 849302, 483290432, 483290483290482039482039,
 
17
                   48930248348219540325894323584235894327865439258743754893066)
 
18
assert random_integers[-1] < 2**256
 
19
 
 
20
negative_ints = (-1, -100, -255, -256, -2342423)
 
21
 
 
22
 
 
23
def test_neg():
 
24
    for n in negative_ints:
 
25
        with pytest.raises(SerializationError):
 
26
            big_endian_int.serialize(n)
 
27
 
 
28
 
 
29
def test_serialization():
 
30
    for n in random_integers:
 
31
        serial = big_endian_int.serialize(n)
 
32
        deserialized = big_endian_int.deserialize(serial)
 
33
        assert deserialized == n
 
34
        if n != 0:
 
35
            assert serial[0] != b'\x00'  # is not checked
 
36
 
 
37
 
 
38
def test_single_byte():
 
39
    for n, s in single_bytes:
 
40
        serial = big_endian_int.serialize(n)
 
41
        assert serial == s
 
42
        deserialized = big_endian_int.deserialize(serial)
 
43
        assert deserialized == n
 
44
 
 
45
 
 
46
def test_valid_data():
 
47
    for n, serial in valid_data:
 
48
        serialized = big_endian_int.serialize(n)
 
49
        deserialized = big_endian_int.deserialize(serial)
 
50
        assert serialized == serial
 
51
        assert deserialized == n
 
52
 
 
53
 
 
54
def test_fixedlength():
 
55
    s = BigEndianInt(4)
 
56
    for i in (0, 1, 255, 256, 256**3, 256**4 - 1):
 
57
        assert len(s.serialize(i)) == 4
 
58
        assert s.deserialize(s.serialize(i)) == i
 
59
    for i in (256**4, 256**4 + 1, 256**5, -1, -256, 'asdf'):
 
60
        with pytest.raises(SerializationError):
 
61
            s.serialize(i)
 
62
 
 
63
 
 
64
import binascii
 
65
 
 
66
 
 
67
def packl(lnum):
 
68
    """Packs the lnum (which must be convertable to a long) into a
 
69
       byte string 0 padded to a multiple of padmultiple bytes in size. 0
 
70
       means no padding whatsoever, so that packing 0 result in an empty
 
71
       string.  The resulting byte string is the big-endian two's
 
72
       complement representation of the passed in long."""
 
73
 
 
74
    if lnum == 0:
 
75
        return b'\0'
 
76
    s = hex(lnum)[2:]
 
77
    s = s.rstrip('L')
 
78
    if len(s) & 1:
 
79
        s = '0' + s
 
80
    s = binascii.unhexlify(s)
 
81
    return s
 
82
 
 
83
 
 
84
try:
 
85
    import ctypes
 
86
    PyLong_AsByteArray = ctypes.pythonapi._PyLong_AsByteArray
 
87
    PyLong_AsByteArray.argtypes = [ctypes.py_object,
 
88
                                   ctypes.c_char_p,
 
89
                                   ctypes.c_size_t,
 
90
                                   ctypes.c_int,
 
91
                                   ctypes.c_int]
 
92
    import sys
 
93
    long_start = sys.maxint + 1
 
94
 
 
95
    def packl_ctypes(lnum):
 
96
        if lnum < long_start:
 
97
            return int_to_big_endian(lnum)
 
98
        a = ctypes.create_string_buffer(lnum.bit_length() // 8 + 1)
 
99
        PyLong_AsByteArray(lnum, a, len(a), 0, 1)
 
100
        return a.raw.lstrip(b'\0')
 
101
except AttributeError:
 
102
    packl_ctypes = packl
 
103
 
 
104
 
 
105
def test_packl():
 
106
    for i in range(256):
 
107
        v = 2**i - 1
 
108
        rc = packl_ctypes(v)
 
109
        assert rc == int_to_big_endian(v)
 
110
        r = packl(v)
 
111
        assert r == int_to_big_endian(v)
 
112
 
 
113
 
 
114
def perf():
 
115
    import time
 
116
 
 
117
    st = time.time()
 
118
    for j in range(100000):
 
119
        for i in random_integers:
 
120
            packl(i)
 
121
    print('packl elapsed {}'.format(time.time() - st))
 
122
 
 
123
    st = time.time()
 
124
    for j in range(100000):
 
125
        for i in random_integers:
 
126
            packl_ctypes(i)
 
127
    print('ctypes elapsed {}'.format(time.time() - st))
 
128
 
 
129
    st = time.time()
 
130
    for j in range(100000):
 
131
        for i in random_integers:
 
132
            int_to_big_endian(i)
 
133
    print('py elapsed {}'.format(time.time() - st))
 
134
 
 
135
 
 
136
if __name__ == '__main__':
 
137
    # test_packl()
 
138
    perf()