Coverage for test/d7a/alp/operands/test_file.py: 90%
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# author: Christophe VG <contact@christophe.vg>
21# unit tests for the D7 File {*} Operands
23import unittest
25from d7a.alp.operands.file import Data
27class TestData(unittest.TestCase):
28 def test_default_data_constructor(self):
29 Data()
31 def test_data_bad_offset(self):
32 def bad(): Data(Data())
33 self.assertRaises(ValueError, bad)
35 def test_data_length(self):
36 d = Data(data=[0xd7, 0x04, 0x00])
37 self.assertEqual(d.length.value, 3)
38 self.assertEqual(len(d), 3)
40 def test_byte_generation(self):
41 bytes = bytearray(Data())
42 self.assertEqual(len(bytes), 3)
43 self.assertEqual(bytes[0], int('00000000', 2)) # offset
44 self.assertEqual(bytes[1], int('00000000', 2)) # offset
45 self.assertEqual(bytes[2], int('00000000', 2))
47 bytes = bytearray(Data(data=[0x01,0x02,0x03,0x04]))
48 self.assertEqual(len(bytes), 7)
49 self.assertEqual(bytes[0], int('00000000', 2)) # offset
50 self.assertEqual(bytes[1], int('00000000', 2)) # offset
51 self.assertEqual(bytes[2], int('00000100', 2)) # length = 4
52 self.assertEqual(bytes[3], int('00000001', 2))
53 self.assertEqual(bytes[4], int('00000010', 2))
54 self.assertEqual(bytes[5], int('00000011', 2))
55 self.assertEqual(bytes[6], int('00000100', 2))
57if __name__ == '__main__':
58 for case in [TestOffset, TestData]:
59 suite = unittest.TestLoader().loadTestsFromTestCase(case)
60 unittest.TextTestRunner(verbosity=2).run(suite)