beam: harden rollback marker validation and add shape/type cells
This commit is contained in:
1 parent
3c1961ef76
commit
a5c0ecb088
2 files changed
+24
No files matched your search
@@ -61,6 +61,8 @@ class DSU:
|
|||||||
return len(self.history)
|
return len(self.history)
|
||||||
|
|
||||||
def rollback(self, marker: int) -> None:
|
def rollback(self, marker: int) -> None:
|
||||||
|
if isinstance(marker, bool) or not isinstance(marker, int):
|
||||||
|
raise TypeError("marker must be int")
|
||||||
if marker < 0 or marker > len(self.history):
|
if marker < 0 or marker > len(self.history):
|
||||||
raise ValueError("invalid marker")
|
raise ValueError("invalid marker")
|
||||||
while len(self.history) > marker:
|
while len(self.history) > marker:
|
||||||
|
|||||||
@@ -91,5 +91,27 @@ class TestDSURollback(unittest.TestCase):
|
|||||||
DSU(-1)
|
DSU(-1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_shape_union_by_size(self):
|
||||||
|
d = DSU(8)
|
||||||
|
d.union(0,1); d.union(2,3); d.union(4,5); d.union(6,7)
|
||||||
|
d.union(1,2); d.union(4,0)
|
||||||
|
# union by size: attach smaller root under larger -> parent[4]==0, depth 2
|
||||||
|
self.assertEqual(d.parent[4], 0)
|
||||||
|
# deepest depth
|
||||||
|
maxdepth = 0
|
||||||
|
for i in range(8):
|
||||||
|
depth=0; cur=i
|
||||||
|
while d.parent[cur]!=cur:
|
||||||
|
cur=d.parent[cur]; depth+=1
|
||||||
|
maxdepth=max(maxdepth,depth)
|
||||||
|
self.assertEqual(maxdepth, 2)
|
||||||
|
|
||||||
|
def test_rollback_marker_type(self):
|
||||||
|
d = DSU(2)
|
||||||
|
for bad in (True, False, 1.5, "x"):
|
||||||
|
with self.assertRaises((TypeError, ValueError)):
|
||||||
|
d.rollback(bad)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
Reference in new issue
Block a user