forked from Varbin/xtea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testCounter.py
53 lines (39 loc) · 1.83 KB
/
testCounter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import unittest
from counter import Counter, to_bytes, from_bytes
class TestIntToBytes(unittest.TestCase):
def testLittleEndian(self):
result = to_bytes(67305985, 4, 'little')
expected = b'\x01\x02\x03\x04'
self.assertEqual(result, expected)
def testBigEndian(self):
result = to_bytes(16909060, 4, 'big')
expected = b'\x01\x02\x03\x04'
self.assertEqual(result, expected)
def testStrangeByteOrder(self):
with self.assertRaises(ValueError):
to_bytes(100, 4, 'banana')
class TestBytesToInt(unittest.TestCase):
def testLittleEndian(self):
result = from_bytes(b'\x01\x02\x03\x04', 'little')
expected = 67305985
self.assertEqual(result, expected)
def testBigEndian(self):
result = from_bytes(b'\x01\x02\x03\x04', 'big')
expected = 16909060
self.assertEqual(result, expected)
def testStrangeByteOrder(self):
with self.assertRaises(ValueError):
from_bytes(b'\x01\x02\x03\x04', 'banana')
class TestCounter(unittest.TestCase):
def testLittleEndian(self):
counter = Counter(b'\x00\x00\x00\x00\x00\x00\x00\x00', 'little')
self.assertEqual(counter(), b'\x00\x00\x00\x00\x00\x00\x00\x00')
self.assertEqual(counter(), b'\x01\x00\x00\x00\x00\x00\x00\x00')
def testBigEndian(self):
counter = Counter(b'\x00\x00\x00\x00\x00\x00\x00\x00', 'big')
self.assertEqual(counter(), b'\x00\x00\x00\x00\x00\x00\x00\x00')
self.assertEqual(counter(), b'\x00\x00\x00\x00\x00\x00\x00\x01')
def testMaxValue(self):
counter = Counter(b'\xff\xff\xff\xff\xff\xff\xff\xff', 'little')
self.assertEqual(counter(), b'\xff\xff\xff\xff\xff\xff\xff\xff')
self.assertEqual(counter(), b'\x00\x00\x00\x00\x00\x00\x00\x00')