Skip to content

Commit a90ae07

Browse files
author
peng.li24
committed
feat(tests): update test_api.py, add test_interpolate_normalized.py
1 parent 9ded4e5 commit a90ae07

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

tests/test_api.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,37 @@ def test_values(self, cpp, line, dist):
414414
assert x == py_pt.x; assert y == py_pt.y
415415

416416

417+
class TestLineStringInterpolateNormalized:
418+
"""Verify interpolate_ls(ls, t, normalized=True) matches Python."""
419+
LINE = [(0,0),(10,0),(10,10)] # L-shaped
420+
LINE_CURVED = [(0,0),(3,4),(6,0),(9,4),(12,0)] # curved path
421+
422+
@pytest.mark.parametrize("line,t", [
423+
(LINE, 0.0), (LINE, 0.25), (LINE, 0.5), (LINE, 0.75), (LINE, 1.0),
424+
(LINE_CURVED, 0.0), (LINE_CURVED, 0.2), (LINE_CURVED, 0.5),
425+
(LINE_CURVED, 0.8), (LINE_CURVED, 1.0),
426+
])
427+
def test_values(self, cpp, line, t):
428+
x, y = cpp.interpolate_linestring(cpp.linestring(line), t, True)
429+
py_pt = PyLineString(line).interpolate(t, normalized=True)
430+
assert x == py_pt.x, f"x: {x} != {py_pt.x}"
431+
assert y == py_pt.y, f"y: {y} != {py_pt.y}"
432+
433+
def test_random_lines(self, cpp):
434+
"""Random lines with exact comparison."""
435+
np.random.seed(42)
436+
for _ in range(100):
437+
n = np.random.randint(2, 10)
438+
pts = np.random.randn(n, 2) * 50
439+
pts[:, 0] += np.arange(n) * 10
440+
line = pts.tolist()
441+
for t in np.linspace(0, 1, 11):
442+
x, y = cpp.interpolate_linestring(cpp.linestring(line), float(t), True)
443+
py_pt = PyLineString(line).interpolate(float(t), normalized=True)
444+
assert x == py_pt.x, f"random line: x {x} != {py_pt.x} at t={t}"
445+
assert y == py_pt.y, f"random line: y {y} != {py_pt.y} at t={t}"
446+
447+
417448
# =============================================================================
418449
# Polygon accessors + same-type distance
419450
# =============================================================================
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Test: shapelycpp interpolate_ls(normalized=True) vs Python shapely."""
2+
import sys, os
3+
sys.path.insert(0, os.path.dirname(__file__))
4+
5+
import numpy as np
6+
from shapely.geometry import LineString as PyLineString
7+
from test_api import cpp # reuse existing test fixture
8+
9+
def test_normalized_random():
10+
"""100 random lines, 11 normalized positions each."""
11+
np.random.seed(42)
12+
failures = []
13+
for _ in range(100):
14+
n = np.random.randint(2, 10)
15+
pts = np.random.randn(n, 2) * 50
16+
pts[:, 0] += np.arange(n) * 10
17+
line = pts.tolist()
18+
cpp_ls = cpp.linestring(line)
19+
py_ls = PyLineString(line)
20+
for i in range(11):
21+
t = float(i) / 10.0
22+
x, y = cpp.interpolate_linestring(cpp_ls, t, True)
23+
py_pt = py_ls.interpolate(t, normalized=True)
24+
dx = abs(x - py_pt.x)
25+
dy = abs(y - py_pt.y)
26+
if dx > 0 or dy > 0:
27+
failures.append((line, t, (x, y), (py_pt.x, py_pt.y), dx, dy))
28+
assert len(failures) == 0, f"{len(failures)} failures: {failures[:3]}"
29+
print(f"PASSED: 100 random lines × 11 positions = 0 mismatch")
30+
31+
if __name__ == "__main__":
32+
test_normalized_random()

0 commit comments

Comments
 (0)