Debian Codegraph @

test/unit/symbolics/test_fraction.py

raw
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 from ffc.log import push_level, pop_level, CRITICAL
28
29
30 def testFraction():
31 "Test simple fraction instance."
32
33 f0 = FloatValue(-2.0)
34 f1 = FloatValue(3.0)
35 f2 = FloatValue(0)
36 s0 = Symbol("x", BASIS)
37 s1 = Symbol("y", GEO)
38
39 F0 = Fraction(f1, f0)
40 F1 = Fraction(f2, f0)
41 F2 = Fraction(s0, s1)
42 F3 = Fraction(s0, f1)
43 F4 = Fraction(f0, s1)
44 F5 = Fraction(f2, s1)
45 F6 = Fraction(s0, s1)
46
47 # Silence output
48 push_level(CRITICAL)
49 with pytest.raises(Exception):
50 Fraction(f0, f2)
51 with pytest.raises(Exception):
52 Fraction(s0, f2)
53 pop_level()
54
55 assert repr(F0) == "Fraction(FloatValue(%s), FloatValue(%s))"\
56 % (format["float"](-1.5), format["float"](1))
57 assert repr(F2) == "Fraction(Symbol('x', BASIS), Symbol('y', GEO))"
58
59 assert str(F0) == "%s" % format["float"](-1.5)
60 assert str(F1) == "%s" % format["float"](0)
61 assert str(F2) == "x/y"
62 assert str(F3) == "x/%s" % format["float"](3)
63 assert str(F4) == "-%s/y" % format["float"](2)
64 assert str(F5) == "%s" % format["float"](0)
65
66 assert F2 == F2
67 assert F2 != F3
68 assert F5 != F4
69 assert F2 == F6
70
71 assert F0.ops() == 0
72 assert F1.ops() == 0
73 assert F2.ops() == 1
74 assert F3.ops() == 1
75 assert F4.ops() == 1
76 assert F5.ops() == 0
77
78 # Test hash
79 ll = [F2]
80 d = {F2: 0}
81
82 assert F2 in ll
83 assert F2 in d
84 assert F6 in ll
85 assert F6 in d