def test_set_min_max(): # without momentum g = Gauge(5, 10) assert g.get_max() == 10 assert g.get_min() == 0 assert g.get() == 5 g.set_range(max=100, min=10) assert g.get_max() == 100 assert g.get_min() == 10 assert g.get() == 10 g.set_min(10) assert g.get() == 10 g.set_min(5) assert g.get() == 10 g.set_range(max=5, min=0) assert g.get_max() == 5 assert g.get_min() == 0 assert g.get() == 5 # with momentum g = Gauge(5, 10, at=0) g.add_momentum(+1) assert g.determination == [(0, 5), (5, 10)] g.set_max(50, at=0) assert g.determination == [(0, 5), (45, 50)] g.set_min(40, at=0) assert g.determination == [(0, 40), (10, 50)]
class Equilibrium(object): gauge = None speed = None def __init__(self, medium, speed): self.speed = speed self.medium = medium @property def medium(self): return self._medium @medium.setter def medium(self, medium, at=None): at = now_or(at) self._medium = medium if self.gauge is None: self.gauge = Gauge(medium, medium, medium) return value = self.gauge.clear_momenta(at) if value == medium: self.gauge._set_limits(medium, medium, at=at) return if value < medium: self.gauge.set_max(medium, at=at) velocity = +self.speed elif value > medium: self.gauge.set_min(medium, at=at) velocity = -self.speed self.gauge.add_momentum(velocity, since=at)
def test_hypergauge_case4(): g = Gauge(12, 100, at=0) g.add_momentum(+1, since=1, until=6) g.add_momentum(-1, since=3, until=8) g.set_max(Gauge(15, 15, at=0), at=0) g.max_gauge.add_momentum(-1) assert g.determination == [ (0, 12), (1, 12), (2, 13), (3, 12), (6, 9), (8, 7), (15, 0)] # bidirectional hyper-gauge g_max = Gauge(10, 10, at=0) g_max.add_momentum(-1, since=0, until=4) g_max.add_momentum(+1, since=6, until=7) g_min = Gauge(0, 10, at=0) g_min.add_momentum(+1, since=1, until=6) g_min.add_momentum(-1, since=6, until=8) g = Gauge(5, g_max, g_min, at=0) g.add_momentum(+1, since=0, until=3) g.add_momentum(-1, since=3, until=6) g.add_momentum(+1, since=6, until=9) g.add_momentum(-1, since=9, until=12) assert g.determination == [ (0, 5), (2.5, 7.5), (3, 7), (4, 6), (5.5, 4.5), (6, 5), (8, 7), (9, 7), (12, 4)] g_min.incr(1, at=5) assert g.determination == [(5, 5), (6, 6), (7, 7), (9, 7), (12, 4)]
def test_hypergauge_case2(): g = Gauge(12, 100, at=0) g.add_momentum(+1, since=1, until=6) g.add_momentum(-1, since=3, until=8) g.set_max(Gauge(15, 15, at=0), at=0) g.max_gauge.add_momentum(-1, until=4) g.max_gauge.add_momentum(+1, since=4, until=6) assert g.determination == [ (0, 12), (1, 12), (2, 13), (3, 12), (4, 11), (6, 11), (8, 9)]
def test_hypergauge_case2(): g = Gauge(12, 100, at=0) g.add_momentum(+1, since=1, until=6) g.add_momentum(-1, since=3, until=8) g.set_max(Gauge(15, 15, at=0), at=0) g.max_gauge.add_momentum(-1, until=4) g.max_gauge.add_momentum(+1, since=4, until=6) assert g.determination == [(0, 12), (1, 12), (2, 13), (3, 12), (4, 11), (6, 11), (8, 9)]
def test_hypergauge_case3(): g = Gauge(12, 100, at=0) g.add_momentum(+1, since=1, until=6) g.add_momentum(-1, since=3, until=8) g.set_max(10, at=0) g.set(12, outbound=OK, at=0) assert g.determination == [ (0, 12), (1, 12), (3, 12), (5, 10), (6, 10), (8, 8)] g.set_max(Gauge(10, 100, at=0), at=0) assert g.determination == [ (0, 12), (1, 12), (3, 12), (5, 10), (6, 10), (8, 8)]
def test_hypergauge_case3(): g = Gauge(12, 100, at=0) g.add_momentum(+1, since=1, until=6) g.add_momentum(-1, since=3, until=8) g.set_max(10, at=0) g.set(12, outbound=OK, at=0) assert g.determination == [(0, 12), (1, 12), (3, 12), (5, 10), (6, 10), (8, 8)] g.set_max(Gauge(10, 100, at=0), at=0) assert g.determination == [(0, 12), (1, 12), (3, 12), (5, 10), (6, 10), (8, 8)]
def test_decr_max_normal(): g = Gauge(0, 10, at=0) g.add_momentum(+2) g.add_momentum(-1) assert g.base[TIME] == 0 assert g.get(10) == 10 g.set_max(5, at=10) g.set(10, outbound=OK, at=10) assert g.base[TIME] == 10 assert g.get(10) == 10 assert g.get(15) == 5 assert g.get(20) == 5
def test_pickle_hypergauge(): # case 1 from :func:`test_hypergauge`. g = Gauge(12, 100, at=0) g.add_momentum(+1, since=1, until=6) g.add_momentum(-1, since=3, until=8) g.set_max(Gauge(15, 15, at=0), at=0) g.max_gauge.add_momentum(-1, until=5) assert g.determination == [ (0, 12), (1, 12), (2, 13), (3, 12), (5, 10), (6, 10), (8, 8)] assert g.max_gauge.determination == [(0, 15), (5, 10)] data = pickle.dumps(g) g2 = pickle.loads(data) assert g2.max_gauge is not None assert g2.determination == [ (0, 12), (1, 12), (2, 13), (3, 12), (5, 10), (6, 10), (8, 8)] assert g2.max_gauge.determination == [(0, 15), (5, 10)] assert g2 in g2.max_gauge.limited_gauges()
def test_repr(): g = Gauge(0, 10, at=0) assert repr(g) == '<Gauge 0.00/10.00>' g.set_min(-10, at=0) assert repr(g) == '<Gauge 0.00 between -10.00~10.00>' g.set_max(Gauge(10, 10), at=0) assert repr(g) == '<Gauge 0.00 between -10.00~<Gauge 10.00/10.00>>' m = Momentum(+100, since=10, until=20) assert repr(m) == '<Momentum +100.00/s 10.00~20.00>' m = Momentum(+100, since=10) assert repr(m) == '<Momentum +100.00/s 10.00~>' m = Momentum(+100, until=20) assert repr(m) == '<Momentum +100.00/s ~20.00>' h = Horizon(10, 20, 30) assert repr(h) == '<Line[HORIZON] 30.00 for 10.00~20.00>' r = Ray(10, 20, 30, 40) assert repr(r) == '<Line[RAY] 30.00+40.00/s for 10.00~20.00>'
def test_pickle_hypergauge(): # case 1 from :func:`test_hypergauge`. g = Gauge(12, 100, at=0) g.add_momentum(+1, since=1, until=6) g.add_momentum(-1, since=3, until=8) g.set_max(Gauge(15, 15, at=0), at=0) g.max_gauge.add_momentum(-1, until=5) assert g.determination == [(0, 12), (1, 12), (2, 13), (3, 12), (5, 10), (6, 10), (8, 8)] assert g.max_gauge.determination == [(0, 15), (5, 10)] data = pickle.dumps(g) g2 = pickle.loads(data) assert g2.max_gauge is not None assert g2.determination == [(0, 12), (1, 12), (2, 13), (3, 12), (5, 10), (6, 10), (8, 8)] assert g2.max_gauge.determination == [(0, 15), (5, 10)] assert g2 in g2.max_gauge.limited_gauges()
def test_limited_gauges(): max_g = Gauge(10, 100, at=0) g = Gauge(0, max_g, at=0) assert g in max_g.limited_gauges() g.set_max(10, at=0) assert g not in max_g.limited_gauges() # clear dead links. g.set_max(max_g, at=0) assert len(max_g.limited_gauges()) == 1 del g # NOTE: Weak references could not be collected by GC immediately in PyPy. for x in range(10): try: assert len(max_g.limited_gauges()) == 0 except AssertionError: continue else: break
def test_hypergauge_case4(): g = Gauge(12, 100, at=0) g.add_momentum(+1, since=1, until=6) g.add_momentum(-1, since=3, until=8) g.set_max(Gauge(15, 15, at=0), at=0) g.max_gauge.add_momentum(-1) assert g.determination == [(0, 12), (1, 12), (2, 13), (3, 12), (6, 9), (8, 7), (15, 0)] # bidirectional hyper-gauge g_max = Gauge(10, 10, at=0) g_max.add_momentum(-1, since=0, until=4) g_max.add_momentum(+1, since=6, until=7) g_min = Gauge(0, 10, at=0) g_min.add_momentum(+1, since=1, until=6) g_min.add_momentum(-1, since=6, until=8) g = Gauge(5, g_max, g_min, at=0) g.add_momentum(+1, since=0, until=3) g.add_momentum(-1, since=3, until=6) g.add_momentum(+1, since=6, until=9) g.add_momentum(-1, since=9, until=12) assert g.determination == [(0, 5), (2.5, 7.5), (3, 7), (4, 6), (5.5, 4.5), (6, 5), (8, 7), (9, 7), (12, 4)] g_min.incr(1, at=5) assert g.determination == [(5, 5), (6, 6), (7, 7), (9, 7), (12, 4)]