| 1 |
"""Unit tests for basic math functions""" |
| 2 |
|
| 3 |
# Copyright (C) 2011-2014 Martin Alnaes |
| 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 |
# Modified by Garth N. Wells, 2011 |
| 21 |
|
| 22 |
import pytest |
| 23 |
import numpy |
| 24 |
from dolfin import * |
| 25 |
|
| 26 |
|
| 27 |
def test_near(): |
| 28 |
eps = DOLFIN_EPS |
| 29 |
# Loop over magnitudes |
| 30 |
for i in range(100): |
| 31 |
# Loop over base values |
| 32 |
for j in range(1, 10): |
| 33 |
# Compute a value v and some values close to it |
| 34 |
v = float(j*10**(i-13)) |
| 35 |
vm = v - eps |
| 36 |
vp = v + eps |
| 37 |
#vm = v - v*eps # Scaling eps does not work |
| 38 |
#vp = v + v*eps |
| 39 |
|
| 40 |
# Check that we return True when we should by definition: |
| 41 |
assert near(v, v) |
| 42 |
assert near(vm, vm) |
| 43 |
assert near(vp, vp) |
| 44 |
#assert near(v, vm) # Can fail |
| 45 |
#assert near(v, vp) |
| 46 |
if not near(v, vm): |
| 47 |
print("not near vm: {}, {}".format(v, vm)) |
| 48 |
if not near(v, vp): |
| 49 |
print("not near vp: {}, {}".format(v, vp)) |
| 50 |
|
| 51 |
# vm and vp can round off to v, make some small values != v |
| 52 |
# that are close to 1 (except for some of the smallest v's) |
| 53 |
v2m = v * (1.0 - 2*eps) - 2*eps |
| 54 |
v2p = v * (1.0 + 2*eps) + 2*eps |
| 55 |
assert v/v2m > 1.0 |
| 56 |
assert v/v2p < 1.0 |
| 57 |
|
| 58 |
# Check that we can fail for fairly close values |
| 59 |
assert not near(v, v2m) |
| 60 |
assert not near(v, v2p) |
| 61 |
|
| 62 |
def test_between(): |
| 63 |
eps = DOLFIN_EPS |
| 64 |
# Loop over magnitudes |
| 65 |
for i in range(100): |
| 66 |
# Loop over base values |
| 67 |
for j in range(1, 10): |
| 68 |
# Compute a value v and some values close to it |
| 69 |
v = float(j*10**(i - 15)) |
| 70 |
vm = v - eps |
| 71 |
vp = v + eps |
| 72 |
|
| 73 |
# Check that we return True when we should by definition: |
| 74 |
assert between(v, (vm, vp)) |
| 75 |
|
| 76 |
# vm and vp can round off to v, make some small values != v |
| 77 |
v2m = v * (1.0 - 2*eps) - 2*eps |
| 78 |
v2p = v * (1.0 + 2*eps) + 2*eps |
| 79 |
|
| 80 |
# Close to 1 except for some of the smallest v's: |
| 81 |
assert v/v2m > 1.0 |
| 82 |
assert v/v2p < 1.0 |
| 83 |
assert between(v, (v2m, v2p)) |
| 84 |
|
| 85 |
# Check that we can fail for fairly close values |
| 86 |
assert not between(v, (v2p, v2m)) |
| 87 |
|
| 88 |
def test_ipow(): |
| 89 |
with pytest.raises(RuntimeError): |
| 90 |
ipow(0, 0) |
| 91 |
assert ipow(0, 1) == 0 |
| 92 |
assert ipow(0, 10) == 0 |
| 93 |
assert ipow(0, 12345) == 0 |
| 94 |
assert ipow(1, 12345) == 1 |
| 95 |
assert ipow(12345, 0) == 1 |
| 96 |
assert ipow(12345, 1) == 12345 |
| 97 |
assert ipow(2, 15) == 32768 |
| 98 |
|
| 99 |
@pytest.mark.xfail |
| 100 |
def test_ipow_overflow(): |
| 101 |
# This will fail because of overflow in dolfin/math/basic.cpp |
| 102 |
# on 64-bit size_t. When __builtin_mul_overflow is employed |
| 103 |
# to do the check, exception will be raised instead. |
| 104 |
assert ipow(3, 41) == 3**41 |