Skip to content

Commit

Permalink
test table constraint first argument. (#428)
Browse files Browse the repository at this point in the history
* test table constraint first argument.

* add test
  • Loading branch information
Wout4 authored Jan 15, 2024
1 parent b529638 commit ee71347
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cpmpy/expressions/globalconstraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ class Table(GlobalConstraint):
"""The values of the variables in 'array' correspond to a row in 'table'
"""
def __init__(self, array, table):
array = flatlist(array)
if not all(isinstance(x, Expression) for x in array):
raise TypeError("the first argument of a Table constraint should only contain variables/expressions")
super().__init__("table", [array, table])

def decompose(self):
Expand Down
13 changes: 12 additions & 1 deletion tests/test_globalconstraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,4 +815,15 @@ def test_count(self):
a = cp.boolvar()

self.assertTrue(cp.Model([cp.Count([x,y],z) == 1]).solve())
self.assertRaises(TypeError, cp.Count, [x,y],[x,False])
self.assertRaises(TypeError, cp.Count, [x,y],[x,False])

def test_table(self):
iv = cp.intvar(-8,8,3)

constraints = [cp.Table([iv[0], [iv[1], iv[2]]], [ (5, 2, 2)])] # not flatlist, should work
model = cp.Model(constraints)
self.assertTrue(model.solve())

self.assertRaises(TypeError, cp.Table, [iv[0], iv[1], iv[2], 5], [(5, 2, 2)])
self.assertRaises(TypeError, cp.Table, [iv[0], iv[1], iv[2], [5]], [(5, 2, 2)])
self.assertRaises(TypeError, cp.Table, [iv[0], iv[1], iv[2], ['a']], [(5, 2, 2)])

0 comments on commit ee71347

Please sign in to comment.