View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jtrolog
4    * %%
5    * Copyright (C) 2021 Prolobjectlink Project
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation, either version 2.1 of the
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package io.github.prolobjectlink.prolog.jtrolog;
23  
24  import static org.junit.Assert.assertArrayEquals;
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertFalse;
27  import static org.junit.Assert.assertTrue;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import org.junit.After;
33  import org.junit.Before;
34  import org.junit.Test;
35  
36  import io.github.prolobjectlink.prolog.PrologAtom;
37  import io.github.prolobjectlink.prolog.PrologDouble;
38  import io.github.prolobjectlink.prolog.PrologFloat;
39  import io.github.prolobjectlink.prolog.PrologInteger;
40  import io.github.prolobjectlink.prolog.PrologList;
41  import io.github.prolobjectlink.prolog.PrologLong;
42  import io.github.prolobjectlink.prolog.PrologStructure;
43  import io.github.prolobjectlink.prolog.PrologTerm;
44  import io.github.prolobjectlink.prolog.PrologTermType;
45  import io.github.prolobjectlink.prolog.PrologVariable;
46  
47  public class PrologReferenceTest extends PrologBaseTest {
48  
49  	private final List<Object> l = new ArrayList<Object>();
50  	private final PrologTerm ref = provider.newReference(l);
51  
52  	@Before
53  	public void setUp() throws Exception {
54  	}
55  
56  	@After
57  	public void tearDown() throws Exception {
58  	}
59  
60  	@Test
61  	public void testGetArgument() {
62  		assertTrue(ref.getArgument(0).isAtom());
63  		assertEquals(provider.newAtom("[]"), ref.getArgument(0));
64  	}
65  
66  	@Test
67  	public void testGetArguments() {
68  		assertArrayEquals(new PrologTerm[] { provider.newAtom("[]") }, ref.getArguments());
69  	}
70  
71  	@Test
72  	public void testGetArity() {
73  		assertEquals(1, ref.getArity());
74  	}
75  
76  	@Test
77  	public void testGetFunctor() {
78  		assertEquals("@", ref.getFunctor());
79  	}
80  
81  	@Test
82  	public void testHashCode() {
83  		assertEquals(provider.newReference(l).hashCode(), ref.hashCode());
84  	}
85  
86  	@Test
87  	public void testGetTerm() {
88  		assertEquals(provider.newStructure("@", provider.newAtom("[]")), ref.getTerm());
89  	}
90  
91  	@Test
92  	public void testIsAtom() {
93  		assertFalse(ref.isAtom());
94  	}
95  
96  	@Test
97  	public void testIsNumber() {
98  		assertFalse(ref.isNumber());
99  	}
100 
101 	@Test
102 	public void testIsFloat() {
103 		assertFalse(ref.isFloat());
104 	}
105 
106 	@Test
107 	public void testIsDouble() {
108 		assertFalse(ref.isDouble());
109 	}
110 
111 	@Test
112 	public void testIsInteger() {
113 		assertFalse(ref.isInteger());
114 	}
115 
116 	@Test
117 	public void testIsLong() {
118 		assertFalse(ref.isLong());
119 	}
120 
121 	@Test
122 	public void testIsVariable() {
123 		assertFalse(ref.isVariable());
124 	}
125 
126 	@Test
127 	public void testIsList() {
128 		assertFalse(ref.isList());
129 	}
130 
131 	@Test
132 	public void testIsStructure() {
133 		assertTrue(ref.isStructure());
134 	}
135 
136 	@Test
137 	public void testIsNil() {
138 		assertFalse(ref.isNil());
139 	}
140 
141 	@Test
142 	public void testIsEmptyList() {
143 		assertFalse(ref.isEmptyList());
144 	}
145 
146 	@Test
147 	public void testIsEvaluable() {
148 		assertFalse(ref.isEvaluable());
149 	}
150 
151 	@Test
152 	public void testIsAtomic() {
153 		assertFalse(ref.isAtomic());
154 	}
155 
156 	@Test
157 	public void testIsCompound() {
158 		assertTrue(ref.isCompound());
159 	}
160 
161 	@Test
162 	public void testIsTrueType() {
163 		assertFalse(ref.isTrueType());
164 		assertTrue(provider.newReference(true).isTrueType());
165 		assertTrue(provider.trueReference().isTrueType());
166 	}
167 
168 	@Test
169 	public void testIsFalseType() {
170 		assertFalse(ref.isFalseType());
171 		assertTrue(provider.newReference(false).isFalseType());
172 		assertTrue(provider.falseReference().isFalseType());
173 	}
174 
175 	@Test
176 	public void testIsNullType() {
177 		assertFalse(ref.isNullType());
178 		assertTrue(provider.newReference(null).isNullType());
179 		assertTrue(provider.nullReference().isNullType());
180 	}
181 
182 	@Test
183 	public void testIsVoidType() {
184 		assertFalse(ref.isVoidType());
185 		assertTrue(provider.newReference(void.class).isVoidType());
186 		assertTrue(provider.voidReference().isVoidType());
187 	}
188 
189 	@Test
190 	public void testIsObjectType() {
191 		assertTrue(ref.isObjectType());
192 	}
193 
194 	@Test
195 	public void testIsReference() {
196 		assertTrue(ref.isReference());
197 	}
198 
199 	@Test
200 	public void testGetObject() {
201 		assertEquals(l, ref.getObject());
202 	}
203 
204 	@Test
205 	public void testEqualsObject() {
206 		assertEquals(provider.newReference(l), ref);
207 	}
208 
209 	@Test
210 	public void testToString() {
211 		assertEquals("'@'([])", ref.toString());
212 	}
213 
214 	@Test
215 	public void testGetIndicator() {
216 		assertEquals("@/1", ref.getIndicator());
217 	}
218 
219 	@Test
220 	public void testHasIndicator() {
221 		assertTrue(ref.hasIndicator("@", 1));
222 	}
223 
224 	@Test
225 	public void testGetType() {
226 		assertEquals(PrologTermType.OBJECT_TYPE, ref.getType());
227 	}
228 
229 	@Test
230 	public void testGetProvider() {
231 		assertEquals(provider, ref.getProvider());
232 	}
233 
234 	@Test
235 	public void testIsEntry() {
236 		assertFalse(ref.isEntry());
237 	}
238 
239 	@Test
240 	public void testIsMap() {
241 		assertFalse(ref.isMap());
242 	}
243 
244 	@Test
245 	public void testUnify() {
246 
247 		// with atom
248 		PrologAtom atom = provider.newAtom("John Doe");
249 		assertFalse(ref.unify(atom));
250 
251 		// with integer
252 		PrologInteger iValue = provider.newInteger(28);
253 		assertFalse(ref.unify(iValue));
254 
255 		// with long
256 		PrologLong lValue = provider.newLong(28);
257 		assertFalse(ref.unify(lValue));
258 
259 		// with float
260 		PrologFloat fValue = provider.newFloat(36.47);
261 		assertFalse(ref.unify(fValue));
262 
263 		// with double
264 		PrologDouble dValue = provider.newDouble(36.47);
265 		assertFalse(ref.unify(dValue));
266 
267 		// with variable
268 		PrologVariable variable = provider.newVariable("X", 0);
269 		// true. case predicate and variable
270 		assertTrue(ref.unify(variable));
271 
272 		// with predicate
273 //		PrologStructure structure1 = provider.parseStructure("@(X)");
274 //		PrologStructure structure2 = provider.parseStructure("@(28)");
275 		PrologStructure structure1 = provider.newStructure("@", variable);
276 		PrologStructure structure2 = provider.newStructure("@", lValue);
277 
278 		// true because are equals
279 		assertTrue(ref.unify(ref));
280 		// true because match and their arguments unify
281 //		assertTrue(ref.unify(structure1)); // FIXME some error occurs with @([]) and '@'(X)
282 		// false because match but their arguments not unify
283 		assertFalse(ref.unify(structure2));
284 
285 		// with list
286 		PrologList flattenList = provider.parseList("['Some Literal']");
287 		PrologList headTailList = provider.parseList("['Some Literal'|[]]");
288 		PrologTerm empty = provider.prologEmpty();
289 		assertFalse(ref.unify(flattenList));
290 		assertFalse(ref.unify(headTailList));
291 		assertFalse(ref.unify(empty));
292 
293 		// with expression
294 		PrologTerm expression = provider.parseTerm("58+93*10");
295 		assertFalse(ref.unify(expression));
296 
297 	}
298 
299 	@Test
300 	public void testCompareTo() {
301 
302 		// with atom
303 		PrologAtom atom = provider.newAtom("John Doe");
304 		assertEquals(1, ref.compareTo(atom));
305 
306 		// with integer
307 		PrologInteger iValue = provider.newInteger(28);
308 		assertEquals(1, ref.compareTo(iValue));
309 
310 		// with long
311 		PrologLong lValue = provider.newLong(28);
312 		assertEquals(1, ref.compareTo(lValue));
313 
314 		// with float
315 		PrologFloat fValue = provider.newFloat(36.47);
316 		assertEquals(1, ref.compareTo(fValue));
317 
318 		// with double
319 		PrologDouble dValue = provider.newDouble(36.47);
320 		assertEquals(1, ref.compareTo(dValue));
321 
322 		// with variable
323 		PrologVariable variable = provider.newVariable("X", 0);
324 		assertEquals(1, ref.compareTo(variable));
325 
326 		// with predicate
327 //		PrologStructure structure1 = provider.parseStructure("@(X)");
328 //		PrologStructure structure2 = provider.parseStructure("@(28)");
329 		PrologStructure structure1 = provider.newStructure("@", variable);
330 		PrologStructure structure2 = provider.newStructure("@", lValue);
331 		// true because are equals
332 		assertEquals(0, ref.compareTo(ref));
333 		// true because match and their arguments compareTo
334 		assertEquals(-1, ref.compareTo(structure1));
335 		// false because match but their arguments not compareTo
336 		assertEquals(-1, ref.compareTo(structure2));
337 
338 		// with list
339 		PrologList flattenList = provider.parseList("['Some Literal']");
340 		PrologList headTailList = provider.parseList("['Some Literal'|[]]");
341 		PrologTerm empty = provider.prologEmpty();
342 		assertEquals(-1, ref.compareTo(flattenList));
343 		assertEquals(-1, ref.compareTo(headTailList));
344 		assertEquals(-1, ref.compareTo(empty));
345 
346 		// with expression
347 		PrologTerm expression = provider.parseTerm("58+93*10");
348 		assertEquals(-1, ref.compareTo(expression));
349 
350 	}
351 
352 }