From 1e2c277da02183116be34f026d20a8a7ae862d39 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Sat, 28 Oct 2023 22:01:34 -0700 Subject: [PATCH] Add test for strict bitwise seeks This shows which drivers use negative and unnecessary seeks. These are all marked as XFAIL right now because there are *so* many offenders. --- chirp/bitwise.py | 28 +++++++++++++++++----------- tests/__init__.py | 1 + tests/test_edges.py | 24 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/chirp/bitwise.py b/chirp/bitwise.py index 3dfb113d..98720a14 100644 --- a/chirp/bitwise.py +++ b/chirp/bitwise.py @@ -732,7 +732,7 @@ class structDataElement(DataElement): s += " %15s: %s%s" % (prop, repr(self._generators[prop]), os.linesep) s += "} %s (%i bytes at 0x%04X)%s" % (self._name, - self.size() / 8, + self.size() // 8, self._offset, os.linesep) return s @@ -823,8 +823,6 @@ class structDataElement(DataElement): class Processor: - strict = False - _types = { "u8": u8DataElement, "u16": u16DataElement, @@ -859,7 +857,7 @@ class Processor: self._generators[name] = gen def do_bitfield(self, dtype, bitfield): - bytes = self._types[dtype](self._data, 0).size() / 8 + bytes = self._types[dtype](self._data, 0).size() // 8 bitsleft = bytes * 8 for _bitdef, defn in bitfield: @@ -916,7 +914,7 @@ class Processor: self._offset += int((i+1) % 8 == 0) else: gen = self._types[dtype](self._data, self._offset) - self._offset += (gen.size() / 8) + self._offset += (gen.size() // 8) res.append(gen) if count == 1: @@ -965,16 +963,24 @@ class Processor: else: raise Exception("Internal error: What is `%s'?" % struct[0][0]) + def assert_negative_seek(self, message): + warnings.warn(message, DeprecationWarning, stacklevel=6) + + def assert_unnecessary_seek(self, message): + warnings.warn(message, DeprecationWarning, stacklevel=6) + def parse_directive(self, directive): name = directive[0][0] value = directive[0][1][0][1] if name == "seekto": - if self._offset == int(value, 0): - if self.strict: - raise SyntaxError('Unnecessary #seekto %s' % value) - else: - LOG.warning('Unnecessary #seekto %s' % value) - self._offset = int(value, 0) + target = int(value, 0) + if self._offset == target: + self.assert_unnecessary_seek('Unnecessary #seekto %s' % value) + elif target < self._offset: + self.assert_negative_seek( + 'Invalid negative seek from 0x%04x to 0x%04x' % ( + self._offset, target)) + self._offset = target elif name == "seek": self._offset += int(value, 0) elif name == "printoffset": diff --git a/tests/__init__.py b/tests/__init__.py index 7640296a..d076136f 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -64,6 +64,7 @@ def _load_tests(loader, tests, pattern, suite=None): pattern = None driver_test_cases = (test_edges.TestCaseEdges, + test_edges.TestBitwiseStrict, test_brute_force.TestCaseBruteForce, test_banks.TestCaseBanks, test_detect.TestCaseDetect, diff --git a/tests/test_edges.py b/tests/test_edges.py index ed9dceeb..2280819c 100644 --- a/tests/test_edges.py +++ b/tests/test_edges.py @@ -1,3 +1,7 @@ +import pytest +from unittest import mock + +from chirp import bitwise from chirp import chirp_common from chirp import errors from tests import base @@ -163,3 +167,23 @@ class TestCaseEdges(base.DriverTest): self.assertEqual(m2.name.rstrip(), m2.name, 'Radio set and returned a memory with trailing ' 'whitespace in the name') + + +class TestBitwiseStrict(base.DriverTest): + def setUp(self): + pass + + def _raise(self, message): + raise SyntaxError(message) + + @pytest.mark.xfail(strict=False) + @mock.patch.object(bitwise.Processor, 'assert_negative_seek', + side_effect=_raise) + def test_bitwise_negative_seek(self, mock_assert): + super().setUp() + + @pytest.mark.xfail(strict=False) + @mock.patch.object(bitwise.Processor, 'assert_unnecessary_seek', + side_effect=_raise) + def test_bitwise_unnecessary_seek(self, mock_assert): + super().setUp()