3 from cStringIO import StringIO
5 from StringIO import StringIO
11 class ElementSizeTests(unittest.TestCase):
12 def assert_roundtrip(self, value, length=None):
13 encoded = encode_element_size(value, length=length)
14 if length is not None:
15 self.assertEqual(length, len(encoded))
16 encoded_stream = StringIO(encoded)
17 self.assertEqual(value, read_element_size(encoded_stream)[0])
19 def test_unknown(self):
20 for length in xrange(1, 9):
21 self.assert_roundtrip(None, length=length)
23 def test_base_10(self):
24 for value in (10**exp for exp in xrange(1, 16)):
25 self.assert_roundtrip(value)
27 def test_base_2(self):
28 for value in (2**exp for exp in xrange(1, 56)):
29 self.assert_roundtrip(value)
31 def test_max_base_2(self):
32 for value in ((2**exp) - 2 for exp in xrange(1, 57)):
33 self.assert_roundtrip(value)
35 def test_random(self):
37 for value in (random.randint(0, maximum) for i in xrange(0, 10000)):
38 self.assert_roundtrip(value)
41 class ElementIDTests(unittest.TestCase):
55 def assert_roundtrip(self, value):
56 encoded = encode_element_id(value)
57 encoded_stream = StringIO(encoded)
58 self.assertEqual(value, read_element_id(encoded_stream)[0])
60 def test_ebml_ids(self):
61 for id_ in self.ebml_ids:
62 self.assert_roundtrip(id_)
65 class ValueTestCase(unittest.TestCase):
69 def assert_roundtrip(self, value, length=None):
70 if self.encoder is not None and self.reader is not None:
71 encoded = self.encoder(value, length)
72 if length is not None:
73 self.assertEqual(length, len(encoded))
74 encoded_stream = StringIO(encoded)
75 self.assertEqual(value, self.reader(encoded_stream, len(encoded)))
77 raise NotImplementedError
80 class UnsignedIntegerTests(ValueTestCase):
81 encoder = staticmethod(encode_unsigned_integer)
82 reader = staticmethod(read_unsigned_integer)
85 def test_random(self):
86 for value in (random.randint(0, self.maximum) for i in xrange(0, 10000)):
87 self.assert_roundtrip(value)
89 def test_random_longer(self):
90 for value in (random.randint(0, (self.maximum / (2**32))) for i in xrange(0, 10000)):
91 self.assert_roundtrip(value, length=8)
93 def test_maximum(self):
94 self.assert_roundtrip(self.maximum)
97 class SignedIntegerTests(ValueTestCase):
98 encoder = staticmethod(encode_signed_integer)
99 reader = staticmethod(read_signed_integer)
101 maximum = (2**63) - 1
103 def test_random(self):
104 for value in (random.randint(self.minimum, self.maximum) for i in xrange(0, 10000)):
105 self.assert_roundtrip(value)
107 def test_random_longer(self):
108 for value in (random.randint((self.minimum / (2**32)), (self.maximum / (2**32))) for i in xrange(0, 10000)):
109 self.assert_roundtrip(value, length=8)
111 def test_minimum(self):
112 self.assert_roundtrip(self.minimum)
114 def test_maximum(self):
115 self.assert_roundtrip(self.maximum)
118 class FloatTests(ValueTestCase):
119 encoder = staticmethod(encode_float)
120 reader = staticmethod(read_float)
122 def test_random(self):
123 raise NotImplementedError
126 class StringTests(ValueTestCase):
127 encoder = staticmethod(encode_string)
128 reader = staticmethod(read_string)
129 letters = ''.join(chr(i) for i in xrange(1, 127))
131 def test_random(self):
132 for length in (random.randint(0, 2**10) for i in xrange(0, 1000)):
133 astring = ''.join(random.sample(self.letters * ((length // len(self.letters)) + 1), length))
134 self.assert_roundtrip(astring)
135 self.assert_roundtrip(astring, length=length*2)
138 class UnicodeStringTests(ValueTestCase):
139 encoder = staticmethod(encode_unicode_string)
140 reader = staticmethod(read_unicode_string)
141 letters = u''.join(unichr(i) for i in xrange(1, sys.maxunicode + 1))
143 def test_random(self):
144 for length in (random.randint(0, 2**10) for i in xrange(0, 1000)):
145 ustring = u''.join(random.sample(self.letters * ((length // len(self.letters)) + 1), length))
146 ustring = ustring.encode('utf_8').decode('utf_8')
147 self.assert_roundtrip(ustring)
148 self.assert_roundtrip(ustring, length=length*5)
151 class DateTests(ValueTestCase):
152 encoder = staticmethod(encode_date)
153 reader = staticmethod(read_date)
155 def test_random(self):
156 raise NotImplementedError
159 if __name__ == '__main__':