Exemple #1
0
def test_term_rich_compare_operations(op, symbol):
    """Test using comparison on variables.

    """
    v = Variable('foo')
    v2 = Variable('bar')
    t1 = Term(v, 10)
    t2 = Term(v2, 20)

    if symbol is not None:
        c = op(t1, t2 + 1)
        assert isinstance(c, Constraint)
        e = c.expression()
        t = e.terms()
        assert len(t) == 2
        if t[0].variable() is not v:
            t = (t[1], t[0])
        assert (t[0].variable() is v and t[0].coefficient() == 10
                and t[1].variable() is v2 and t[1].coefficient() == -20)
        assert e.constant() == -1
        assert c.op() == symbol and c.strength() == strength.required

    else:
        with pytest.raises(TypeError):
            op(t1, t2)
Exemple #2
0
def test_term_rich_compare_operations():
    """Test using comparison on variables.

    """
    v = Variable('foo')
    v2 = Variable('bar')
    t1 = Term(v, 10)
    t2 = Term(v2, 20)

    for op, symbol in ((operator.le, '<='), (operator.eq, '=='), (operator.ge,
                                                                  '>=')):
        c = op(t1, t2 + 1)
        assert isinstance(c, Constraint)
        e = c.expression()
        t = e.terms()
        assert len(t) == 2
        if t[0].variable() is not v:
            t = (t[1], t[0])
        assert (t[0].variable() is v and t[0].coefficient() == 10
                and t[1].variable() is v2 and t[1].coefficient() == -20)
        assert e.constant() == -1
        assert c.op() == symbol and c.strength() == strength.required

    for op in (operator.lt, operator.ne, operator.gt):
        with pytest.raises(TypeError):
            op(v, v2)
Exemple #3
0
def terms():
    """Terms for testing.

    """
    v = Variable('foo')
    v2 = Variable('bar')
    t = Term(v, 10)
    t2 = Term(v2)

    return t, t2, v, v2
def expressions():
    """Build expressions, terms and variables to test operations.

    """
    v = Variable('foo')
    v2 = Variable('bar')
    t = Term(v, 10)
    t2 = Term(v2)
    e = t + 5
    e2 = v2 - 10
    return e, e2, t, t2, v, v2
def test_expression_rich_compare_operations(op, symbol):
    """Test using comparison on variables.

    """
    v1 = Variable('foo')
    v2 = Variable('bar')
    t1 = Term(v1, 10)
    e1 = t1 + 5
    e2 = v2 - 10

    if symbol is not None:
        c = op(e1, e2)
        assert isinstance(c, Constraint)
        e = c.expression()
        t = e.terms()
        assert len(t) == 2
        if t[0].variable() is not v1:
            t = (t[1], t[0])
        assert (t[0].variable() is v1 and t[0].coefficient() == 10
                and t[1].variable() is v2 and t[1].coefficient() == -1)
        assert e.constant() == 15
        assert c.op() == symbol and c.strength() == strength.required

    else:
        with pytest.raises(TypeError) as excinfo:
            op(e1, e2)
        assert "kiwisolver.Expression" in excinfo.exconly()
Exemple #6
0
def test_term_creation():
    """Test the Term constructor.

    """
    v = Variable('foo')
    t = Term(v)
    assert t.variable() is v
    assert t.coefficient() == 1

    t = Term(v, 100)
    assert t.variable() is v
    assert t.coefficient() == 100

    assert str(t) == '100 * foo'
Exemple #7
0
def test_term_creation():
    """Test the Term constructor.

    """
    v = Variable('foo')
    t = Term(v)
    assert t.variable() is v
    assert t.coefficient() == 1

    t = Term(v, 100)
    assert t.variable() is v
    assert t.coefficient() == 100

    assert str(t) == '100 * foo'

    with pytest.raises(TypeError) as excinfo:
        Term('')
    assert 'Variable' in excinfo.exconly()

    # ensure we test garbage collection
    del t
    gc.collect()
Exemple #8
0
def test_term_creation():
    """Test the Term constructor.

    """
    v = Variable('foo')
    t = Term(v)
    assert t.variable() is v
    assert t.coefficient() == 1

    t = Term(v, 100)
    assert t.variable() is v
    assert t.coefficient() == 100

    assert str(t) == '100 * foo'
