Skip to content

Commit

Permalink
Merge pull request #744 from Joao-Dionisio/expand_testing
Browse files Browse the repository at this point in the history
Add some tests. Remove unnecessary if __name__ == __main__
  • Loading branch information
mmghannam authored Nov 6, 2023
2 parents 63db0c8 + a31ab0f commit 11f8f8f
Show file tree
Hide file tree
Showing 28 changed files with 109 additions and 126 deletions.
5 changes: 1 addition & 4 deletions tests/test_alldiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,4 @@ def test_main():
out = ''
for col in range(9):
out += str(round(scip.getVal(x[row,col]))) + ' '
print(out)

if __name__ == "__main__":
test_main()
print(out)
6 changes: 1 addition & 5 deletions tests/test_benders.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,4 @@ def test_flpbenders():
# the solution will be lost
master.freeBendersSubproblems()

assert master.getObjVal() == 5.61e+03


if __name__ == "__main__":
test_flpbenders()
assert master.getObjVal() == 5.61e+03
5 changes: 1 addition & 4 deletions tests/test_branch_probing_lp.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,4 @@ def test_branching():
print("t", m.getVal(t))

assert my_branchrule.was_called_val
assert my_branchrule.was_called_int

if __name__ == "__main__":
test_branching()
assert my_branchrule.was_called_int
17 changes: 16 additions & 1 deletion tests/test_cons.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,19 @@ def test_getConsVars():

c = m.addCons(quicksum(x[i] for i in x) <= 1)
assert m.getConsVars(c) == [x[i] for i in x]


def test_constraint_option_setting():
m = Model()
x = m.addVar()
c = m.addCons(x >= 3)

for option in [True, False]:
m.setCheck(c, option)
m.setEnforced(c, option)
m.setRemovable(c, option)
m.setInitial(c, option)

assert c.isChecked() == option
assert c.isEnforced() == option
assert c.isRemovable() == option
assert c.isInitial() == option
5 changes: 1 addition & 4 deletions tests/test_conshdlr.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,4 @@ def create_model():
assert "consdisable" in calls
#assert "consdelvars" in calls
#assert "consprint" in calls
assert "consgetnvars" in calls

if __name__ == "__main__":
test_conshdlr()
assert "consgetnvars" in calls
3 changes: 0 additions & 3 deletions tests/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,3 @@ def test_copy():
s2.optimize()

assert s.getObjVal() == s2.getObjVal()

if __name__ == "__main__":
test_copy()
15 changes: 8 additions & 7 deletions tests/test_customizedbenders.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def make_data():
return I,J,d,M,f,c


def test_flpbenders_defcuts():
def flpbenders_defcuts_test():
'''
test the Benders' decomposition plugins with the facility location problem.
'''
Expand Down Expand Up @@ -235,7 +235,7 @@ def test_flpbenders_defcuts():

return master.getObjVal()

def test_flpbenders_customcuts():
def flpbenders_customcuts_test():
'''
test the Benders' decomposition plugins with the facility location problem.
'''
Expand Down Expand Up @@ -285,7 +285,7 @@ def test_flpbenders_customcuts():

return master.getObjVal()

def test_flp():
def flp_test():
'''
test the Benders' decomposition plugins with the facility location problem.
'''
Expand All @@ -309,10 +309,11 @@ def test_flp():

return master.getObjVal()

if __name__ == "__main__":
defcutsobj = test_flpbenders_defcuts()
customcutsobj = test_flpbenders_customcuts()
monolithicobj = test_flp()

def test_customized_benders():
defcutsobj = flpbenders_defcuts_test()
customcutsobj = flpbenders_customcuts_test()
monolithicobj = flp_test()

assert defcutsobj == customcutsobj
assert defcutsobj == monolithicobj
4 changes: 0 additions & 4 deletions tests/test_cutsel.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,3 @@ def test_cut_selector():
scip.setObjective(quicksum(c[i] * e[i] for i in range(len(A))), sense='minimize')

