Example #1
0
def test_pubo_addition():

    temp = PUBO({('0', '0'): 1, ('0', 1): 2})
    temp1 = {('0', ): -1, (1, '0'): 3}
    temp2 = {(1, '0'): 5}, {('0', 1): 5}
    temp3 = {('0', ): 2, (1, '0'): -1}, {('0', ): 2, ('0', 1): -1}

    # constant
    d = temp.copy()
    d += 5
    assert d in ({
        ('0', ): 1,
        (1, '0'): 2,
        (): 5
    }, {
        ('0', ): 1,
        ('0', 1): 2,
        (): 5
    })

    # __add__
    d = temp.copy()
    g = d + temp1
    assert g in temp2

    # __iadd__
    d = temp.copy()
    d += temp1
    assert d in temp2

    # __radd__
    d = temp.copy()
    g = temp1 + d
    assert g in temp2

    # __sub__
    d = temp.copy()
    g = d - temp1
    assert g in temp3

    # __isub__
    d = temp.copy()
    d -= temp1
    assert d in temp3

    # __rsub__
    d = temp.copy()
    g = temp1 - d
    assert g == PUBO(temp3[0]) * -1
Example #2
0
def test_pubo_multiplication():

    temp = PUBO({('0', '0'): 1, ('0', 1): 2})
    temp1 = {('0', ): 3, (1, '0'): 6}, {('0', ): 3, ('0', 1): 6}
    temp2 = {('0', ): .5, (1, '0'): 1}, {('0', ): .5, ('0', 1): 1}

    # constant
    d = temp.copy()
    d += 3
    d *= -2
    assert d in ({
        ('0', ): -2,
        (1, '0'): -4,
        (): -6
    }, {
        ('0', ): -2,
        ('0', 1): -4,
        (): -6
    })

    # __mul__
    d = temp.copy()
    g = d * 3
    assert g in temp1

    d = temp.copy()
    g = d * 0
    assert g == {}

    # __imul__
    d = temp.copy()
    d *= 3
    assert d in temp1

    d = temp.copy()
    d *= 0
    assert d == {}

    # __rmul__
    d = temp.copy()
    g = 3 * d
    assert g in temp1

    d = temp.copy()
    g = 0 * d
    assert g == {}

    # __truediv__
    d = temp.copy()
    g = d / 2
    assert g in temp2

    # __itruediv__
    d = temp.copy()
    d /= 2
    assert d in temp2

    # __floordiv__
    d = temp.copy()
    g = d // 2
    assert g in ({(1, '0'): 1}, {('0', 1): 1})

    # __ifloordiv__
    d = temp.copy()
    d //= 2
    assert d in ({(1, '0'): 1}, {('0', 1): 1})

    # __mul__ but with dict
    d = temp.copy()
    d *= {(1, ): 2, ('0', '0'): -1}
    assert d in ({('0', ): -1, (1, '0'): 4}, {('0', ): -1, ('0', 1): 4})

    # __pow__
    d = temp.copy()
    d -= 2
    d **= 2
    assert d == {('0', ): -3, (): 4}

    d = temp.copy()
    assert d**3 == d * d * d