add test for vector(using unittest)
This commit is contained in:
parent
31faeb2a70
commit
d7e27657f3
3
DR.py
3
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()
|
||||
|
@ -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: ...
|
||||
|
||||
|
||||
|
@ -24,5 +24,7 @@ fn module_init(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<sprite::Sprite>()?;
|
||||
// vector
|
||||
m.add_class::<pymath::python_class::PyVector2>()?;
|
||||
m.add_class::<pymath::python_class::PyVector3>()?;
|
||||
m.add_class::<pymath::python_class::PyVector4>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
66
libs/pyglet_rs/src/test/vector.py
Normal file
66
libs/pyglet_rs/src/test/vector.py
Normal file
@ -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=}")
|
Loading…
Reference in New Issue
Block a user