summaryrefslogtreecommitdiff
path: root/prolog/problems/algol/algol_if_3/common.py
diff options
context:
space:
mode:
Diffstat (limited to 'prolog/problems/algol/algol_if_3/common.py')
-rw-r--r--prolog/problems/algol/algol_if_3/common.py42
1 files changed, 41 insertions, 1 deletions
diff --git a/prolog/problems/algol/algol_if_3/common.py b/prolog/problems/algol/algol_if_3/common.py
index 9198831..7e27430 100644
--- a/prolog/problems/algol/algol_if_3/common.py
+++ b/prolog/problems/algol/algol_if_3/common.py
@@ -1,3 +1,10 @@
+from operator import itemgetter
+import socket
+
+import prolog.engine
+import prolog.util
+from server.hints import Hint, HintPopup
+
id = 177
number = 20
visible = True
@@ -170,5 +177,38 @@ eval(E1 * E2, State, Val) :- !,
eval(E1 / E2, State, Val) :- !,
eval(E1, State, V1),
eval(E2, State, V2),
- Val is V1 / V2.
+ Val is V1 / V2.
'''
+
+test_cases = [
+ ('findall(W, (W = [_], algol_if(_, W, [])), Words), Words == []',
+ [{}]),
+ ('algol_if(F, [begin,if,a,<,0,then,a,:=,0-a,else,a,:=,a,end,end], []), \
+ apply([a= -2], Out, F), msort(Out, OutSorted), \
+ OutSorted == [a=2,printout=[]]',
+ [{}]),
+ ('algol_if(F, [begin,if,0,<,a,then,print(a),else,b,:=,a+5,print(b),end,end], []), \
+ apply([a= -2,b=0], Out, F), msort(Out, OutSorted), \
+ OutSorted == [a= -2,b=3,printout=[3]]',
+ [{}]),
+]
+
+def test(code, aux_code):
+ n_correct = 0
+ engine_id = None
+ try:
+ engine_id, output = prolog.engine.create(code=code+aux_code, timeout=1.0)
+ if engine_id is not None and 'error' not in map(itemgetter(0), output):
+ # Engine successfully created, and no syntax error in program.
+ for query, answers in test_cases:
+ if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0):
+ n_correct += 1
+ except socket.timeout:
+ pass
+ finally:
+ if engine_id:
+ prolog.engine.destroy(engine_id)
+
+ hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}]
+ return n_correct, len(test_cases), hints
+