| 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 math |
| 9 |
import operator |
| 10 |
import sys |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
from kiwisolver import Constraint, Expression, Term, Variable, strength |
| 15 |
|
| 16 |
|
| 17 |
def test_variable_methods() -> None: |
| 18 |
"""Test the variable modification methods.""" |
| 19 |
v = Variable() |
| 20 |
assert v.name() == "" |
| 21 |
v.setName("Δ") |
| 22 |
assert v.name() == "Δ" |
| 23 |
v.setName("foo") |
| 24 |
assert v.name() == "foo" |
| 25 |
with pytest.raises(TypeError): |
| 26 |
v.setName(1) # type: ignore |
| 27 |
if sys.version_info >= (3,): |
| 28 |
with pytest.raises(TypeError): |
| 29 |
v.setName(b"r") # type: ignore |
| 30 |
|
| 31 |
assert v.value() == 0.0 |
| 32 |
|
| 33 |
assert v.context() is None |
| 34 |
ctx = object() |
| 35 |
v.setContext(ctx) |
| 36 |
assert v.context() is ctx |
| 37 |
|
| 38 |
assert str(v) == "foo" |
| 39 |
|
| 40 |
with pytest.raises(TypeError): |
| 41 |
Variable(1) # type: ignore |
| 42 |
|
| 43 |
|
| 44 |
def test_variable_neg() -> None: |
| 45 |
"""Test neg on a variable.""" |
| 46 |
v = Variable("foo") |
| 47 |
|
| 48 |
neg = -v |
| 49 |
assert isinstance(neg, Term) |
| 50 |
assert neg.variable() is v and neg.coefficient() == -1 |
| 51 |
|
| 52 |
|
| 53 |
def test_variable_mul() -> None: |
| 54 |
"""Test variable multiplications.""" |
| 55 |
v = Variable("foo") |
| 56 |
v2 = Variable("bar") |
| 57 |
|
| 58 |
for mul in (v * 2.0, 2 * v): |
| 59 |
assert isinstance(mul, Term) |
| 60 |
assert mul.variable() is v and mul.coefficient() == 2 |
| 61 |
|
| 62 |
with pytest.raises(TypeError): |
| 63 |
v * v2 # type: ignore |
| 64 |
|
| 65 |
|
| 66 |
def test_variable_division() -> None: |
| 67 |
"""Test variable divisions.""" |
| 68 |
v = Variable("foo") |
| 69 |
v2 = Variable("bar") |
| 70 |
|
| 71 |
div = v / 2.0 |
| 72 |
assert isinstance(div, Term) |
| 73 |
assert div.variable() is v and div.coefficient() == 0.5 |
| 74 |
|
| 75 |
with pytest.raises(TypeError): |
| 76 |
v / v2 # type: ignore |
| 77 |
|
| 78 |
with pytest.raises(ZeroDivisionError): |
| 79 |
v / 0 |
| 80 |
|
| 81 |
|
| 82 |
def test_variable_addition() -> None: |
| 83 |
"""Test variable additions.""" |
| 84 |
v = Variable("foo") |
| 85 |
v2 = Variable("bar") |
| 86 |
|
| 87 |
for add in (v + 2, 2.0 + v): |
| 88 |
assert isinstance(add, Expression) |
| 89 |
assert add.constant() == 2 |
| 90 |
terms = add.terms() |
| 91 |
assert ( |
| 92 |
len(terms) == 1 and terms[0].variable() is v and terms[0].coefficient() == 1 |
| 93 |
) |
| 94 |
|
| 95 |
add2 = v + v2 |
| 96 |
assert isinstance(add2, Expression) |
| 97 |
assert add2.constant() == 0 |
| 98 |
terms = add2.terms() |
| 99 |
assert ( |
| 100 |
len(terms) == 2 |
| 101 |
and terms[0].variable() is v |
| 102 |
and terms[0].coefficient() == 1 |
| 103 |
and terms[1].variable() is v2 |
| 104 |
and terms[1].coefficient() == 1 |
| 105 |
) |
| 106 |
|
| 107 |
with pytest.raises(TypeError): |
| 108 |
v + "" # type: ignore |
| 109 |
|
| 110 |
|
| 111 |
def test_variable_sub() -> None: |
| 112 |
"""Test variable substractions.""" |
| 113 |
v = Variable("foo") |
| 114 |
v2 = Variable("bar") |
| 115 |
|
| 116 |
for sub, diff in zip((v - 2, 2 - v), (-2, 2)): |
| 117 |
assert isinstance(sub, Expression) |
| 118 |
assert sub.constant() == diff |
| 119 |
terms = sub.terms() |
| 120 |
assert ( |
| 121 |
len(terms) == 1 |
| 122 |
and terms[0].variable() is v |
| 123 |
and terms[0].coefficient() == -math.copysign(1, diff) |
| 124 |
) |
| 125 |
|
| 126 |
sub2 = v - v2 |
| 127 |
assert isinstance(sub2, Expression) |
| 128 |
assert sub2.constant() == 0 |
| 129 |
terms = sub2.terms() |
| 130 |
assert ( |
| 131 |
len(terms) == 2 |
| 132 |
and terms[0].variable() is v |
| 133 |
and terms[0].coefficient() == 1 |
| 134 |
and terms[1].variable() is v2 |
| 135 |
and terms[1].coefficient() == -1 |
| 136 |
) |
| 137 |
|
| 138 |
|
| 139 |
def test_variable_rich_compare_operations() -> None: |
| 140 |
"""Test using comparison on variables.""" |
| 141 |
v = Variable("foo") |
| 142 |
v2 = Variable("Δ") |
| 143 |
|
| 144 |
for op, symbol in ((operator.le, "<="), (operator.eq, "=="), (operator.ge, ">=")): |
| 145 |
c = op(v, v2 + 1) |
| 146 |
assert isinstance(c, Constraint) |
| 147 |
e = c.expression() |
| 148 |
t = e.terms() |
| 149 |
assert len(t) == 2 |
| 150 |
if t[0].variable() is not v: |
| 151 |
t = (t[1], t[0]) |
| 152 |
assert ( |
| 153 |
t[0].variable() is v |
| 154 |
and t[0].coefficient() == 1 |
| 155 |
and t[1].variable() is v2 |
| 156 |
and t[1].coefficient() == -1 |
| 157 |
) |
| 158 |
assert e.constant() == -1 |
| 159 |
assert c.op() == symbol and c.strength() == strength.required |
| 160 |
|
| 161 |
for op in (operator.lt, operator.ne, operator.gt): |
| 162 |
with pytest.raises(TypeError): |
| 163 |
op(v, v2) |