Beispiel #1
0
def test_calculator_2():
    calculator = Calculator(4)
    calculator.type(3)
    assert 3 == calculator.output()
    calculator.multiply()
    calculator.type(2)
    calculator.type(0)
    assert 60 == calculator.output()
Beispiel #2
0
def test_strategy_execute():
    memory = Calculator(5)
    strategy = Strategy([
        Action(memory.add),
        Action(partial(memory.type, digit=3)),
        Action(memory.deduct),
        Action(partial(memory.type, digit=4)),
    ])
    strategy.execute(OperationsCounter())
    assert 4 == memory.output()
Beispiel #3
0
def test_memory_collection():
    def a():
        return 1

    def b():
        return 2

    def c():
        return 1

    calculator = Calculator()
    tape = Tape(5, (1,), zeros([1, 2]))
    collection = MemoryCollection(
        {'calculator': calculator, 'tape': tape},
        input_element=calculator,
        output_element=tape,
        additional_actions=[a],
        additional_control_structures=[b],
        additional_conditions=[c]
    )
    assert calculator.add in collection.actions
    assert tape.get in collection.actions
    assert a in collection.actions
    assert b in collection.control_structures
    assert c in collection.conditions
Beispiel #4
0
def test_supervised_learning_1():
    training_data = [DataSample(2, 5), DataSample(3, 7), DataSample(5, 11)]
    testing_data = [DataSample(1, 3), DataSample(4, 9), DataSample(6, 13)]
    calculator = Calculator()
    supervised_learning = SupervisedLearning(
        calculator, brute_force_generator_max_length=3)
    supervised_learning.fit(training_data)
    for sample in testing_data:
        assert sample.output == supervised_learning.predict(sample.input)
def test_brute_force_generator_2():
    calculator_memory = Calculator()
    generator = BruteForceGenerator(calculator_memory.actions, [], [], 3)
    generated = generator.generate()

    strategy = Strategy([
        Action(calculator_memory.multiply),
        Action(partial(calculator_memory.type, digit=2)),
        Action(partial(calculator_memory.type, digit=1))
    ])
    assert strategy in generated
Beispiel #6
0
def test_strategy_eq():
    def a():
        return 1

    def b():
        return 2

    def c(x):
        if isinstance(x, list):
            x.append(1)
            return x
        return x + 2

    def d(x):
        return x + 3

    strategy1 = Strategy([a, b, partial(c, x=5)])
    strategy2 = Strategy([a, b, partial(c, x=5)])
    strategy3 = Strategy([partial(c, x=3), b, a])
    strategy4 = Strategy([a])
    strategy5 = Strategy([a, b, partial(c, x=[partial(d, x=5)])])
    strategy6 = Strategy([a, b, partial(c, x=[partial(d, x=5)])])
    strategy7 = Strategy([a, b, partial(c, x=[partial(d, x=7)])])
    strategy8 = Strategy([a, b, partial(c, x=[partial(d, y=7)])])
    assert strategy1 == strategy2
    assert strategy1 != strategy3
    assert strategy4 != strategy1
    assert strategy5 == strategy6
    assert strategy7 != strategy8
    assert strategy5 != strategy7

    calculator = Calculator(4)
    strategy9 = Strategy(
        [
            calculator.multiply,
            partial(calculator.type, digit=2),
            calculator.add,
            partial(calculator.type, digit=1)
        ]
    )
    strategy10 = Strategy(
        [
            calculator.add,
            partial(calculator.type, digit=2),
            calculator.add,
            partial(calculator.type, digit=1)
        ]
    )
    assert strategy9 != strategy10
Beispiel #7
0
def test_calculator_1():
    calculator = Calculator(3)
    calculator.input(8)
    assert 8 == calculator.output()
    calculator.type(5)
    calculator.type(3)
    assert 53 == calculator.output()
    calculator.add()
    calculator.type(4)
    calculator.type(0)
    assert 93 == calculator.output()
    calculator.divide()
    calculator.type(3)
    assert 31 == calculator.output()
    calculator.multiply()
    calculator.type(2)
    assert 62 == calculator.output()
Beispiel #8
0
def test_calculator3():
    calculator = Calculator(3)
    calculator.add()
    calculator.input(3)
    calculator.multiply()
    calculator.type(2)
    calculator.add()
    calculator.type(1)
    assert 7 == calculator.output()