Coverage for test/d7a/types/test_ct.py: 87%
31 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-24 08:03 +0200
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-24 08:03 +0200
1#
2# Copyright (c) 2015-2021 University of Antwerp, Aloxy NV.
3#
4# This file is part of pyd7a.
5# See https://github.com/Sub-IoT/pyd7a for further info.
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
20# unit tests for Compressed Time
22import unittest
24from d7a.types.ct import CT
26class TestCT(unittest.TestCase):
27 def test_default_constructor_is_zero(self):
28 t = CT()
29 self.assertEqual(int(t), 0)
31 def test_ct_construction(self):
32 for exp in [1, 7]:
33 for mant in [1, 31]:
34 try:
35 t = CT(exp=exp, mant=mant)
36 except ValueError:
37 self.fail("CT constructor raised ExceptionType unexpectedly " +
38 "for exp={0}, mant={1}".format(exp, mant))
40 def test_invalid_ct_constructions(self):
41 def bad(args, kwargs): CT(**kwargs)
42 self.assertRaises(ValueError, bad, [], { "exp" : -1 })
43 self.assertRaises(ValueError, bad, [], { "mant" : -1 })
44 self.assertRaises(ValueError, bad, [], { "exp" : 8 })
45 self.assertRaises(ValueError, bad, [], { "mant" : 32 })
47 def test_ct_conversion_to_int(self):
48 self.assertEqual(int(CT(1,1)), 4)
49 self.assertEqual(int(CT(2,2)), 32)
50 self.assertEqual(int(CT(3,3)), 192)
52 def test_byte_generation(self):
53 self.assertEqual( bytearray(CT(1, 1))[0], int('00100001', 2))
54 self.assertEqual( bytearray(CT(7,31))[0], int('11111111', 2))
56 def test_compress(self):
57 self.assertEqual(1024, CT.compress(1024).decompress())
59if __name__ == '__main__':
60 suite = unittest.TestLoader().loadTestsFromTestCase(TestCT)
61 unittest.TextTestRunner(verbosity=2).run(suite)