001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package test.org.mandarax.util;
019:
020: import java.util.*;
021: import junit.framework.TestCase;
022: import org.mandarax.kernel.*;
023: import org.mandarax.util.*;
024:
025: /**
026: * Abstract test case for various auto facts test cases where we test <code>clauses()</code>.
027: * Note that plenty of test cases for the <code>clauses(Clause,Object)</code> method are contained
028: * in the package <code>test.org.mandarax.math</code>!
029: * @see org.mandarax.util.AutoFacts
030: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
031: * @version 3.4 <7 March 05>
032: * @since 1.2
033: */
034: public abstract class AutoFactsTest extends TestCase {
035:
036: /**
037: * Constructor.
038: */
039: public AutoFactsTest() {
040: super ("test");
041: }
042:
043: /**
044: * Get the auto facts.
045: * @return auto facts
046: */
047: protected AutoFacts getAutoFacts() {
048:
049: // build object wrappers for extensions (for convenience, extensions are defined
050: // as primitives in test cases)
051: double[] dd = getDoubleTestSet();
052: final Vector doubles = new Vector();
053:
054: for (int i = 0; i < dd.length; i++) {
055: doubles.add(new Double(dd[i]));
056: }
057:
058: int[] ii = getIntTestSet();
059: final Vector integers = new Vector();
060:
061: for (int i = 0; i < ii.length; i++) {
062: integers.add(new Integer(ii[i]));
063: }
064:
065: // create auto facts
066: AutoFacts af = new AutoFacts() {
067:
068: public Collection getExtension(Class type) {
069: if (type == Double.class) {
070: return doubles;
071: }
072:
073: if (type == Integer.class) {
074: return integers;
075: }
076:
077: return super .getExtension(type);
078: }
079: };
080:
081: // register predicates
082: Predicate[] p = { getPredicate1(), getPredicate2() };
083:
084: af.setPredicates(p);
085:
086: return af;
087: }
088:
089: /**
090: * Get the set of double numbers used for testing.
091: * @return an array of doubles
092: */
093: protected abstract double[] getDoubleTestSet();
094:
095: /**
096: * Get the expected number of generated facts.
097: * @return the number of facts expected
098: */
099: protected abstract int getExpected();
100:
101: /**
102: * Get the set of int numbers used for testing.
103: * @return an array of integers
104: */
105: protected abstract int[] getIntTestSet();
106:
107: /**
108: * Get the first predicate we use for testing.
109: * This predicate means "an integer equals a rounded double".
110: * @return a predicate
111: */
112: private Predicate getPredicate1() {
113: Class[] struct = { Integer.class, Double.class };
114: Predicate p = new SimplePredicate("= (round)", struct) {
115:
116: public Object perform(Term[] t, Session session) {
117: int i;
118: double d;
119:
120: try {
121: i = ((Number) (t[0].resolve(null))).intValue();
122: d = ((Number) (t[1].resolve(null))).doubleValue();
123: } catch (Exception x) {
124: throw new IllegalArgumentException();
125: }
126:
127: return (Math.round(d) == i) ? Boolean.TRUE
128: : Boolean.FALSE;
129: }
130: };
131:
132: return p;
133: }
134:
135: /**
136: * Get the second predicate we use for testing.
137: * This predicate means "an integer equals a truncated double".
138: * @return a predicate
139: */
140: private Predicate getPredicate2() {
141: Class[] struct = { Integer.class, Double.class };
142: Predicate p = new SimplePredicate("= (floor)", struct) {
143:
144: public Object perform(Term[] t, Session session) {
145: int i;
146: double d;
147:
148: try {
149: i = ((Number) (t[0].resolve(null))).intValue();
150: d = ((Number) (t[1].resolve(null))).doubleValue();
151: } catch (Exception x) {
152: throw new IllegalArgumentException();
153: }
154:
155: return (Math.floor(d) == i) ? Boolean.TRUE
156: : Boolean.FALSE;
157: }
158: };
159:
160: return p;
161: }
162:
163: /**
164: * Perform the test.
165: */
166: public void test() {
167: AutoFacts af = getAutoFacts();
168:
169: System.out.println("PERFORMING TEST " + getClass());
170:
171: int counter = 0;
172:
173: for (Iterator it = af.clauses(); it.hasNext();) {
174: counter = counter + 1;
175: System.out.println("Next generated fact is: " + it.next());
176: }
177:
178: System.out.println();
179: assertTrue(getExpected() == counter);
180: }
181: }
|