def test_cons(self, numbers, capsys): # generate cons expression s = "".join(["(cons %s" % x for x in numbers]) # close with (cons 1 ()) and add closing parens s = "%s (cons 1 ()) %s" % (s, ")" * len(numbers)) eval_and_print(s) assert_stripped_output_is(capsys, "(%s 1)" % " ".join(str(n) for n in numbers))
def test_define_recursive_nary_function(self, capsys): eval_and_print(""" (define (max x . z) (if (null? z) x (apply max (cons (if (b< x (car z)) (car z) x) (cdr z))))) """) capsys.readouterr() eval_and_print("(max 10 2 3 4 5 15 100 2)") assert_stripped_output_is(capsys, "100")
def test_binary_addition(self, v1, v2, capsys): eval_and_print("(b+ %s %s)" % (v1, v2)) assert_stripped_output_is(capsys, str(v1 + v2))
def test_define_functions(self, v1, v2, capsys): eval_and_print("(define (a x) (b+ %s x))" % v1) capsys.readouterr() eval_and_print("(a %s)" % v2) assert_stripped_output_is(capsys, str(v1 + v2))
def test_define_variables(self, v, capsys): eval_and_print("(define x %s)" % v) capsys.readouterr() eval_and_print("x") assert_stripped_output_is(capsys, str(v))
def test_set_cdr(self, numbers, capsys): nums = " ".join(str(n) for n in numbers) eval_and_print("(set-cdr! '(11 2 3 4) '(%s))" % nums) assert_stripped_output_is(capsys, "(11 %s)" % nums)
def test_set_car(self, number, capsys): eval_and_print("(set-car! '(11 2 3 4) %s)" % number) assert_stripped_output_is(capsys, "(%s 2 3 4)" % number)
def test_cdr(self, numbers, capsys): eval_and_print("(cdr '(%s))" % " ".join(str(n) for n in numbers)) assert_stripped_output_is( capsys, "(%s)" % " ".join(str(n) for n in numbers[1:]))
def test_cons_dot(self, capsys): eval_and_print("(cons 1 2)") assert_stripped_output_is(capsys, "(1 . 2)")
def test_binary_subtraction(self, v1, v2, capsys): eval_and_print("(b- %s %s)" % (v1, v2)) assert_stripped_output_is(capsys, str(v1 - v2))