1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
import sys
import mymath_swig.bindings as mymath
import numpy as np
import pytest
def test_dot():
v1 = [1.0, 2, 3, -5.5, 42]
v2 = [-3.2, 0, 13, 6, -3.14]
result = mymath.dot(vector1=v1, vector2=v2)
assert pytest.approx(result) == np.dot(v1, v2)
def test_normalize():
v = [1.0, 2, 3, -5.5, 42]
result = mymath.normalize(data=v)
assert pytest.approx(result) == np.array(v) / np.linalg.norm(v)
def test_dot_numpy():
v1 = np.array([1.0, 2, 3, -5.5, 42])
v2 = np.array([-3.2, 0, 13, 6, -3.14])
result = mymath.dot_numpy(in_1=v1, in_2=v2)
assert pytest.approx(result) == np.dot(v1, v2)
def test_normalize_numpy():
v = np.array([1.0, 2, 3, -5.5, 42])
result = mymath.normalize_numpy(in_1=v)
assert isinstance(result, np.ndarray)
assert pytest.approx(result) == np.array(v) / np.linalg.norm(v)
def test_assertion():
v1 = np.array([1.0, 2, 3, -5.5])
v2 = np.array([42.0])
with pytest.raises(RuntimeError):
_ = mymath.dot_numpy(in_1=v1, in_2=v2)
@pytest.mark.skipif(
sys.version_info < (3, 7), reason="capture_output and text require Python 3.7"
)
def test_executable():
import subprocess
result = subprocess.run("print_answer_swig", capture_output=True, text=True)
assert result.stdout.strip() == "42"
result = subprocess.run(
[sys.executable, "-m", "mymath_swig.bin", "print_answer_swig"],
capture_output=True,
text=True,
)
assert result.stdout.strip() == "42"
|