Skip to content

Commit d079951

Browse files
committed
added tests
1 parent 1da6c61 commit d079951

File tree

4 files changed

+343
-0
lines changed

4 files changed

+343
-0
lines changed

src/test/java/com/igormaznitsa/prologparser/IntegrationTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,9 @@ private void assertReadTerms(final int expected, final String resource, final Op
571571

572572
@Test
573573
public void testParseSourceFiles() {
574+
assertReadTerms(17, "calc.p");
575+
assertReadTerms(23, "tictac.p");
576+
assertReadTerms(33, "poly.p");
574577
assertReadTerms(36, "cover.p", Op.make(400, FY, "private"), Op.make(400, XFX, "is_atom"));
575578
assertReadTerms(70, "sincos.p");
576579
assertReadTerms(49, "moddiv.p");
@@ -976,6 +979,10 @@ public void testOperatorAsFunctor() throws Exception {
976979

977980
@Test
978981
public void testPairOfOperatorsWithIncompatiblePrecedence() throws Exception {
982+
// assertEquals("", parseEd("1---1.").next().toString());
983+
assertEquals("1 + 1 * a * a + a - 1", parseEd("1+1*a*a+a-1.").next().toString());
984+
assertEquals("-1 + 2 ** (- 3 ** (-4))", parseEd("-1+2**-3**-4.").next().toString());
985+
assertEquals("X = (discontiguous)", parseEd("X=discontiguous.").next().toString());
979986
assertEquals("- (discontiguous)", parseEd("-discontiguous.").next().toString());
980987
assertEquals("2 ** (-1)", parseEd("2**-1.").next().toString());
981988
assertEquals("0.2 is 5 ** (-1)", parseEd("0.2 is 5** -1.").next().toString());
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* Prolog code for the DCG calculator.
3+
*
4+
* DCGs are a simple case of Colmerauers Metamorphosis grammars
5+
* an were presented to the community in the 1980 paper
6+
* Definite Clause Grammars for Language Analysis, by F.C.N.
7+
* Pereira and D.H.D. Warren.
8+
*
9+
* The DCG calculator is usually considered Prolog homework.
10+
*
11+
* Warranty & Liability
12+
* To the extent permitted by applicable law and unless explicitly
13+
* otherwise agreed upon, XLOG Technologies GmbH makes no warranties
14+
* regarding the provided information. XLOG Technologies GmbH assumes
15+
* no liability that any problems might be solved with the information
16+
* provided by XLOG Technologies GmbH.
17+
*
18+
* Rights & License
19+
* All industrial property rights regarding the information - copyright
20+
* and patent rights in particular - are the sole property of XLOG
21+
* Technologies GmbH. If the company was not the originator of some
22+
* excerpts, XLOG Technologies GmbH has at least obtained the right to
23+
* reproduce, change and translate the information.
24+
*
25+
* Reproduction is restricted to the whole unaltered document. Reproduction
26+
* of the information is only allowed for non-commercial uses. Selling,
27+
* giving away or letting of the execution of the library is prohibited.
28+
* The library can be distributed as part of your applications and libraries
29+
* for execution provided this comment remains unchanged.
30+
*
31+
* Trademarks
32+
* Jekejeke is a registered trademark of XLOG Technologies GmbH.
33+
*/
34+
35+
/*****************************************************************/
36+
/* Normal Test Cases */
37+
/*****************************************************************/
38+
39+
% calc
40+
calc :-
41+
phrase(expr(_), "-12+34*56+78").
42+
calc :-
43+
phrase(expr(_), "(-12+34)*(56+78)").
44+
45+
/*****************************************************************/
46+
/* Reduced Test Cases */
47+
/*****************************************************************/
48+
49+
% rcalc
50+
rcalc :-
51+
phrase(expr(_), "(-12+34)*56+78").
52+
53+
/*****************************************************************/
54+
/* The Calculator */
55+
/*****************************************************************/
56+
57+
% expr(-Integer)
58+
expr(Z) --> "-", !,
59+
term(X),
60+
Y is -X,
61+
expr_rest(Y, Z).
62+
expr(Y) -->
63+
term(X),
64+
expr_rest(X, Y).
65+
66+
% expr_rest(+Integer, -Integer)
67+
expr_rest(X, T) --> "+", !,
68+
term(Y),
69+
Z is X+Y,
70+
expr_rest(Z, T).
71+
expr_rest(X, T) --> "-", !,
72+
term(Y),
73+
Z is X-Y,
74+
expr_rest(Z, T).
75+
expr_rest(X, X) --> [].
76+
77+
% term(-Integer)
78+
term(Y) -->
79+
factor(X),
80+
term_rest(X, Y).
81+
82+
% term_rest(+Integer, -Integer)
83+
term_rest(X, T) --> "*", !,
84+
factor(Y),
85+
Z is X*Y,
86+
factor_rest(Z, T).
87+
term_rest(X, T) --> "/", !,
88+
factor(Y),
89+
Z is X/Y,
90+
factor_rest(Z, T).
91+
term_rest(X, X) --> [].
92+
93+
% factor(-Integer)
94+
factor(X) --> "(", !,
95+
expr(X), ")".
96+
factor(Y) -->
97+
digit(X),
98+
factor_rest(X, Y).
99+
100+
% factor_rest(+Integer, -Integer)
101+
factor_rest(X, T) -->
102+
digit(Y), !,
103+
Z is X*10+Y,
104+
factor_rest(Z, T).
105+
factor_rest(X, X) --> [].
106+
107+
% digit(-Integer)
108+
digit(Y) -->
109+
[X],
110+
48 =< X,
111+
X =< 57,
112+
Y is X-48.
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/**
2+
* Prolog code for the polynominal reduction benchmark.
3+
*
4+
* This benchmark can be found in:
5+
* Haygood, R. (1989): A Prolog Benchmark Suite for Aquarius,
6+
* Computer Science Division, University of California
7+
* Berkely, April 30, 1989
8+
*
9+
* It has its root in some Lisp code by R.P. Gabriel.
10+
*
11+
* We used a brushed up version where polynomials are better
12+
* normalized. Therefore we find some additional predicates
13+
* such as make_poly/3 and make_term/4.
14+
*
15+
* Warranty & Liability
16+
* To the extent permitted by applicable law and unless explicitly
17+
* otherwise agreed upon, XLOG Technologies GmbH makes no warranties
18+
* regarding the provided information. XLOG Technologies GmbH assumes
19+
* no liability that any problems might be solved with the information
20+
* provided by XLOG Technologies GmbH.
21+
*
22+
* Rights & License
23+
* All industrial property rights regarding the information - copyright
24+
* and patent rights in particular - are the sole property of XLOG
25+
* Technologies GmbH. If the company was not the originator of some
26+
* excerpts, XLOG Technologies GmbH has at least obtained the right to
27+
* reproduce, change and translate the information.
28+
*
29+
* Reproduction is restricted to the whole unaltered document. Reproduction
30+
* of the information is only allowed for non-commercial uses. Selling,
31+
* giving away or letting of the execution of the library is prohibited.
32+
* The library can be distributed as part of your applications and libraries
33+
* for execution provided this comment remains unchanged.
34+
*
35+
* Trademarks
36+
* Jekejeke is a registered trademark of XLOG Technologies GmbH.
37+
*/
38+
39+
/*****************************************************************/
40+
/* Normal Test Cases */
41+
/*****************************************************************/
42+
43+
% poly
44+
poly :-
45+
poly_add(1, poly(x,[term(1,-1)]), X1),
46+
poly_add(X1, poly(y,[term(1,1)]), X2),
47+
poly_add(X2, poly(z,[term(1,-1)]), X3),
48+
poly_exp(10, X3, _).
49+
50+
/*****************************************************************/
51+
/* Reduced Test Cases */
52+
/*****************************************************************/
53+
54+
% rpoly
55+
rpoly :-
56+
poly_add(1, poly(x,[term(1,-1)]), X1),
57+
poly_add(X1, poly(y,[term(1,1)]), X2),
58+
poly_exp(10, X2, _).
59+
60+
/*****************************************************************/
61+
/* The Simplifier */
62+
/*****************************************************************/
63+
64+
% make_poly(+Sum, +Var, -Expr)
65+
make_poly([], _, 0) :- !.
66+
make_poly(Terms, Var, poly(Var,Terms)).
67+
68+
% poly_add(+Expr, +Expr, -Expr)
69+
poly_add(poly(Var,Terms1), poly(Var,Terms2), Res) :- !,
70+
term_add(Terms1, Terms2, Terms),
71+
make_poly(Terms, Var, Res).
72+
poly_add(poly(Var1,Terms1), poly(Var2,Terms2), poly(Var1,Terms)) :-
73+
Var1 @< Var2, !,
74+
add_to_order_zero_term(Terms1, poly(Var2,Terms2), Terms).
75+
poly_add(Poly, poly(Var,Terms2), poly(Var,Terms)) :- !,
76+
add_to_order_zero_term(Terms2, Poly, Terms).
77+
poly_add(poly(Var,Terms1), C, poly(Var,Terms)) :- !,
78+
add_to_order_zero_term(Terms1, C, Terms).
79+
poly_add(C1, C2, C) :-
80+
C is C1+C2.
81+
82+
% make_term(+Expr, +Integer, +Sum, -Sum)
83+
make_term(0, _, Terms, Terms) :- !.
84+
make_term(C, E, Terms, [term(E,C)|Terms]).
85+
86+
% term_add(+Sum, +Sum, -Sum)
87+
term_add([], X, X) :- !.
88+
term_add(X, [], X) :- !.
89+
term_add([term(E,C1)|Terms1], [term(E,C2)|Terms2], Res) :- !,
90+
poly_add(C1, C2, C),
91+
term_add(Terms1, Terms2, Terms),
92+
make_term(C, E, Terms, Res).
93+
term_add([term(E1,C1)|Terms1], [term(E2,C2)|Terms2], [term(E1,C1)|Terms]) :-
94+
E1 < E2, !,
95+
term_add(Terms1, [term(E2,C2)|Terms2], Terms).
96+
term_add(Terms1, [term(E2,C2)|Terms2], [term(E2,C2)|Terms]) :-
97+
term_add(Terms1, Terms2, Terms).
98+
99+
% add_to_order_zero_term(+Sum, +Expr, -Sum)
100+
add_to_order_zero_term([term(0,C1)|Terms], C2, [term(0,C)|Terms]) :- !,
101+
poly_add(C1, C2, C).
102+
add_to_order_zero_term(Terms, C, [term(0,C)|Terms]).
103+
104+
% poly_mul(+Expr, +Expr, -Expr)
105+
poly_mul(poly(Var,Terms1), poly(Var,Terms2), poly(Var,Terms)) :- !,
106+
term_mul(Terms1, Terms2, Terms).
107+
poly_mul(poly(Var1,Terms1), poly(Var2,Terms2), poly(Var1,Terms)) :-
108+
Var1 @< Var2, !,
109+
mul_through(Terms1, poly(Var2,Terms2), Terms).
110+
poly_mul(P, poly(Var,Terms2), Res) :- !,
111+
mul_through(Terms2, P, Terms),
112+
make_poly(Terms, Var, Res).
113+
poly_mul(poly(Var,Terms1), C, Res) :- !,
114+
mul_through(Terms1, C, Terms),
115+
make_poly(Terms, Var, Res).
116+
poly_mul(C1, C2, C) :-
117+
C is C1*C2.
118+
119+
% term_mul(+Sum, +Sum, -Sum)
120+
term_mul([], _, []) :- !.
121+
term_mul(_, [], []) :- !.
122+
term_mul([Term|Terms1], Terms2, Terms) :-
123+
single_term_mul(Terms2, Term, PartA),
124+
term_mul(Terms1, Terms2, PartB),
125+
term_add(PartA, PartB, Terms).
126+
127+
% single_term_mul(+Sum, +Summand, -Sum)
128+
single_term_mul([], _, []).
129+
single_term_mul([term(E1,C1)|Terms1], term(E2,C2), [term(E,C)|Terms]) :-
130+
E is E1+E2,
131+
poly_mul(C1, C2, C),
132+
single_term_mul(Terms1, term(E2,C2), Terms).
133+
134+
% mul_through(+Sum, +Expr, -Sum)
135+
mul_through([], _, []).
136+
mul_through([term(E,Term)|Terms], Poly, Res) :-
137+
poly_mul(Term, Poly, NewTerm),
138+
mul_through(Terms, Poly, NewTerms),
139+
make_term(NewTerm, E, NewTerms, Res).
140+
141+
% poly_expr(+Integer, +Expr, -Expr)
142+
poly_exp(0, _, 1) :- !.
143+
poly_exp(N, Poly, Result) :-
144+
N rem 2 =:= 0, !,
145+
M is N//2,
146+
poly_exp(M, Poly, Part),
147+
poly_mul(Part, Part, Result).
148+
poly_exp(N, Poly, Result) :-
149+
M is N-1,
150+
poly_exp(M, Poly, Part),
151+
poly_mul(Poly, Part, Result).
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Prolog code for the tic-tac-toe game.
3+
*
4+
* Min-max search via negation.
5+
*
6+
* Warranty & Liability
7+
* To the extent permitted by applicable law and unless explicitly
8+
* otherwise agreed upon, XLOG Technologies GmbH makes no warranties
9+
* regarding the provided information. XLOG Technologies GmbH assumes
10+
* no liability that any problems might be solved with the information
11+
* provided by XLOG Technologies GmbH.
12+
*
13+
* Rights & License
14+
* All industrial property rights regarding the information - copyright
15+
* and patent rights in particular - are the sole property of XLOG
16+
* Technologies GmbH. If the company was not the originator of some
17+
* excerpts, XLOG Technologies GmbH has at least obtained the right to
18+
* reproduce, change and translate the information.
19+
*
20+
* Reproduction is restricted to the whole unaltered document. Reproduction
21+
* of the information is only allowed for non-commercial uses. Selling,
22+
* giving away or letting of the execution of the library is prohibited.
23+
* The library can be distributed as part of your applications and libraries
24+
* for execution provided this comment remains unchanged.
25+
*
26+
* Trademarks
27+
* Jekejeke is a registered trademark of XLOG Technologies GmbH.
28+
*/
29+
30+
% move(+Board, +Player, -Board)
31+
move([[-,B,C],[D,E,F],[G,H,I]], P, [[P,B,C],[D,E,F],[G,H,I]]).
32+
move([[A,-,C],[D,E,F],[G,H,I]], P, [[A,P,C],[D,E,F],[G,H,I]]).
33+
move([[A,B,-],[D,E,F],[G,H,I]], P, [[A,B,P],[D,E,F],[G,H,I]]).
34+
move([[A,B,C],[-,E,F],[G,H,I]], P, [[A,B,C],[P,E,F],[G,H,I]]).
35+
move([[A,B,C],[D,-,F],[G,H,I]], P, [[A,B,C],[D,P,F],[G,H,I]]).
36+
move([[A,B,C],[D,E,-],[G,H,I]], P, [[A,B,C],[D,E,P],[G,H,I]]).
37+
move([[A,B,C],[D,E,F],[-,H,I]], P, [[A,B,C],[D,E,F],[P,H,I]]).
38+
move([[A,B,C],[D,E,F],[G,-,I]], P, [[A,B,C],[D,E,F],[G,P,I]]).
39+
move([[A,B,C],[D,E,F],[G,H,-]], P, [[A,B,C],[D,E,F],[G,H,P]]).
40+
41+
% init(+Board)
42+
init([[-,-,-],[-,-,-],[-,-,-]]).
43+
44+
% win(+Board, +Player)
45+
win([[P,P,P],[_,_,_],[_,_,_]], P).
46+
win([[_,_,_],[P,P,P],[_,_,_]], P).
47+
win([[_,_,_],[_,_,_],[P,P,P]], P).
48+
win([[P,_,_],[P,_,_],[P,_,_]], P).
49+
win([[_,P,_],[_,P,_],[_,P,_]], P).
50+
win([[_,_,P],[_,_,P],[_,_,P]], P).
51+
win([[P,_,_],[_,P,_],[_,_,P]], P).
52+
win([[_,_,P],[_,P,_],[P,_,_]], P).
53+
54+
% other(+Player, -Player).
55+
other(o, x).
56+
other(x, o).
57+
58+
% tie(+Board, +Player)
59+
tie(X, P) :-
60+
\+ move(X, P, _).
61+
62+
% best(+Board, +Player, -Board)
63+
best(X, P, Y) :-
64+
move(X, P, Y),
65+
( win(Y, P) -> true
66+
; other(P, Q),
67+
\+ tie(Y, Q),
68+
\+ best(Y, Q, _)).
69+
70+
% tictac
71+
tictac :-
72+
init(X),
73+
best(X, x, _).

0 commit comments

Comments
 (0)