Debian Codegraph @

test/unit/symbolics/test_sum.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
28
29 def testSum():
30 "Test simple sum instance."
31
32 f_0 = format["float"](0)
33 f_1 = format["float"](1)
34 f_2 = format["float"](2)
35 f_3 = format["float"](3)
36
37 f0 = FloatValue(-2.0)
38 f1 = FloatValue(3.0)
39 f2 = FloatValue(0)
40 s0 = Symbol("x", BASIS)
41 s1 = Symbol("y", GEO)
42 s2 = Symbol("z", GEO)
43
44 S0 = Sum([])
45 S1 = Sum([s0])
46 S2 = Sum([s0, s1])
47 S3 = Sum([s0, s0])
48 S4 = Sum([f0, s0])
49 S5 = Sum([s0, f0, s0])
50 S6 = Sum([s0, f0, s0, f1])
51 S7 = Sum([s0, f0, s1, f2])
52 S8 = Sum([s0, f1, s0])
53 S9 = Sum([f0, f0, f0, f1, f1, s1])
54 S10 = Sum([s1, s0])
55
56 assert repr(S0) == "Sum([FloatValue(%s)])" % f_0
57 assert S0.t == CONST
58 assert repr(S1) == "Sum([Symbol('x', BASIS)])"
59 assert repr(S4) == "Sum([FloatValue(-%s), Symbol('x', BASIS)])" % f_2
60 assert repr(S9) == "Sum([Symbol('y', GEO)])"
61
62 assert str(S2) == "(x + y)"
63 assert str(S3) == "(x + x)"
64 assert str(S5) == "(x + x-%s)" % f_2
65 assert str(S6) == "(%s + x + x)" % f_1
66 assert str(S7) == "(x + y-%s)" % f_2
67 assert str(S8) == "(%s + x + x)" % f_3
68 assert str(S9) == "y"
69
70 assert S2 == S2
71 assert S2 != S3
72 assert S5 != S6
73 assert S2 == S10
74
75 assert S0.ops() == 0
76 assert S1.ops() == 0
77 assert S2.ops() == 1
78 assert S3.ops() == 1
79 assert S4.ops() == 1
80 assert S5.ops() == 2
81 assert S6.ops() == 2
82 assert S7.ops() == 2
83 assert S8.ops() == 2
84 assert S9.ops() == 0
85
86 # Test hash
87 ll = [S2]
88 d = {S2: 0}
89
90 assert S2 in ll
91 assert S2 in d
92 assert S10 in ll
93 assert S10 in d