Skip to content

Commit 307153a

Browse files
committed
updated tests for vec array types
1 parent 687b75e commit 307153a

File tree

11 files changed

+241
-16
lines changed

11 files changed

+241
-16
lines changed

src/ncca/ngl/vec2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def __repr__(self):
267267

268268
def __truediv__(self, rhs):
269269
if isinstance(rhs, (float, int)):
270-
if math.isclose(rhs, 0.0):
270+
if rhs == 0.0:
271271
raise ZeroDivisionError("division by zero")
272272
r = Vec2()
273273
r._data = self._data / rhs

src/ncca/ngl/vec2_array.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ def __iter__(self):
8686
row = self._data[i]
8787
yield Vec2(row[0], row[1])
8888

89+
def __eq__(self, other):
90+
"""
91+
Compare two Vec2Array instances for equality.
92+
93+
Args:
94+
other: Another Vec2Array instance to compare with.
95+
96+
Returns:
97+
bool: True if the arrays contain the same data, False otherwise.
98+
"""
99+
if not isinstance(other, Vec2Array):
100+
return NotImplemented
101+
return np.array_equal(self._data, other._data)
102+
89103
def append(self, value):
90104
"""
91105
Append a Vec2 object to the array.

src/ncca/ngl/vec3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def __truediv__(self, rhs):
333333
ValueError: If the right-hand side is not a float.
334334
"""
335335
if isinstance(rhs, (float, int)):
336-
if math.isclose(rhs, 0.0):
336+
if rhs == 0.0:
337337
raise ZeroDivisionError("division by zero")
338338
r = Vec3()
339339
r._data = self._data / rhs

src/ncca/ngl/vec3_array.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ def __iter__(self):
8686
row = self._data[i]
8787
yield Vec3(row[0], row[1], row[2])
8888

89+
def __eq__(self, other):
90+
"""
91+
Compare two Vec3Array instances for equality.
92+
93+
Args:
94+
other: Another Vec3Array instance to compare with.
95+
96+
Returns:
97+
bool: True if the arrays contain the same data, False otherwise.
98+
"""
99+
if not isinstance(other, Vec3Array):
100+
return NotImplemented
101+
return np.array_equal(self._data, other._data)
102+
89103
def append(self, value):
90104
"""
91105
Append a Vec3 object to the array.

src/ncca/ngl/vec4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def __rmul__(self, rhs):
148148

149149
def __truediv__(self, rhs):
150150
if isinstance(rhs, (float, int)):
151-
if math.isclose(rhs, 0.0):
151+
if rhs == 0.0:
152152
raise ZeroDivisionError("division by zero")
153153
r = Vec4()
154154
r._data = self._data / rhs

src/ncca/ngl/vec4_array.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ def __iter__(self):
8686
row = self._data[i]
8787
yield Vec4(row[0], row[1], row[2], row[3])
8888

89+
def __eq__(self, other):
90+
"""
91+
Compare two Vec4Array instances for equality.
92+
93+
Args:
94+
other: Another Vec4Array instance to compare with.
95+
96+
Returns:
97+
bool: True if the arrays contain the same data, False otherwise.
98+
"""
99+
if not isinstance(other, Vec4Array):
100+
return NotImplemented
101+
return np.array_equal(self._data, other._data)
102+
89103
def append(self, value):
90104
"""
91105
Append a Vec4 object to the array.

tests/test_util.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
import pytest
22

3-
from ncca.ngl import (
4-
Mat4,
5-
Vec3,
6-
calc_normal,
7-
clamp,
8-
frustum,
9-
lerp,
10-
look_at,
11-
ortho,
12-
perspective,
13-
)
3+
from ncca.ngl import Mat4, PerspMode, Vec3, calc_normal, clamp, frustum, lerp, look_at, ortho, perspective
144

155

166
def test_clamp():
@@ -58,18 +48,28 @@ def test_perspective():
5848

5949
assert project.to_list() == pytest.approx(result.to_list(), abs=1e-3)
6050

51+
# fmt: off
52+
result=Mat4.from_list([1.69749,0,0,0,0,2.41421,0,0,0,0,-1.002004008016032,-1,0,0,-0.02004008016032064,0])
53+
# fmt: on
54+
55+
project = perspective(fov, aspect, near, far, PerspMode.WebGPU)
56+
assert project.to_list() == pytest.approx(result.to_list(), abs=1e-3)
57+
6158

