Coverage for test/d7a/phy/test_channel_header.py: 100%

31 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.phy.channel_header import ChannelHeader, ChannelCoding, ChannelClass, ChannelBand 

24 

25 

26class TestChannelHeader(unittest.TestCase): 

27 def test_validation_ok(self): 

28 ch = ChannelHeader(channel_coding=ChannelCoding.PN9, 

29 channel_class=ChannelClass.NORMAL_RATE, 

30 channel_band=ChannelBand.BAND_433) 

31 

32 def test_validation_channel_coding(self): 

33 def bad(): 

34 ch = ChannelHeader(channel_coding="wrong", 

35 channel_class=ChannelClass.NORMAL_RATE, 

36 channel_band=ChannelBand.BAND_433) 

37 

38 self.assertRaises(ValueError, bad) 

39 

40 def test_validation_channel_class(self): 

41 def bad(): 

42 ch = ChannelHeader(channel_coding=ChannelCoding.PN9, 

43 channel_class="wrong", 

44 channel_band=ChannelBand.BAND_433) 

45 

46 self.assertRaises(ValueError, bad) 

47 

48 def test_validation_channel_band(self): 

49 def bad(): 

50 ch = ChannelHeader(channel_coding=ChannelCoding.PN9, 

51 channel_class=ChannelClass.NORMAL_RATE, 

52 channel_band="wrong") 

53 

54 self.assertRaises(ValueError, bad) 

55 

56 def test_byte_generation(self): 

57 expected = [ 

58 0b00101000 

59 ] 

60 

61 channel_header = ChannelHeader(channel_coding=ChannelCoding.PN9, 

62 channel_class=ChannelClass.NORMAL_RATE, 

63 channel_band=ChannelBand.BAND_433) 

64 bytes = bytearray(channel_header) 

65 for i in range(len(bytes)): 

66 self.assertEqual(expected[i], bytes[i]) 

67 

68 self.assertEqual(len(expected), len(bytes)) 

69 

70 def test_parse(self): 

71 bytes = [ 

72 0b00101000 

73 ] 

74 

75 ch = ChannelHeader.parse(ConstBitStream(bytes=bytes)) 

76 

77 self.assertEqual(ch.channel_coding, ChannelCoding.PN9) 

78 self.assertEqual(ch.channel_class, ChannelClass.NORMAL_RATE) 

79 self.assertEqual(ch.channel_band, ChannelBand.BAND_433)