Exemple #9
0
def test_term_creation():
    """Test the Term constructor.

    """
    v = Variable('foo')
    t = Term(v)
    assert t.variable() is v
    assert t.coefficient() == 1

    t = Term(v, 100)
    assert t.variable() is v
    assert t.coefficient() == 100

    assert str(t) == '100 * foo'

    with pytest.raises(TypeError) as excinfo:
        Term('')
    assert 'Variable' in excinfo.exconly()

    # ensure we test garbage collection
    del t
    gc.collect()
Exemple #10
0
def test_expression_arith_operators():
    """Test the arithmetic operation on terms.

    """
    v = Variable('foo')
    v2 = Variable('bar')
    t = Term(v, 10)
    t2 = Term(v2)
    e = t + 5
    e2 = v2 - 10

    neg = -e
    assert isinstance(neg, Expression)
    neg_t = neg.terms()
    assert (len(neg_t) == 1 and neg_t[0].variable() is v
            and neg_t[0].coefficient() == -10 and neg.constant() == -5)

    mul = e * 2
    assert isinstance(mul, Expression)
    mul_t = mul.terms()
    assert (len(mul_t) == 1 and mul_t[0].variable() is v
            and mul_t[0].coefficient() == 20 and mul.constant() == 10)

    with pytest.raises(TypeError):
        e * v

    div = e / 2
    assert isinstance(div, Expression)
    div_t = div.terms()
    assert (len(div_t) == 1 and div_t[0].variable() is v
            and div_t[0].coefficient() == 5 and div.constant() == 2.5)

    with pytest.raises(TypeError):
        e / v2

    add = e + 2
    assert isinstance(add, Expression)
    assert add.constant() == 7
    terms = add.terms()
    assert (len(terms) == 1 and terms[0].variable() is v
            and terms[0].coefficient() == 10)

    add2 = e + v2
    assert isinstance(add2, Expression)
    assert add2.constant() == 5
    terms = add2.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == 1)

    add3 = e + t2
    assert isinstance(add3, Expression)
    assert add3.constant() == 5
    terms = add3.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == 1)

    add4 = e + e2
    assert isinstance(add4, Expression)
    assert add4.constant() == -5
    terms = add4.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == 1)

    sub = e - 2
    assert isinstance(sub, Expression)
    assert sub.constant() == 3
    terms = sub.terms()
    assert (len(terms) == 1 and terms[0].variable() is v
            and terms[0].coefficient() == 10)

    sub2 = e - v2
    assert isinstance(sub2, Expression)
    assert sub2.constant() == 5
    terms = sub2.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == -1)

    sub3 = e - t2
    assert isinstance(sub3, Expression)
    assert sub3.constant() == 5
    terms = sub3.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == -1)

    sub4 = e - e2
    assert isinstance(sub3, Expression)
    assert sub4.constant() == 15
    terms = sub4.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == -1)
Exemple #11
0
def test_term_arith_operators():
    """Test the arithmetic operation on terms.

    """
    v = Variable('foo')
    v2 = Variable('bar')
    t = Term(v, 10)
    t2 = Term(v2)

    neg = -t
    assert isinstance(neg, Term)
    assert neg.variable() is v and neg.coefficient() == -10

    mul = t * 2
    assert isinstance(mul, Term)
    assert mul.variable() is v and mul.coefficient() == 20

    with pytest.raises(TypeError):
        t * v

    div = t / 2
    assert isinstance(div, Term)
    assert div.variable() is v and div.coefficient() == 5

    with pytest.raises(TypeError):
        t / v2

    add = t + 2
    assert isinstance(add, Expression)
    assert add.constant() == 2
    terms = add.terms()
    assert (len(terms) == 1 and terms[0].variable() is v
            and terms[0].coefficient() == 10)

    add2 = t + v2
    assert isinstance(add2, Expression)
    assert add2.constant() == 0
    terms = add2.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == 1)

    add2 = t + t2
    assert isinstance(add2, Expression)
    assert add2.constant() == 0
    terms = add2.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == 1)

    sub = t - 2
    assert isinstance(sub, Expression)
    assert sub.constant() == -2
    terms = sub.terms()
    assert (len(terms) == 1 and terms[0].variable() is v
            and terms[0].coefficient() == 10)

    sub2 = t - v2
    assert isinstance(sub2, Expression)
    assert sub2.constant() == 0
    terms = sub2.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == -1)

    sub2 = t - t2
    assert isinstance(sub2, Expression)
    assert sub2.constant() == 0
    terms = sub2.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == -1)