Exemplo n.º 1
0
def test_func_parse_descriptor_log():
    f = Curve.parse_descriptor({'$log': 10})
    assert f.y(0) == approx(math.log(10), abs=0.01)
    f = Curve.parse_descriptor({'$log': {'@args': [10], 'base': 10}})
    assert f.y(0) == 1
    f = Curve.parse_descriptor({'$log2': 16})
    assert f.y(0) == 4
Exemplo n.º 2
0
def test_func_parse_descriptor_add():
    f = Curve.parse_descriptor({'$add': [1, 2]})
    assert f.y(0) == 3
    f = Curve.parse_descriptor({'$add': [1, 2, 3]})
    assert f.y(0) == 6
    f = Curve.parse_descriptor({'$add': [1]})
    assert f.y(0) == 1
Exemplo n.º 3
0
def test_first():
    funcs = [[(0, None), (1, 12), (2, None)], [(0, 1), (1, 1), (2, 1)]]
    first = Curve.first(funcs)
    assert first(-1) is None
    assert first(0) == 1
    assert first(1) == 12
    assert first(2) == 1
    assert first(3) is None

    funcs = [[(0, None), (1, 12), (2, None)], 1]
    first = Curve.first(funcs)
    assert first(-1) == 1
    assert first(0) == 1
    assert first(1) == 12
    assert first(2) == 1
    assert first(3) == 1

    first = Curve.first([
        Generic(lambda x: 1, domain=Interval(0, 2)),
        Generic(lambda x: 2, domain=Interval.gte(1))
    ])
    assert first(-1) is None
    assert first(0) == 1
    assert first(1) == 1
    assert first(2) == 1
    assert first(2.1) == 2
    assert first(3) == 2
    assert first.sample_points(Interval(0, 3), min_step=1) == [(0, 1), (1, 1),
                                                               (2, 1), (3, 2)]
Exemplo n.º 4
0
def test_func_parse_descriptor_log_decorator():
    f = Curve.parse_descriptor({'@log2': {'$add': [16, 16]}})
    assert f.y(0) == 256

    v = Curve.parse_descriptor({'@log10': 10}, fragment=True)
    assert v == 10
    v = Curve.parse_descriptor({'@log': 10}, fragment=True)
    assert v == approx(10, abs=0.01)
Exemplo n.º 5
0
def test_min_max():
    funcs = [2, Line(const=0, slope=1)]
    minf = Curve.min(funcs)
    maxf = Curve.max(funcs)

    assert minf(0) == 0
    assert minf(1) == 1
    assert minf(2) == 2
    assert minf(3) == 2
    assert minf(4) == 2

    assert maxf(0) == 2
    assert maxf(1) == 2
    assert maxf(2) == 2
    assert maxf(3) == 3
    assert maxf(4) == 4
Exemplo n.º 6
0
def test_func_parse_descriptor_chain_with_decorator():
    f = Curve.parse_descriptor({'@log': {'$constant': 16}, '$max': 10})
    assert f.y(0) == approx(16, abs=0.01)
    f = Curve.parse_descriptor({'@log': {'$constant': 16}, '$max': [10]})
    assert f.y(0) == approx(16, abs=0.01)

    # 2 ^ (10 + 2) = 4096
    f = Curve.parse_descriptor({
        '@log10': {
            '$constant': 10
        },
        '@log2': {
            '$add': 4
        }
    })
    assert f.y(0) == approx(4096, abs=0.01)
Exemplo n.º 7
0
def test_max():
    f = Curve.max([Points([(0, -1), (1, 0), (2, 1)]), 0])
    assert f.domain == Interval.closed(0, 2)
    assert f.y(-1) is None
    assert f.y(0) == 0
    assert f.y(1) == 0
    assert f.y(2) == 1
    assert f.y(3) is None

    f = Curve.max([0, Points([(0, -1), (1, 0), (2, 1)])])
    assert f.domain == Interval.closed(0, 2)
    assert f.y(-1) is None
    assert f.y(0) == 0
    assert f.y(1) == 0
    assert f.y(2) == 1
    assert f.y(3) is None
