00001
00002 package jpl.test;
00003
00004 import java.util.Map;
00005
00006 import jpl.Atom;
00007 import jpl.Compound;
00008 import jpl.Integer;
00009 import jpl.JPL;
00010 import jpl.PrologException;
00011 import jpl.Query;
00012 import jpl.Term;
00013 import jpl.Util;
00014 import jpl.Variable;
00015 import jpl.fli.Prolog;
00016 import junit.framework.TestCase;
00017 import junit.framework.TestSuite;
00018
00019
00020
00021 public class TestJUnit extends TestCase {
00022 public static long fac(long n) {
00023 if (n == 1) {
00024 return 1;
00025 } else if (n > 1) {
00026 return n * ((jpl.Integer) Query.oneSolution("jpl_test_fac(?,F)", new Term[] { new jpl.Integer(n - 1) }).get("F")).longValue();
00027 } else {
00028 return 0;
00029 }
00030 }
00031 public TestJUnit(String name) {
00032 super(name);
00033 }
00034 public static junit.framework.Test suite() {
00035 return new TestSuite(TestJUnit.class);
00036 }
00037 public static void main(String args[]) {
00038 junit.textui.TestRunner.run(suite());
00039 }
00040 protected void setUp() {
00041
00042
00043 Prolog.set_default_init_args(new String[] { "libpl.dll", "-f", "none", "-g", "true", "-q" });
00044 assertTrue((new Query("consult(test_jpl)")).hasSolution());
00045 }
00046 protected void tearDown() {
00047
00048 }
00049
00050 public void testMasstest() {
00051 assertTrue((new Query("assert(diagnose_declaration(_,_,_,[not,a,real,error]))")).hasSolution());
00052 }
00053 public void testSameLibVersions1() {
00054 String java_lib_version = JPL.version_string();
00055 String c_lib_version = jpl.fli.Prolog.get_c_lib_version();
00056 assertTrue("java_lib_version(" + java_lib_version + ") is same as c_lib_version(" + c_lib_version + ")", java_lib_version.equals(c_lib_version));
00057 }
00058 public void testSameLibVersions2() {
00059 String java_lib_version = JPL.version_string();
00060 String pl_lib_version = ((Term) (new Query(new Compound("jpl_pl_lib_version", new Term[] { new Variable("V") })).oneSolution().get("V"))).name();
00061 assertTrue("java_lib_version(" + java_lib_version + ") is same as pl_lib_version(" + pl_lib_version + ")", java_lib_version.equals(pl_lib_version));
00062 }
00063 public void testAtomName1() {
00064 String name = "fred";
00065 Atom a = new Atom(name);
00066 assertEquals("an Atom's name is that with which it was created", a.name(), name);
00067 }
00068 public void testAtomName2() {
00069 String name = "ha ha";
00070 Atom a = new Atom(name);
00071 assertEquals("an Atom's name is that with which it was created", a.name(), name);
00072 }
00073 public void testAtomName3() {
00074 String name = "3";
00075 Atom a = new Atom(name);
00076 assertEquals("an Atom's name is that with which it was created", a.name(), name);
00077 }
00078 public void testAtomToString1() {
00079 String name = "fred";
00080 String toString = "fred";
00081 Atom a = new Atom(name);
00082 assertEquals("an Atom's .toString() value is quoted iff appropriate", a.toString(), toString);
00083 }
00084 public void testAtomToString2() {
00085 String name = "ha ha";
00086 String toString = "'ha ha'";
00087 Atom a = new Atom(name);
00088 assertEquals("an Atom's .toString() value is quoted iff appropriate", a.toString(), toString);
00089 }
00090 public void testAtomToString3() {
00091 String name = "3";
00092 String toString = "'3'";
00093 Atom a = new Atom(name);
00094 assertEquals("an Atom's .toString() value is quoted iff appropriate", a.toString(), toString);
00095 }
00096 public void testAtomArity() {
00097 Atom a = new Atom("willy");
00098 assertEquals("an Atom has arity zero", a.arity(), 0);
00099 }
00100 public void testAtomEquality1() {
00101 String name = "fred";
00102 Atom a1 = new Atom(name);
00103 Atom a2 = new Atom(name);
00104 assertEquals("two Atoms created with the same name are equal", a1, a2);
00105 }
00106 public void testAtomIdentity() {
00107 String name = "fred";
00108 Atom a1 = new Atom(name);
00109 Atom a2 = new Atom(name);
00110 assertNotSame("two Atoms created with the same name are not identical", a1, a2);
00111 }
00112 public void testAtomHasFunctorNameZero() {
00113 String name = "sam";
00114 Atom a = new Atom(name);
00115 assertTrue(a.hasFunctor(name, 0));
00116 }
00117 public void testAtomHasFunctorWrongName() {
00118 assertFalse("an Atom does not have a functor whose name is other than that with which the Atom was created", new Atom("wally").hasFunctor("poo", 0));
00119 }
00120 public void testAtomHasFunctorWrongArity() {
00121 String name = "ted";
00122 assertFalse("an Atom does not have a functor whose arity is other than zero", new Atom(name).hasFunctor(name, 1));
00123 }
00124 public void testVariableBinding1() {
00125 Term lhs = new Compound("p", new Term[] { new Variable("X"), new Variable("Y") });
00126 Term rhs = new Compound("p", new Term[] { new Atom("a"), new Atom("b") });
00127 Term goal = new Compound("=", new Term[] { lhs, rhs });
00128 Map soln = new Query(goal).oneSolution();
00129 assertTrue("two variables with different names can bind to distinct atoms", soln != null && ((Term) soln.get("X")).name().equals("a") && ((Term) soln.get("Y")).name().equals("b"));
00130 }
00131 public void testVariableBinding2() {
00132 Term lhs = new Compound("p", new Term[] { new Variable("X"), new Variable("X") });
00133 Term rhs = new Compound("p", new Term[] { new Atom("a"), new Atom("b") });
00134 Term goal = new Compound("=", new Term[] { lhs, rhs });
00135 assertFalse("two distinct Variables with same name cannot unify with distinct atoms", new Query(goal).hasSolution());
00136 }
00137 public void testVariableBinding3() {
00138 Variable X = new Variable("X");
00139 Term lhs = new Compound("p", new Term[] { X, X });
00140 Term rhs = new Compound("p", new Term[] { new Atom("a"), new Atom("b") });
00141 Term goal = new Compound("=", new Term[] { lhs, rhs });
00142 assertFalse("two occurrences of same named Variable cannot unify with distinct atoms", new Query(goal).hasSolution());
00143 }
00144 public void testVariableBinding4() {
00145 Term lhs = new Compound("p", new Term[] { new Variable("_"), new Variable("_") });
00146 Term rhs = new Compound("p", new Term[] { new Atom("a"), new Atom("b") });
00147 Term goal = new Compound("=", new Term[] { lhs, rhs });
00148 assertTrue("two distinct anonymous Variables can unify with distinct atoms", new Query(goal).hasSolution());
00149 }
00150 public void testVariableBinding5() {
00151 Variable Anon = new Variable("_");
00152 Term lhs = new Compound("p", new Term[] { Anon, Anon });
00153 Term rhs = new Compound("p", new Term[] { new Atom("a"), new Atom("b") });
00154 Term goal = new Compound("=", new Term[] { lhs, rhs });
00155 assertTrue("two occurrences of same anonymous Variable can unify with distinct atoms", new Query(goal).hasSolution());
00156 }
00157 public void testAtomEquality2() {
00158 Atom a = new Atom("a");
00159 assertTrue("two occurrences of same Atom are equal by .equals()", a.equals(a));
00160 }
00161 public void testAtomEquality3() {
00162 assertTrue("two distinct Atoms with same names are equal by .equals()", (new Atom("a")).equals(new Atom("a")));
00163 }
00164 public void testTextToTerm1() {
00165 String text = "fred(B,p(A),[A,B,C])";
00166 Term t = Util.textToTerm(text);
00167 assertTrue("Util.textToTerm() converts \"fred(B,p(A),[A,B,C])\" to a corresponding Term", t.hasFunctor("fred", 3) && t.arg(1).isVariable() && t.arg(1).name().equals("B")
00168 && t.arg(2).hasFunctor("p", 1) && t.arg(2).arg(1).isVariable() && t.arg(2).arg(1).name().equals("A"));
00169 }
00170 public void testArrayToList1() {
00171 Term l2 = Util.termArrayToList(new Term[] { new Atom("a"), new Atom("b"), new Atom("c"), new Atom("d"), new Atom("e") });
00172 Query q9 = new Query(new Compound("append", new Term[] { new Variable("Xs"), new Variable("Ys"), l2 }));
00173 assertTrue("append(Xs,Ys,[a,b,c,d,e]) has 6 solutions", q9.allSolutions().length == 6);
00174 }
00175 public void testArrayToList2() {
00176 String goal = "append(Xs,Ys,[a,b,c,d,e])";
00177 assertTrue(goal + " has 6 solutions", Query.allSolutions(goal).length == 6);
00178 }
00179 public void testLength1() {
00180 Query q5 = new Query(new Compound("length", new Term[] { new Variable("Zs"), new jpl.Integer(2) }));
00181 Term zs = (Term) (q5.oneSolution().get("Zs"));
00182 assertTrue("length(Zs,2) binds Zs to a list of two distinct variables " + zs.toString(), zs.hasFunctor(".", 2) && zs.arg(1).isVariable() && zs.arg(2).hasFunctor(".", 2)
00183 && zs.arg(2).arg(1).isVariable() && zs.arg(2).arg(2).hasFunctor("[]", 0) && !zs.arg(1).name().equals(zs.arg(2).arg(1).name()));
00184 }
00185 public void testGenerate1() {
00186 String goal = "append(Xs,Ys,[_,_,_,_,_])";
00187 assertTrue(goal + " has 6 solutions", Query.allSolutions(goal).length == 6);
00188 }
00189 public void testPrologException1() {
00190 try {
00191 new Query("p(]");
00192 } catch (PrologException e) {
00193 assertTrue("new Query(\"p(]\") throws a PrologException " + e.toString(), true);
00194 return;
00195 }
00196 fail("new Query(\"p(]\") oughta throw a PrologException");
00197 }
00198 public void testAtom1() {
00199 assertTrue("new Atom(\"3 3\")" + (new Atom("3 3")).toString(), true);
00200 }
00201 public void testTextToTerm2() {
00202 String text1 = "fred(?,2,?)";
00203 String text2 = "[first(x,y),A]";
00204 Term plist = Util.textToTerm(text2);
00205 Term[] ps = plist.toTermArray();
00206 Term t = Util.textToTerm(text1).putParams(ps);
00207 assertTrue("fred(?,2,?) .putParams( [first(x,y),A] )", t.hasFunctor("fred", 3) && t.arg(1).hasFunctor("first", 2) && t.arg(1).arg(1).hasFunctor("x", 0) && t.arg(1).arg(2).hasFunctor("y", 0)
00208 && t.arg(2).hasFunctor(2, 0) && t.arg(3).isVariable() && t.arg(3).name().equals("A"));
00209 }
00210 public void testDontTellMeMode1() {
00211 final Query q = new Query("setof(_M,current_module(_M),_Ms),length(_Ms,N)");
00212 JPL.setDTMMode(true);
00213 assertTrue("in dont-tell-me mode, setof(_M,current_module(_M),_Ms),length(_Ms,N) returns binding for just one variable", q.oneSolution().keySet().size() == 1);
00214 }
00215 public void testDontTellMeMode2() {
00216 final Query q = new Query("setof(_M,current_module(_M),_Ms),length(_Ms,N)");
00217 JPL.setDTMMode(false);
00218 assertTrue("not in dont-tell-me mode, setof(_M,current_module(_M),_Ms),length(_Ms,N) returns binding for three variables", q.oneSolution().keySet().size() == 3);
00219 }
00220 public void testModulePrefix1() {
00221 assertTrue(Query.hasSolution("call(user:true)"));
00222 }
00223 private void testMutualRecursion(int n, long f) {
00224 try {
00225 assertEquals("mutual recursive Java<->Prolog factorial: fac(" + n + ") = " + f, fac(n), f);
00226 } catch (Exception e) {
00227 fail("fac(" + n + ") threw " + e);
00228 }
00229 }
00230 public void testMutualRecursion1() {
00231 testMutualRecursion(1, 1);
00232 }
00233 public void testMutualRecursion2() {
00234 testMutualRecursion(2, 2);
00235 }
00236 public void testMutualRecursion3() {
00237 testMutualRecursion(3, 6);
00238 }
00239 public void testMutualRecursion10() {
00240 testMutualRecursion(10, 3628800);
00241 }
00242 public void testIsJNull1() {
00243 Term t = (Term) (new Query("X = @(null)")).oneSolution().get("X");
00244 assertTrue("@(null) . isJNull() succeeds", t.isJNull());
00245 }
00246 public void testIsJNull2() {
00247 Term t = (Term) (new Query("X = @(3)")).oneSolution().get("X");
00248 assertFalse("@(3) . isJNull() fails", t.isJNull());
00249 }
00250 public void testIsJNull3() {
00251 Term t = (Term) (new Query("X = _")).oneSolution().get("X");
00252 assertFalse("_ . isJNull() fails", t.isJNull());
00253 }
00254 public void testIsJNull4() {
00255 Term t = (Term) (new Query("X = @(true)")).oneSolution().get("X");
00256 assertFalse("@(true) . isJNull() fails", t.isJNull());
00257 }
00258 public void testIsJNull5() {
00259 Term t = (Term) (new Query("X = @(false)")).oneSolution().get("X");
00260 assertFalse("@(false) . isJNull() fails", t.isJNull());
00261 }
00262 public void testIsJTrue1() {
00263 Term t = (Term) (new Query("X = @(true)")).oneSolution().get("X");
00264 assertTrue("@(true) . isJTrue() succeeds", t.isJTrue());
00265 }
00266 public void testIsJTrue2() {
00267 Term t = (Term) (new Query("X = @(3)")).oneSolution().get("X");
00268 assertFalse("@(3) . isJTrue() fails", t.isJTrue());
00269 }
00270 public void testIsJTrue3() {
00271 Term t = (Term) (new Query("X = _")).oneSolution().get("X");
00272 assertFalse("_ . isJTrue() fails", t.isJTrue());
00273 }
00274 public void testIsJTrue4() {
00275 Term t = (Term) (new Query("X = @(false)")).oneSolution().get("X");
00276 assertFalse("@(false) . isJTrue() fails", t.isJTrue());
00277 }
00278 public void testIsJVoid1() {
00279 Term t = (Term) (new Query("X = @(void)")).oneSolution().get("X");
00280 assertTrue("@(void) . isJVoid() succeeds", t.isJVoid());
00281 }
00282 public void testIsJVoid2() {
00283 Term t = (Term) (new Query("X = @(3)")).oneSolution().get("X");
00284 assertFalse("@(3) . isJVoid() fails", t.isJVoid());
00285 }
00286 public void testIsJVoid3() {
00287 Term t = (Term) (new Query("X = _")).oneSolution().get("X");
00288 assertFalse("_ . isJVoid() fails", t.isJVoid());
00289 }
00290 public void testTypeName1() {
00291 assertEquals("Y = foo binds Y to an Atom", ((Term) Query.oneSolution("Y = foo").get("Y")).typeName(), "Atom");
00292 }
00293 public void testTypeName2() {
00294 assertEquals("Y = 3.14159 binds Y to a Float", ((Term) Query.oneSolution("Y = 3.14159").get("Y")).typeName(), "Float");
00295 }
00296 public void testTypeName4() {
00297 assertEquals("Y = 6 binds Y to an Integer", ((Term) Query.oneSolution("Y = 6").get("Y")).typeName(), "Integer");
00298 }
00299 public void testTypeName5() {
00300 assertEquals("Y = _ binds Y to a Variable", ((Term) Query.oneSolution("Y = _").get("Y")).typeName(), "Variable");
00301 }
00302 public void testTypeName3() {
00303 assertEquals("Y = f(x) binds Y to a Compound", ((Term) Query.oneSolution("Y = f(x)").get("Y")).typeName(), "Compound");
00304 }
00305 public void testGoalWithModulePrefix1() {
00306 String goal = "jpl:jpl_modifier_bit(volatile,I)";
00307 assertTrue(goal + " binds I to an integer", ((Term) Query.oneSolution(goal).get("I")).isInteger());
00308 }
00309 public void testGoalWithModulePrefix2() {
00310 String goal = "user:length([],0)";
00311 assertTrue(goal + " succeeds", Query.hasSolution(goal));
00312 }
00313 public void testGoalWithModulePrefix3() {
00314 try {
00315 (new Query("3:length([],0)")).hasSolution();
00316
00317 fail("(new Query(\"3:length([],0)\")).hasSolution() didn't throw exception");
00318 } catch (jpl.PrologException e) {
00319
00320 if (e.term().hasFunctor("error", 2) && e.term().arg(1).hasFunctor("type_error", 2) && e.term().arg(1).arg(1).hasFunctor("atom", 0)) {
00321
00322 } else {
00323 fail("(new Query(\"3:length([],0)\")).hasSolution() threw incorrect PrologException: " + e);
00324 }
00325 } catch (Exception e) {
00326 fail("(new Query(\"3:length([],0)\")).hasSolution() threw wrong class of exception: " + e);
00327 }
00328 }
00329 public void testGoalWithModulePrefix4() {
00330 try {
00331 (new Query("_:length([],0)")).hasSolution();
00332
00333 fail("bad (unbound) module prefix");
00334 } catch (jpl.PrologException e) {
00335
00336 if (e.term().hasFunctor("error", 2) && e.term().arg(1).hasFunctor("instantiation_error", 0)) {
00337
00338 } else {
00339 fail("(new Query(\"_:length([],0)\")).hasSolution() threw incorrect PrologException: " + e);
00340 }
00341 } catch (Exception e) {
00342 fail("(new Query(\"_:length([],0)\")).hasSolution() threw wrong class of exception: " + e);
00343 }
00344 }
00345 public void testGoalWithModulePrefix5() {
00346 try {
00347 (new Query("f(x):length([],0)")).hasSolution();
00348
00349 fail("bad (compound) module prefix");
00350 } catch (jpl.PrologException e) {
00351
00352 if (e.term().hasFunctor("error", 2) && e.term().arg(1).hasFunctor("type_error", 2) && e.term().arg(1).arg(1).hasFunctor("atom", 0)) {
00353
00354 } else {
00355 fail("(new Query(\"f(x):length([],0)\")).hasSolution() threw incorrect PrologException: " + e);
00356 }
00357 } catch (Exception e) {
00358 fail("(new Query(\"f(x):length([],0)\")).hasSolution() threw wrong class of exception: " + e);
00359 }
00360 }
00361 public void testGoalWithModulePrefix6() {
00362 try {
00363 (new Query("no_such_module:no_such_predicate(0)")).hasSolution();
00364
00365 fail("bad (nonexistent) module prefix");
00366 } catch (jpl.PrologException e) {
00367
00368 if (e.term().hasFunctor("error", 2) && e.term().arg(1).hasFunctor("existence_error", 2) && e.term().arg(1).arg(1).hasFunctor("procedure", 0)) {
00369
00370 } else {
00371 fail("(new Query(\"f(x):length([],0)\")).hasSolution() threw incorrect PrologException: " + e);
00372 }
00373 } catch (Exception e) {
00374 fail("(new Query(\"f(x):length([],0)\")).hasSolution() threw wrong class of exception: " + e);
00375 }
00376 }
00377
00378
00379
00380 public void testFetchLongList0() {
00381 assertTrue((new Query("findall(foo(N),between(0,10,N),L)")).hasSolution());
00382 }
00383 public void testFetchLongList1() {
00384 assertTrue((new Query("findall(foo(N),between(0,100,N),L)")).hasSolution());
00385 }
00386 public void testFetchLongList2() {
00387 assertTrue((new Query("findall(foo(N),between(0,1000,N),L)")).hasSolution());
00388 }
00389 public void testFetchLongList2c() {
00390 assertTrue((new Query("findall(foo(N),between(0,1023,N),L)")).hasSolution());
00391 }
00392 public void testFetchLongList2a() {
00393 assertTrue((new Query("findall(foo(N),between(0,2000,N),L)")).hasSolution());
00394 }
00395
00396
00397
00398
00399
00400
00401 public void testUnicode0() {
00402 assertTrue(Query.hasSolution("atom_codes(?,[32])", new Term[] { new Atom(" ") }));
00403 }
00404 public void testUnicode0a() {
00405 assertTrue(Query.hasSolution("atom_codes(?,[32])", new Term[] { new Atom("\u0020") }));
00406 }
00407 public void testUnicode0b() {
00408 assertTrue(Query.hasSolution("atom_codes(?,[0])", new Term[] { new Atom("\u0000") }));
00409 }
00410 public void testUnicode0c() {
00411 assertTrue(Query.hasSolution("atom_codes(?,[1])", new Term[] { new Atom("\u0001") }));
00412 }
00413 public void testUnicode0d() {
00414 assertTrue(Query.hasSolution("atom_codes(?,[127])", new Term[] { new Atom("\u007F") }));
00415 }
00416 public void testUnicode0e() {
00417 assertTrue(Query.hasSolution("atom_codes(?,[128])", new Term[] { new Atom("\u0080") }));
00418 }
00419 public void testUnicode0f() {
00420 assertTrue(Query.hasSolution("atom_codes(?,[255])", new Term[] { new Atom("\u00FF") }));
00421 }
00422 public void testUnicode0g() {
00423 assertTrue(Query.hasSolution("atom_codes(?,[256])", new Term[] { new Atom("\u0100") }));
00424 }
00425 public void testUnicode1() {
00426 assertTrue(Query.hasSolution("atom_codes(?,[0,127,128,255])", new Term[] { new Atom("\u0000\u007F\u0080\u00FF") }));
00427 }
00428 public void testUnicode2() {
00429 assertTrue(Query.hasSolution("atom_codes(?,[256,32767,32768,65535])", new Term[] { new Atom("\u0100\u7FFF\u8000\uFFFF") }));
00430 }
00431 public void testStringXput1() {
00432 Term a = (Term) (Query.oneSolution("string_concat(foo,bar,S)").get("S"));
00433 assertTrue(a.name().equals("foobar"));
00434 }
00435 public void testStringXput2() {
00436 String s1 = "\u0000\u007F\u0080\u00FF";
00437 String s2 = "\u0100\u7FFF\u8000\uFFFF";
00438 String s = s1 + s2;
00439 Term a1 = new Atom(s1);
00440 Term a2 = new Atom(s2);
00441 Term a = (Term) (Query.oneSolution("string_concat(?,?,S)", new Term[] { a1, a2 }).get("S"));
00442 assertEquals(a.name(), s);
00443 }
00444
00445
00446
00447
00448
00449
00450 public void testStaticQueryInvalidSourceText2() {
00451 String goal = "p(]";
00452 try {
00453 Query.hasSolution(goal);
00454 } catch (jpl.PrologException e) {
00455 if (e.term().hasFunctor("error", 2) && e.term().arg(1).hasFunctor("syntax_error", 1) && e.term().arg(1).arg(1).hasFunctor("cannot_start_term", 0)) {
00456
00457 } else {
00458 fail("Query.hasSolution(" + goal + ") threw incorrect PrologException: " + e);
00459 }
00460 } catch (Exception e) {
00461 fail("Query.hasSolution(" + goal + ") threw wrong class of exception: " + e);
00462 }
00463 }
00464 public void testStaticQueryInvalidSourceText1() {
00465 String goal = "bad goal";
00466 try {
00467 Query.hasSolution(goal);
00468 } catch (jpl.PrologException e) {
00469 if (e.term().hasFunctor("error", 2) && e.term().arg(1).hasFunctor("syntax_error", 1) && e.term().arg(1).arg(1).hasFunctor("operator_expected", 0)) {
00470
00471 } else {
00472 fail("Query.hasSolution(" + goal + ") threw incorrect PrologException: " + e);
00473 }
00474 } catch (Exception e) {
00475 fail("Query.hasSolution(" + goal + ") threw wrong class of exception: " + e);
00476 }
00477 }
00478 public void testStaticQueryNSolutions1() {
00479 String goal = "member(X, [0,1,2,3,4,5,6,7,8,9])";
00480 int n = 5;
00481 assertTrue("Query.nSolutions(" + goal + ", " + n + ") returns " + n + " solutions", Query.nSolutions(goal, n).length == n);
00482 }
00483 public void testStaticQueryNSolutions2() {
00484 String goal = "member(X, [0,1,2,3,4,5,6,7,8,9])";
00485 int n = 0;
00486 assertTrue("Query.nSolutions(" + goal + ", " + n + ") returns " + n + " solutions", Query.nSolutions(goal, n).length == n);
00487 }
00488 public void testStaticQueryNSolutions3() {
00489 String goal = "member(X, [0,1,2,3,4,5,6,7,8,9])";
00490 int n = 20;
00491 assertTrue("Query.nSolutions(" + goal + ", " + n + ") returns 10 solutions", Query.nSolutions(goal, n).length == 10);
00492 }
00493 public void testStaticQueryAllSolutions1() {
00494 String goal = "member(X, [0,1,2,3,4,5,6,7,8,9])";
00495 assertTrue("Query.allSolutions(" + goal + ") returns 10 solutions", Query.allSolutions(goal).length == 10);
00496 }
00497 public void testStaticQueryHasSolution1() {
00498 String goal = "memberchk(13, [?,?,?])";
00499 Term[] params = new Term[] { new Integer(12), new Integer(13), new Integer(14) };
00500 assertTrue(Query.hasSolution(goal, params));
00501 }
00502 public void testStaticQueryHasSolution2() {
00503 String goal = "memberchk(23, [?,?,?])";
00504 Term[] params = new Term[] { new Integer(12), new Integer(13), new Integer(14) };
00505 assertFalse(Query.hasSolution(goal, params));
00506 }
00507 public void testUtilListToTermArray1() {
00508 String goal = "T = [a,b,c]";
00509 Term list = (Term) Query.oneSolution(goal).get("T");
00510 Term[] array = Util.listToTermArray(list);
00511 assertTrue(array[2].isAtom() && array[2].name().equals("c"));
00512 }
00513 public void testTermToTermArray1() {
00514 String goal = "T = [a,b,c]";
00515 Term list = (Term) Query.oneSolution(goal).get("T");
00516 Term[] array = list.toTermArray();
00517 assertTrue(array[2].isAtom() && array[2].name().equals("c"));
00518 }
00519 public void testJRef1() {
00520
00521
00522 int i = 76543;
00523 Integer I = new Integer(i);
00524 Query q = new Query("jpl_call(?,intValue,[],I2)", new Term[] { Term.objectToJRef(I) });
00525 Term I2 = (Term) q.oneSolution().get("I2");
00526 assertTrue(I2.isInteger() && I2.intValue() == i);
00527 }
00528 public void testBerhhard1() {
00529 assertTrue(Query.allSolutions( "consult(library('lists'))" ).length == 1);
00530 }
00531 public void testJRef2() {
00532 int i = 76543;
00533 Integer I = new Integer(i);
00534 Query q = new Query("jpl_call(?,intValue,[],I2)", jpl.JPL.newJRef(I));
00535 Term I2 = (Term) q.oneSolution().get("I2");
00536 assertTrue(I2.isInteger() && I2.intValue() == i);
00537 }
00538 public void testJRef3() {
00539 StringBuffer sb = new StringBuffer();
00540 Query.oneSolution("jpl_call(?,append,['xyz'],_)", new Term[] {JPL.newJRef(sb)});
00541 assertTrue(sb.toString().equals("xyz"));
00542 }
00543 public void testJRef4() {
00544 Term jrefSB = (Term) Query.oneSolution("jpl_new('java.lang.StringBuffer',['abc'],SB)").get("SB");
00545 assertTrue(jrefSB.isJRef() && ((StringBuffer) jrefSB.jrefToObject()).toString().equals("abc"));
00546 }
00547 public void testJRef5() {
00548 String token = "foobar345";
00549 Term a = (Term) (Query.oneSolution("jpl_new('java.lang.StringBuffer',[?],A)", new Term[] {new Atom(token)}).get("A"));
00550 assertTrue(((java.lang.StringBuffer) (a.jrefToObject())).toString().equals(token));
00551 }
00552 public void testRef6() {
00553 Term nullJRef = new Compound("@", new Term[] {new Atom("null")});
00554 Object nullObject = nullJRef.jrefToObject();
00555 assertNull("@(null) .jrefToObject() yields null", nullObject);
00556 }
00557 public void testRef7() {
00558 Term badJRef = new Compound("@", new Term[] {new Atom("foobar")});
00559 try {
00560 badJRef.jrefToObject();
00561
00562 fail("@(foobar) .jrefToObject() shoulda thrown JPLException");
00563 } catch (jpl.JPLException e) {
00564
00565 if (e.getMessage().endsWith("term is not a JRef")) {
00566
00567 } else {
00568 fail("@(foobar) .jrefToObject() threw incorrect JPLException: " + e);
00569 }
00570 } catch (Exception e) {
00571 fail("@(foobar) .jrefToObject() threw wrong class of exception: " + e);
00572 }
00573 }
00574 public void testForeignFrame1() {
00575 int ls1 = ((Term) (Query.oneSolution("statistics(localused,LS)").get("LS"))).intValue();
00576 int ls2 = ((Term) (Query.oneSolution("statistics(localused,LS)").get("LS"))).intValue();
00577 assertTrue("local stack size unchanged after query", ls1 == ls2);
00578 }
00579 public void testOpenGetClose1() {
00580 StringBuffer sb = new StringBuffer();
00581 Query q = new Query("atom_chars(prolog, Cs), member(C, Cs)");
00582 Map soln;
00583 q.open();
00584 while ((soln = q.getSolution()) != null) {
00585 sb.append(((Atom) soln.get("C")).name());
00586 }
00587 q.close();
00588 assertEquals("prolog", sb.toString());
00589 }
00590 public void testOpenGetClose2() {
00591 Query q = new Query("dummy");
00592 try {
00593 q.getSolution();
00594 } catch (jpl.JPLException e) {
00595 if (e.getMessage().endsWith("Query is not open")) {
00596
00597 } else {
00598 fail("jpl.Query#getSolution() threw incorrect JPLException: " + e);
00599 }
00600 } catch (Exception e) {
00601 fail("jpl.Query#getSolution() threw wrong class of exception: " + e);
00602 }
00603 }
00604 public void testOpen1() {
00605 Query q = new Query("dummy");
00606 assertTrue("a newly created query is not open", !q.isOpen());
00607 }
00608 public void testOpen2() {
00609 Query q = new Query("fail");
00610 q.open();
00611 assertTrue("a newly opened query which has no solutions is open", q.isOpen());
00612 }
00613 public void testGetSolution1() {
00614 Query q = new Query("fail");
00615 q.open();
00616 q.getSolution();
00617 assertTrue("an opened query on which getSolution has failed once is closed", !q.isOpen());
00618 }
00619 public void testGetSolution2() {
00620 Query q = new Query("fail");
00621 q.open();
00622 q.getSolution();
00623 try {
00624 q.getSolution();
00625
00626 fail("jpl.Query#getSolution() shoulda thrown JPLException");
00627 } catch (jpl.JPLException e) {
00628 if (e.getMessage().endsWith("Query is not open")) {
00629
00630 } else {
00631 fail("jpl.Query#getSolution() threw incorrect JPLException: " + e);
00632 }
00633 } catch (Exception e) {
00634 fail("jpl.Query#getSolution() threw wrong class of exception: " + e);
00635 }
00636 }
00637 public void testHasMoreSolutions1() {
00638 StringBuffer sb = new StringBuffer();
00639 Query q = new Query("atom_chars(prolog, Cs), member(C, Cs)");
00640 Map soln;
00641 q.open();
00642 while (q.hasMoreSolutions()) {
00643 soln = q.nextSolution();
00644 sb.append(((Atom) soln.get("C")).name());
00645 }
00646 q.close();
00647 assertEquals("Query#hasMoreSolutions() + Query#nextSolution() work as intended", "prolog", sb.toString());
00648 }
00649 public void testHasMoreElements1() {
00650 StringBuffer sb = new StringBuffer();
00651 Query q = new Query("atom_chars(prolog, Cs), member(C, Cs)");
00652 Map soln;
00653 q.open();
00654 while (q.hasMoreElements()) {
00655 soln = (Map) q.nextElement();
00656 sb.append(((Atom) soln.get("C")).name());
00657 }
00658 q.close();
00659 assertEquals("Query#hasMoreElements() + Query#nextElement() work as intended", "prolog", sb.toString());
00660 }
00661 public void testStackedQueries1() {
00662 StringBuffer sb = new StringBuffer();
00663 Query q = new Query("atom_chars(prolog, Cs), member(C, Cs)");
00664 Map soln;
00665 q.open();
00666 while ((soln = q.getSolution()) != null) {
00667 Atom a = (Atom) soln.get("C");
00668 if (Query.hasSolution("memberchk(?, [l,o,r])", new Term[] {a})) {
00669 sb.append(((Atom) soln.get("C")).name());
00670 }
00671 }
00672 assertTrue(!q.isOpen());
00673 assertEquals("rolo", sb.toString());
00674 }
00675
00676 }