Debian Codegraph @

py/tests/test_term.py

raw
1 # --------------------------------------------------------------------------------------
2 # Copyright (c) 2014-2024,, Nucleic Development Team.
3 #
4 # Distributed under the terms of the Modified BSD License.
5 #
6 # The full license is in the file LICENSE, distributed with this software.
7 # --------------------------------------------------------------------------------------
8 import gc
9 import math
10 import operator
11 from typing import Tuple
12
13 import pytest
14
15 from kiwisolver import Constraint, Expression, Term, Variable, strength
16
17
18 def test_term_creation() -> None:
19 """Test the Term constructor."""
20 v = Variable("foo")
21 t = Term(v)
22 assert t.variable() is v
23 assert t.coefficient() == 1
24
25 t = Term(v, 100)
26 assert t.variable() is v
27 assert t.coefficient() == 100
28
29 assert str(t) == "100 * foo"
30
31 with pytest.raises(TypeError) as excinfo:
32 Term("") # type: ignore
33 assert "Variable" in excinfo.exconly()
34
35 # ensure we test garbage collection
36 del t
37 gc.collect()
38
39
40 @pytest.fixture()
41 def terms():
42 """Terms for testing."""
43 v = Variable("foo")
44 v2 = Variable("bar")
45 t = Term(v, 10)
46 t2 = Term(v2)
47
48 return t, t2, v, v2
49
50
51 def test_term_neg(terms: Tuple[Term, Term, Variable, Variable]) -> None:
52 """Test neg on a term."""
53 t, _, v, _ = terms
54
55 neg = -t
56 assert isinstance(neg, Term)
57 assert neg.variable() is v and neg.coefficient() == -10
58
59
60 def test_term_mul(terms: Tuple[Term, Term, Variable, Variable]) -> None:
61 """Test term multiplications"""
62 t, _, v, _ = terms
63
64 for mul in (t * 2, 2.0 * t):
65 assert isinstance(mul, Term)
66 assert mul.variable() is v and mul.coefficient() == 20
67
68 with pytest.raises(TypeError):
69 t * v # type: ignore
70
71
72 def test_term_div(terms: Tuple[Term, Term, Variable, Variable]) -> None:
73 """Test term divisions."""
74 t, _, v, v2 = terms
75
76 div = t / 2
77 assert isinstance(div, Term)
78 assert div.variable() is v and div.coefficient() == 5
79
80 with pytest.raises(TypeError):
81 t / v2 # type: ignore
82
83 with pytest.raises(ZeroDivisionError):
84 t / 0
85
86
87 def test_term_add(terms: Tuple[Term, Term, Variable, Variable]) -> None:
88 """Test term additions."""
89 t, t2, v, v2 = terms
90
91 for add in (t + 2, 2.0 + t):
92 assert isinstance(add, Expression)
93 assert add.constant() == 2
94 terms_ = add.terms()
95 assert (
96 len(terms_) == 1
97 and terms[0].variable() is v
98 and terms_[0].coefficient() == 10
99 )
100
101 for add2, order in zip((t + v2, v2 + t), ((0, 1), (1, 0))):
102 assert isinstance(add2, Expression)
103 assert add2.constant() == 0
104 terms_ = add2.terms()
105 assert (
106 len(terms_) == 2
107 and terms_[order[0]].variable() is v
108 and terms_[order[0]].coefficient() == 10
109 and terms_[order[1]].variable() is v2
110 and terms_[order[1]].coefficient() == 1
111 )
112
113 add2 = t + t2
114 assert isinstance(add2, Expression)
115 assert add2.constant() == 0
116 terms_ = add2.terms()
117 assert (
118 len(terms_) == 2
119 and terms_[0].variable() is v
120 and terms_[0].coefficient() == 10
121 and terms_[1].variable() is v2
122 and terms_[1].coefficient() == 1
123 )
124
125
126 def test_term_sub(terms: Tuple[Term, Term, Variable, Variable]) -> None:
127 """Test term substractions."""
128 t, t2, v, v2 = terms
129
130 for sub, diff in zip((t - 2, 2.0 - t), (-2, 2)):
131 assert isinstance(sub, Expression)
132 assert sub.constant() == diff
133 terms_ = sub.terms()
134 assert (
135 len(terms_) == 1
136 and terms[0].variable() is v
137 and terms_[0].coefficient() == -math.copysign(10, diff)
138 )
139
140 for sub2, order in zip((t - v2, v2 - t), ((0, 1), (1, 0))):
141 assert isinstance(sub2, Expression)
142 assert sub2.constant() == 0
143 terms_ = sub2.terms()
144 assert (
145 len(terms_) == 2
146 and terms_[order[0]].variable() is v
147 and terms_[order[0]].coefficient() == 10 * (-1) ** order[0]
148 and terms_[order[1]].variable() is v2
149 and terms_[order[1]].coefficient() == -1 * (-1) ** order[0]
150 )
151
152 sub2 = t - t2
153 assert isinstance(sub2, Expression)
154 assert sub2.constant() == 0
155 terms_ = sub2.terms()
156 assert (
157 len(terms_) == 2
158 and terms_[0].variable() is v
159 and terms_[0].coefficient() == 10
160 and terms_[1].variable() is v2
161 and terms_[1].coefficient() == -1
162 )
163
164
165 @pytest.mark.parametrize(
166 "op, symbol",
167 [
168 (operator.le, "<="),
169 (operator.eq, "=="),
170 (operator.ge, ">="),
171 (operator.lt, None),
172 (operator.ne, None),
173 (operator.gt, None),
174 ],
175 )
176 def test_term_rich_compare_operations(op, symbol):
177 """Test using comparison on variables."""
178 v = Variable("foo")
179 v2 = Variable("bar")
180 t1 = Term(v, 10)
181 t2 = Term(v2, 20)
182
183 if symbol is not None:
184 c = op(t1, t2 + 1)
185 assert isinstance(c, Constraint)
186 e = c.expression()
187 t = e.terms()
188 assert len(t) == 2
189 if t[0].variable() is not v:
190 t = (t[1], t[0])
191 assert (
192 t[0].variable() is v
193 and t[0].coefficient() == 10
194 and t[1].variable() is v2
195 and t[1].coefficient() == -20
196 )
197 assert e.constant() == -1
198 assert c.op() == symbol and c.strength() == strength.required
199
200 else:
201 with pytest.raises(TypeError):
202 op(t1, t2)