|
| 1 | +""" |
| 2 | + Tests for the NURBS-Python package |
| 3 | + Released under The MIT License. See LICENSE file for details. |
| 4 | + Copyright (c) 2018 Onur Rauf Bingol |
| 5 | +
|
| 6 | + Tests geomdl.linalg module. Requires "pytest" to run. |
| 7 | +""" |
| 8 | + |
| 9 | +import pytest |
| 10 | +from geomdl import linalg |
| 11 | + |
| 12 | +GEOMDL_DELTA = 10e-6 |
| 13 | + |
| 14 | + |
| 15 | +def test_linspace(): |
| 16 | + start = 5 |
| 17 | + stop = 11 |
| 18 | + num = 4 |
| 19 | + result = [5.0, 7.0, 9.0, 11.0] |
| 20 | + to_check = linalg.linspace(start, stop, num) |
| 21 | + assert to_check == result |
| 22 | + |
| 23 | + |
| 24 | +def test_vector_dot1(): |
| 25 | + with pytest.raises(ValueError): |
| 26 | + vec1 = () |
| 27 | + vec2 = () |
| 28 | + linalg.vector_dot(vec1, vec2) |
| 29 | + |
| 30 | + |
| 31 | +def test_vector_dot2(): |
| 32 | + result = 32 |
| 33 | + vec1 = (1, 2, 3) |
| 34 | + vec2 = (1, 5, 7) |
| 35 | + to_check = linalg.vector_dot(vec1, vec2) |
| 36 | + assert to_check == result |
| 37 | + |
| 38 | + |
| 39 | +def test_vector_dot3(): |
| 40 | + with pytest.raises(TypeError): |
| 41 | + linalg.vector_dot(5, 9.7) |
| 42 | + |
| 43 | + |
| 44 | +def test_vector_cross1(): |
| 45 | + with pytest.raises(ValueError): |
| 46 | + vec1 = () |
| 47 | + vec2 = () |
| 48 | + linalg.vector_cross(vec1, vec2) |
| 49 | + |
| 50 | + |
| 51 | +def test_vector_cross2(): |
| 52 | + with pytest.raises(ValueError): |
| 53 | + vec1 = (1, 2, 3, 4) |
| 54 | + vec2 = (1, 5, 7, 9) |
| 55 | + linalg.vector_cross(vec1, vec2) |
| 56 | + |
| 57 | + |
| 58 | +def test_vector_cross3(): |
| 59 | + result = (-1.0, -4.0, 3.0) |
| 60 | + vec1 = (1, 2, 3) |
| 61 | + vec2 = (1, 5, 7) |
| 62 | + to_check = linalg.vector_cross(vec1, vec2) |
| 63 | + assert to_check == result |
| 64 | + |
| 65 | + |
| 66 | +def test_vector_cross4(): |
| 67 | + with pytest.raises(TypeError): |
| 68 | + linalg.vector_cross(5, 9.7) |
| 69 | + |
| 70 | + |
| 71 | +def test_vector_normalize1(): |
| 72 | + with pytest.raises(ValueError): |
| 73 | + vec = () |
| 74 | + linalg.vector_normalize(vec) |
| 75 | + |
| 76 | + |
| 77 | +def test_vector_normalize2(): |
| 78 | + with pytest.raises(ValueError): |
| 79 | + vec = (0, 0) |
| 80 | + linalg.vector_normalize(vec) |
| 81 | + |
| 82 | + |
| 83 | +def test_vector_normalize3(): |
| 84 | + with pytest.raises(TypeError): |
| 85 | + linalg.vector_normalize(5) |
| 86 | + |
| 87 | + |
| 88 | +def test_vector3_normalize(): |
| 89 | + vec = (5, 2.5, 5) |
| 90 | + result = (0.667, 0.333, 0.667) |
| 91 | + to_check = linalg.vector_normalize(vec, decimals=3) |
| 92 | + assert to_check == result |
| 93 | + |
| 94 | + |
| 95 | +def test_vector4_normalize(): |
| 96 | + vec = (5, 2.5, 5, 10) |
| 97 | + result = (0.4, 0.2, 0.4, 0.8) |
| 98 | + to_check = linalg.vector_normalize(vec) |
| 99 | + assert to_check == result |
| 100 | + |
| 101 | + |
| 102 | +def test_vector_generate1(): |
| 103 | + with pytest.raises(ValueError): |
| 104 | + pt1 = () |
| 105 | + pt2 = (1, 2, 3) |
| 106 | + linalg.vector_generate(pt1, pt2) |
| 107 | + |
| 108 | + |
| 109 | +def test_vector_generate2(): |
| 110 | + pt1 = (0, 0, 0) |
| 111 | + pt2 = (5, 3, 4) |
| 112 | + result = (5, 3, 4) |
| 113 | + result_normalized = (0.707107, 0.424264, 0.565685) |
| 114 | + to_check = linalg.vector_generate(pt1, pt2) |
| 115 | + to_check_normalized = linalg.vector_generate(pt1, pt2, normalize=True) |
| 116 | + assert abs(to_check[0] - result[0]) <= GEOMDL_DELTA |
| 117 | + assert abs(to_check[1] - result[1]) <= GEOMDL_DELTA |
| 118 | + assert abs(to_check[2] - result[2]) <= GEOMDL_DELTA |
| 119 | + assert abs(to_check_normalized[0] - result_normalized[0]) <= GEOMDL_DELTA |
| 120 | + assert abs(to_check_normalized[1] - result_normalized[1]) <= GEOMDL_DELTA |
| 121 | + assert abs(to_check_normalized[2] - result_normalized[2]) <= GEOMDL_DELTA |
| 122 | + |
| 123 | + |
| 124 | +def test_vector_generate3(): |
| 125 | + with pytest.raises(TypeError): |
| 126 | + linalg.vector_generate(5, 9.7) |
| 127 | + |
| 128 | + |
| 129 | +def test_point_translate1(): |
| 130 | + with pytest.raises(ValueError): |
| 131 | + pt1 = () |
| 132 | + pt2 = (1, 2, 3) |
| 133 | + linalg.point_translate(pt1, pt2) |
| 134 | + |
| 135 | + |
| 136 | +def test_point_translate2(): |
| 137 | + pt = (1, 0, 0) |
| 138 | + vec = (5, 5, 5) |
| 139 | + result = (6, 5, 5) |
| 140 | + to_check = linalg.point_translate(pt, vec) |
| 141 | + assert to_check == result |
| 142 | + |
| 143 | + |
| 144 | +def test_point_translate3(): |
| 145 | + with pytest.raises(TypeError): |
| 146 | + linalg.point_translate(5, 9.7) |
| 147 | + |
| 148 | + |
| 149 | +def test_binomial_coefficient1(): |
| 150 | + result = 0.0 |
| 151 | + to_check = linalg.binomial_coefficient(13, 14) |
| 152 | + assert to_check == result |
| 153 | + |
| 154 | + |
| 155 | +def test_binomial_coefficient2(): |
| 156 | + result = 1.0 |
| 157 | + to_check = linalg.binomial_coefficient(13, 13) |
| 158 | + assert to_check == result |
| 159 | + |
| 160 | + |
| 161 | +def test_binomial_coefficient3(): |
| 162 | + result = 680.0 |
| 163 | + to_check = linalg.binomial_coefficient(17, 3) |
| 164 | + assert to_check == result |
| 165 | + |
| 166 | + |
| 167 | +def test_frange1(): |
| 168 | + start = 5 |
| 169 | + stop = 11 |
| 170 | + step = 2 |
| 171 | + to_check = [] |
| 172 | + for fr in linalg.frange(start, stop, step): |
| 173 | + to_check.append(fr) |
| 174 | + result = [5.0, 7.0, 9.0, 11.0] |
| 175 | + assert to_check == result |
| 176 | + |
| 177 | + |
| 178 | +def test_frange2(): |
| 179 | + check = list(linalg.frange(0, 1, 0.1)) |
| 180 | + result = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] |
| 181 | + check_flag = True |
| 182 | + for c, r in zip(check, result): |
| 183 | + if abs(c - r) > GEOMDL_DELTA: |
| 184 | + check_flag = False |
| 185 | + assert check_flag |
| 186 | + |
| 187 | + |
| 188 | +def test_vector_multiply(): |
| 189 | + result = (2, 4, 6) |
| 190 | + computed = linalg.vector_multiply((1, 2, 3), 2) |
| 191 | + assert result == computed |
| 192 | + |
| 193 | + |
| 194 | +def test_vector_mean(): |
| 195 | + vector_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] |
| 196 | + result = (4, 5, 6) |
| 197 | + computed = linalg.vector_mean(*vector_list) |
| 198 | + assert result == computed |
| 199 | + |
| 200 | + |
| 201 | +def test_vector_angle_between(): |
| 202 | + computed_deg = linalg.vector_angle_between((1, 2, 3), (3, 2, 1), degrees=True) |
| 203 | + computed_rad = linalg.vector_angle_between((1, 2, 3), (3, 2, 1), degrees=False) |
| 204 | + result_deg = 44.415308597193 |
| 205 | + result_rad = 0.775193373310361 |
| 206 | + assert abs(computed_deg - result_deg) < GEOMDL_DELTA |
| 207 | + assert abs(computed_rad - result_rad) < GEOMDL_DELTA |
| 208 | + |
| 209 | + |
| 210 | +def test_point_distance(): |
| 211 | + result = 17.691806 |
| 212 | + computed = linalg.point_distance((5, 7, 9), (-7, -5, 4)) |
| 213 | + assert abs(result - computed) < GEOMDL_DELTA |
| 214 | + |
| 215 | + |
| 216 | +def test_point_mid(): |
| 217 | + result = (2.5, 3.5, 4.5) |
| 218 | + computed = linalg.point_mid((1, 2, 3), (4, 5, 6)) |
| 219 | + assert result == computed |
0 commit comments