| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (C) 2010 Kristian B. Oelgaard |
| 3 |
# |
| 4 |
# This file is part of FFC. |
| 5 |
# |
| 6 |
# FFC is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU Lesser General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# FFC is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU Lesser General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU Lesser General Public License |
| 17 |
# along with FFC. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
import pytest |
| 20 |
import time |
| 21 |
|
| 22 |
# FFC modules |
| 23 |
from ffc.quadrature.symbolics import * |
| 24 |
from ffc.quadrature.cpp import format, set_float_formatting |
| 25 |
from ffc.parameters import FFC_PARAMETERS |
| 26 |
set_float_formatting(FFC_PARAMETERS["precision"]) |
| 27 |
|
| 28 |
|
| 29 |
def testFloat(): |
| 30 |
"Test simple FloatValue instance." |
| 31 |
f0 = FloatValue(1.5) |
| 32 |
f1 = FloatValue(-5) |
| 33 |
f2 = FloatValue(-1e-14) |
| 34 |
f3 = FloatValue(-1e-11) |
| 35 |
f4 = FloatValue(1.5) |
| 36 |
|
| 37 |
assert repr(f0) == "FloatValue(%s)" % format["float"](1.5) |
| 38 |
assert repr(f1) == "FloatValue(%s)" % format["float"](-5) |
| 39 |
# This depends on the chosen precision and certain rounding behaviour in code generation: |
| 40 |
#assert repr(f2) == "FloatValue(%s)" % format["float"](0.0) |
| 41 |
assert repr(f3) == "FloatValue(%s)" % format["float"](-1e-11) |
| 42 |
|
| 43 |
#assert f2.val == 0 # This test documents incorrect float behaviour of quadrature repsentation |
| 44 |
assert not f3.val == 0 |
| 45 |
|
| 46 |
assert f0.ops() == 0 |
| 47 |
assert f1.ops() == 0 |
| 48 |
assert f2.ops() == 0 |
| 49 |
assert f3.ops() == 0 |
| 50 |
|
| 51 |
assert f0 == f4 |
| 52 |
assert f1 != f3 |
| 53 |
assert not f0 < f1 |
| 54 |
#assert f2 > f3 |
| 55 |
# ^^^ NB! The > operator of FloatValue is not a comparison of numbers, |
| 56 |
# it does something else entirely, and is affected by precision in indirect ways. |
| 57 |
|
| 58 |
# Test hash |
| 59 |
ll = [f0] |
| 60 |
d = {f0: 0} |
| 61 |
assert f0 in ll |
| 62 |
assert f0 in d |
| 63 |
assert f4 in ll |
| 64 |
assert f4 in d |
| 65 |
assert f1 not in ll |
| 66 |
assert f1 not in d |