| Home | Trees | Indices | Help |
|---|
|
|
1
2 # The contents of this file are subject to the Mozilla Public License
3 # (MPL) Version 1.1 (the "License"); you may not use this file except
4 # in compliance with the License. You may obtain a copy of the License
5 # at http://www.mozilla.org/MPL/
6 #
7 # Software distributed under the License is distributed on an "AS IS"
8 # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9 # the License for the specific language governing rights and
10 # limitations under the License.
11 #
12 # The Original Code is LEPL (http://www.acooke.org/lepl)
13 # The Initial Developer of the Original Code is Andrew Cooke.
14 # Portions created by the Initial Developer are Copyright (C) 2009-2010
15 # Andrew Cooke (andrew@acooke.org). All Rights Reserved.
16 #
17 # Alternatively, the contents of this file may be used under the terms
18 # of the LGPL license (the GNU Lesser General Public License,
19 # http://www.gnu.org/licenses/lgpl.html), in which case the provisions
20 # of the LGPL License are applicable instead of those above.
21 #
22 # If you wish to allow use of your version of this file only under the
23 # terms of the LGPL License and not to allow others to use your version
24 # of this file under the MPL, indicate your decision by deleting the
25 # provisions above and replace them with the notice and other provisions
26 # required by the LGPL License. If you do not delete the provisions
27 # above, a recipient may use your version of this file under either the
28 # MPL or the LGPL License.
29
30 '''
31 Tests for the lepl.bin.bits module.
32 '''
33
34 if bytes is str:
35 print('Binary parsing unsupported in this Python version')
36 else:
37
38 #from logging import basicConfig, DEBUG
39 from unittest import TestCase
40
41 from lepl.bin import *
42
43 # pylint: disable-msg=C0103, C0111, C0301, W0702, C0324
44 # (dude this is just a test)
45
46
57
58
60
62 assert 0 == unpack_length(0), unpack_length(0)
63 assert 1 == unpack_length(1), unpack_length(1)
64 assert 7 == unpack_length(7), unpack_length(7)
65 assert 8 == unpack_length(8), unpack_length(8)
66 assert 9 == unpack_length(9), unpack_length(9)
67 assert 0 == unpack_length(0.), unpack_length(0.)
68 assert 1 == unpack_length(0.1), unpack_length(0.1)
69 assert 7 == unpack_length(0.7), unpack_length(0.7)
70 assert 8 == unpack_length(1.), unpack_length(1.)
71 assert 8 == unpack_length(1.0), unpack_length(1.0)
72 assert 9 == unpack_length(1.1), unpack_length(1.1)
73 assert 15 == unpack_length(1.7), unpack_length(1.7)
74 assert 16 == unpack_length(2.), unpack_length(2.)
75 self.assert_error(lambda: unpack_length(0.8))
76
83
85 assert len(b) == length, (len(b), length)
86 assert b.to_bytes() == value, (b.to_bytes(), value, b)
87
89 self.assert_error(lambda: BitString.from_byte(-1))
90 self.assert_length_value(8, b'\x00', BitString.from_byte(0))
91 self.assert_length_value(8, b'\x01', BitString.from_byte(1))
92 self.assert_length_value(8, b'\xff', BitString.from_byte(255))
93 self.assert_error(lambda: BitString.from_byte(256))
94
96 self.assert_length_value(8, b'\x00', BitString.from_bytearray(b'\x00'))
97 self.assert_length_value(16, b'ab', BitString.from_bytearray(b'ab'))
98 self.assert_length_value(16, b'ab', BitString.from_bytearray(bytearray(b'ab')))
99
101 self.assert_length_value(3, b'\x00', BitString.from_int('0o0'))
102 self.assert_error(lambda: BitString.from_int('1o0'))
103 self.assert_error(lambda: BitString.from_int('00o0'))
104 self.assert_error(lambda: BitString.from_int('100o0'))
105 self.assert_error(lambda: BitString.from_int('777o0'))
106 self.assert_length_value(9, b'\x40\x00', BitString.from_int('0o100'))
107 self.assert_length_value(9, b'\xfe\x01', BitString.from_int('0o776'))
108 self.assert_length_value(12, b'\xff\x03', BitString.from_int('0x3ff'))
109 self.assert_length_value(12, b'\xff\x03', BitString.from_int('0o1777'))
110 self.assert_length_value(16, b'\x03\xff', BitString.from_int('03ffx0'))
111 self.assert_length_value(3, b'\x04', BitString.from_int('0b100'))
112 self.assert_length_value(1, b'\x01', BitString.from_int('1b0'))
113 self.assert_length_value(2, b'\x02', BitString.from_int('01b0'))
114 self.assert_length_value(9, b'\x00\x01', BitString.from_int('000000001b0'))
115 self.assert_length_value(9, b'\x01\x01', BitString.from_int('100000001b0'))
116 self.assert_length_value(16, b'\x0f\x33', BitString.from_int('1111000011001100b0'))
117
119 self.assert_length_value(8, b'\x01', BitString.from_sequence([1], BitString.from_byte))
120 self.assert_error(lambda: BitString.from_sequence([256], BitString.from_byte))
121 self.assert_length_value(16, b'\x01\x02', BitString.from_sequence([1,2], BitString.from_byte))
122
124 self.assert_error(lambda: BitString.from_int(1, 0))
125 self.assert_error(lambda: BitString.from_int(0, 1))
126 self.assert_error(lambda: BitString.from_int(0, 7))
127 self.assert_length_value(8, b'\x00', BitString.from_int(0, 8))
128 self.assert_error(lambda: BitString.from_int(0, 0.1))
129 self.assert_length_value(8, b'\x00', BitString.from_int(0, 1.))
130 self.assert_length_value(1, b'\x00', BitString.from_int('0x0', 1))
131 self.assert_length_value(7, b'\x00', BitString.from_int('0x0', 7))
132 self.assert_length_value(8, b'\x00', BitString.from_int('0x0', 8))
133 self.assert_length_value(1, b'\x00', BitString.from_int('0x0', 0.1))
134 self.assert_length_value(8, b'\x00', BitString.from_int('0x0', 1.))
135 self.assert_length_value(16, b'\x34\x12', BitString.from_int(0x1234, 16))
136 self.assert_length_value(16, b'\x34\x12', BitString.from_int('0x1234', 16))
137 self.assert_length_value(16, b'\x12\x34', BitString.from_int('1234x0', 16))
138 self.assert_length_value(16, b'\x34\x12', BitString.from_int('4660', 16))
139 self.assert_length_value(16, b'\x34\x12', BitString.from_int('0d4660', 16))
140 self.assert_length_value(16, b'\x12\x34', BitString.from_int('4660d0', 16))
141
143 b = BitString.from_int32(0xabcd1234)
144 assert str(b) == '00101100 01001000 10110011 11010101b0/32', str(b)
145 b = BitString.from_int('0b110')
146 assert str(b) == '011b0/3', str(b)
147
149 #basicConfig(level=DEBUG)
150 self.assert_length_value(12, b'\x00\x0c', ~BitString.from_int('0x3ff'))
151
153 acc = BitString()
154 for i in range(8):
155 acc += BitString.from_int('0o' + str(i))
156 # >>> hex(0o76543210)
157 # '0xfac688'
158 self.assert_length_value(24, b'\x88\xc6\xfa', acc)
159 acc = BitString()
160 for i in range(7):
161 acc += BitString.from_int('0o' + str(i))
162 self.assert_length_value(21, b'\x88\xc6\x1a', acc)
163
165 a = BitString.from_int('01001100011100001111b0')
166 b = a[:]
167 assert a == b, (a, b)
168 b = a[0:]
169 assert a == b, (a, b)
170 b = a[-1::-1]
171 assert BitString.from_int('11110000111000110010b0') == b, b
172 b = a[0]
173 assert BitString.from_int('0b0') == b, (b, str(b), BitString.from_int('0b0'))
174 b = a[1]
175 assert BitString.from_int('1b0') == b, b
176 b = a[0:2]
177 assert BitString.from_int('01b0') == b, b
178 b = a[0:2]
179 assert BitString.from_int('0b10') == b, b
180 b = a[-5:]
181 assert BitString.from_int('01111b0') == b, b
182 b = a[-1:-6:-1]
183 assert BitString.from_int('11110b0') == b, b
184 b = a[1:-1]
185 assert BitString.from_int('100110001110000111b0') == b, b
186
188 if stop == None:
189 stop = start
190 result = BitString.from_int(start, length=length).to_int()
191 assert result == stop, (result, stop)
192 if length is not None:
193 assert len(result) == length, (result, length)
194
196 self.assert_round_trip(0)
197 self.assert_round_trip(1)
198 self.assert_round_trip(1, length=1)
199 self.assert_round_trip(467)
200 self.assert_round_trip(467, length=16)
201 self.assert_round_trip(467, length=19)
202
203
205
207 table = swap_table()
208 assert table[0x0f] == 0xf0, hex(table[0x0f])
209 assert table[0xff] == 0xff
210 assert table[0] == 0
211 assert table[10] == 80, table[10]
212
| Home | Trees | Indices | Help |
|---|
| Generated by Epydoc 3.0.1 on Sat Jun 9 21:51:00 2012 | http://epydoc.sourceforge.net |