| 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 |
import sys |
| 12 |
from typing import Tuple |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from kiwisolver import Constraint, Expression, Term, Variable, strength |
| 17 |
|
| 18 |
|
| 19 |
def test_expression_creation() -> None: |
| 20 |
"""Test the Term constructor.""" |
| 21 |
v = Variable("foo") |
| 22 |
v2 = Variable("bar") |
| 23 |
v3 = Variable("aux") |
| 24 |
e1 = Expression((v * 1, v2 * 2, v3 * 3)) |
| 25 |
e2 = Expression((v * 1, v2 * 2, v3 * 3), 10) |
| 26 |
|
| 27 |
for e, val in ((e1, 0), (e2, 10)): |
| 28 |
t = e.terms() |
| 29 |
assert ( |
| 30 |
len(t) == 3 |
| 31 |
and t[0].variable() is v |
| 32 |
and t[0].coefficient() == 1 |
| 33 |
and t[1].variable() is v2 |
| 34 |
and t[1].coefficient() == 2 |
| 35 |
and t[2].variable() is v3 |
| 36 |
and t[2].coefficient() == 3 |
| 37 |
) |
| 38 |
assert e.constant() == val |
| 39 |
|
| 40 |
assert str(e2) == "1 * foo + 2 * bar + 3 * aux + 10" |
| 41 |
|
| 42 |
with pytest.raises(TypeError) as excinfo: |
| 43 |
Expression((1, v2 * 2, v3 * 3)) # type: ignore |
| 44 |
assert "Term" in excinfo.exconly() |
| 45 |
|
| 46 |
# ensure we test garbage collection. |
| 47 |
del e2 |
| 48 |
gc.collect() |
| 49 |
|
| 50 |
|
| 51 |
@pytest.fixture() |
| 52 |
def expressions(): |
| 53 |
"""Build expressions, terms and variables to test operations.""" |
| 54 |
v = Variable("foo") |
| 55 |
v2 = Variable("bar") |
| 56 |
t = Term(v, 10) |
| 57 |
t2 = Term(v2) |
| 58 |
e = t + 5 |
| 59 |
e2 = v2 - 10 |
| 60 |
return e, e2, t, t2, v, v2 |
| 61 |
|
| 62 |
|
| 63 |
def test_expression_neg( |
| 64 |
expressions: Tuple[Expression, Expression, Term, Term, Variable, Variable], |
| 65 |
): |
| 66 |
"""Test neg on an expression.""" |
| 67 |
e, _, _, _, v, _ = expressions |
| 68 |
|
| 69 |
neg = -e |
| 70 |
assert isinstance(neg, Expression) |
| 71 |
neg_t = neg.terms() |
| 72 |
assert ( |
| 73 |
len(neg_t) == 1 |
| 74 |
and neg_t[0].variable() is v |
| 75 |
and neg_t[0].coefficient() == -10 |
| 76 |
and neg.constant() == -5 |
| 77 |
) |
| 78 |
|
| 79 |
|
| 80 |
def test_expression_mul( |
| 81 |
expressions: Tuple[Expression, Expression, Term, Term, Variable, Variable], |
| 82 |
): |
| 83 |
"""Test expresion multiplication.""" |
| 84 |
e, _, _, _, v, _ = expressions |
| 85 |
|
| 86 |
for mul in (e * 2.0, 2.0 * e): |
| 87 |
assert isinstance(mul, Expression) |
| 88 |
mul_t = mul.terms() |
| 89 |
assert ( |
| 90 |
len(mul_t) == 1 |
| 91 |
and mul_t[0].variable() is v |
| 92 |
and mul_t[0].coefficient() == 20 |
| 93 |
and mul.constant() == 10 |
| 94 |
) |
| 95 |
|
| 96 |
with pytest.raises(TypeError): |
| 97 |
e * v # type: ignore |
| 98 |
|
| 99 |
|
| 100 |
def test_expression_div( |
| 101 |
expressions: Tuple[Expression, Expression, Term, Term, Variable, Variable], |
| 102 |
): |
| 103 |
"""Test expression divisions.""" |
| 104 |
e, _, _, _, v, v2 = expressions |
| 105 |
|
| 106 |
div = e / 2 |
| 107 |
assert isinstance(div, Expression) |
| 108 |
div_t = div.terms() |
| 109 |
assert ( |
| 110 |
len(div_t) == 1 |
| 111 |
and div_t[0].variable() is v |
| 112 |
and div_t[0].coefficient() == 5 |
| 113 |
and div.constant() == 2.5 |
| 114 |
) |
| 115 |
|
| 116 |
with pytest.raises(TypeError): |
| 117 |
e / v2 # type: ignore |
| 118 |
|
| 119 |
with pytest.raises(ZeroDivisionError): |
| 120 |
e / 0 |
| 121 |
|
| 122 |
|
| 123 |
def test_expression_addition( |
| 124 |
expressions: Tuple[Expression, Expression, Term, Term, Variable, Variable], |
| 125 |
): |
| 126 |
"""Test expressions additions.""" |
| 127 |
e, e2, _, t2, v, v2 = expressions |
| 128 |
|
| 129 |
for add in (e + 2, 2.0 + e): |
| 130 |
assert isinstance(add, Expression) |
| 131 |
assert add.constant() == 7 |
| 132 |
terms = add.terms() |
| 133 |
assert ( |
| 134 |
len(terms) == 1 |
| 135 |
and terms[0].variable() is v |
| 136 |
and terms[0].coefficient() == 10 |
| 137 |
) |
| 138 |
|
| 139 |
add2 = e + v2 |
| 140 |
assert isinstance(add2, Expression) |
| 141 |
assert add2.constant() == 5 |
| 142 |
terms = add2.terms() |
| 143 |
assert ( |
| 144 |
len(terms) == 2 |
| 145 |
and terms[0].variable() is v |
| 146 |
and terms[0].coefficient() == 10 |
| 147 |
and terms[1].variable() is v2 |
| 148 |
and terms[1].coefficient() == 1 |
| 149 |
) |
| 150 |
|
| 151 |
add3 = e + t2 |
| 152 |
assert isinstance(add3, Expression) |
| 153 |
assert add3.constant() == 5 |
| 154 |
terms = add3.terms() |
| 155 |
assert ( |
| 156 |
len(terms) == 2 |
| 157 |
and terms[0].variable() is v |
| 158 |
and terms[0].coefficient() == 10 |
| 159 |
and terms[1].variable() is v2 |
| 160 |
and terms[1].coefficient() == 1 |
| 161 |
) |
| 162 |
|
| 163 |
add4 = e + e2 |
| 164 |
assert isinstance(add4, Expression) |
| 165 |
assert add4.constant() == -5 |
| 166 |
terms = add4.terms() |
| 167 |
assert ( |
| 168 |
len(terms) == 2 |
| 169 |
and terms[0].variable() is v |
| 170 |
and terms[0].coefficient() == 10 |
| 171 |
and terms[1].variable() is v2 |
| 172 |
and terms[1].coefficient() == 1 |
| 173 |
) |
| 174 |
|
| 175 |
|
| 176 |
def test_expressions_substraction( |
| 177 |
expressions: Tuple[Expression, Expression, Term, Term, Variable, Variable], |
| 178 |
): |
| 179 |
"""Test expression substraction.""" |
| 180 |
e, e2, _, t2, v, v2 = expressions |
| 181 |
|
| 182 |
for sub, diff in zip((e - 2, 2.0 - e), (3, -3)): |
| 183 |
assert isinstance(sub, Expression) |
| 184 |
assert sub.constant() == diff |
| 185 |
terms = sub.terms() |
| 186 |
assert ( |
| 187 |
len(terms) == 1 |
| 188 |
and terms[0].variable() is v |
| 189 |
and terms[0].coefficient() == math.copysign(10, diff) |
| 190 |
) |
| 191 |
|
| 192 |
for sub2, diff in zip((e - v2, v2 - e), (5, -5)): |
| 193 |
assert isinstance(sub2, Expression) |
| 194 |
assert sub2.constant() == diff |
| 195 |
terms = sub2.terms() |
| 196 |
assert ( |
| 197 |
len(terms) == 2 |
| 198 |
and terms[0].variable() is v |
| 199 |
and terms[0].coefficient() == math.copysign(10, diff) |
| 200 |
and terms[1].variable() is v2 |
| 201 |
and terms[1].coefficient() == -math.copysign(1, diff) |
| 202 |
) |
| 203 |
|
| 204 |
for sub3, diff in zip((e - t2, t2 - e), (5, -5)): |
| 205 |
assert isinstance(sub3, Expression) |
| 206 |
assert sub3.constant() == diff |
| 207 |
terms = sub3.terms() |
| 208 |
assert ( |
| 209 |
len(terms) == 2 |
| 210 |
and terms[0].variable() is v |
| 211 |
and terms[0].coefficient() == math.copysign(10, diff) |
| 212 |
and terms[1].variable() is v2 |
| 213 |
and terms[1].coefficient() == -math.copysign(1, diff) |
| 214 |
) |
| 215 |
|
| 216 |
sub4 = e - e2 |
| 217 |
assert isinstance(sub3, Expression) |
| 218 |
assert sub4.constant() == 15 |
| 219 |
terms = sub4.terms() |
| 220 |
assert ( |
| 221 |
len(terms) == 2 |
| 222 |
and terms[0].variable() is v |
| 223 |
and terms[0].coefficient() == 10 |
| 224 |
and terms[1].variable() is v2 |
| 225 |
and terms[1].coefficient() == -1 |
| 226 |
) |
| 227 |
|
| 228 |
|
| 229 |
@pytest.mark.parametrize( |
| 230 |
"op, symbol", |
| 231 |
[ |
| 232 |
(operator.le, "<="), |
| 233 |
(operator.eq, "=="), |
| 234 |
(operator.ge, ">="), |
| 235 |
(operator.lt, None), |
| 236 |
(operator.ne, None), |
| 237 |
(operator.gt, None), |
| 238 |
], |
| 239 |
) |
| 240 |
def test_expression_rich_compare_operations(op, symbol) -> None: |
| 241 |
"""Test using comparison on variables.""" |
| 242 |
v1 = Variable("foo") |
| 243 |
v2 = Variable("bar") |
| 244 |
t1 = Term(v1, 10) |
| 245 |
e1 = t1 + 5 |
| 246 |
e2 = v2 - 10 |
| 247 |
|
| 248 |
if symbol is not None: |
| 249 |
c = op(e1, e2) |
| 250 |
assert isinstance(c, Constraint) |
| 251 |
e = c.expression() |
| 252 |
t = e.terms() |
| 253 |
assert len(t) == 2 |
| 254 |
if t[0].variable() is not v1: |
| 255 |
t = (t[1], t[0]) |
| 256 |
assert ( |
| 257 |
t[0].variable() is v1 |
| 258 |
and t[0].coefficient() == 10 |
| 259 |
and t[1].variable() is v2 |
| 260 |
and t[1].coefficient() == -1 |
| 261 |
) |
| 262 |
assert e.constant() == 15 |
| 263 |
assert c.op() == symbol and c.strength() == strength.required |
| 264 |
|
| 265 |
else: |
| 266 |
with pytest.raises(TypeError) as excinfo: |
| 267 |
op(e1, e2) |
| 268 |
if "PyPy" in sys.version: |
| 269 |
assert "Expression" in excinfo.exconly() |
| 270 |
else: |
| 271 |
assert "kiwisolver.Expression" in excinfo.exconly() |