def test_pwr2(): env = stdlib.copy() evaldef( "pwr2 = ap ap s ap ap c ap eq 0 1 ap ap b ap mul 2 ap ap b pwr2 ap add -1", env, ) assert eval(parse("ap pwr2 8".split()), env) == Number(256)
def test_mul_with_two_numbers_returns_result(): assert eval(parse("ap ap mul 3 4".split())) == Number(12)
def test_parse_app2(): # From the video exp = parse("ap dec ap ap add 1 2".split()) assert eval(exp) == Number(2)
def test_parse_app(): exp = parse(["ap", "ap", "add", "1", "2"]) assert eval(exp) == Number(3)
def test_parse_number_returns_number(): assert parse(["-123"]) == Number(-123) assert parse(["123"]) == Number(123)
def test_number_add_returns_sum(): assert Number(5).add(Number(6)) == Number(11)
def test_c_combinator(): assert eval(parse("ap ap ap c add 1 2".split())) == Number(3)
def test_eval_with_positive_number_returns_number(): program = Number(5) assert eval(program) == program
def test_eval_with_var_looks_up_value(): assert eval(Var("x"), {"x": Number(5)}) == Number(5)
def test_f_combinator(): assert eval(parse("ap ap f 1 5".split())) == Number(5) assert eval(parse("ap ap f f ap inc 5".split())) == Number(6) assert eval(parse("ap ap f ap inc 5 t".split())) == stdlib["t"]
def test_eval_with_negative_number_returns_number(): program = Number(-5) assert eval(program) == program
def test_t_combinator(): assert eval(parse("ap ap t 1 5".split())) == Number(1) assert eval(parse("ap ap t t ap inc 5".split())) == stdlib["t"] assert eval(parse("ap ap t ap inc 5 t".split())) == Number(6) assert eval(parse("ap ap t t i".split())) == stdlib["t"]
def test_b_combinator(): assert eval(parse("ap ap ap b inc dec 5".split())) == Number(5)
def test_div_with_two_numbers_returns_result(): assert eval(parse("ap ap div 12 4".split())) == Number(3)
def test_inc_with_number_returns_next_number(): assert eval(Apply(Var("inc"), Number(5))) == Number(6)
def test_div_returns_integer(): assert eval(parse("ap ap div 10 4".split())) == Number(2)
def test_add_with_two_numbers_returns_result(): # ap (ap add 5) 6 assert eval(Apply(Apply(Var("add"), Number(5)), Number(6))) == Number(11)
def test_neg_with_negative_returns_positive(): assert eval(parse("ap neg -5".split())) == Number(5)
def test_s_combinator(): assert eval(parse("ap ap ap s add inc 1".split())) == Number(3) assert eval(parse("ap ap ap s mul ap add 1 6".split())) == Number(42)