hexathon

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

test_basic.py (2030B)


      1 import unittest
      2 
      3 import hexathon
      4 
      5 
      6 class HexTest(unittest.TestCase):
      7 
      8     def test_valid(self):
      9         with self.assertRaises(ValueError):
     10             hexathon.valid('0x')
     11 
     12         with self.assertRaises(ValueError):
     13             hexathon.valid('ag')
     14 
     15         with self.assertRaises(ValueError):
     16             hexathon.valid('abc')
     17 
     18         hexathon.valid('9876543210fedcba')
     19 
     20 
     21     def test_0x(self):
     22         self.assertEqual(hexathon.strip_0x('0xabcd'), 'abcd')
     23         self.assertEqual(hexathon.add_0x('abcd'), '0xabcd')
     24         self.assertEqual(hexathon.strip_0x('0x000abcd'), '0000abcd')
     25         self.assertEqual(hexathon.add_0x('000abcd'), '0x0000abcd')
     26 
     27 
     28     def test_0x_compact(self):
     29         self.assertEqual(hexathon.strip_0x('0x000abcd', compact_value=True), 'abcd')
     30         self.assertEqual(hexathon.add_0x('000abcd', compact_value=True), '0xabcd')
     31 
     32 
     33     def test_0x_nopad(self):
     34         v = '0xabcde'
     35         r = hexathon.strip_0x(v)
     36         self.assertEqual(r, '0abcde')
     37         r = hexathon.strip_0x(v, pad=False)
     38         self.assertEqual(r, 'abcde')
     39 
     40 
     41     def test_even(self):
     42         self.assertEqual(hexathon.even('aBc'), '0aBc')
     43 
     44 
     45     def test_uniform(self):
     46         self.assertEqual(hexathon.uniform('aBc'), '0abc')
     47 
     48 
     49     def test_unpad(self):
     50         self.assertEqual(hexathon.unpad('000abc'), '0abc')
     51 
     52 
     53     def test_compact(self):
     54         self.assertEqual(hexathon.compact('000abc'), 'abc')
     55         self.assertEqual(hexathon.compact('abc'), 'abc')
     56 
     57     
     58     def test_same(self):
     59         x = 'deadbeef'
     60         y = '0xdeadbeef'
     61         self.assertTrue(hexathon.same(x, y))
     62 
     63 
     64         x = '0deadbeef'
     65         y = '00deadbeef'
     66         self.assertTrue(hexathon.same(x, y))
     67         self.assertFalse(hexathon.same(x, y, pad=False))
     68         self.assertTrue(hexathon.same(x, y, compact_value=True))
     69 
     70         x = '0x'
     71         y = ''
     72         with self.assertRaises(ValueError):
     73             hexathon.same(x, y)
     74         self.assertTrue(hexathon.same(x, y, allow_empty=True))
     75 
     76 
     77 if __name__ == '__main__':
     78     unittest.main()