6259
def test_ortho():
6360
project = ortho(-1.0, 1.0, -1.0, 1.0, 1.0, -1.0)
6461
# fmt: off
6562
m = [ +1.000000,+0.000000, +0.000000, -0.000000, +0.000000, +1.000000, +0.000000,-0.000000,+0.000000,+0.000000,+1.000000,+0.000000,+0.000000,+0.000000,+0.000000,+1.000000,]
6663
# fmt: on
67-
6864
result = Mat4.from_list(m)
65+
# fmt: off
6966

67+
assert project.to_list() == pytest.approx(result.to_list(), abs=1e-3)
68+
m = [ +1.000000,+0.000000, +0.000000, -0.000000, +0.000000, +1.000000, +0.000000,-0.000000,+0.000000,+0.000000,+0.500000,+0.000000,+0.000000,+0.000000,+0.500000,+1.000000,]
7069
# fmt: on
70+
result = Mat4.from_list(m)
7171

72-
assert project.to_list() == pytest.approx(result.to_list(), abs=1e-3)
72+
project = ortho(-1.0, 1.0, -1.0, 1.0, 1.0, -1.0, PerspMode.WebGPU)
7373

7474

7575
def test_calc_normal():

tests/test_vec2.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,25 @@ def test_truediv():
189189
v2 = v / 2.0
190190
assert v2.x == pytest.approx(1.0)
191191
assert v2.y == pytest.approx(2.0)
192+
with pytest.raises(ValueError):
193+
_ = v / "a"
194+
195+
196+
def test_div():
197+
v = Vec2(2.0, 4.0)
198+
v2 = v / 2.0
199+
assert v2.x == pytest.approx(1.0)
200+
assert v2.y == pytest.approx(2.0)
201+
with pytest.raises(ZeroDivisionError):
202+
_ = v / 0.0
203+
with pytest.raises(ZeroDivisionError):
204+
_ = v / Vec2(0.0, 0.0)
205+
206+
# v3 = v / Vec2(2.0, 2.0)
207+
# result = v2 / v3
208+
# print(result)
209+
# assert result.x == pytest.approx(1.0)
210+
# assert result.y == pytest.approx(2.0)
192211

193212

194213
def test_str():

tests/test_vec2_array.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ def test_append():
3333
a.append("not a vec2")
3434

3535

36+
def test_get_array():
37+
"""Test the get_array method"""
38+
a = Vec2Array([Vec2(1, 2), Vec2(4, 5)])
39+
arr = a.get_array()
40+
assert len(arr) == 2
41+
assert np.equal(arr[0], np.array([1.0, 2.0])).all()
42+
assert np.equal(arr[1], np.array([4.0, 5.0])).all()
43+
44+
45+
def test_setitem():
46+
"""Test the __setitem__ method"""
47+
a = Vec2Array([Vec2(1, 2), Vec2(4, 5)])
48+
v = Vec2(7, 8)
49+
a[1] = v
50+
assert len(a) == 2
51+
assert a[0] == Vec2(1, 2)
52+
assert a[1] == v
53+
with pytest.raises(TypeError):
54+
a[1] = "not a vec2"
55+
56+
3657
def test_extend():
3758
"""Test the extend method"""
3859
a = Vec2Array()
@@ -45,6 +66,18 @@ def test_extend():
4566
a.extend(["not a vec2"])
4667

4768

69+
def test_extend_existing():
70+
"""Test the extend method with existing array"""
71+
a = Vec2Array([Vec2(1, 2), Vec2(4, 5)])
72+
v = [Vec2(7, 8), Vec2(10, 11)]
73+
a.extend(v)
74+
assert len(a) == 4
75+
assert a[2] == Vec2(7, 8)
76+
assert a[3] == Vec2(10, 11)
77+
with pytest.raises(TypeError):
78+
a.extend(["not a vec2"])
79+
80+
4881
def test_getitem():
4982
"""Test the __getitem__ method"""
5083
v = [Vec2(1, 2), Vec2(4, 5)]
@@ -104,6 +137,16 @@ def test_str():
104137
assert str(a) == "[Vec2 [1.0,2.0]]"
105138

106139

