hexathon

Common and uncommon hex string operations for python3
git clone git://git.defalsify.org/python-hexathon.git
Info | Log | Files | Refs | LICENSE

test_int.py (1264B)


      1 # standard imports
      2 import unittest
      3 
      4 # local imports
      5 import hexathon
      6 
      7 class TestInt(unittest.TestCase):
      8     
      9     def test_1_bytes_min(self):
     10 
     11         v = 129
     12         r = hexathon.int_to_minbytes(v)
     13         self.assertEqual(r, b'\x81')
     14 
     15         v = 1025
     16         r = hexathon.int_to_minbytes(v)
     17         self.assertEqual(r, b'\x04\x01')
     18 
     19         v = 65536
     20         r = hexathon.int_to_minbytes(v)
     21         self.assertEqual(r, b'\x01\x00\x00')
     22 
     23 
     24     def test_2_hex_min(self):
     25 
     26         v = 129
     27         r = hexathon.int_to_minhex(v)
     28         self.assertEqual(r, '81')
     29 
     30         v = 1025
     31         r = hexathon.int_to_minhex(v)
     32         self.assertEqual(r, '0401')
     33 
     34         v = 65536
     35         r = hexathon.int_to_minhex(v)
     36         self.assertEqual(r, '010000')
     37 
     38 
     39     def test_to_int(self):
     40 
     41         v = '123'
     42         r = hexathon.to_int(v)
     43         self.assertEqual(r, 291)
     44 
     45         v = '0123'
     46         r = hexathon.to_int(v)
     47         self.assertEqual(r, 291)
     48 
     49         with self.assertRaises(ValueError):
     50             hexathon.to_int(v, need_prefix=True)
     51 
     52         r = hexathon.to_int(hexathon.add_0x(v), need_prefix=True)
     53         self.assertEqual(r, 291)
     54 
     55         with self.assertRaises(ValueError):
     56             hexathon.to_int('foo')
     57 
     58 
     59 if __name__ == '__main__':
     60     unittest.main()