def chain_test(n: int64) -> None: """ This is the standard DeltaBlue benchmark. A long chain of equality constraints is constructed with a stay constraint on one end. An edit constraint is then added to the opposite end and the time is measured for adding and removing this constraint, and extracting and executing a constraint satisfaction plan. There are two cases. In case 1, the added constraint is stronger than the stay constraint and values must propagate down the entire length of the chain. In case 2, the added constraint is weaker than the stay constraint so it cannot be accomodated. The cost in this case is, of course, very low. Typical situations lie somewhere between these two extremes. """ planner = recreate_planner() prev: Variable | None = None first: Variable | None = None last: Variable | None = None # We need to go up to n inclusively. i: int64 = 0 end: int64 = n + 1 while i < n + 1: name = "v%s" % box(i) v = Variable(name) if prev is not None: EqualityConstraint(prev, v, REQUIRED) if i == 0: first = v if i == n: last = v prev = v i = i + 1 first = cast(Variable, first) last = cast(Variable, last) StayConstraint(last, STRONG_DEFAULT) edit = EditConstraint(first, PREFERRED) edits: CheckedList[UrnaryConstraint] = [] edits.append(edit) plan = planner.extract_plan_from_constraints(edits) i = 0 while i < 100: first.value = i plan.execute() if last.value != i: print("Chain test failed.") i = i + 1
def fn(self, pkt: Optional[Packet], r: TaskRec) -> Task: d: DeviceTaskRec = cast(DeviceTaskRec, r) if pkt is None: pkt = d.pending if pkt is None: return self.waitTask() else: d.pending = None return self.qpkt(pkt) else: d.pending = pkt if tracing: trace(box(pkt.datum)) return cast(Task, self.hold())
def test_cast_subtype(self): class Base: pass class Sub(Base): pass s = Sub() self.assertIs(cast(Base, s), s)
def test_cast_generic_type(self): T = TypeVar("T") class G(Generic[T]): pass g = G() self.assertIs(cast(G[int], g), g)
def fn(self, pkt: Packet | None, r: TaskRec) -> Task | None: i: IdleTaskRec = cast(IdleTaskRec, r) i.count -= 1 if i.count == 0: return self.hold() elif i.control & 1 == 0: i.control //= 2 return self.release(I_DEVA) else: i.control = i.control // 2 ^ 0xd008 return self.release(I_DEVB)
def fn(self, pkt: Optional[Packet], r: TaskRec) -> Optional[Task]: i: IdleTaskRec = cast(IdleTaskRec, r) i.count -= 1 if i.count == 0: return self.hold() elif i.control & 1 == 0: i.control //= 2 # TODO: We should automatically support an INVOKE_FUNCTION here # if we make IdleTask final, and support primitives in method calls return Task.release(self, int64(I_DEVA)) else: i.control = i.control // 2 ^ 0xd008 return Task.release(self, int64(I_DEVB))
def projection_test(n: int64) -> None: """ This test constructs a two sets of variables related to each other by a simple linear transformation (scale and offset). The time is measured to change a variable on either side of the mapping and to change the scale and offset factors. """ planner = recreate_planner() scale = Variable("scale", 10) offset = Variable("offset", 1000) src: Variable | None = None dests: CheckedList[Variable] = [] i: int64 = 0 while i < n: bi = box(i) src = Variable("src%s" % bi, i) dst = Variable("dst%s" % bi, i) dests.append(dst) StayConstraint(src, NORMAL) ScaleConstraint(src, scale, offset, dst, REQUIRED) i = i + 1 src = cast(Variable, src) change(src, 17) if dst.value != 1170: print("Projection 1 failed") change(dst, 1050) if src.value != 5: print("Projection 2 failed") change(scale, 5) i = 0 while i < n - 1: if dests[i].value != (i * 5 + 1000): print("Projection 3 failed") i = i + 1 change(offset, 2000) i = 0 while i < n - 1: if dests[i].value != (i * 5 + 2000): print("Projection 4 failed") i = i + 1
def fn(self, pkt: Optional[Packet], r: TaskRec) -> Task: h: HandlerTaskRec = cast(HandlerTaskRec, r) if pkt is not None: if pkt.kind == int64(K_WORK): h.workInAdd(pkt) else: h.deviceInAdd(pkt) work: Optional[Packet] = h.work_in if work is None: return self.waitTask() count: int64 = work.datum if count >= int64(BUFSIZE): h.work_in = work.link return self.qpkt(work) dev: Optional[Packet] = h.device_in if dev is None: return self.waitTask() h.device_in = dev.link dev.datum = work.data[count] work.datum = count + 1 return self.qpkt(dev)
def fn(self, pkt: Packet | None, r: TaskRec) -> Task: h: HandlerTaskRec = cast(HandlerTaskRec, r) if pkt is not None: if pkt.kind == K_WORK: h.workInAdd(pkt) else: h.deviceInAdd(pkt) work = h.work_in if work is None: return self.waitTask() count = work.datum if count >= BUFSIZE: h.work_in = work.link return self.qpkt(work) dev = h.device_in if dev is None: return self.waitTask() h.device_in = dev.link dev.datum = work.data[count] work.datum = count + 1 return self.qpkt(dev)
def fn(self, pkt: Optional[Packet], r: TaskRec) -> Task: w: WorkerTaskRec = cast(WorkerTaskRec, r) if pkt is None: return self.waitTask() if w.destination == int64(I_HANDLERA): dest: int64 = int64(I_HANDLERB) else: dest = int64(I_HANDLERA) w.destination = dest pkt.ident = dest pkt.datum = 0 i = 0 while i < BUFSIZE: x: int64 = w.count + 1 w.count = x if w.count > 26: w.count = 1 pkt.data[i] = int64(A) + w.count - 1 i = i + 1 return self.qpkt(pkt)
def fn(self, pkt: Packet | None, r: TaskRec) -> Task: w: WorkerTaskRec = cast(WorkerTaskRec, r) if pkt is None: return self.waitTask() if w.destination == I_HANDLERA: dest = I_HANDLERB else: dest = I_HANDLERA w.destination = dest pkt.ident = dest pkt.datum = 0 i = 0 while i < BUFSIZE: x = w.count + 1 w.count = x if w.count > 26: w.count = 1 pkt.data[i] = A + w.count - 1 i = i + 1 return self.qpkt(pkt)
def findtcb(self, id) -> "Task": t = taskWorkArea.taskTab[id] if t is None: pass return cast(Task, t)
def test_cast_type_too_complex(self): with self.assertRaisesRegex(ValueError, r"cast expects type or Optional\[T\]"): cast(Union[int, str], int)
def findtcb(self, id: int64) -> Task: t = taskWorkArea.taskTab[id] return cast(Task, t)
def test_cast_optional(self): self.assertIs(cast(Optional[int], None), None) self.assertIs(cast(int | None, None), None) self.assertIs(cast(None | int, None), None)
def test_cast_fail(self): with self.assertRaisesRegex(TypeError, "expected int, got str"): cast(int, "foo")
def test_cast(self): self.assertIs(cast(int, 2), 2)