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

36 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 

24from d7a.alp.operands.offset import Offset 

25 

26 

27class TestOffset(unittest.TestCase): 

28 def test_byte_generation(self): 

29 bytes = bytearray(Offset()) 

30 self.assertEqual(len(bytes), 2) 

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

32 self.assertEqual(bytes[1], int('00000000', 2)) 

33 

34 bytes = bytearray(Offset(id=0xFF)) 

35 self.assertEqual(len(bytes), 2) 

36 self.assertEqual(bytes[0], int('11111111', 2)) 

37 self.assertEqual(bytes[1], int('00000000', 2)) 

38 

39 bytes = bytearray(Offset(offset=Length(65120))) 

40 self.assertEqual(len(bytes), 4) 

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

42 self.assertEqual(bytes[1], int('10000000', 2)) 

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

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

45 

46 def test_parse(self): 

47 offset_bytes = [0x20, 0x01] 

48 offset = Offset.parse(ConstBitStream(bytes=offset_bytes)) 

49 self.assertEqual(offset.id, 32) 

50 self.assertEqual(offset.offset.value, 1) 

51 

52 def test_parse_two_bytes(self): 

53 offset_bytes = [0x20, 0x40, 0x41] 

54 offset = Offset.parse(ConstBitStream(bytes=offset_bytes)) 

55 self.assertEqual(offset.offset.value, 65) 

56 

57 def test_parse_three_bytes(self): 

58 offset_bytes = [0x01, 0x80, 0xfe, 0x60] 

59 offset = Offset.parse(ConstBitStream(bytes=offset_bytes)) 

60 self.assertEqual(offset.id, 1) 

61 self.assertEqual(offset.offset.value, 65120) 

62 

63 def test_to_str(self): 

64 Offset().__str__()