def test_properties(): temp = PCSO({('0', '0'): 1, ('0', 1): 2}) assert temp.offset == 1 d = PCSO() d[(0, )] += 1 d[(1, )] += 2 assert d == d.to_quso() == {(0, ): 1, (1, ): 2} assert d.mapping == d.reverse_mapping == {0: 0, 1: 1} d.set_mapping({1: 0, 0: 1}) assert d.to_quso() == {(1, ): 1, (0, ): 2} assert d.mapping == d.reverse_mapping == {0: 1, 1: 0} assert d.constraints == {} temp = d.copy() d.add_constraint_eq_zero(temp) assert d.constraints == {'eq': [temp]} # an old bug d = PCSO() d.set_mapping({0: 0}) d[(0, )] += 1 assert d.num_binary_variables == 1 assert d.variables == {0}
def test_quso_addition(): temp = PCSO({('0', '0'): 1, ('0', 1): 2}) temp1 = {('0', ): -1, (1, '0'): 3} temp2 = { (1, '0'): 5, (): 1, ('0', ): -1 }, { ('0', 1): 5, (): 1, ('0', ): -1 } temp3 = { (): 1, (1, '0'): -1, ('0', ): 1 }, { (): 1, ('0', 1): -1, ('0', ): 1 } # constant d = temp.copy() d += 5 assert d in ({(1, '0'): 2, (): 6}, {('0', 1): 2, (): 6}) # __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 == PCSO(temp3[0]) * -1
def test_quso_multiplication(): temp = PCSO({('0', '0'): 1, ('0', 1): 2}) temp1 = {(): 3, (1, '0'): 6}, {(): 3, ('0', 1): 6} temp2 = {(): .5, (1, '0'): 1}, {(): .5, ('0', 1): 1} temp3 = {(1, '0'): 1}, {('0', 1): 1} # constant d = temp.copy() d += 3 d *= -2 assert d in ({(1, '0'): -4, (): -8}, {('0', 1): -4, (): -8}) # __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 temp3 # __ifloordiv__ d = temp.copy() d //= 2 assert d in temp3 # __mul__ but with dict d = temp.copy() d *= {(1, ): 2, ('0', '0'): -1} assert d in ({ (1, ): 2, (): -1, ('0', ): 4, ('0', 1): -2 }, { (1, ): 2, (): -1, ('0', ): 4, (1, '0'): -2 }) # __pow__ d = temp.copy() d -= 2 d **= 2 assert d in ({(): 5, ('0', 1): -4}, {(): 5, (1, '0'): -4}) d = temp.copy() assert d**2 == d * d assert d**3 == d * d * d d = PCSO({('0', 1): 1, ('1', 2): -1})**2 assert d**4 == d * d * d * d