def test_set_get(self) -> None: cc = CubeCanvas() addr = Addr(cc, None, 1) self.assertEqual(cc[addr], None) cc[addr] = 0 self.assertEqual(cc[addr], 0) cc[addr] = 1 self.assertEqual(cc[addr], 1)
def test_actioncanvas_jump(self) -> None: ca = ActionCanvas.make() as0 = Addr(ca, 'situation', 0) as1 = Addr(ca, 'situation', 1) aa0 = Addr(ca, 'action', 0) aa1 = Addr(ca, 'action', 1) self.assertEqual(ca.jump(as0, NEXT), {as1}) self.assertEqual(ca.jump(as0, PREV), empty_set) self.assertEqual(ca.jump(as0, ACTION), {aa0}) self.assertEqual(ca.jump(as0, SITUATION), {as0}) self.assertEqual(ca.jump(aa0, ACTION), {aa0}) self.assertEqual(ca.jump(aa0, SITUATION), {as0}) with self.assertRaises(RelationUnknownToCanvas) as cm: ca.jump(as0, 'undefined') self.assertEqual(cm.exception, RelationUnknownToCanvas(ca, as0, 'undefined'))
def single_jump(self, addr: Addr, relation: Value) -> Set[Addr]: if relation not in self.relations: raise RelationUnknownToCanvas( canvas=self, addr=addr, relation=relation ) i = addr.index_as_int() try: return {replace(addr, index=self.jump_table[(i, relation)])} except KeyError: return set()
def test_jump(self) -> None: cc = CubeCanvas() for i in range(1, 9): for relation in (Horiz, Vert, Diag): # jumping across the same kind of line twice should return # you to the original corner addr1 = Addr(cc, None, i) addr2 = cc.jump_or_fizzle(addr1, relation) addr3 = cc.jump_or_fizzle(addr2, relation) self.assertNotEqual(addr2, addr1) self.assertEqual(addr3, addr1)
def all_addrs(self) -> Iterable[Addr]: return (Addr(self, None, i) for i in range(len(self._cells)))
def __setitem__(self, addr: Addr, v: Value) -> None: i = addr.index_as_int() if i < 1 or i > len(self._cells): raise NotImplementedError # TODO raise specific exception self._cells[i - 1].set_contents(v)
def __getitem__(self, addr: Addr) -> CellContents: i = addr.index_as_int() if i < 1 or i > len(self._cells): raise NotImplementedError # TODO raise specific exception return self._cells[i - 1].contents
def __init__(self): self._cells = [ Cell(Addr(self, None, i)) for i in range(1, 9) ]