Package lepl :: Package bin :: Package _test :: Module bits
[hide private]
[frames] | no frames]

Source Code for Module lepl.bin._test.bits

  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       
47 - class IntTest(TestCase):
48
49 - def test_int(self):
50 one = Int(1, 1) 51 assert type(one) == Int 52 assert 1 == one 53 assert len(one) == 1 54 assert str(one) == '1' 55 assert repr(one) == 'Int(1,1)' 56 assert 3 * one == 3
57 58
59 - class BitStringTest(TestCase):
60
61 - def test_lengths(self):
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
77 - def assert_error(self, thunk):
78 try: 79 thunk() 80 assert False, 'expected error' 81 except: 82 pass
83
84 - def assert_length_value(self, length, value, b):
85 assert len(b) == length, (len(b), length) 86 assert b.to_bytes() == value, (b.to_bytes(), value, b)
87
88 - def test_from_byte(self):
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
95 - def test_from_bytearray(self):
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
100 - def test_from_int(self):
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
118 - def test_from_sequence(self):
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
142 - def test_str(self):
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
148 - def test_invert(self):
149 #basicConfig(level=DEBUG) 150 self.assert_length_value(12, b'\x00\x0c', ~BitString.from_int('0x3ff'))
151
152 - def test_add(self):
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
164 - def test_get_item(self):
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
187 - def assert_round_trip(self, start, stop=None, length=None):
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
195 - def test_to_int(self):
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
204 - class SwapTableTest(TestCase):
205
206 - def test_swap(self):
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