scip.optimize()


if __name__ == "__main__":
test_cut_selector()
3 changes: 0 additions & 3 deletions tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,3 @@ def test_event():
assert 'eventexit' in calls
assert 'eventexec' in calls
assert len(calls) == 3

if __name__ == "__main__":
test_event()
3 changes: 0 additions & 3 deletions tests/test_gomory.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,3 @@ def test_CKS():
# solve problem
s.optimize()
s.printStatistics()

if __name__ == "__main__":
test_CKS()
3 changes: 0 additions & 3 deletions tests/test_heur.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,3 @@ def inner():
heur_prox.name

assert is_memory_freed()

if __name__ == "__main__":
test_heur()
5 changes: 1 addition & 4 deletions tests/test_knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,4 @@ def test_knapsack():
print ("\tIncluded Weight:", weights[i]*solValue, "\tItem Cost:", costs[i]*solValue)

includedWeight = sum([weights[i]*varSolutions[i] for i in range(len(weights))])
assert includedWeight > 0 and includedWeight <= knapsackSize

if __name__ == "__main__":
test_knapsack()
assert includedWeight > 0 and includedWeight <= knapsackSize
3 changes: 0 additions & 3 deletions tests/test_lp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,3 @@ def test_lp():
solval = myLP.solve()

assert round(5.0 == solval)

if __name__ == "__main__":
test_lp()
6 changes: 1 addition & 5 deletions tests/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,4 @@ def test_freed():
assert is_memory_freed()

def test_print_memory_in_use():
print_memory_in_use()

if __name__ == "__main__":
test_not_freed()
test_freed()
print_memory_in_use()
34 changes: 34 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ def test_multiple_cons_names():

assert len(conss) == 5
assert all([c.name.startswith(name + "_") for c in conss])
assert conss == m.getConss()
assert m.getNConss() == 5


def test_multiple_cons_params():
Expand Down Expand Up @@ -170,6 +172,38 @@ def assert_conss_neq(a, b):

assert_conss_neq(conss[0], conss[1])

def test_addCoefLinear():
m = Model()
x = m.addVar(obj=1)
y = m.addVar(obj=0)
c = m.addCons(x >= 1)

m.addCoefLinear(c, y, 1)

m.optimize()
assert m.getVal(x) == 0

def test_delCoefLinear():
m = Model()
x = m.addVar(obj=1)
y = m.addVar(obj=0)
c = m.addCons(x + y >= 1)

m.delCoefLinear(c,y)

m.optimize()
assert m.getVal(x) == 1

def test_chgCoefLinear():
m = Model()
x = m.addVar(obj=10)
y = m.addVar(obj=1)
c = m.addCons(x + y >= 1)

m.chgCoefLinear(c, y, 0.001)

m.optimize()
assert m.getObjVal() == 10

def test_model_ptr():
model1 = Model()
Expand Down
10 changes: 7 additions & 3 deletions tests/test_nlrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def test_nlrow():

# collect nonlinear rows
nlrows = m.getNlRows()

# test getNNlRows
assert len(nlrows) == m.getNNlRows()

# to test printing of NLRows
for row in nlrows:
m.printNlRow(row)

# the nlrow that corresponds to the linear (third) constraint is added before the nonlinear rows,
# because Initsol of the linear conshdlr gets called first
Expand Down Expand Up @@ -65,6 +72,3 @@ def test_nlrow():

linterms = nlrows[2].getLinearTerms()
assert len(linterms) == 0

if __name__ == "__main__":
test_nlrow()
6 changes: 1 addition & 5 deletions tests/test_nodesel.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,4 @@ def test_nodesel():
m.addCons(x1 * x2 >= x0)

m.setObjective(x1 + x0)
m.optimize()


if __name__ == "__main__":
test_nodesel()
m.optimize()
9 changes: 1 addition & 8 deletions tests/test_nonlinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,4 @@ def test_quad_coeffs():
assert quadterms[0][1] == 0.5

