From d7e27657f3df92aa7ddc4ea18304c9dc80800474 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Tue, 28 Mar 2023 23:23:50 +0800 Subject: [PATCH] add test for vector(using unittest) --- DR.py | 3 +- libs/pyglet_rs/__init__.py | 41 +++++++++++-- libs/pyglet_rs/src/src/lib.rs | 2 + libs/pyglet_rs/src/src/pymath.rs | 98 +++++++++++++++++++++++++++++++ libs/pyglet_rs/src/test/vector.py | 66 +++++++++++++++++++++ 5 files changed, 205 insertions(+), 5 deletions(-) create mode 100644 libs/pyglet_rs/src/test/vector.py diff --git a/DR.py b/DR.py index 4313519..d710ecd 100644 --- a/DR.py +++ b/DR.py @@ -51,7 +51,8 @@ def main() -> None: try: from libs.pyglet_rs import get_version_str print('pyglet_rs available:', get_version_str()) - + print('trying to patch pyglet_rs') + from libs.pyglet_rs import patch_vector except ImportError as e: print('pyglet_rs import error') traceback.print_exc() diff --git a/libs/pyglet_rs/__init__.py b/libs/pyglet_rs/__init__.py index f139b94..d2ec978 100644 --- a/libs/pyglet_rs/__init__.py +++ b/libs/pyglet_rs/__init__.py @@ -13,8 +13,9 @@ from .lib import (Sprite_rs, """ from .lib import * -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Union +# noinspection PyUnresolvedReferences __version__ = get_version_str() __all__ = ['patch_sprite', 'patch_vector', 'patch_matrix', 'patch_all', 'Sprite_rs', @@ -24,16 +25,48 @@ __all__ = ['patch_sprite', 'patch_vector', 'patch_matrix', 'patch_all', if TYPE_CHECKING: from pyglet.event import EventDispatcher + Number = Union[int, float] + + def get_version_str() -> str: ... class Sprite_rs(EventDispatcher): ... - class Vector2_rs: ... - class Vector3_rs: ... - class Vector4_rs: ... + + class Vector2_rs: + def __init__(self, x: Number, y: Number): ... + def __add__(self, other: "Vector2_rs") -> "Vector2_rs": ... + def __sub__(self, other: "Vector2_rs") -> "Vector2_rs": ... + def __mul__(self, other: "Vector2_rs") -> "Vector2_rs": ... + def __truediv__(self, other: "Vector2_rs") -> "Vector2_rs": ... + def __floordiv__(self, other: "Vector2_rs") -> "Vector2_rs": ... + def __repr__(self) -> str: ... + + + class Vector3_rs: + def __init__(self, x: Number, y: Number, z: Number): ... + def __add__(self, other: "Vector3_rs") -> "Vector3_rs": ... + def __sub__(self, other: "Vector3_rs") -> "Vector3_rs": ... + def __mul__(self, other: "Vector3_rs") -> "Vector3_rs": ... + def __truediv__(self, other: "Vector3_rs") -> "Vector3_rs": ... + def __floordiv__(self, other: "Vector3_rs") -> "Vector3_rs": ... + def __repr__(self) -> str: ... + + + class Vector4_rs: + def __init__(self, x: Number, y: Number, z: Number, w: Number): ... + def __add__(self, other: "Vector4_rs") -> "Vector4_rs": ... + def __sub__(self, other: "Vector4_rs") -> "Vector4_rs": ... + def __mul__(self, other: "Vector4_rs") -> "Vector4_rs": ... + def __truediv__(self, other: "Vector4_rs") -> "Vector4_rs": ... + def __floordiv__(self, other: "Vector4_rs") -> "Vector4_rs": ... + def __repr__(self) -> str: ... + class Matrix3_rs: ... + + class Matrix4_rs: ... diff --git a/libs/pyglet_rs/src/src/lib.rs b/libs/pyglet_rs/src/src/lib.rs index ad4f006..a64836c 100644 --- a/libs/pyglet_rs/src/src/lib.rs +++ b/libs/pyglet_rs/src/src/lib.rs @@ -24,5 +24,7 @@ fn module_init(_py: Python<'_>, m: &PyModule) -> PyResult<()> { m.add_class::()?; // vector m.add_class::()?; + m.add_class::()?; + m.add_class::()?; Ok(()) } diff --git a/libs/pyglet_rs/src/src/pymath.rs b/libs/pyglet_rs/src/src/pymath.rs index 3646d59..b4ea9d9 100644 --- a/libs/pyglet_rs/src/src/pymath.rs +++ b/libs/pyglet_rs/src/src/pymath.rs @@ -94,5 +94,103 @@ pub mod python_class { data: self.data.floordiv(&other.data), }; } + + fn __repr__(&self) -> String { + return format!("Vector2_rs({}, {})", self.data.x, self.data.y); + } + } + + #[pymethods] + impl PyVector3 { + #[new] + fn py_new(x: f64, y: f64, z: f64) -> Self { + return Self { + data: Vector3::new(x, y, z), + }; + } + + fn __add__(&self, other: &Self) -> Self { + return Self { + data: self.data + other.data, + }; + } + + fn __sub__(&self, other: &Self) -> Self { + return Self { + data: self.data - other.data, + }; + } + + fn __mul__(&self, other: &Self) -> Self { + return Self { + data: self.data * other.data, + }; + } + + fn __truediv__(&self, other: &Self) -> Self { + return Self { + data: self.data / other.data, + }; + } + + fn __floordiv__(&self, other: &Self) -> Self { + return Self { + data: self.data.floordiv(&other.data), + }; + } + + fn __repr__(&self) -> String { + return format!( + "Vector3_rs({}, {}, {})", + self.data.x, self.data.y, self.data.z + ); + } + } + + #[pymethods] + impl PyVector4 { + #[new] + fn py_new(x: f64, y: f64, z: f64, w: f64) -> Self { + return Self { + data: Vector4::new(x, y, z, w), + }; + } + + fn __add__(&self, other: &Self) -> Self { + return Self { + data: self.data + other.data, + }; + } + + fn __sub__(&self, other: &Self) -> Self { + return Self { + data: self.data - other.data, + }; + } + + fn __mul__(&self, other: &Self) -> Self { + return Self { + data: self.data * other.data, + }; + } + + fn __truediv__(&self, other: &Self) -> Self { + return Self { + data: self.data / other.data, + }; + } + + fn __floordiv__(&self, other: &Self) -> Self { + return Self { + data: self.data.floordiv(&other.data), + }; + } + + fn __repr__(&self) -> String { + return format!( + "Vector4_rs({}, {}, {}, {})", + self.data.x, self.data.y, self.data.z, self.data.w + ); + } } } diff --git a/libs/pyglet_rs/src/test/vector.py b/libs/pyglet_rs/src/test/vector.py new file mode 100644 index 0000000..7ede6ce --- /dev/null +++ b/libs/pyglet_rs/src/test/vector.py @@ -0,0 +1,66 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com +# All rights reserved +# ------------------------------- + +import random +import unittest + +from libs.pyglet_rs import Vector2_rs, Vector3_rs, Vector4_rs + + +# 用于自动在测试运行前后输出测试信息的装饰器 +def print_test_info(func): + def wrapper(*args, **kwargs): + print(f"{'=' * 20} {func.__name__} {'=' * 20}") + func(*args, **kwargs) + print(f"{'=' * 20} {func.__name__} {'=' * 20}") + + return wrapper + + +class TestVector(unittest.TestCase): + @print_test_info + def test1_create_print_vector(self): + vec2 = Vector2_rs(1, 2) + vec3 = Vector3_rs(1, 2, 3) + vec4 = Vector4_rs(1, 2, 3, 4) + + print(f"{vec2=}") + print(f"{vec3=}") + print(f"{vec4=}") + + @print_test_info + def test2_calculate_vector(self): + vec2 = Vector2_rs(random.randint(1, 100), random.randint(1, 100)) + vec3 = Vector3_rs(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)) + vec4 = Vector4_rs(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)) + + vec2_1 = Vector2_rs(random.randint(1, 100), random.randint(1, 100)) + vec3_1 = Vector3_rs(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)) + vec4_1 = Vector4_rs(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)) + + print(f"{vec2=} {vec2_1=}") + print(f"{vec3=} {vec3_1=}") + print(f"{vec4=} {vec4_1=}") + + print(f"{vec2 + vec2_1=}") + print(f"{vec3 + vec3_1=}") + print(f"{vec4 + vec4_1=}") + + print(f"{vec2 - vec2_1=}") + print(f"{vec3 - vec3_1=}") + print(f"{vec4 - vec4_1=}") + + print(f"{vec2 * vec2_1=}") + print(f"{vec3 * vec3_1=}") + print(f"{vec4 * vec4_1=}") + + print(f"{vec2 / vec2_1=}") + print(f"{vec3 / vec3_1=}") + print(f"{vec4 / vec4_1=}") + + print(f"{vec2 // vec2_1=}") + print(f"{vec3 // vec3_1=}") + print(f"{vec4 // vec4_1=}") \ No newline at end of file