tests.pl (5573B)
1 :- use_module(library(debug), [assertion/1]). 2 3 :- use_module(interpreter). 4 5 portray_long :- portray_depth(32). 6 portray_depth(Depth) :- 7 set_prolog_flag(answer_write_options, [quoted(true), portrayed(true), max_depth(Depth), spacing(next_argument)]), 8 set_prolog_flag(debugger_write_options, [quoted(true), portrayed(true), max_depth(Depth), spacing(next_argument)]). 9 10 test_one_result(Template, Goal, Output) :- 11 findall(Template, Goal, Results), 12 % write(Results), nl, 13 assertion(length(Results, 1)), 14 Results = [Output]. 15 16 decode(Codes, Expression) :- 17 test_one_result(E, parse_input_expression(Codes, E), Expression). 18 19 encode_then_decode(Expression) :- 20 once(parse_input_expression(Codes, Expression)), 21 decode(Codes, Decoded), 22 assertion(Expression =@= Decoded). 23 24 :- begin_tests(errors). 25 %:- use_module('$messages', [message_to_string/2]). 26 test(read_error) :- 27 message_to_string(error(read_error, _), Message), 28 assertion(Message =@= "Failed to read line."). 29 :- end_tests(errors). 30 :- begin_tests(sexp). 31 32 test(sexp_atom) :- 33 decode(`a`, Expression), 34 assertion(Expression =@= a), 35 encode_then_decode(Expression). 36 37 test(sexp_atom2) :- 38 decode(`hello-world!`, Expression), 39 assertion(Expression =@= 'hello-world!'), 40 encode_then_decode(Expression). 41 42 test(sexp_number) :- 43 decode(`0`, Expression), 44 assertion(Expression =@= 0), 45 encode_then_decode(Expression). 46 47 test(sexp_number2) :- 48 decode(`31337`, Expression), 49 assertion(Expression =@= 31337), 50 encode_then_decode(Expression). 51 52 test(sexp_list_empty) :- 53 decode(`()`, Expression), 54 assertion(Expression =@= []), 55 encode_then_decode(Expression). 56 57 test(sexp_list_one_atom) :- 58 decode(`(a)`, Expression), 59 assertion(Expression =@= [a]), 60 encode_then_decode(Expression). 61 62 test(sexp_list_one_two_atoms) :- 63 decode(`(a b)`, Expression), 64 assertion(Expression =@= [a, b]), 65 encode_then_decode(Expression). 66 67 test(sexp_def) :- 68 decode(`(define proc (lambda (e f g) (lambda () e)))`, Expression), 69 assertion(Expression =@= [define, proc, [lambda, [e, f, g], [lambda, [], e]]]), 70 encode_then_decode(Expression). 71 72 test(sexp_qq) :- 73 Expression = {|sexp||(define proc (lambda (e f g) (lambda () e)))|}, 74 assertion(Expression =@= [define, proc, [lambda, [e, f, g], [lambda, [], e]]]), 75 encode_then_decode(Expression). 76 77 :- end_tests(sexp). 78 79 apply_example(car, [{|sexp||(a b c)|}], {|sexp||a|}). 80 apply_example(car, [{|sexp||((a b c) x y z)|}], {|sexp||(a b c)|}). 81 apply_example(car, [{|sexp||(((hotdogs)) (and) (pickle) relish)|}], {|sexp||((hotdogs))|}). 82 apply_example(car, [{|sexp||((hotdogs))|}], {|sexp||(hotdogs)|}). 83 84 apply_example(cdr, [{|sexp||(a b c)|}], {|sexp||(b c)|}). 85 apply_example(cdr, [{|sexp||((a b c) x y z)|}], {|sexp||(x y z)|}). 86 apply_example(cdr, [{|sexp||(hamburger)|}], {|sexp||()|}). 87 apply_example(cdr, [{|sexp||((x) t r)|}], {|sexp||(t r)|}). 88 89 apply_example(Func, Args, ExpectedResult) :- 90 apply_example(CallCs, ResultCs), 91 decode(CallCs, [Func|Args]), 92 decode(ResultCs, ExpectedResult). 93 94 apply_example(`(cons peanut (butter and jelly))`, 95 `(peanut butter and jelly)`). 96 97 apply_example(`(cons ((help) this) (is very ((hard) to learn)))`, 98 `(((help) this) is very ((hard) to learn))`). 99 100 apply_example(`(cons (a b (c)) ())`, `((a b (c)))`). 101 102 apply_example(`(+ 0 0)`, `0`). 103 apply_example(`(+ 3 2)`, `5`). 104 105 :- begin_tests(primitive_apply). 106 test(apply_example, [forall(apply_example(Func, Args, ExpectedResult))]) :- 107 initial_state(State0), 108 primitive_arity(Func, Arity), 109 interp_apply(primitive(Func, Arity), Args, Result, State0, _State1), 110 assertion(Result =@= ExpectedResult). 111 112 test(apply_arity_error, [error(interp_error(arity, car))]) :- 113 Func=car, 114 initial_state(State0), 115 primitive_arity(Func, Arity), 116 interp_apply(primitive(Func, Arity), [[a], [b]], _Result, State0, _State1). 117 118 :- end_tests(primitive_apply). 119 :- begin_tests(eval). 120 121 quoted_args([], []). 122 quoted_args([H|T], [[quote, H]|QT]) :- quoted_args(T, QT). 123 124 test_eval(Call, ExpectedResult) :- 125 initial_state(State0), 126 test_eval(Call, ExpectedResult, State0, _State1). 127 test_eval(Call, ExpectedResult, State0, State1) :- 128 format('testing call: ~w → expecting ~w | ~w~n', [Call, ExpectedResult, State0]), 129 interp_eval(Call, Result, State0, State1), 130 assertion(Result =@= ExpectedResult). 131 132 eval_example(`(car (cdr (quote ((b) (x y) ((c))))))`, `(x y)`). 133 eval_example(`(cdr (cdr (quote ((b) (x y) ((c))))))`, `(((c)))`). 134 135 eval_example_decoded(Call, ExpectedResult) :- 136 eval_example(CallCodes, ResultCodes), 137 decode(CallCodes, Call), 138 decode(ResultCodes, ExpectedResult). 139 140 test(eval_quote) :- 141 test_eval({|sexp||(quote a)|}, a). 142 143 test(eval_quote2) :- 144 test_eval({|sexp||(quote ())|}, []). 145 146 test(eval_apply_example, [forall(apply_example(Func, Args, ExpectedResult))]) :- 147 quoted_args(Args, QArgs), 148 Call = [Func|QArgs], 149 test_eval(Call, ExpectedResult). 150 151 test(eval_example, [forall(eval_example_decoded(Call, ExpectedResult))]) :- 152 test_eval(Call, ExpectedResult). 153 154 eval_define_lat :- 155 DefLat = {|sexp|| 156 (define lat? 157 (lambda (l) 158 (cond 159 ((null? l) #t) 160 ((atom? (car l)) (lat? (cdr l))) 161 (else #f))))|}, 162 initial_state(State0), 163 interp_eval(DefLat, _, State0, State1), 164 lookup_variable_value('lat?', Proc, State1, _), 165 assertion(Proc = proc([l], _, _)), 166 Call0 = {|sexp||(lat? (quote ()))|}, 167 test_eval(Call0, '#t', State1, _), 168 Call1 = {|sexp||(lat? (quote (Jack Sprat could eat no chicken fat)))|}, 169 test_eval(Call1, '#t', State1, _), 170 Call2 = {|sexp||(lat? (quote ((Jack) Sprat could eat no chicken fat)))|}, 171 test_eval(Call2, '#f', State1, _). 172 test(eval_define_lat) :- eval_define_lat. 173 174 :- end_tests(eval).