def test_parser__parse_zp_field(): input_lines = [ 'Z_p', ' 5', '3 ', ' 4 ', 'a ( 1 , 2) (3, 4 )', 'm (3, 4) 3', 'm 2 (2, 1)', ] parser = Parser() actual = parser.parse(iter(input_lines)) expected = TaskRunnerConfig( field_type=FieldType.Z_p, field_args=[5], curve_args=[3, 4], task_configs=[ TaskConfig( task_type=TaskType.ADD, points=(Point(1, 2), Point(3, 4)), ), TaskConfig( task_type=TaskType.MUL, points=(Point(3, 4), ), scalar=3, ), TaskConfig( task_type=TaskType.MUL, points=(Point(2, 1), ), scalar=2, ), ], ) assert actual == expected
def test_parser__get_number_base_metric(): input_lines = [ 'Z_p', '0x5', '0o3', '0b100 ', 'a ( 0b1 , 0xAA) (3, 0x4 )', ] parser = Parser() actual = parser.parse(iter(input_lines)) expected = TaskRunnerConfig( field_type=FieldType.Z_p, field_args=[5], curve_args=[3, 4], task_configs=[ TaskConfig( task_type=TaskType.ADD, points=(Point(1, 170), Point(3, 4)), ), ], ) assert actual == expected assert metrics.number_base == { 2: 2, 8: 1, 10: 1, 16: 3, }
def test_point_formatter(point_formatter): polynomial_point = Point( x=Polynomial([0., 0., 1.]), y=Polynomial([1., 1., 1.]), ) assert point_formatter.format(polynomial_point, {}) == '(4, 7)' int_point = Point(x=14, y=27) assert point_formatter.format(int_point, {}) == '(14, 27)'
def test_task_result_formatter(task_result_formatter): task_result = TaskResult( task_config=TaskConfig( task_type=TaskType.ADD, points=(Point(1, 1), Point(3, 5)), ), result=Point(4, 6), ) assert task_result_formatter.format(task_result, {}) == '(1, 1) + (3, 5) = (4, 6)'
def test_parser__parse_gf__irreducible_polynomial(irreducible_polynomial: str, expected_field_args: list): input_lines = [ 'GF(2^m)', irreducible_polynomial, '1', '2 ', ' 3', ' 4', '5', 'a ( 1 , 2) (3, 4 )', 'm (3, 4) 3', 'm 2 (2, 1)', ] parser = Parser() actual = parser.parse(iter(input_lines)) expected = TaskRunnerConfig( field_type=FieldType.GF, field_args=expected_field_args, curve_args=[ Polynomial([1.]), Polynomial([0., 1.]), Polynomial([1., 1.]), Polynomial([0., 0., 1.]), Polynomial([1., 0., 1.]), ], task_configs=[ TaskConfig( task_type=TaskType.ADD, points=( Point(Polynomial([1.]), Polynomial([0., 1.])), Point(Polynomial([1., 1.]), Polynomial([0., 0., 1.])), ), ), TaskConfig( task_type=TaskType.MUL, points=(Point( Polynomial([1., 1.]), Polynomial([0., 0., 1.]), ), ), scalar=3, ), TaskConfig( task_type=TaskType.MUL, points=(Point( Polynomial([0., 1.]), Polynomial([1.]), ), ), scalar=2, ), ], ) assert actual == expected
def test_task_config_formatter(task_config_formatter): task_config_add = TaskConfig( task_type=TaskType.ADD, points=(Point(1, 1), Point(3, 5)), ) assert task_config_formatter.format(task_config_add, {}) == '(1, 1) + (3, 5)' task_config_mul = TaskConfig( task_type=TaskType.MUL, points=(Point(1, 2), ), scalar=101, ) assert task_config_formatter.format(task_config_mul, {}) == '101 * (1, 2)'
def format(self, item: Point, context: Dict[str, Any]) -> str: if item.is_infinite(): return 'O' formatter = self._registry.get(type(item.x)) x_str = formatter.format(item.x, context) y_str = formatter.format(item.y, context) return f'({x_str}, {y_str})'
def _parse_task_operand(self, operand: str) -> Union[int, Point[T]]: if '(' not in operand: return _parse_int(operand) match = self.POINT_PATTERN.match(operand) if match is None: raise ParserError(f'Неверный формат точки: {operand}') return Point( x=self._parse_point_operand_function(match.group(1)), y=self._parse_point_operand_function(match.group(2)), )
from src.polynomial.polynomial import polyone from src.polynomial.polynomial import polyzero def pone() -> Polynomial: return Polynomial(polyone) def pzero() -> Polynomial: return Polynomial(polyzero) @pytest.mark.parametrize( 'first_point, second_point, result', [ (Point(None, None), Point(1, 1), Point(1, 1)), (Point(1, 1), Point(None, None), Point(1, 1)), (Point(1, 1), Point(1, -1), Point.infinity()), (Point(1, 2), Point(2, 1), Point(1, 1)), (Point(1, 2), Point(1, 2), Point(2, 2)), ], ) def test_zp_curve__add(zp_curve, first_point, second_point, result): assert zp_curve.add(first_point, second_point) == result @pytest.mark.parametrize( 'first_point, scalar, result', [ (Point(None, None), 2, Point(None, None)), (Point(1, 2), 2, Point(2, 2)),