def test_increment_state():
    loops = build_loops(config)
    state = initial_state(loops)
    state = increment_state(loops, state)
    assert state == (Decimal('2.0'), Decimal('0.1'), Decimal('0.1'),
                     Decimal('0.02'), 'a')
    # loop 9 more times
    for i in range(9):
        state = increment_state(loops, state)
    assert state == (Decimal('1.0'), Decimal('0.2'), Decimal('0.1'),
                     Decimal('0.02'), 'a')
    # Loop 89 more times
    for i in range(9, 98):
        state = increment_state(loops, state)
    assert state == (Decimal('10.0'), Decimal('1.0'), Decimal('0.1'),
                     Decimal('0.02'), 'a')
    # loop 400 more times
    for i in range(99, 499):
        state = increment_state(loops, state)
    assert state == (Decimal('10.0'), Decimal('1.0'), Decimal('9.971220736'),
                     Decimal('0.02'), 'a')
    # loop 1000 more times
    for i in range(499, 1499):
        state = increment_state(loops, state)
    assert state == (Decimal('10.0'), Decimal('1.0'), Decimal('9.971220736'),
                     Decimal('0.1'), 'a')
    # loop 1000 more times
    for i in range(1499, 4499):
        state = increment_state(loops, state)
    assert state == (Decimal('10.0'), Decimal('1.0'), Decimal('9.971220736'),
                     Decimal('0.1'), 'c')
    #Make sure it ends now
    assert increment_state(loops, state) == None
def test_build_loop():
    ret = build_loops(config)
    assert ret == [
        ('demo', 'x', Decimal('1.0'), Decimal('10.0'), Decimal('1.0'), "ADD"),
        ('demo', 'y', Decimal('0.1'), Decimal('1.0'), Decimal('.1'), "ADD"),
        ('demo', 'z', Decimal('0.1'), Decimal('10'), Decimal('3.16'), "MUL"),
        ('demo', 't', Decimal('0.02'), Decimal('0.1'),
         [Decimal('0.02'), Decimal('0.05'),
          Decimal('0.1')], "LIST"),
        ('demo', 'u', 'a', 'c', ['a', 'b', 'c'], "STRLIST")
    ]
def test_run_experiment():
    loops = build_loops(config)
    state = initial_state(loops)
    # To test the callback, we collect all of the (x,y) pairs in the callback
    quintuplets = []

    def callback(config):
        quintuplets.append(
            (config['demo']['x'], config['demo']['y'], config['demo']['z'],
             config['demo']['t'], config['demo']['u']))

    config[driver.EXPERIMENT][
        driver.EXPERIMENT_DIR] = '.'  # use the current directory
    run_experiment(config=config, callback=callback)
    # I should have 4500 quintuplets
    assert len(quintuplets) == 4500
    # Check for some key values
    assert ('1', '0.1', '0.1', '0.02', 'a') in quintuplets
    assert ('5', '0.5', '3.1554496', '0.05', 'c') in quintuplets
    assert ('10', '1.0', '9.971220736', '0.1', 'b') in quintuplets
def test_substitute_config():
    loops = build_loops(config)
    state = initial_state(loops)
    newconfig = substitute_config(loops=loops, state=state, config=config)
    assert newconfig['demo'][
        'junk'] == "10"  # make sure that the old section is still there
    assert newconfig['demo']['x'] == "1"  # make sure we have a "1"
    assert newconfig['demo']['y'] == "0.1"  # make sure we have a "0.1"
    assert newconfig['demo']['z'] == "0.1"  # make sure we have a "0.1"
    assert newconfig['demo']['t'] == "0.02"  # make sure we have a "0.02"
    assert newconfig['demo']['u'] == "a"  # make sure we have a "a"

    # for giggles, lets try incrementing
    state = increment_state(loops, state)
    newconfig = substitute_config(loops=loops, state=state, config=config)
    assert newconfig['demo']['x'] == "2"  # make sure we have a "1"
    assert newconfig['demo']['y'] == "0.1"  # make sure we have a "0.1"
    assert newconfig['demo']['z'] == "0.1"  # make sure we have a "0.1"
    assert newconfig['demo']['t'] == "0.02"  # make sure we have a "0.02"
    assert newconfig['demo']['u'] == "a"  # make sure we have a "a"
def test_initial_state():
    state = initial_state(build_loops(config))
    assert state == (Decimal('1.0'), Decimal('0.1'), Decimal('0.1'),
                     Decimal('0.02'), 'a')