Best Python code snippet using fMBT_python
test_eval.py
Source:test_eval.py
...5def test_eval_obj():6 w_num = W_Pair(W_Symbol("+"),7 W_Pair(W_Integer(4), W_Pair(W_Integer(5), w_nil)))8 assert w_num.eval(ExecutionContext()).to_number() == 9 9def eval_(ctx, expr):10 return parse(expr)[0].eval(ctx)11def eval_noctx(expr):12 return parse(expr)[0].eval(ExecutionContext())13def test_numerical():14 w_num = eval_noctx("(+)")15 assert w_num.to_number() == 016 w_num = eval_noctx("(+ 4)")17 assert w_num.to_number() == 418 w_num = eval_noctx("(+ 4 -5)")19 assert w_num.to_number() == -120 w_num = eval_noctx("(+ 4 -5 6.1)")21 assert w_num.to_number() == 5.122 w_num = eval_noctx("(*)")23 assert w_num.to_number() == 124 w_num = eval_noctx("(* 4)")25 assert w_num.to_number() == 426 w_num = eval_noctx("(* 4 -5)")27 assert w_num.to_number() == -2028 w_num = eval_noctx("(* 4 -5 6.1)")29 assert w_num.to_number() == (4 * -5 * 6.1)30 py.test.raises(WrongArgsNumber, eval_noctx, "(/)")31 w_num = eval_noctx("(/ 4)")32 assert w_num.to_number() == 1 / 433 w_num = eval_noctx("(/ 4 -5)")34 assert w_num.to_number() == 4 / -535 w_num = eval_noctx("(/ 4 -5 6.1)")36 assert w_num.to_number() == (4 / -5 / 6.1)37 py.test.raises(WrongArgsNumber, eval_noctx, "(-)")38 w_num = eval_noctx("(- 4)")39 assert w_num.to_number() == -440 w_num = eval_noctx("(- 4 5)")41 assert w_num.to_number() == -142 w_num = eval_noctx("(- 4 -5 6.1)")43 assert w_num.to_number() == 4 - (-5) - 6.144 py.test.raises(WrongArgType, eval_noctx, "(+ 'a)")45 py.test.raises(WrongArgType, eval_noctx, "(+ 1 'a)")46 py.test.raises(WrongArgType, eval_noctx, "(- 'a)")47 py.test.raises(WrongArgType, eval_noctx, "(- 1 'a)")48 py.test.raises(WrongArgType, eval_noctx, "(* 'a)")49 py.test.raises(WrongArgType, eval_noctx, "(* 1 'a)")50 py.test.raises(WrongArgType, eval_noctx, "(/ 'a)")51 py.test.raises(WrongArgType, eval_noctx, "(/ 1 'a)")52def test_numerical_nested():53 w_num = eval_noctx("(+ 4 (* (+ 5) 6) (+ 1 2))")54 assert w_num.to_number() == 3755def test_ctx_simple():56 ctx = ExecutionContext()57 ctx.put("v1", W_Integer(4))58 ctx.put("v2", W_Integer(5))59 w_num = eval_(ctx, "(+ 1 v1 v2)")60 assert w_num.to_number() == 1061 ctx.put("v2", W_Real(3.2))62 w_num = eval_(ctx, "(+ 1 v1 v2)")63 assert w_num.to_number() == 8.264def test_ctx_define():65 ctx = ExecutionContext()66 eval_(ctx, "(define v1 42)")67 assert ctx.get("v1").to_number() == 4268 w_num = eval_(ctx, "v1")69 assert w_num.to_number() == 4270 eval_(ctx, "(define v2 2.1)")71 assert ctx.get("v2").to_number() == 2.172 w_num = eval_(ctx, "(+ 1 v1 v2)")73 assert w_num.to_number() == 45.174 eval_(ctx, "(define v2 3.1)")75 w_num = eval_(ctx, "(+ 1 v1 v2)")76 assert w_num.to_number() == 46.177def text_unbound():78 ctx = ExecutionContext()79 py.test.raises(UnboundVariable, eval_, ctx, "y")80def test_sete():81 ctx = ExecutionContext()82 eval_(ctx, "(define x 42)")83 loc1 = ctx.get_location("x")84 eval_(ctx, "(set! x 43)")85 loc2 = ctx.get_location("x")86 assert ctx.get("x").to_number() == 4387 assert loc1 is loc288 py.test.raises(UnboundVariable, eval_, ctx, "(set! y 42)")89def test_func():90 ctx = ExecutionContext()91 w_func = eval_(ctx, "+")92 assert isinstance(w_func, W_Procedure)93def test_if_simple():94 ctx = ExecutionContext()95 w_t = eval_(ctx, "(if #t #t #f)")96 assert w_t.to_boolean() is True97 w_f = eval_(ctx, "(if #f #t #f)")98 assert w_f.to_boolean() is False99 w_f = eval_(ctx, "(if 1 #f #t)")100 assert w_f.to_boolean() is False101 w_f = eval_(ctx, "(if #t #t)")102 assert w_f.to_boolean() is True103 w_f = eval_(ctx, "(if #f #t)")104 assert w_f.to_boolean() is False105def test_if_evaluation():106 ctx = ExecutionContext()107 eval_(ctx, "(define then #f)")108 eval_(ctx, "(define else #f)")109 eval_(ctx, "(if #t (define then #t) (define else #t))")110 assert ctx.get("then").to_boolean() is True111 assert ctx.get("else").to_boolean() is False112 eval_(ctx, "(define then #f)")113 eval_(ctx, "(define else #f)")114 eval_(ctx, "(if #f (define then #t) (define else #t))")115 assert ctx.get("then").to_boolean() is False116 assert ctx.get("else").to_boolean() is True117def test_cons_simple():118 w_pair = eval_noctx("(cons 1 2)")119 assert isinstance(w_pair, W_Pair)120 assert w_pair.car.to_number() == 1121 assert w_pair.cdr.to_number() == 2122 w_pair = eval_noctx("(cons 1 (cons 2 3))")123 assert isinstance(w_pair, W_Pair)124 assert isinstance(w_pair.cdr, W_Pair)125 assert w_pair.car.to_number() == 1126 assert w_pair.cdr.car.to_number() == 2127 assert w_pair.cdr.cdr.to_number() == 3128def test_car_simple():129 w_car = eval_noctx("(car (cons 1 2))")130 assert w_car.to_number() == 1131 w_cdr = eval_noctx("(cdr (cons 1 2))")132 assert w_cdr.to_number() == 2133 w_cadr = eval_noctx("(car (cdr (cons 1 (cons 2 3))))")134 assert w_cadr.to_number() == 2135 w_cddr = eval_noctx("(cdr (cdr (cons 1 (cons 2 3))))")136 assert w_cddr.to_number() == 3137def test_comparison_homonums():138 w_bool = eval_noctx("(=)")139 assert w_bool.to_boolean() is True140 w_bool = eval_noctx("(= 1)")141 assert w_bool.to_boolean() is True142 w_bool = eval_noctx("(= 1 2)")143 assert w_bool.to_boolean() is False144 w_bool = eval_noctx("(= 2 2)")145 assert w_bool.to_boolean() is True146 w_bool = eval_noctx("(= 2 2 2 2)")147 assert w_bool.to_boolean() is True148 w_bool = eval_noctx("(= 2 2 3 2)")149 assert w_bool.to_boolean() is False150 w_bool = eval_noctx("(= 2.1 1.2)")151 assert w_bool.to_boolean() is False152 w_bool = eval_noctx("(= 2.1 2.1)")153 assert w_bool.to_boolean() is True154 w_bool = eval_noctx("(= 2.1 2.1 2.1 2.1)")155 assert w_bool.to_boolean() is True156 w_bool = eval_noctx("(= 2.1 2.1 2.1 2)")157 assert w_bool.to_boolean() is False158 py.test.raises(WrongArgType, eval_noctx, "(= 'a 1)")159def test_comparison_heteronums():160 w_bool = eval_noctx("(= 1 1.0 1.1)")161 assert w_bool.to_boolean() is False162 w_bool = eval_noctx("(= 2.0 2 2.0)")163 assert w_bool.to_boolean() is True164def test_lambda_noargs():165 ctx = ExecutionContext()166 w_lambda = eval_(ctx, "(lambda () 12)")167 assert isinstance(w_lambda, W_Procedure)168 assert isinstance(w_lambda, W_Lambda)169 ctx.put("f1", w_lambda)170 w_result = eval_(ctx, "(f1)")171 assert isinstance(w_result, W_Integer)172 assert w_result.to_number() == 12173def test_lambda_args():174 ctx = ExecutionContext()175 w_lam = eval_(ctx, "(define f1 (lambda (n) n))")176 assert isinstance(w_lam, W_Lambda)177 w_result = eval_(ctx, "(f1 42)")178 assert isinstance(w_result, W_Integer)179 assert w_result.to_number() == 42180 w_result = eval_(ctx, "((lambda (n m) (+ n m)) 42 -42)")181 assert isinstance(w_result, W_Integer)182 assert w_result.to_number() == 0183def test_lambda_top_ctx():184 ctx = ExecutionContext()185 eval_(ctx, "(define n 42)")186 eval_(ctx, "(define f1 (lambda (m) (+ n m)))")187 w_result = eval_(ctx, "(f1 -42)")188 assert isinstance(w_result, W_Integer)189 assert w_result.to_number() == 0190 eval_(ctx, "(define n 84)")191 w_result = eval_(ctx, "(f1 -42)")192 assert isinstance(w_result, W_Integer)193 assert w_result.to_number() == 42194def test_lambda_fac():195 ctx = ExecutionContext()196 eval_(ctx, """197 (define fac198 (lambda (n)199 (if (= n 1)200 n201 (* (fac (- n 1)) n))))""")202 assert isinstance(ctx.get("fac"), W_Lambda)203 w_result = eval_(ctx, "(fac 4)")204 assert w_result.to_number() == 24205 w_result = eval_(ctx, "(fac 5)")206 assert w_result.to_number() == 120207def test_lambda2():208 ctx = ExecutionContext()209 eval_(ctx, """(define adder (lambda (x) (lambda (y) (+ x y))))""")210 w_lambda = eval_(ctx, "(adder 6)")211 assert isinstance(w_lambda, W_Lambda)212 eval_(ctx, """(define add6 (adder 6))""")213 w_result = eval_(ctx, "(add6 5)")214 assert isinstance(w_result, W_Integer)215 assert w_result.to_number() == 11216 w_result = eval_(ctx, "((adder 6) 5)")217 assert isinstance(w_result, W_Integer)218 assert w_result.to_number() == 11219def test_lambda_long_body():220 ctx = ExecutionContext()221 eval_(ctx, """(define long_body (lambda () (define x 42) (+ x 1)))""")222 w_result = eval_(ctx, "(long_body)")223 assert w_result.to_number() == 43224 py.test.raises(UnboundVariable, ctx.get, "x")225def test_lambda_lstarg():226 ctx = ExecutionContext()227 w_result = eval_(ctx, """((lambda x x) 1 2 3)""")228 assert isinstance(w_result, W_Pair)229 assert w_result.car.to_number() == 1230 assert w_result.cdr.car.to_number() == 2231 assert w_result.cdr.cdr.car.to_number() == 3232def test_lambda_dotted_lstarg():233 ctx = ExecutionContext()234 w_result = eval_(ctx, """((lambda (x y . z) z) 3 4)""")235 assert w_result is w_nil236 w_result = eval_(ctx, """((lambda (x y . z) z) 3 4 5 6)""")237 assert isinstance(w_result, W_Pair)238 assert w_result.car.to_number() == 5239 assert w_result.cdr.car.to_number() == 6240 assert w_result.cdr.cdr is w_nil241def test_define_lambda_sugar():242 ctx = ExecutionContext()243 eval_(ctx, """(define (f x) (+ x 1))""")244 w_result = eval_(ctx, "(f 1)")245 assert isinstance(w_result, W_Integer)246 assert w_result.to_number() == 2247 eval_(ctx, """(define (f2) (+ 1 1))""")248 w_result = eval_(ctx, "(f2)")249 assert isinstance(w_result, W_Integer)250 assert w_result.to_number() == 2251 eval_(ctx, """(define (f3 . x) x)""")252 w_result = eval_(ctx, "(f3 1 2)")253 assert isinstance(w_result, W_Pair)254 assert w_result.car.to_number() == 1255 assert w_result.cdr.car.to_number() == 2256 eval_(ctx, """(define (f4 x . y) x y)""")257 w_result = eval_(ctx, "(f4 1 2)")258 assert isinstance(w_result, W_Pair)259 assert w_result.car.to_number() == 2260 assert w_result.cdr is w_nil261def test_quote():262 w_fnum = eval_noctx("(quote 42)")263 assert isinstance(w_fnum, W_Integer)264 assert w_fnum.to_number() == 42265 w_sym = eval_noctx("(quote symbol)")266 assert isinstance(w_sym, W_Symbol)267 assert w_sym.to_string() == "symbol"268 w_lst = eval_noctx("(quote (1 2 3))")269 assert isinstance(w_lst, W_Pair)270 assert w_lst.car.to_number() == 1271 assert w_lst.cdr.car.to_number() == 2272 assert w_lst.cdr.cdr.car.to_number() == 3273 w_lst = eval_noctx("(quote (a (x y) c))")274 assert isinstance(w_lst, W_Pair)275 assert isinstance(w_lst.car, W_Symbol)276 assert w_lst.car.to_string() == "a"277 w_pair = w_lst.cdr.car278 assert isinstance(w_lst.cdr.cdr.car, W_Symbol)279 assert w_lst.cdr.cdr.car.to_string() == "c"280 assert isinstance(w_pair.car, W_Symbol)281 assert w_pair.car.to_string() == "x"282 assert isinstance(w_pair.cdr.car, W_Symbol)283 assert w_pair.cdr.car.to_string() == "y"284def test_quote_parse():285 w_fnum = eval_noctx("'42")286 assert isinstance(w_fnum, W_Integer)287 assert w_fnum.to_number() == 42288 w_sym = eval_noctx("'symbol")289 assert isinstance(w_sym, W_Symbol)290 assert w_sym.to_string() == "symbol"291 w_lst = eval_noctx("'(1 2 3)")292 assert isinstance(w_lst, W_Pair)293 assert w_lst.car.to_number() == 1294 assert w_lst.cdr.car.to_number() == 2295 assert w_lst.cdr.cdr.car.to_number() == 3296 w_lst = eval_noctx("'(a (x y) c)")297 assert isinstance(w_lst, W_Pair)298 assert isinstance(w_lst.car, W_Symbol)299 assert w_lst.car.to_string() == "a"300 w_pair = w_lst.cdr.car301 assert isinstance(w_lst.cdr.cdr.car, W_Symbol)302 assert w_lst.cdr.cdr.car.to_string() == "c"303 assert isinstance(w_pair.car, W_Symbol)304 assert w_pair.car.to_string() == "x"305 assert isinstance(w_pair.cdr.car, W_Symbol)306 assert w_pair.cdr.car.to_string() == "y"307def test_list():308 ctx = ExecutionContext()309 ctx.put("var", W_Integer(42))310 w_lst = eval_(ctx, "(list 1 var (+ 2 1) 'a)")311 assert isinstance(w_lst, W_Pair)312 assert w_lst.car.to_number() == 1313 assert w_lst.cdr.car.to_number() == 42314 assert w_lst.cdr.cdr.car.to_number() == 3315 assert w_lst.cdr.cdr.cdr.car.to_string() == "a"316 assert w_lst.cdr.cdr.cdr.cdr is w_nil317def test_begin():318 ctx = ExecutionContext()319 w_global = W_Integer(0)320 ctx.put("var", w_global)321 w_result = eval_(ctx, "(begin (set! var 11) (+ var 33))")322 assert w_result.to_number() == 44323 assert ctx.get("var").to_number() == 11324def test_let():325 ctx = ExecutionContext()326 w_global = W_Integer(0)327 ctx.put("var", w_global)328 w_result = eval_(ctx, "(let ((var 42) (x (+ 2 var))) (+ var x))")329 assert w_result.to_number() == 44330 assert ctx.get("var") is w_global331 w_result = eval_(ctx, """332 (let ((x (lambda () 1)))333 (let ((y (lambda () (x)))334 (x (lambda () 2))) (y)))""")335 assert w_result.to_number() == 1336 py.test.raises(UnboundVariable, eval_noctx, "(let ((y 0) (x y)) x)")337def test_letrec():338 ctx = ExecutionContext()339 w_result = eval_(ctx, """340 (letrec ((even?341 (lambda (n)342 (if (= n 0)343 #t344 (odd? (- n 1)))))345 (odd?346 (lambda (n)347 (if (= n 0)348 #f349 (even? (- n 1))))))350 (even? 2000))""")351 assert w_result.to_boolean() is True352 w_result = eval_(ctx, """353 (let ((x (lambda () 1)))354 (letrec ((y (lambda () (x)))355 (x (lambda () 2))) (y)))""")356 assert w_result.to_number() == 2357 py.test.raises(UnboundVariable, eval_noctx, "(letrec ((y 0) (x y)) x)")358def test_letstar():359 #test for (let* ...)360 w_result = eval_noctx("""361 (let* ((x 42)362 (y (- x 42))363 (z (+ x y)))364 z)""")365 assert w_result.to_number() == 42366 py.test.raises(UnboundVariable, eval_noctx, "(let* ((x (+ 1 y)) (y 0)) x)")367def test_quit():368 py.test.raises(SchemeQuit, eval_noctx, "(quit)")369def test_numbers():370 assert eval_noctx("(integer? 42)").to_boolean()371 assert eval_noctx("(integer? 42.0)").to_boolean()372 assert not eval_noctx("(integer? 42.1)").to_boolean()373 assert eval_noctx("(rational? 42)").to_boolean()374 assert eval_noctx("(rational? 42.1)").to_boolean()375 assert eval_noctx("(real? 42)").to_boolean()376 assert eval_noctx("(real? 42.1)").to_boolean()377 assert eval_noctx("(complex? 42)").to_boolean()378 assert eval_noctx("(complex? 42.1)").to_boolean()379 assert eval_noctx("(number? 42)").to_boolean()380 assert eval_noctx("(number? 42.1)").to_boolean()381 py.test.raises(WrongArgType, eval_noctx, "(number? 'a)" )382def test_exactness():383 assert eval_noctx("(exact? 42)").to_boolean()384 assert not eval_noctx("(exact? 42.0)").to_boolean()385 py.test.raises(WrongArgType, eval_noctx, "(exact? 'a)" )386 assert not eval_noctx("(inexact? 42)").to_boolean()387 assert eval_noctx("(inexact? 42.0)").to_boolean()388 py.test.raises(WrongArgType, eval_noctx, "(inexact? 'a)" )389def test_number_predicates():390 assert eval_noctx("(zero? 0)").to_boolean()391 assert eval_noctx("(zero? 0.0)").to_boolean()392 assert not eval_noctx("(zero? 1.0)").to_boolean()393 py.test.raises(WrongArgType, eval_noctx, "(zero? 'a)" )394 assert not eval_noctx("(odd? 0)").to_boolean()395 assert eval_noctx("(odd? 1)").to_boolean()396 py.test.raises(WrongArgType, eval_noctx, "(odd? 1.1)" )397 assert eval_noctx("(even? 0)").to_boolean()398 assert not eval_noctx("(even? 1)").to_boolean()399 py.test.raises(WrongArgType, eval_noctx, "(even? 1.1)" )400def test_delay_promise_force():401 ctx = ExecutionContext()402 w_promise = eval_(ctx, "(delay (+ 1 2))")403 assert isinstance(w_promise, W_Promise)404 ctx.put("d", w_promise)405 w_promise2 = eval_(ctx, "d")406 assert w_promise2 is w_promise407 py.test.raises(NotCallable, eval_, ctx, "(d)")408 w_value = eval_(ctx, "(force d)")409 assert w_value.to_number() == 3410 py.test.raises(WrongArgType, eval_noctx, "(force 'a)")411 eval_(ctx, "(define d2 (delay (+ 1 x)))")412 eval_(ctx, "(define x 42)")413 w_result = eval_(ctx, "(force d2)")414 assert w_result.to_number() == 43415 eval_(ctx, "(set! x 0)")416 w_result = eval_(ctx, "(force d2)")417 assert w_result.to_number() == 43418def test_lambda_context():419 ctx = ExecutionContext()420 eval_(ctx, """421 (define b (lambda ()422 (define lam (lambda () (set! a 42)))423 (define a 12)424 (lam)425 a))426 """)427 w_num = eval_(ctx, "(b)")428 assert w_num.to_number() == 42429def test_evaluator():430 ctx = ExecutionContext()431 eval_(ctx, "(define a 0)")432 w_obj = parse("(let () (set! a 42) a)")[0]433 (w_expr, new_ctx) = w_obj.eval_tr(ctx)434 assert ctx.get("a").to_number() == 42435 assert isinstance(w_expr, W_Symbol)436 assert new_ctx is not ctx437 assert isinstance(new_ctx, ExecutionContext)438 (w_obj, newer_ctx) = w_expr.eval_tr(new_ctx)439 assert isinstance(w_obj, W_Number)440 assert w_obj.to_number() == 42441 assert newer_ctx is None442def test_deep_recursion():443 ctx = ExecutionContext()444 eval_(ctx, "(define a 0)")445 eval_(ctx, """446 (define loop (lambda (n)447 (set! a (+ a 1))448 (if (= n 0)449 n450 (loop (- n 1)))))""")451 eval_(ctx, "(loop 2000)")452 assert ctx.get("a").to_number() == 2001453def test_setcar():454 ctx = ExecutionContext()455 w_pair = eval_(ctx, "(define lst '(1 2 3 4))")456 eval_(ctx, "(set-car! lst 11)")457 assert w_pair is eval_(ctx, "lst")458 assert eval_(ctx, "(car lst)").to_number() == 11459 eval_(ctx, "(set-car! (cdr lst) 12)")460 assert eval_(ctx, "(car (cdr lst))").to_number() == 12461def test_setcdr():462 ctx = ExecutionContext()463 w_pair = eval_(ctx, "(define lst '(1 2 3 4))")464 eval_(ctx, "(set-cdr! lst (cdr (cdr lst)))")465 w_lst = eval_(ctx, "lst")466 assert w_pair is w_lst467 assert w_lst.to_string() == "(1 3 4)"468 eval_(ctx, "(set-cdr! (cdr lst) '(12))")469 w_lst = eval_(ctx, "lst")470 assert w_lst.to_string() == "(1 3 12)"471 #warning circural list472 eval_(ctx, "(set-cdr! (cdr (cdr lst)) lst)")473 w_lst = eval_(ctx, "lst")474 assert w_lst is eval_(ctx, "(cdr (cdr (cdr lst)))")475def test_quasiquote():476 w_res = eval_noctx("(quasiquote (list (unquote (+ 1 2)) 4))")477 assert w_res.to_string() == "(list 3 4)"478 w_res = eval_noctx("""479 (let ((name 'a))480 (quasiquote (list (unquote name)481 (quote (unquote name)))))""")482 assert w_res.to_string() == "(list a (quote a))"483 py.test.raises(UnboundVariable, eval_noctx, "`(,,(+ 1 2))")484def test_quasiquote_nested():485 w_res = eval_noctx("""486 (quasiquote487 (a (quasiquote488 (b (unquote (+ 1 2))489 (unquote (foo490 (unquote (+ 1 3))491 d))492 e))493 f))""")494 assert w_res.to_string() == \495 "(a (quasiquote (b (unquote (+ 1 2)) (unquote (foo 4 d)) e)) f)"496 w_res = eval_noctx("""497 (let ((name1 'x)498 (name2 'y))499 (quasiquote (a500 (quasiquote (b501 (unquote (unquote name1))502 (unquote (quote503 (unquote name2)))504 d))505 e)))""")506 assert w_res.to_string() == \507 "(a (quasiquote (b (unquote x) (unquote (quote y)) d)) e)"508def test_quasiquote_splicing():509 w_res = eval_noctx("""`(1 2 ,@(list 3 4) 5 6)""")510 assert w_res.to_string() == "(1 2 3 4 5 6)"511 py.test.raises(UnboundVariable, eval_noctx, "`(,@(list 1 ,@(list 2 3)))")512 w_res = eval_noctx("""`(1 2 ,@(list 3 4) . ,(+ 2 3))""")513 assert w_res.to_string() == "(1 2 3 4 . 5)"514 w_res = eval_noctx("""`(( foo 7) ,@(cdr '(c)) . ,(car '(cons)))""")515 assert w_res.to_string() == "((foo 7) . cons)"516def test_quasiquote_splicing2():517 w_res = eval_noctx("""`(1 `(2 ,@(list ,@(list 3 4) 5 6 ,(+ 0 7))))""")518 assert w_res.to_string() == \519 "(1 (quasiquote (2 (unquote-splicing (list 3 4 5 6 7)))))"520def test_nil_eval():521 ctx = ExecutionContext()522 py.test.raises(SchemeSyntaxError, eval_, ctx, "()")523def test_type_predicates():524 ctx = ExecutionContext()525 assert eval_(ctx, "(pair? 1)").to_boolean() is False526 assert eval_(ctx, "(pair? 'symb)").to_boolean() is False527 assert eval_(ctx, "(pair? #f)").to_boolean() is False528 assert eval_(ctx, "(pair? '())").to_boolean() is False529 assert eval_(ctx, "(pair? +)").to_boolean() is False530 assert eval_(ctx, "(pair? (lambda () 1))").to_boolean() is False531 assert eval_(ctx, "(pair? '(1))").to_boolean() is True532 assert eval_(ctx, "(pair? (list 1))").to_boolean() is True533 assert eval_(ctx, "(pair? (cons 1 2))").to_boolean() is True534 assert eval_(ctx, "(procedure? 1)").to_boolean() is False535 assert eval_(ctx, "(procedure? 'symb)").to_boolean() is False536 assert eval_(ctx, "(procedure? #f)").to_boolean() is False537 assert eval_(ctx, "(procedure? '())").to_boolean() is False538 assert eval_(ctx, "(procedure? '(1))").to_boolean() is False539 assert eval_(ctx, "(procedure? (list 1))").to_boolean() is False540 assert eval_(ctx, "(procedure? (cons 1 2))").to_boolean() is False541 assert eval_(ctx, "(procedure? +)").to_boolean() is True542 assert eval_(ctx, "(procedure? (lambda () 1))").to_boolean() is True543 assert eval_(ctx, "(symbol? 1)").to_boolean() is False544 assert eval_(ctx, "(symbol? 'symb)").to_boolean() is True545 assert eval_(ctx, "(symbol? #f)").to_boolean() is False546 assert eval_(ctx, "(symbol? '())").to_boolean() is False547 assert eval_(ctx, "(symbol? '(1))").to_boolean() is False548 assert eval_(ctx, "(symbol? (list 1))").to_boolean() is False549 assert eval_(ctx, "(symbol? (cons 1 2))").to_boolean() is False550 assert eval_(ctx, "(symbol? +)").to_boolean() is False551 assert eval_(ctx, "(symbol? (lambda () 1))").to_boolean() is False552 assert eval_(ctx, "(boolean? 1)").to_boolean() is False553 assert eval_(ctx, "(boolean? 'symb)").to_boolean() is False554 assert eval_(ctx, "(boolean? #f)").to_boolean() is True555 assert eval_(ctx, "(boolean? #t)").to_boolean() is True556 assert eval_(ctx, "(boolean? '())").to_boolean() is False557 assert eval_(ctx, "(boolean? '(1))").to_boolean() is False558 assert eval_(ctx, "(boolean? (list 1))").to_boolean() is False559 assert eval_(ctx, "(boolean? (cons 1 2))").to_boolean() is False560 assert eval_(ctx, "(boolean? +)").to_boolean() is False561 assert eval_(ctx, "(boolean? (lambda () 1))").to_boolean() is False562def test_eqv():563 ctx = ExecutionContext()564 assert eval_(ctx, "(eqv? #t #t)").to_boolean() is True565 assert eval_(ctx, "(eqv? #f #f)").to_boolean() is True566 assert eval_(ctx, "(eqv? 'symb 'symb)").to_boolean() is True567 assert eval_(ctx, "(eqv? 'symb 'SYMB)").to_boolean() is True568 assert eval_(ctx, "(eqv? 42 42)").to_boolean() is True569 assert eval_(ctx, "(eqv? 42.1 42.1)").to_boolean() is True570 #assert eval_(ctx, "(eqv? #\a #\a)").to_boolean() is True571 assert eval_(ctx, "(eqv? '() '())").to_boolean() is True572 assert eval_(ctx, """(let ((p (cons 1 2)))573 (eqv? p p))""").to_boolean() is True574 #assert eval_(ctx, """(let ((p "a string"))575 # (eqv? p p))""").to_boolean() is True576 assert eval_(ctx, """(let ((p (lambda (x) x)))577 (eqv? p p))""").to_boolean() is True578 assert eval_(ctx, "(eqv? #t 'symb)").to_boolean() is False579 assert eval_(ctx, "(eqv? #f 42)").to_boolean() is False580 assert eval_(ctx, "(eqv? #t #f)").to_boolean() is False581 assert eval_(ctx, "(eqv? 'symb1 'symb2)").to_boolean() is False582 assert eval_(ctx, "(eqv? 42 42.0)").to_boolean() is False583 assert eval_(ctx, "(eqv? 42.0 42)").to_boolean() is False584 assert eval_(ctx, "(eqv? 42 43)").to_boolean() is False585 assert eval_(ctx, "(eqv? 42.1 42.2)").to_boolean() is False586 #assert eval_(ctx, "(eqv? #\a #\b)").to_boolean() is False587 assert eval_(ctx, "(eqv? (cons 1 2) (cons 1 2))").to_boolean() is False588 #assert eval_(ctx, """(eqv? "a string"589 # "a string")""").to_boolean() is False590 assert eval_(ctx, """(eqv? (lambda () 1)591 (lambda () 2))""").to_boolean() is False592def test_eq():593 ctx = ExecutionContext()594 assert eval_(ctx, "(eq? #t #t)").to_boolean() is True595 assert eval_(ctx, "(eq? #f #f)").to_boolean() is True596 assert eval_(ctx, "(eq? 'symb 'symb)").to_boolean() is True597 assert eval_(ctx, "(eq? 'symb 'SYMB)").to_boolean() is True598 assert eval_(ctx, "(eq? '() '())").to_boolean() is True599 assert eval_(ctx, """(let ((n 42))600 (eq? n n))""").to_boolean() is True601 assert eval_(ctx, """(let ((p (cons 1 2)))602 (eq? p p))""").to_boolean() is True603 #assert eval_(ctx, """(let ((p "a string"))604 # (eq? p p))""").to_boolean() is True605 assert eval_(ctx, """(let ((p (lambda (x) x)))606 (eq? p p))""").to_boolean() is True607 assert eval_(ctx, "(eq? #t 'symb)").to_boolean() is False608 assert eval_(ctx, "(eq? #f 42)").to_boolean() is False609 assert eval_(ctx, "(eq? #t #f)").to_boolean() is False610 assert eval_(ctx, "(eq? 'symb1 'symb2)").to_boolean() is False611 assert eval_(ctx, "(eq? 42 42)").to_boolean() is False612 assert eval_(ctx, "(eq? 42.1 42.1)").to_boolean() is False613 #assert eval_(ctx, "(eq? #\a #\a)").to_boolean() is False614 assert eval_(ctx, "(eq? 42 42.0)").to_boolean() is False615 assert eval_(ctx, "(eq? 42.0 42)").to_boolean() is False616 assert eval_(ctx, "(eq? 42 43)").to_boolean() is False617 assert eval_(ctx, "(eq? 42.1 42.2)").to_boolean() is False618 #assert eval_(ctx, "(eq? #\a #\b)").to_boolean() is False619 assert eval_(ctx, "(eq? (cons 1 2) (cons 1 2))").to_boolean() is False620 #assert eval_(ctx, """(eq? "a string"621 # "a string")""").to_boolean() is False622 assert eval_(ctx, """(eq? (lambda () 1)623 (lambda () 2))""").to_boolean() is False624def test_equal():625 ctx = ExecutionContext()626 assert eval_(ctx, "(equal? #t #t)").to_boolean() is True627 assert eval_(ctx, "(equal? #f #f)").to_boolean() is True628 assert eval_(ctx, "(equal? 'symb 'symb)").to_boolean() is True629 assert eval_(ctx, "(equal? 'symb 'SYMB)").to_boolean() is True630 assert eval_(ctx, "(equal? 42 42)").to_boolean() is True631 assert eval_(ctx, "(equal? 42.1 42.1)").to_boolean() is True632 #assert eval_(ctx, "(equal? #\a #\a)").to_boolean() is True633 assert eval_(ctx, "(equal? '() '())").to_boolean() is True634 assert eval_(ctx, """(let ((p (cons 1 2)))635 (equal? p p))""").to_boolean() is True636 #assert eval_(ctx, """(let ((p "a string"))637 # (equal? p p))""").to_boolean() is True638 assert eval_(ctx, """(let ((p (lambda (x) x)))639 (equal? p p))""").to_boolean() is True640 assert eval_(ctx, "(equal? #t 'symb)").to_boolean() is False641 assert eval_(ctx, "(equal? #f 42)").to_boolean() is False642 assert eval_(ctx, "(equal? #t #f)").to_boolean() is False643 assert eval_(ctx, "(equal? 'symb1 'symb2)").to_boolean() is False644 assert eval_(ctx, "(equal? 42 42.0)").to_boolean() is False645 assert eval_(ctx, "(equal? 42.0 42)").to_boolean() is False646 assert eval_(ctx, "(equal? 42 43)").to_boolean() is False647 assert eval_(ctx, "(equal? 42.1 42.2)").to_boolean() is False648 #assert eval_(ctx, "(equal? #\a #\b)").to_boolean() is False649 assert eval_(ctx, "(equal? (cons 1 2) (cons 1 2))").to_boolean() is True650 #assert eval_(ctx, """(equal? "a string"651 # "a string")""").to_boolean() is True652 assert eval_(ctx, """(equal? (lambda () 1)653 (lambda () 2))""").to_boolean() is False654 assert eval_(ctx, "(equal? '(a (b) c) '(a (b) c))").to_boolean() is True655 assert eval_(ctx, "(equal? '(a (b) c) '(a (e) c))").to_boolean() is False656def test_apply():657 ctx = ExecutionContext()658 assert eval_(ctx, "(apply + (list 3 4))").to_number() == 7659 eval_(ctx, """(define compose660 (lambda (f g)661 (lambda args662 (f (apply g args)))))""")663 w_result = eval_(ctx, "((compose (lambda (x) (* x x)) +) 3 5)")664 assert w_result.to_number() == 64665 assert eval_(ctx, "(apply + '())").to_number() == 0666 py.test.raises(WrongArgsNumber, eval_, ctx, "(apply 1)")667 py.test.raises(WrongArgType, eval_, ctx, "(apply 1 '(1))")...
test_continuation.py
Source:test_continuation.py
1import py2from pypy.lang.scheme.ssparser import parse3from pypy.lang.scheme.execution import ExecutionContext4from pypy.lang.scheme.object import *5def eval_(ctx, expr):6 try:7 return parse(expr)[0].eval(ctx)8 except ContinuationReturn, e:9 return e.result10def test_callcc():11 ctx = ExecutionContext()12 eval_(ctx, "(define cont #f)")13 w_result = eval_(ctx, """(call/cc (lambda (k) (set! cont k) 3))""")14 w_result = eval_(ctx, "(cont 3)")15 assert w_result.to_number() == 316 w_result = eval_(ctx, "(cont #f)")17 assert w_result.to_boolean() is False18 #this (+ 1 [...]) should be ingored19 w_result = eval_(ctx, "(+ 1 (cont 3))")20 assert w_result.to_number() == 321 w_result = eval_(ctx, "(+ 1 (cont #t))")22 assert w_result.to_boolean() is True23def test_callcc_callcc():24 ctx = ExecutionContext()25 w_procedure = eval_(ctx, "(call/cc call/cc)")26 assert isinstance(w_procedure, W_Procedure)27 print w_procedure28 eval_(ctx, "(define cont 'none)")29 w_result = eval_(ctx, """((call/cc call/cc) (lambda (k)30 (set! cont k)31 'done))""")32 assert w_result.to_string() == "done"33 assert isinstance(eval_(ctx, "cont"), W_Procedure)34 eval_(ctx, "(cont +)")35 assert eval_(ctx, "cont") is ctx.get("+")36def test_simple_multi_shot():37 ctx = ExecutionContext()38 eval_(ctx, "(define cont #f)")39 w_result = eval_(ctx, """40 (+ 1 2 (call/cc (lambda (k) (set! cont k) 3)) 4)""")41 assert w_result.to_number() == 1042 assert isinstance(eval_(ctx, "cont"), W_Procedure)43 w_result = eval_(ctx, "(cont 0)")44 assert w_result.to_number() == 745 w_result = eval_(ctx, "(cont 3)")46 assert w_result.to_number() == 1047def test_nested_multi_shot():48 ctx = ExecutionContext()49 eval_(ctx, "(define cont #f)")50 w_result = eval_(ctx, """51 (* 2 (+ 1 2 (call/cc (lambda (k) (set! cont k) 3)) 4) 1)""")52 assert w_result.to_number() == 2053 w_result = eval_(ctx, "(cont 0)")54 assert w_result.to_number() == 1455 w_result = eval_(ctx, "(cont 3)")56 assert w_result.to_number() == 2057def test_as_lambda_arg():58 ctx = ExecutionContext()59 eval_(ctx, "(define cont #f)")60 eval_(ctx, "(define (add3 a1 a2 a3) (+ a3 a2 a1))")61 w_result = eval_(ctx, """62 (add3 (call/cc (lambda (k) (set! cont k) 3)) 2 1)""")63 assert w_result.to_number() == 664 w_result = eval_(ctx, "(cont 0)")65 assert w_result.to_number() == 366 w_result = eval_(ctx, "(cont 3)")67 assert w_result.to_number() == 668def test_the_continuation():69 ctx = ExecutionContext()70 eval_(ctx, "(define con #f)")71 eval_(ctx, """72 (define (test)73 (let ((i 0))74 (call/cc (lambda (k) (set! con k)))75 ; The next time con is called, we start here.76 (set! i (+ i 1))77 i))""")78 assert eval_(ctx, "(test)").to_number() == 179 assert eval_(ctx, "(con)").to_number() == 280 assert eval_(ctx, "(con)").to_number() == 381 eval_(ctx, "(define con2 con)")82 assert eval_(ctx, "(test)").to_number() == 183 assert eval_(ctx, "(con)").to_number() == 284 assert eval_(ctx, "(con2)").to_number() == 485 assert eval_(ctx, "(+ 1 (con2))").to_number() == 586def test_the_continuation_x2():87 ctx = ExecutionContext()88 eval_(ctx, "(define con #f)")89 eval_(ctx, """90 (define (test)91 (* 2 92 (let ((i 0))93 (call/cc (lambda (k) (set! con k)))94 (set! i (+ i 1))95 i)))""")96 assert eval_(ctx, "(test)").to_number() == 297 assert eval_(ctx, "(con)").to_number() == 498 assert eval_(ctx, "(con)").to_number() == 699 eval_(ctx, "(define con2 con)")100 assert eval_(ctx, "(test)").to_number() == 2101 assert eval_(ctx, "(con)").to_number() == 4102 assert eval_(ctx, "(con2)").to_number() == 8103 assert eval_(ctx, "(+ 1 (con2))").to_number() == 10104def test_escape_continuation():105 ctx = ExecutionContext()106 eval_(ctx, "(define ret-failed #f)")107 w_result = eval_(ctx, """108 (let ((test 17))109 (call/cc (lambda (return)110 (if (odd? test) (return 'odd))111 (set! ret-failed #t)112 'even)))""")113 assert w_result.to_string() == "odd"114 assert eval_(ctx, "ret-failed").to_boolean() == False115def test_loop():116 ctx = ExecutionContext()117 eval_(ctx, "(define k 'none)")118 eval_(ctx, "(define num 'none)")119 w_result = eval_(ctx, """120 (call/cc121 (lambda (return)122 (letrec ((loop123 (lambda (n)124 (if (zero? n)125 0126 (begin127 (call/cc (lambda (cc)128 (set! k cc)129 (return n)))130 (set! num n)131 (loop (- n 1)))))))132 (loop 10))))""")133 assert w_result.to_number() == 10134 assert isinstance(ctx.get("k"), Continuation)135 assert ctx.get("num").to_string() == "none"136 for i in range(9, -1, -1):137 w_result = eval_(ctx, "(k)")138 assert w_result.to_number() == i139 assert ctx.get("num").to_number() == i+1140 w_result = eval_(ctx, "(k)")141 assert w_result.to_number() == 0142 assert ctx.get("num").to_number() == 1143def test_let_define():144 ctx = ExecutionContext()145 146 eval_(ctx, """(define oo147 (let ((cont (call/cc (lambda (k) k))))148 cont))""")149 assert isinstance(ctx.get("oo"), Continuation)150 eval_(ctx, "(oo +)")151 assert ctx.get("oo") is ctx.get("+")152def test_lambda_call():153 ctx = ExecutionContext()154 eval_(ctx, "(define c1 'none)")155 eval_(ctx, "(define c2 'none)")156 eval_(ctx, """(define fun (lambda (x y z)157 (call/cc (lambda (k)158 (set! c1 k)))159 (+ x y z)))""")160 assert ctx.get("c1").to_string() == "none"161 assert ctx.get("c2").to_string() == "none"162 eval_(ctx, """(fun (call/cc (lambda (k)163 (set! c2 k)164 1))165 2 3)""")166 w_result = eval_(ctx, "(c1)")167 assert w_result.to_number() == 6168 w_result = eval_(ctx, "(c2 0)")169 assert w_result.to_number() == 5170 w_result = eval_(ctx, "(c1)")171 assert w_result.to_number() == 5172 w_result = eval_(ctx, "(c2 5)")173 assert w_result.to_number() == 10174 w_result = eval_(ctx, "(c1)")175 assert w_result.to_number() == 10176def test_pitfall_1_1():177 ctx = ExecutionContext()178 w_result = eval_(ctx, """179 (let ((cont #f))180 (letrec ((x (call/cc (lambda (c) (set! cont c) 0)))181 (y (call/cc (lambda (c) (set! cont c) 0))))182 (if cont183 (let ((c cont))184 (set! cont #f)185 (set! x 1)186 (set! y 1)187 (c 0))188 (+ x y 1))))""")189 assert w_result.to_number() == 1190def test_pitfall_1_2():191 #py.test.skip("(cond ...), (and ...) not implemented")192 ctx = ExecutionContext()193 w_result = eval_(ctx, """194 (letrec ((x (call/cc list)) (y (call/cc list)))195 (cond ((procedure? x) (x (pair? y)))196 ((procedure? y) (y (pair? x))))197 (let ((x (car x)) (y (car y)))198 (and (call/cc x) (call/cc y) (call/cc x))))""")199 assert isinstance(w_result, W_Boolean)200 assert w_result.to_boolean() is True201def test_pitfall_1_3():202 ctx = ExecutionContext()203 w_result = eval_(ctx, """204 (letrec ((x (call/cc205 (lambda (c)206 (list #t c)))))207 (if (car x)208 ((car (cdr x)) (list #f (lambda () x)))209 (eq? x ((car (cdr x))))))""")210 assert isinstance(w_result, W_Boolean)211 assert w_result.to_boolean() is True212def test_pitfall_7_4():213 ctx = ExecutionContext()214 w_result = eval_(ctx, """215 (let ((x '())216 (y 0))217 (call/cc 218 (lambda (escape)219 (let* ((yin ((lambda (foo) 220 (set! x (cons y x))221 (if (= y 10)222 (escape x)223 (begin224 (set! y 0)225 foo)))226 (call/cc (lambda (bar) bar))))227 (yang ((lambda (foo) 228 (set! y (+ y 1))229 foo)230 (call/cc (lambda (baz) baz)))))231 (yin yang)))))""")232 assert isinstance(w_result, W_Pair)233 assert w_result.to_string() == "(10 9 8 7 6 5 4 3 2 1 0)"234def test_hefty1_computation():235 ctx = ExecutionContext()236 eval_(ctx, "(define side-effects '())")237 eval_(ctx, """238 (define (hefty-computation do-other-stuff)239 (letrec240 ((loop (lambda (n)241 (set! side-effects (cons (list 'hefty-a n) side-effects))242 (set! do-other-stuff (call/cc do-other-stuff))243 (set! side-effects (cons (list 'hefty-b n) side-effects))244 (set! do-other-stuff (call/cc do-other-stuff))245 (set! side-effects (cons (list 'hefty-c n) side-effects))246 (set! do-other-stuff (call/cc do-other-stuff))247 (if (zero? n)248 '()249 (loop (- n 1))))))250 (loop 1)))""")251 eval_(ctx, """252 (define (superfluous-computation do-other-stuff)253 (letrec254 ((loop (lambda ()255 (set! side-effects (cons 'break side-effects))256 (set! do-other-stuff (call/cc do-other-stuff))257 (loop))))258 (loop)))""")259 eval_(ctx, "(hefty-computation superfluous-computation)")260 assert ctx.get("side-effects").to_string() == \261 """(break (hefty-c 0) break (hefty-b 0) break (hefty-a 0) break (hefty-c 1) break (hefty-b 1) break (hefty-a 1))"""262def test_hefty2_computation():263 ctx = ExecutionContext()264 eval_(ctx, "(define side-effects '())")265 eval_(ctx, """266 (define (hefty-computation do-other-stuff)267 (letrec268 ((loop (lambda (n)269 (set! side-effects (cons (list 'hefty-a n) side-effects))270 (set! do-other-stuff (call/cc do-other-stuff))271 (set! side-effects (cons (list 'hefty-b n) side-effects))272 (set! do-other-stuff (call/cc do-other-stuff))273 (set! side-effects (cons (list 'hefty-c n) side-effects))274 (set! do-other-stuff (call/cc do-other-stuff))275 (if (zero? n)276 '()277 (loop (- n 1))))))278 (loop 1)))""")279 eval_(ctx, """280 (define (superfluous-computation do-other-stuff)281 (letrec282 ((loop (lambda ()283 (lst-loop '(straight quarter-past half quarter-til))284 (loop)))285 (lst-loop (lambda (lst) 286 (if (pair? lst)287 (let ((graphic (car lst)))288 (set! side-effects (cons graphic side-effects))289 (set! do-other-stuff (call/cc do-other-stuff))290 (lst-loop (cdr lst)))))))291 (loop)))""")292 eval_(ctx, "(hefty-computation superfluous-computation)")293 assert ctx.get("side-effects").to_string() == \...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!