Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
106 lines (90 sloc) 4.72 KB
import unittest
from bit_twiddler import BitTwiddler, BTException
class TestBasics(unittest.TestCase):
def setUp(self):
self.bt = BitTwiddler()
self.input = self.bt.processExpr
self.getvar = self.bt.getVar
def test_conversion(self):
self.input('dec32')
self.assertEqual(self.bt.base, 'dec')
self.assertEqual(self.bt.bit_width, 32)
self.assertRaises(BTException, self.input, 'dec0')
self.assertRaises(BTException, self.input, 'dec65')
self.assertEqual(self.input('maxval'), '4294967295')
self.assertEqual(self.input('bin8 32'), '00100000')
self.assertEqual(self.input('bin8 256'), '00000000')
self.assertEqual(self.input('bin8 288'), '00100000')
self.input('bin8')
self.assertEqual(self.input('0d32'), '00100000')
self.assertEqual(self.input('0d256'), '00000000')
self.assertEqual(self.input('0d288'), '00100000')
self.assertEqual(self.input('dec16 00100000'), '32')
self.assertEqual(self.input('dec16 100000000'), '256')
self.assertEqual(self.input('dec16 100100000'), '288')
self.input('hex16')
self.assertEqual(self.input('maxval'), 'ffff')
self.assertEqual(self.input('0d288'), '0120')
self.assertEqual(self.input('bin16 0x0f0f'), '0000111100001111')
self.assertRaises(BTException, self.input, '0f0f')
def test_assignment(self):
self.input('dec32')
self.input('a = 42')
self.assertEqual(self.getvar('a'), 42)
self.input('b = a')
self.assertEqual(self.getvar('b'), self.getvar('a'))
self.assertRaises(BTException, self.input, 'c')
self.assertRaises(BTException, self.input, 'and = 42')
self.assertRaises(BTException, self.input, 'maxval = 42')
def test_expression(self):
self.input('bin8')
self.assertEqual(self.input('10 + 01'), '00000011')
self.assertEqual(self.input('10+01'), '00000011')
self.assertEqual(self.input('0d2+0d1'), '00000011')
self.assertEqual(self.input('10 or 01 and 01'), '00000011')
self.assertEqual(self.input('(10 or 01) and 01'), '00000001')
self.assertEqual(self.input('(((((10 or 01))))) and 01'), '00000001')
self.assertEqual(self.input('10 - 11'), '11111111')
self.assertRaises(BTException, self.input, '+')
self.assertRaises(BTException, self.input, '10 + 01 10')
def test_pattern(self):
self.input('bin8')
self.assertEqual(self.input('{0}'), '00000000')
self.assertEqual(self.input('{01}'), '01010101')
self.assertEqual(self.input('{0x3}'), '00110011')
self.assertEqual(self.input('{0xf0}'), '11110000')
self.assertEqual(self.input('{0xff00}'), '00000000')
self.assertRaises(BTException, self.input, '{}')
self.assertRaises(BTException, self.input, '{0d8}')
def test_tables(self):
self.input('dec8')
self.input('a = [12, 14, 7, 6, 9]')
self.assertEqual(self.input('a'), '[12, 14, 7, 6, 9]')
self.assertEqual(int(self.input('len a')), 5)
self.assertEqual(int(self.input('len []')), 0)
self.assertEqual(self.input('a[2]'), '7')
self.assertEqual(self.input('a lookup 2'), '7')
self.assertEqual(self.input('[1, 2, 3][2]'), '3')
self.assertEqual(self.input('a * 2'), '[24, 28, 14, 12, 18]')
self.input('b = [a, a * 2, a * 3, a * 4]')
self.assertEqual(int(self.input('len b')), 20)
self.assertEqual(self.input('b[16]'), '56')
self.input('c = a + [8, 6, 13, 14, 11]')
self.assertEqual(self.input('c[2]'), '20')
self.assertEqual(int(self.input('len c')), 5)
self.assertRaises(BTException, self.input, 'b[20]')
self.assertRaises(BTException, self.input, 'a[1, 2, 3]')
self.assertRaises(BTException, self.input, '2 lookup a')
self.assertRaises(BTException, self.input, '[0, 1] + [1, 2, 3]')
def test_big_table(self):
self.input('dec32')
self.input('a = [1, 1, 2, 3, 5, 8, 13, 21]')
self.input('b = [a, a, a * 2, a * 3, a * 5, a * 8, a * 13, a * 21]')
self.input('c = [b, b, b * 2, b * 3, b * 5, b * 8, b * 13, b * 21]')
self.input('d = [c, c, c * 2, c * 3, c * 5, c * 8, c * 13, c * 21]')
self.input('e = [d, d, d * 2, d * 3, d * 5, d * 8, d * 13, d * 21]')
self.input('r = [e, e, e * 2, e * 3, e * 5, e * 8, e * 13, e * 21]')
self.assertEqual(int(self.input('len r')), 8 ** 6)
self.assertEqual(int(self.input('r[8**6 - 1]')), 21 ** 6)
if __name__ == '__main__':
unittest.main(verbosity = 2)
Something went wrong with that request. Please try again.