| 1 |
"""Unit tests for Point interface""" |
| 2 |
|
| 3 |
# Copyright (C) 2017 Jan Blechta |
| 4 |
# |
| 5 |
# This file is part of DOLFIN. |
| 6 |
# |
| 7 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU Lesser General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# DOLFIN is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU Lesser General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU Lesser General Public License |
| 18 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
import pytest |
| •21 |
from pytest import approx |
| 22 |
import numpy as np |
| 23 |
|
| 24 |
from dolfin import * |
| 25 |
|
| 26 |
|
| 27 |
def test_point_getitem(): |
| 28 |
p = Point(1, 2, 3) |
| •29 |
assert p[0] == approx(1.0) |
| 30 |
assert p[1] == approx(2.0) |
| 31 |
assert p[2] == approx(3.0) |
| 32 |
with pytest.raises(IndexError): |
| 33 |
p[3] |
| •34 |
assert np.all(p[:] == approx(np.array((1.0, 2.0, 3.0)))) |
| 35 |
|
| 36 |
|
| 37 |
def test_point_setitem(): |
| 38 |
p = Point() |
| 39 |
|
| 40 |
p[0] = 6.0 |
| •41 |
assert p[0] == approx(6.0) |
| 42 |
|
| 43 |
p[1] = 16.0 |
| 44 |
p[1] += 600.0 |
| 45 |
assert np.isclose(p[1], 616.0) |
| 46 |
|
| 47 |
p[2] = 111.0 |
| 48 |
p[2] *= 12.0 |
| 49 |
p[2] /= 2 |
| 50 |
assert np.isclose(p[2], 666.0) |
| 51 |
|
| 52 |
with pytest.raises(IndexError): |
| 53 |
p[3] = 6666.0 |
| 54 |
|
| 55 |
p[:] = (0, 0, 0) |
| 56 |
assert np.all(p[:] == 0) |
| 57 |
|
| 58 |
p[:] = (1, 2, 3) |
| 59 |
assert np.all(p[:] == (1, 2, 3)) |
| 60 |
|
| 61 |
p[:] += np.array((1, 2, 3)) |
| 62 |
assert np.all(p[:] == (2, 4, 6)) |
| 63 |
|
| 64 |
p[:] /= 2 |
| 65 |
assert np.all(p[:] == (1, 2, 3)) |
| 66 |
|
| 67 |
p[:] *= np.array((2., 2., 2.)) |
| 68 |
assert np.all(p[:] == (2, 4, 6)) |
| 69 |
|
| 70 |
|
| 71 |
def test_point_array(): |
| 72 |
p = Point(1, 2, 3) |
| 73 |
assert np.all(p.array() == (1, 2, 3)) |
| 74 |
|
| 75 |
# Point.array() is a copy, no in-place modification |
| 76 |
p.array()[:] += 1000.0 |
| 77 |
assert np.all(p.array() == (1, 2, 3)) |
| 78 |
|
| 79 |
|
| 80 |
def test_point_equality(): |
| 81 |
p = Point(1.23, 2, DOLFIN_PI) |
| 82 |
q = Point(1.23, 2, DOLFIN_PI) |
| 83 |
r = Point(1.23+DOLFIN_EPS, 2, DOLFIN_PI) |
| 84 |
assert p == q |
| 85 |
assert p != r |
| 86 |
|
| 87 |
|
| 88 |
def test_point_dot(): |
| 89 |
p = Point(1.0, 2.0, 3.0) |
| 90 |
q = Point(3.1, 4.5, 5.6) |
| 91 |
r = Point(-1.6, -2.5, 3.3) |
| 92 |
s = Point(152.25) |
| 93 |
|
| •94 |
assert p.dot(q) == approx(p[0]*q[0] + p[1]*q[1] + p[2]*q[2]) |
| 95 |
assert p.dot(r) == approx(p[0]*r[0] + p[1]*r[1] + p[2]*r[2]) |
| 96 |
assert p.dot(s) == approx(p[0]*s[0]) |