Exemplo n.º 8
0
def test_func_parse_descriptor_line():
    f = Curve.parse_descriptor(
        {'$line': {
            'points': [[100.0, 1000.0], [200.0, 2000.0]]
        }})
    assert f.y(100) == 1000.0
    assert f.y(200) == 2000.0
    assert f.y(300) == 3000.0
Exemplo n.º 9
0
def test_func_parse_descriptor_chain():
    f = Curve.parse_descriptor({'$constant': 16, '$log': {'base': 2}})
    assert f.y(0) == 4
Exemplo n.º 10
0
def test_func_parse_descriptor_log2():
    f = Curve.parse_descriptor({'$log2': 16})
    assert f.y(0) == 4
Exemplo n.º 11
0
def test_func_parse_descriptor_raised():
    f = Curve.parse_descriptor({'$raised': {'@args': [4], 'base': 2}})
    assert f.y(0) == 16
Exemplo n.º 12
0
def test_func_parse_descriptor_constant():
    f = Curve.parse_descriptor({'$constant': 10})
    assert f.y(0) == 10
Exemplo n.º 13
0
def test_func_parse_descriptor_args_decorator():
    f = Curve.parse_descriptor({'$add': {'@args': [10, 20]}})
    assert f.y(0) == 30
Exemplo n.º 14
0
def test_func_parse_descriptor_min():
    f = Curve.parse_descriptor({'$min': [1, 2, 3, -1]})
    assert f.y(0) == -1
Exemplo n.º 15
0
def test_func_parse_descriptor_chain_with_instance_method():
    f = Curve.parse_descriptor({'$constant': -16, '$abs': []})
    assert f.y(0) == 16
Exemplo n.º 16
0
def test_func_parse_descriptor_chain_with_class_method():
    f = Curve.parse_descriptor({'$constant': 16, '$max': [10]})
    assert f.y(0) == 16
Exemplo n.º 17
0
def test_func_parse_descriptor_fragment_date_decorator():
    date = Curve.parse_descriptor({'@date': '2020-02-12 01:23+1200'},
                                  fragment=True)
    assert date == 1581427380
Exemplo n.º 18
0
def test_func_update():
    begin_update_count = 0
    begin_update_interval = None

    def begin_update(domain):
        nonlocal begin_update_count
        nonlocal begin_update_interval
        begin_update_count += 1
        begin_update_interval = domain

    end_update_count = 0
    end_update_interval = None

    def end_update(domain):
        nonlocal end_update_count
        nonlocal end_update_interval
        end_update_count += 1
        end_update_interval = domain

    f = Curve()
    t = f.add_observer(domain=(0, 2), begin=begin_update, end=end_update)

    f.begin_update(Interval(1, 3))
    assert begin_update_count == 1
    assert end_update_count == 0
    assert begin_update_interval.start == 1
    assert begin_update_interval.end == 3
    f.end_update(Interval(1, 3))
    assert begin_update_count == 1
    assert end_update_count == 1
    assert end_update_interval.start == 1
    assert end_update_interval.end == 3

    f.begin_update(Interval(3, 4))
    assert begin_update_count == 1
    assert end_update_count == 1
    f.end_update(Interval(3, 4))
    assert begin_update_count == 1
    assert end_update_count == 1

    f.remove_observer(t)
    f.begin_update(Interval(1, 3))
    assert begin_update_count == 1
    assert end_update_count == 1
    f.end_update(Interval(1, 3))
    assert begin_update_count == 1
    assert end_update_count == 1
Exemplo n.º 19
0
def test_update_with_obj_autoremove():
    begin_update_count = 0

    def begin_update(domain):
        nonlocal begin_update_count
        begin_update_count += 1

    end_update_count = 0

    def end_update(domain):
        nonlocal end_update_count
        end_update_count += 1

    f = Curve()

    # Add and remove observer using object
    obj = T()
    f.add_observer(obj, begin=begin_update, end=end_update, autoremove=True)

    f.begin_update(Interval(1, 3))
    assert begin_update_count == 1
    f.end_update(Interval(1, 3))
    assert end_update_count == 1

    obj = None
    f.begin_update(Interval(1, 3))
    assert begin_update_count == 1
    f.end_update(Interval(1, 3))
    assert end_update_count == 1