Coverage for test/d7a/alp/operands/test_length.py: 100%

26 statements  

« 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# 

19import unittest 

20 

21from bitstring import ConstBitStream 

22 

23from d7a.alp.operands.length import Length 

24 

25 

26class TestLength(unittest.TestCase): 

27 def test_byte_generation(self): 

28 bytes = bytearray(Length()) 

29 self.assertEqual(len(bytes), 1) 

30 self.assertEqual(bytes[0], int('00000000', 2)) 

31 

32 bytes = bytearray(Length(value=4)) 

33 self.assertEqual(len(bytes), 1) 

34 self.assertEqual(bytes[0], int('00000100', 2)) 

35 

36 bytes = bytearray(Length(value=65120)) 

37 self.assertEqual(len(bytes), 3) 

38 self.assertEqual(bytes[0], int('10000000', 2)) 

39 self.assertEqual(bytes[1], int('11111110', 2)) 

40 self.assertEqual(bytes[2], int('01100000', 2)) 

41 

42 def test_parsing(self): 

43 length_bytes = [0x01] 

44 length = Length.parse(ConstBitStream(bytes=length_bytes)) 

45 self.assertEqual(length.value, 1) 

46 

47 length_bytes = [0x40, 0x41] 

48 length = Length.parse(ConstBitStream(bytes=length_bytes)) 

49 self.assertEqual(length.value, 65) 

50 

51 length_bytes = [0xC0, 0x41, 0x10, 0x00] 

52 length = Length.parse(ConstBitStream(bytes=length_bytes)) 

53 self.assertEqual(length.value, 4263936)