assert linterms[0][0].name == z.name
assert linterms[0][1] == 4

if __name__ == "__main__":
test_string_poly()
test_string()
test_circle()
test_gastrans()
test_quad_coeffs()
assert linterms[0][1] == 4
14 changes: 7 additions & 7 deletions tests/test_pricer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def pricerredcost(self):

objval = 1 + subMIP.getObjVal()


# Adding the column to the master problem
if objval < -1e-08:
currentNumVar = len(self.data['var'])
Expand All @@ -53,6 +54,9 @@ def pricerredcost(self):

newPattern.append(coeff)

# Testing getVarRedcost
assert round(self.model.getVarRedcost(newVar),6) == round(objval,6)

# Storing the new variable in the pricer data.
self.data['patterns'].append(newPattern)
self.data['var'].append(newVar)
Expand Down Expand Up @@ -112,6 +116,7 @@ def test_cuttingstock():
pricer.data['demand'] = demand
pricer.data['rollLength'] = rollLength
pricer.data['patterns'] = patterns
pricer.data['redcosts'] = []

# solve problem
s.optimize()
Expand Down Expand Up @@ -142,7 +147,7 @@ def test_cuttingstock():
print(outline)

print('\t\t\tTotal Output:\t', '\t'.join(str(e) for e in widthOutput))

assert s.getObjVal() == 452.25

def test_incomplete_pricer():
Expand All @@ -155,9 +160,4 @@ class IncompletePricer(Pricer):
model.includePricer(pricer, "", "")

with pytest.raises(Exception):
model.optimize()


if __name__ == '__main__':
test_cuttingstock()
test_incomplete_pricer()
model.optimize()
6 changes: 1 addition & 5 deletions tests/test_quadcons.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,4 @@ def test_niceqcqp():
s.optimize()

assert round(s.getVal(x)) == 1.0
assert round(s.getVal(y)) == 1.0

if __name__ == "__main__":
test_niceqp()
test_niceqcqp()
assert round(s.getVal(y)) == 1.0
5 changes: 0 additions & 5 deletions tests/test_quickprod.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,3 @@ def test_largequadratic():
assert cons.expr.degree() == 2*dim*dim
m.addCons(cons)
# TODO: what can we test beyond the lack of crashes?

if __name__ == "__main__":
test_quickprod()
test_quickprod_model()
test_largequadratic()
5 changes: 0 additions & 5 deletions tests/test_quicksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,3 @@ def test_largequadratic():
assert len(cons.expr.terms) == dim * (dim-1) / 2 + dim
m.addCons(cons)
# TODO: what can we test beyond the lack of crashes?

if __name__ == "__main__":
test_quicksum()
test_quicksum_model()
test_largequadratic()
14 changes: 5 additions & 9 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ def createFile(filename):
def deleteFile(filename):
os.remove(filename)

def test():
def test_sudoku_reader():
createFile("tmp.sod")

m = Model("soduko")
m = Model("sudoku")
reader = SudokuReader()

m.includeReader(reader, "sodreader", "PyReader for soduko problem", "sod")
m.includeReader(reader, "sudreader", "PyReader for sudoku problem", "sod")

m.readProblem("tmp.sod")

Expand All @@ -76,10 +76,6 @@ def test():
m.writeProblem("model.sod")
with open("model.sod", "r") as f:
input = f.readline()
assert input == "soduko"
assert input == "sudoku"

deleteFile("model.sod")


if __name__ == "__main__":
test()
deleteFile("model.sod")
6 changes: 1 addition & 5 deletions tests/test_relax.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,4 @@ def test_relax():
m.optimize()

assert 'relaxexec' in calls
assert len(calls) >= 1


if __name__ == "__main__":
test_relax()
assert len(calls) >= 1
Loading

0 comments on commit 11f8f8f

Please sign in to comment.