Coverage for test/d7a/alp/operands/test_query.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 

24from d7a.alp.operands.offset import Offset 

25from d7a.alp.operands.query import QueryOperand, QueryType, ArithComparisonType, ArithQueryParams 

26 

27 

28class TestQuery(unittest.TestCase): 

29 def test_arith_comp_with_value_byte_generation(self): 

30 query = QueryOperand( 

31 type=QueryType.ARITH_COMP_WITH_VALUE, 

32 mask_present=False, 

33 params=ArithQueryParams(signed_data_type=False, comp_type=ArithComparisonType.GREATER_THAN), 

34 compare_length = Length(1), 

35 compare_value=[25], 

36 file_a_offset=Offset(id=32, offset=Length(1)) 

37 ) 

38 

39 bytes = bytearray(query) 

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

41 self.assertEqual(bytes[0], 0x44) 

42 self.assertEqual(bytes[1], 0x01) 

43 self.assertEqual(bytes[2], 25) 

44 self.assertEqual(bytes[3], 0x20) 

45 self.assertEqual(bytes[4], 0x01) 

46 

47 

48 def test_arith_comp_with_value_parsing(self): 

49 bytes = [ 

50 0x44, # arith comp with value, no mask, unsigned, > 

51 0x01, # compare length 

52 25, # compare value 

53 0x20, 0x01 # file offset 

54 ] 

55 

56 query = QueryOperand.parse(ConstBitStream(bytes=bytes)) 

57 self.assertEqual(query.type, QueryType.ARITH_COMP_WITH_VALUE) 

58 self.assertEqual(query.compare_length, 1) 

59 self.assertEqual(query.mask_present, False) 

60 self.assertEqual(query.params.signed_data_type, False) 

61 self.assertEqual(query.params.comp_type, ArithComparisonType.GREATER_THAN) 

62 self.assertEqual(query.compare_value, [25]) 

63 self.assertEqual(query.file_a_offset.id, 32) 

64 self.assertEqual(query.file_a_offset.offset.value, 1)