140+
def test_eq():
141+
"""Test the __eq__ method"""
142+
a = Vec2Array([Vec2(1, 2), Vec2(4, 5)])
143+
b = Vec2Array([Vec2(1, 2), Vec2(4, 5)])
144+
assert a == b
145+
c = Vec2Array([Vec2(1, 2), Vec2(4, 6)])
146+
assert a != c
147+
assert a.__eq__(1) == NotImplemented
148+
149+
107150
def test_sizeof():
108151
a = Vec2Array()
109152
assert a.sizeof() == 0
@@ -115,3 +158,14 @@ def test_sizeof():
115158
assert a.sizeof() == 3 * Vec2.sizeof()
116159
a.append(Vec2())
117160
assert a.sizeof() == 4 * Vec2.sizeof()
161+
162+
163+
def test_slice():
164+
"""Test the __getitem__ method with slice"""
165+
v = [Vec2(1, 2), Vec2(4, 5), Vec2(7, 8)]
166+
a = Vec2Array(v)
167+
assert a[0:2] == Vec2Array([Vec2(1, 2), Vec2(4, 5)])
168+
assert a[1:] == Vec2Array([Vec2(4, 5), Vec2(7, 8)])
169+
assert a[:] == Vec2Array(v)
170+
with pytest.raises(IndexError):
171+
_ = a[3]

tests/test_vec3_array.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ def test_append():
3333
a.append("not a vec3")
3434

3535

36+
def test_get_array():
37+
"""Test the get_array method"""
38+
a = Vec3Array([Vec3(1, 2, 3), Vec3(4, 5, 6)])
39+
arr = a.get_array()
40+
assert len(arr) == 2
41+
assert np.equal(arr[0], np.array([1.0, 2.0, 3.0])).all()
42+
assert np.equal(arr[1], np.array([4.0, 5.0, 6.0])).all()
43+
44+
45+
def test_setitem():
46+
"""Test the __setitem__ method"""
47+
a = Vec3Array([Vec3(1, 2, 3), Vec3(4, 5, 6)])
48+
v = Vec3(7, 8, 9)
49+
a[1] = v
50+
assert len(a) == 2
51+
assert a[0] == Vec3(1, 2, 3)
52+
assert a[1] == v
53+
with pytest.raises(TypeError):
54+
a[1] = "not a vec3"
55+
56+
3657
def test_getitem():
3758
"""Test the __getitem__ method"""
3859
v = [Vec3(1, 2, 3), Vec3(4, 5, 6)]
@@ -94,6 +115,18 @@ def test_extend():
94115
a.extend(["not a vec3"])
95116

96117

118+
def test_extend_existing():
119+
"""Test the extend method with existing array"""
120+
a = Vec3Array([Vec3(1, 2, 3), Vec3(4, 5, 6)])
121+
v = [Vec3(7, 8, 9), Vec3(10, 11, 12)]
122+
a.extend(v)
123+
assert len(a) == 4
124+
assert a[2] == Vec3(7, 8, 9)
125+
assert a[3] == Vec3(10, 11, 12)
126+
with pytest.raises(TypeError):
127+
a.extend(["not a vec3"])
128+
129+
97130
def test_repr():
98131
a = Vec3Array([Vec3(1, 2, 3)])
99132
assert repr(a) == "Vec3Array([Vec3 [1.0,2.0,3.0]])"
@@ -130,3 +163,24 @@ def test_setitem():
130163
# Test index out of range
131164
with pytest.raises(IndexError):
132165
a[2] = Vec3()
166+
167+
168+
def test_eq():
169+
"""Test the __eq__ method"""
170+
a = Vec3Array([Vec3(1, 2, 3), Vec3(4, 5, 6)])
171+
b = Vec3Array([Vec3(1, 2, 3), Vec3(4, 5, 6)])
172+
assert a == b
173+
c = Vec3Array([Vec3(1, 2, 3), Vec3(4, 5, 7)])
174+
assert a != c
175+
assert a.__eq__(1) == NotImplemented
176+
177+
178+
def test_slice():
179+
"""Test the __getitem__ method with slice"""
180+
v = [Vec3(1, 2, 3), Vec3(5, 6, 7), Vec3(9, 10, 11)]
181+
a = Vec3Array(v)
182+
assert a[0:2] == Vec3Array([Vec3(1, 2, 3), Vec3(5, 6, 7)])
183+
assert a[1:] == Vec3Array([Vec3(5, 6, 7), Vec3(9, 10, 11)])
184+
assert a[:] == Vec3Array(v)
185+
with pytest.raises(IndexError):
186+
_ = a[3]

0 commit comments

Comments
 (0)