File: test_pybind11.py

package info (click to toggle)
python-cmake-build-extension 0.6.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 488 kB
  • sloc: python: 504; cpp: 70; sh: 13; makefile: 5
file content (71 lines) | stat: -rw-r--r-- 1,628 bytes parent folder | download | duplicates (2)
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
69
70
71
import sys

import mymath_pybind11.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(vector1=v1, vector2=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(data=v)

    assert isinstance(result, np.ndarray)
    assert pytest.approx(result) == np.array(v) / np.linalg.norm(v)


@pytest.mark.skipif(
    sys.platform == "win32", reason="fails with the error: access violation"
)
def test_assertion():

    v1 = np.array([1.0, 2, 3, -5.5])
    v2 = np.array([42.0])

    with pytest.raises(RuntimeError):
        _ = mymath.dot(vector1=v1, vector2=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_pybind11", capture_output=True, text=True)
    assert result.stdout.strip() == "42"

    result = subprocess.run(
        [sys.executable, "-m", "mymath_pybind11.bin", "print_answer_pybind11"],
        capture_output=True,
        text=True,
    )
    assert result.stdout.strip() == "42"