def reduce_to_const(exp): repl_available = Flag(simplify(exp) is not exp) exp.replacement_available.connect(repl_available.set) yield exp print(dump(simplify(exp))) assert all(exp == simplify(exp)) assert isinstance(simplify(exp), Constant), type(exp.replacement) assert repl_available
def test_value_fix_1(): exp = Value(6) exp.fix() assert exp == 6 with pytest.raises(ValueError): exp.set(4) assert exp == 6 assert simplify(exp) == 6 assert isinstance(simplify(exp), Constant)
def test_interpolation(): val1 = Value(0, 1, 5, 1) val2 = Value(10, 1, 0, 2) t = Value(0) exp = Interpolation(val1, val2, t) assert all(exp == simplify(exp)) assert all(exp == (0, 1, 5, 1)) t.set(1) assert all(exp == simplify(exp)) assert all(exp == (10, 1, 0, 2)) t.set(0.5) assert all(exp == simplify(exp)) assert all(exp == (5, 1, 2.5, 1.5))
def test_fixed_falue_constant_propegation(formula, args, maybe_numpy): values = tuple(Value(a) for a in args) for value in values: value.fix() got = check_formula(maybe_numpy, formula, args, values) if got: assert isinstance(simplify(got), Constant)
def test_concat_of_slice_simplification_1(check_dump): exp = Concat(Value(0, 1), Value(2)) exp = simplify(exp[1]) check_dump(exp, """ [1:2] <1.0>: Value <0.0, 1.0> """)
def test_interpolation_ramps(): t = Value(0) for x in range(-2, 5): exp = Interpolation(0, x, t) for i in range(-10, 20): t.set(i / 10) assert exp == simplify(exp) == t * x
def test_progress_clamped(clock): exp = Progress(clock, 2, delay=1) assert exp == 0 clock.advance_sync(1) assert exp == 0 clock.advance_sync(1) assert exp == 0.5 clock.advance_sync(1) assert exp == 1 assert isinstance(simplify(exp), Constant)
def __get__(self, instance, owner=None): if instance is None: return self else: exp = self._vector_property.__get__(instance, owner) return _ComponentPropertyValue( name=self.name, parent_property=self, instance=instance, expression=simplify(exp)[self._start:self._end], start=self._start, end=self._end)
def test_complex_concat_simplification(check_dump): val1 = Value(1) val2 = Value(4, 5) cat = Concat(val1, 2, 3, val2) assert all(cat == (1, 2, 3, 4, 5)) check_dump(cat, """ Concat <1.0, 2.0, 3.0, 4.0, 5.0>: Value <1.0> Constant <2.0, 3.0> Value <4.0, 5.0> """) val1.fix() val2.fix() check_dump(simplify(cat), 'Constant <1.0, 2.0, 3.0, 4.0, 5.0>')
def test_reduce_simplification_2(check_dump, cls_name, identity, is_commutative, range_4): cls = getattr(expressions, cls_name) const = Constant(identity, identity, identity) vals = [const] * 3 if range_4 < 3: vals[range_4] = Value(identity, identity, identity) if is_commutative or range_4 == 0: expected = "Value <{0}, {0}, {0}>".format(float(identity)) else: expected = """ {1} <{0}, {0}, {0}>: Constant <{0}, {0}, {0}> Value <{0}, {0}, {0}> """.format(float(identity), cls.pretty_name) else: expected = "Constant <{0}, {0}, {0}>".format(float(identity)) exp = cls(vals) check_dump(simplify(exp), expected)
def test_slice_replace_simplification2(check_dump): val = Value(0, 1, 2) val = val.replace(1, val[1] + 3) val = val.replace(0, val[0] + 3) val = val.replace(2, val[2] + 3) check_dump(simplify(val), """ Concat <3.0, 4.0, 5.0>: + <3.0>: [0:1] <0.0>: Value <0.0, 1.0, 2.0> (&1) Constant <3.0> + <4.0>: [1:2] <1.0>: Value <0.0, 1.0, 2.0> (*1) Constant <3.0> + <5.0>: [2:3] <2.0>: Value <0.0, 1.0, 2.0> (*1) Constant <3.0> """)
def test_slice_of_concat_simplification_3(start, end, dump, check_dump): val = Concat(Value(0, 1, 2), Value(3, 4, 5)) check_dump(simplify(val[start:end]), dump)
def children(self): yield simplify(self._parent_property.__get__(self._instance))
def test_concat_of_slice_simplification_2(check_dump): exp = Value(0, 1, 2) exp = Concat(exp[0], exp[1], exp[2]) exp = simplify(exp) check_dump(exp, "Value <0.0, 1.0, 2.0>")
def test_concat_of_slice_simplification_0(i, check_dump): val = Value(0, 1, 2) exp = Concat(val[0], val[1], val[2]) val.fix() exp = simplify(exp[i]) check_dump(exp, "Constant <%s.0>" % i)
def test_simple_concat_simplification(check_dump): exp = Concat(Constant(0, 1), Constant(2, 3)) check_dump(simplify(exp), 'Constant <0.0, 1.0, 2.0, 3.0>')
def test_interpolation_simplification(): val1 = Value(0, 1, 5, 1) val2 = Value(10, 1, 0, 2) assert simplify(Interpolation(val1, val2, 0)) is val1 assert simplify(Interpolation(val1, val2, 1)) is val2
def simplify(self, node): expr, expected_map = node expr = simplify(expr) note(dump(expr)) return expr, expected_map
def __init__(self, parent_property, instance, expression): self._parent_property = parent_property self._instance = instance self.replacement = simplify(expression)
def test_slice_of_concat_simplification(start, stop, tuples): expected = sum(tuples, ())[start:stop] exp = Concat(*(Value(*t) for t in tuples))[start:stop] assert exp.get() == expected assert simplify(exp).get() == expected
def __set__(self, instance, value): exp = coerce(value, size=self._size) if id(instance) not in self._instance_expressions: finalize(instance, self._instance_expressions.pop, id(instance), None) self._instance_expressions[id(instance)] = simplify(exp)