From e87e7ffa37e750546d0b7f12144804c028e26641 Mon Sep 17 00:00:00 2001 From: Aleksander Sadikov Date: Sun, 15 May 2016 22:45:17 +0200 Subject: DCG: test cases added. --- prolog/problems/dcg/ab_2/common.py | 40 +++++++++++++++++++++++ prolog/problems/dcg/digit_2/common.py | 36 +++++++++++++++++++++ prolog/problems/dcg/flower_2/common.py | 40 +++++++++++++++++++++++ prolog/problems/dcg/flower_2/sl.py | 2 ++ prolog/problems/dcg/number_2/common.py | 42 ++++++++++++++++++++++++ prolog/problems/dcg/number_3/common.py | 40 +++++++++++++++++++++++ prolog/problems/dcg/number_proper_2/common.py | 40 +++++++++++++++++++++++ prolog/problems/dcg/paren_2/common.py | 45 ++++++++++++++++++++++++++ prolog/problems/dcg/paren_3/common.py | 46 +++++++++++++++++++++++++++ 9 files changed, 331 insertions(+) (limited to 'prolog/problems') diff --git a/prolog/problems/dcg/ab_2/common.py b/prolog/problems/dcg/ab_2/common.py index 4c20a86..7646b95 100644 --- a/prolog/problems/dcg/ab_2/common.py +++ b/prolog/problems/dcg/ab_2/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 = 162 number = 10 visible = True @@ -8,3 +15,36 @@ ab --> [a], ab. ab --> t162. t162 --> [b], t162. t162 --> [].''' + +test_cases = [ + ('ab([], [])', + [{}]), + ('setof(W, [A]^(W = [A], ab(W, [])), Words), Words == [[a],[b]]', + [{}]), + ('setof(W, [A,B]^(W = [A,B], ab(W, [])), Words), Words == [[a,a],[a,b],[b,b]]', + [{}]), + ('setof(W, [A,B,C]^(W = [A,B,C], ab(W, [])), Words), Words == [[a,a,a],[a,a,b],[a,b,b],[b,b,b]]', + [{}]), + ('setof(W, [A,B,C,D]^(W = [A,B,C,D], ab(W, [])), Words), Words == [[a,a,a,a],[a,a,a,b],[a,a,b,b],[a,b,b,b],[b,b,b,b]]', + [{}]), +] + +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 + diff --git a/prolog/problems/dcg/digit_2/common.py b/prolog/problems/dcg/digit_2/common.py index 5b4fcec..4a0d825 100644 --- a/prolog/problems/dcg/digit_2/common.py +++ b/prolog/problems/dcg/digit_2/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 = 164 number = 30 visible = True @@ -6,3 +13,32 @@ facts = None solution = '''\ digit --> ([0] ; [1] ; [2] ; [3] ; [4] ; [5] ; [6] ; [7] ; [8] ; [9]). ''' + +test_cases = [ + ('digit([2], [])', + [{}]), + ('digit([5], [])', + [{}]), + ('setof(W, digit(W, []), Words), Words == [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]]', + [{}]), +] + +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 + diff --git a/prolog/problems/dcg/flower_2/common.py b/prolog/problems/dcg/flower_2/common.py index 0967068..cf08ec5 100644 --- a/prolog/problems/dcg/flower_2/common.py +++ b/prolog/problems/dcg/flower_2/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 = 163 number = 20 visible = True @@ -10,3 +17,36 @@ flower --> [+], flower, [+]. iflower163 --> [-]. iflower163 --> [-], iflower163. ''' + +test_cases = [ + ('setof(W, [A]^(W = [A], flower(W, [])), Words), Words == [[-]]', + [{}]), + ('setof(W, [A,B]^(W = [A,B], flower(W, [])), Words), Words == [[-,-]]', + [{}]), + ('setof(W, [A,B,C]^(W = [A,B,C], flower(W, [])), Words), Words == [[+,-,+],[-,-,-]]', + [{}]), + ('setof(W, [A,B,C,D]^(W = [A,B,C,D], flower(W, [])), Words), Words == [[+,-,-,+],[-,-,-,-]]', + [{}]), + ('setof(W, [A,B,C,D,E]^(W = [A,B,C,D,E], flower(W, [])), Words), Words == [[+,+,-,+,+],[+,-,-,-,+],[-,-,-,-,-]]', + [{}]), +] + +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 + diff --git a/prolog/problems/dcg/flower_2/sl.py b/prolog/problems/dcg/flower_2/sl.py index 941c59a..20f65a6 100644 --- a/prolog/problems/dcg/flower_2/sl.py +++ b/prolog/problems/dcg/flower_2/sl.py @@ -1,3 +1,5 @@ + + name = 'flower/2' slug = 'Besede v obliki "rožice", npr. +++--+++' diff --git a/prolog/problems/dcg/number_2/common.py b/prolog/problems/dcg/number_2/common.py index 04226b4..9560d23 100644 --- a/prolog/problems/dcg/number_2/common.py +++ b/prolog/problems/dcg/number_2/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 = 165 number = 40 visible = True @@ -9,3 +16,38 @@ digit165 --> ([0] ; [1] ; [2] ; [3] ; [4] ; [5] ; [6] ; [7] ; [8] ; [9]). number --> digit165. number --> digit165, number. ''' + +test_cases = [ + ('setof(W, [A]^(W = [A], number(W, [])), Words), Words == [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]]', + [{}]), + ('setof(W, [A,B]^(W = [A,B], number(W, [])), Words), length(Words, 100)', + [{}]), + ('setof(W, [A,B,C]^(W = [A,B,C], number(W, [])), Words), length(Words, 1000)', + [{}]), + ('W = [_,_], number(W, []), W == [0,5]', + [{}]), + ('W = [_,_,_], number(W, []), W == [4,2,2]', + [{}]), + ('W = [1,0,_,_], number(W, []), W == [1,0,0,1]', + [{}]), +] + +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 + diff --git a/prolog/problems/dcg/number_3/common.py b/prolog/problems/dcg/number_3/common.py index b4a9f58..82981d5 100644 --- a/prolog/problems/dcg/number_3/common.py +++ b/prolog/problems/dcg/number_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 = 167 number = 60 visible = True @@ -27,3 +34,36 @@ l2n167([H|T], S, N) :- S1 is S*10+H, l2n167(T, S1, N). ''' + +test_cases = [ + ('number(N, [0], []), N == 0', + [{}]), + ('number(N, [3,4,1,5], []), N == 3415', + [{}]), + ('number(N, [9,8,7], []), N == 987', + [{}]), + ('W = [_], number(0, W, []), W == [0]', + [{}]), + ('W = [_,_], number(62, W, []), W == [6,2]', + [{}]), +] + +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 + diff --git a/prolog/problems/dcg/number_proper_2/common.py b/prolog/problems/dcg/number_proper_2/common.py index 3dc0dd5..6e1c376 100644 --- a/prolog/problems/dcg/number_proper_2/common.py +++ b/prolog/problems/dcg/number_proper_2/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 = 166 number = 50 visible = True @@ -13,3 +20,36 @@ num_next166 --> digit166, num_next166. digit166 --> ([0] ; [1] ; [2] ; [3] ; [4] ; [5] ; [6] ; [7] ; [8] ; [9]). nzdigit166 --> ([1] ; [2] ; [3] ; [4] ; [5] ; [6] ; [7] ; [8] ; [9]). ''' + +test_cases = [ + ('setof(W, [A]^(W = [A], number_proper(W, [])), Words), Words == [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]]', + [{}]), + ('setof(W, [A,B]^(W = [A,B], number_proper(W, [])), Words), length(Words, 90)', + [{}]), + ('setof(W, [A,B,C]^(W = [A,B,C], number_proper(W, [])), Words), length(Words, 900)', + [{}]), + ('W = [_,_], number_proper(W, []), W == [3,9]', + [{}]), + ('W = [_,_,_], number_proper(W, []), W == [4,2,2]', + [{}]), +] + +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 + diff --git a/prolog/problems/dcg/paren_2/common.py b/prolog/problems/dcg/paren_2/common.py index 8c040e7..2fa050f 100644 --- a/prolog/problems/dcg/paren_2/common.py +++ b/prolog/problems/dcg/paren_2/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 = 168 number = 70 visible = True @@ -7,3 +14,41 @@ solution = '''\ paren --> []. paren --> ['('], paren, [')'], paren. ''' + +test_cases = [ + ('findall(W, (W = [_], paren(W, [])), Words), Words == []', + [{}]), + ('findall(W, (W = [_,_,_], paren(W, [])), Words), Words == []', + [{}]), + ('findall(W, (W = [_,_,_,_,_], paren(W, [])), Words), Words == []', + [{}]), + ("setof(W, [A,B]^(W = [A,B], paren(W, [])), Words), Words == [['(',')']]", + [{}]), + ("setof(W, [A,B,C,D]^(W = [A,B,C,D], paren(W, [])), Words), Words == [['(','(',')',')'],['(',')','(',')']]", + [{}]), + ("setof(W, [A,B,C,D,E,F]^(W = [A,B,C,D,E,F], paren(W, [])), Words), \ + Words == [['(','(','(',')',')',')'],['(','(',')','(',')',')'], \ + ['(','(',')',')','(',')'],['(',')','(','(',')',')'], \ + ['(',')','(',')','(',')']]", + [{}]), +] + +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 + diff --git a/prolog/problems/dcg/paren_3/common.py b/prolog/problems/dcg/paren_3/common.py index 60c6353..3d7131f 100644 --- a/prolog/problems/dcg/paren_3/common.py +++ b/prolog/problems/dcg/paren_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 = 169 number = 80 visible = True @@ -7,3 +14,42 @@ solution = '''\ paren(0) --> []. paren(D) --> ['('], paren(D1), [')'], paren(D2), { D1 >= D2, D is D1 + 1 ; D1 < D2 , D is D2 }. ''' + +test_cases = [ + ('findall(W, (W = [_], paren(_, W, [])), Words), Words == []', + [{}]), + ('findall(W, (W = [_,_,_], paren(_, W, [])), Words), Words == []', + [{}]), + ('findall(W, (W = [_,_,_,_,_], paren(_, W, [])), Words), Words == []', + [{}]), + ("paren(D, ['(',')'], []), D == 1", + [{}]), + ("paren(D, ['(',')','(',')','(',')'], []), D == 1", + [{}]), + ("paren(D, ['(','(',')','(',')',')'], []), D == 2", + [{}]), + ("paren(D, ['(','(','(',')',')',')'], []), D == 3", + [{}]), + ("paren(D, ['(','(','(',')',')',')','(',')','(','(',')',')'], []), D == 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 + -- cgit v1.2.1