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

28 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 

24from d7a.phy.channel_id import ChannelID 

25 

26 

27class TestChannelID(unittest.TestCase): 

28 def test_byte_generation(self): 

29 expected = [ 

30 0b00101000, 

31 0, 16 # channel_id 

32 ] 

33 

34 channel_id = ChannelID( 

35 ChannelHeader(channel_coding=ChannelCoding.PN9, channel_class=ChannelClass.NORMAL_RATE,channel_band=ChannelBand.BAND_433), 

36 16 

37 ) 

38 

39 bytes = bytearray(channel_id) 

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

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

42 

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

44 

45 def test_parse(self): 

46 bytes = [ 

47 0b00101000, # header 

48 0, 16 # channel_id 

49 ] 

50 

51 ch = ChannelID.parse(ConstBitStream(bytes=bytes)) 

52 

53 self.assertEqual(ch.channel_header.channel_coding, ChannelCoding.PN9) 

54 self.assertEqual(ch.channel_header.channel_class, ChannelClass.NORMAL_RATE) 

55 self.assertEqual(ch.channel_header.channel_band, ChannelBand.BAND_433) 

56 self.assertEqual(ch.channel_index, 16) 

57 

58 def test_generate_channel_id_string(self): 

59 ch = ChannelID( 

60 channel_header=ChannelHeader( 

61 channel_class=ChannelClass.NORMAL_RATE, 

62 channel_coding=ChannelCoding.PN9, 

63 channel_band=ChannelBand.BAND_433 

64 ), 

65 channel_index=16 

66 ) 

67 

68 self.assertEqual(str(ch), "433NP016") 

69 

70 def test_parse_channel_id_string(self): 

71 s = "868LF048" 

72 expected_ch = ChannelID( 

73 channel_header=ChannelHeader( 

74 channel_class=ChannelClass.LO_RATE, 

75 channel_coding=ChannelCoding.FEC_PN9, 

76 channel_band=ChannelBand.BAND_868 

77 ), 

78 channel_index=48 

79 ) 

80 

81 ch = ChannelID.from_string(s) 

82 self.assertEqual(ch.channel_header, expected_ch.channel_header) 

83 self.assertEqual(ch.channel_index, expected_ch.channel_index)