def test_hypergauge_hybrid1(): # hybrid 1: same velocity of `g` and `g.max_gauge`. # (suggested by @hybrid0) g = Gauge(0, Gauge(1, 5, at=0), at=0) g.add_momentum(+1) g.max_gauge.add_momentum(+1, since=1) assert g.determination == [(0, 0), (1, 1), (5, 5)]
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)]
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_with_different_base_time(): g = Gauge(0, Gauge(10, 100, at=100), at=0) g.add_momentum(+1) assert g.max_gauge.get(0) == 10 assert g.get(10) == 10 g = Gauge(0, Gauge(10, 100, at=0), at=100) g.add_momentum(+1) assert g.max_gauge.get(100) == 10 assert g.get(110) == 10
def test_over_max_on_hypergauge(): g = Gauge(1, Gauge(10, 20, at=0), at=0) g.max_gauge.add_momentum(+1) with pytest.raises(ValueError): g.set(20, at=0) g.set(20, at=0, outbound=OK) assert g.get(at=0) == 20 g.set(20, at=10) assert g.get(at=10) == 20 assert g.get(at=0) == 20 # past was forgot
def test_decr_max_hyper(): g = Gauge(0, Gauge(10, 100, at=0), at=0) g.add_momentum(+2) g.add_momentum(-1) assert g.base[TIME] == 0 assert g.get(10) == 10 g.max_gauge.decr(5, at=10) assert g.base[TIME] == 10 assert g.get(10) == 5 assert g.get(20) == 5
def random_gauge2(random=random, far=1000, near=1, until=20): # 0 <= g <= (near ~ far) g_max = Gauge(random.uniform(near, far), far, near, at=0) value = random.uniform(0, g_max.max_value) g = Gauge(value, g_max, at=0) for x in range(0, until, 5): g_max.add_momentum(random.uniform(-far, +far), since=x, until=x + 5) for x in range(0, until, 2): g.add_momentum(random.uniform(-far, +far), since=x, until=x + 2) return g
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_set_range(): g = Gauge(0, 100, at=0) g.add_momentum(+1) assert g.determination == [(0, 0), (100, 100)] g.set_range(Gauge(100, 100, at=0), Gauge(0, 100, at=0), at=0) g.max_gauge.add_momentum(-1, until=40) g.min_gauge.add_momentum(+1, until=40) assert g.determination == [(0, 0), (60, 60)] g.clear_momenta(at=30) g.add_momentum(-1) assert g.determination == [(30, 30), (40, 40)]
def bidir(): g = Gauge(5, Gauge(10, 10, at=0), Gauge(0, 10, at=0), 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) g.max_gauge.add_momentum(-1, since=0, until=4) g.max_gauge.add_momentum(+1, since=6, until=7) g.min_gauge.add_momentum(+1, since=1, until=6) g.min_gauge.add_momentum(-1, since=6, until=8) return g
def test_case8_simple(): max_ = Gauge(10, 10, at=0) max_.add_momentum(-1) g = Gauge(10, max_, at=0) max_.forget_past(at=2) with pytest.raises(ValueError): max_.forget_past(at=1) # forget older past. assert g.get(99999) == approx(0)
def zigzag(): g = Gauge(1, Gauge(2, 3, 2, at=0), Gauge(1, 1, 0, at=0), at=0) for x in range(6): g.max_gauge.add_momentum(+1, since=x * 2, until=x * 2 + 1) g.max_gauge.add_momentum(-1, since=x * 2 + 1, until=x * 2 + 2) g.min_gauge.add_momentum(-1, since=x * 2, until=x * 2 + 1) g.min_gauge.add_momentum(+1, since=x * 2 + 1, until=x * 2 + 2) for x in range(3): t = sum(y * 2 for y in range(x + 1)) g.add_momentum(+1, since=t, until=t + (x + 1)) g.add_momentum(-1, since=t + (x + 1), until=t + 2 * (x + 1)) return g
def test_decr_max_before_base_time(): # decr max earlier than the gauge's base time. g = Gauge(0, Gauge(10, 100, at=10), at=5) g.add_momentum(+1) assert g.determination == [(5, 0), (15, 10)] with pytest.raises(ValueError): g.max_gauge.decr(5, at=0) assert g.determination == [(5, 0), (15, 10)] g.max_gauge.incr(10, at=10) assert g.determination == [(10, 5), (25, 20)]
def random_gauge1(random=random, far=10, near=3, until=20): # (-far ~ -near) <= g <= (near ~ far) g_max = Gauge(random.uniform(near, far), far, near, at=0) g_min = Gauge(random.uniform(-far, -near), -near, -far, at=0) value = random.uniform(g_min.min_value, g_max.max_value) g = Gauge(value, g_max, g_min, at=0) for x in range(0, until, 5): g_max.add_momentum(random.uniform(-far, +far), since=x, until=x + 5) for x in range(0, until, 2): g.add_momentum(random.uniform(-far, +far), since=x, until=x + 2) for x in range(0, until, 1): g_min.add_momentum(random.uniform(-far, +far), since=x, until=x + 1) return g
def test_hypergauge_zigzag2(): g = Gauge(2, Gauge(3, 5, 3, at=0), Gauge(2, 2, 0, at=0), at=0) for x in range(5): g.max_gauge.add_momentum(+1, since=x * 4, until=x * 4 + 2) g.max_gauge.add_momentum(-1, since=x * 4 + 2, until=x * 4 + 4) g.min_gauge.add_momentum(-1, since=x * 4, until=x * 4 + 2) g.min_gauge.add_momentum(+1, since=x * 4 + 2, until=x * 4 + 4) for x in range(4): t = sum(y * 2 for y in range(x + 1)) g.add_momentum(+1, since=t, until=t + (x + 1)) g.add_momentum(-1, since=t + (x + 1), until=t + 2 * (x + 1)) assert g.determination == [(0, 2), (1, 3), (2, 2), (3.5, 3.5), (4, 3), (6, 1), (8, 3), (9, 4), (11.5, 1.5), (12, 2), (14.5, 4.5), (16, 3), (18.5, 0.5), (20, 2)]
def test_case8(): """There's a hyper-gauge. When the same effects are affected twice, the underlying gauge became to be out of the limited range. """ m = Gauge(679, 679, at=1503918965.158631) m.add_momentum(+0.001157) g = Gauge(679, m, at=1503918965.158631) g.add_momentum(+1) # Gauge "g" should be always in the range of "m". def G_SHOULD_BE_FULLY_IN_RANGE(): assert g.determination.in_range_since == g.base[TIME] G_SHOULD_BE_FULLY_IN_RANGE() # first effect ------------------------------------------------------------ m.forget_past(at=1503919261.248346) G_SHOULD_BE_FULLY_IN_RANGE() m.add_momentum(0, since=1503919261.248346, until=1503919266.248346) m.forget_past(at=1503919261.248346) G_SHOULD_BE_FULLY_IN_RANGE() m.add_momentum(-0.2, since=1503919261.248346, until=1503919561.248346) G_SHOULD_BE_FULLY_IN_RANGE() # second effect ----------------------------------------------------------- m.forget_past(at=1503919279.381339) G_SHOULD_BE_FULLY_IN_RANGE() m.forget_past(at=1503919279.381339) G_SHOULD_BE_FULLY_IN_RANGE() m.add_momentum(0, since=1503919279.381339, until=1503919284.381339) G_SHOULD_BE_FULLY_IN_RANGE() m.forget_past(at=1503919279.482356) m.remove_momentum(-0.2, since=1503919261.248346, until=1503919561.248346) G_SHOULD_BE_FULLY_IN_RANGE() with pytest.raises(ValueError): m.forget_past(at=1503919279.381339) m.add_momentum(-0.2, since=1503919279.381339, until=1503919579.381339) G_SHOULD_BE_FULLY_IN_RANGE() # failing! m.forget_past(at=1503919287.680848) G_SHOULD_BE_FULLY_IN_RANGE() # failing!
def __init__(self, id, inputHandler, parent, *groups): super().__init__(groups) # rocket sprite from https://opengameart.org/content/rocket self.image = pygame.image.load("assets/cohete_bw.png") self.image = pygame.transform.scale(self.image, config.player_size) self.image.fill(config.player_color[id], special_flags=pygame.BLEND_MULT) self.originalImage = self.image.copy() self.rect = self.image.get_rect() self.id = id self.parent = parent self.keymap = config.keymaps[id] self.inputHandler = inputHandler self.angle = 0 self.shootDelay = config.shoot_delay self.direction = pygame.Vector2(0, -1) self.position = pygame.Vector2(config.starting_positions[id]) self.rect.center = self.position self.velocity = pygame.Vector2(0, 0) self.acceleration = pygame.Vector2(0, 0) self.fuel = config.starting_fuel self.fuelBar = Gauge(self.fuel, self) self.fuelBar.add(self.groups())
def test_in_range(): g = Gauge(20, 10, at=0) assert not g.in_range(0) assert not g.in_range(20) g.add_momentum(-1) assert not g.in_range(0) assert g.in_range(20)
def test_case1(): g = Gauge(0, 5, at=0) g.add_momentum(+1) g.add_momentum(-2, since=1, until=3) g.add_momentum(+1, since=5, until=7) assert g.determination == [(0, 0), (1, 1), (2, 0), (3, 0), (5, 2), (6.5, 5), (7, 5)]
def test_permanent(): g = Gauge(10, 10, at=0) g.add_momentum(-1) assert g.determination == [(0, 10), (10, 0)] g = Gauge(0, 10, at=0) g.add_momentum(+1) assert g.determination == [(0, 0), (10, 10)] g = Gauge(12, 10, at=0) g.add_momentum(-1) assert g.determination == [(0, 12), (2, 10), (12, 0)] g = Gauge(5, 10, at=0) g.add_momentum(+1, since=3) assert g.determination == [(0, 5), (3, 5), (8, 10)] g = Gauge(5, 10, at=0) g.add_momentum(+1, until=8) assert g.determination == [(0, 5), (5, 10), (8, 10)]
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_clear_momentum_events(): g = Gauge(0, 10, at=0) m = g.add_momentum(+1, since=10, until=20) assert list(g.momentum_events()) == \ [(0, NONE, None), (10, ADD, m), (20, REMOVE, m), (+inf, NONE, None)] # assert len(g._events) == 2 g.remove_momentum(m) assert list(g.momentum_events()) == [(0, NONE, None), (+inf, NONE, None)]
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_make_momentum(): g = Gauge(0, 10, at=0) m = g.add_momentum(+1) assert isinstance(m, Momentum) with pytest.raises(TypeError): g.add_momentum(m, since=1) with pytest.raises(TypeError): g.add_momentum(m, until=2)
def test_intersection_of_vertical_segment_reversed(): f = FakeGauge([(0, 0), (1e-309, -1)]) g = Gauge(-2.5, 0, f, at=-1) g.add_momentum(+2) g.add_momentum(-1) assert \ round_determination(g.determination, precision=1) == \ [(-1, -2.5), (0, -0.5), (0.5, 0)]
def test_goal(): g = Gauge(100, 100, at=0) assert g.goal() == 100 g.add_momentum(-1) assert g.goal() == 0 g.add_momentum(+1) assert g.goal() == 100 g.add_momentum(-1, since=10000, until=10001) assert g.goal() == 99
def test_thin_momenta(): g = Gauge(0, 100, at=0) for x in range(1000): g.add_momentum(+1000000000, since=x, until=x + 1e-10) assert_all_in_range(g) assert g.get(0) == 0 assert g.get(1001) == 100 for x, y in zip(range(9999), range(1, 10000)): assert 0 <= g.get(x / 10.) <= g.get(y / 10.) <= 100
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_over_max(): g = Gauge(8, 10, at=0) g.add_momentum(+1, since=0, until=4) assert g.determination == [(0, 8), (2, 10), (4, 10)] g = Gauge(12, 10, at=0) g.add_momentum(-1, since=0, until=4) assert g.determination == [(0, 12), (2, 10), (4, 8)] g = Gauge(12, 10, at=0) g.add_momentum(+1, since=0, until=4) g.add_momentum(-2, since=0, until=4) assert g.determination == [(0, 12), (1, 10), (4, 7)] g = Gauge(12, 10, at=0) g.add_momentum(+1, since=1, until=6) g.add_momentum(-1, since=3, until=8) g.add_momentum(+1, since=10, until=14) g.add_momentum(-1, since=13, until=16) assert g.determination == [(0, 12), (1, 12), (3, 12), (5, 10), (6, 10), (8, 8), (10, 8), (12, 10), (13, 10), (14, 10), (16, 8)]
def test_pickle(): g = Gauge(0, 10, at=0) r = Random(17171771) for x in range(10000): since = r.randrange(1000) until = since + 1 + r.randrange(1000) g.add_momentum(r.uniform(-10, +10), since=since, until=until) data = pickle.dumps(g) g2 = pickle.loads(data) assert g.determination == g2.determination