PageRenderTime 40ms CodeModel.GetById 16ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist091109/test/edu/vub/at/objects/natives/PrimitivesTest.java

http://ambienttalk.googlecode.com/
Java | 361 lines | 216 code | 58 blank | 87 comment | 1 complexity | a7399bfdc46aafd94702d1b6fd924e3a MD5 | raw file
  1package edu.vub.at.objects.natives;
  2
  3import edu.vub.at.AmbientTalkTest;
  4import edu.vub.at.eval.Evaluator;
  5import edu.vub.at.exceptions.InterpreterException;
  6import edu.vub.at.exceptions.XIllegalArgument;
  7import edu.vub.at.exceptions.XTypeMismatch;
  8import edu.vub.at.objects.ATObject;
  9import edu.vub.at.objects.ATTable;
 10import edu.vub.at.objects.ATText;
 11import edu.vub.at.objects.mirrors.NativeClosure;
 12import edu.vub.at.objects.natives.grammar.AGSymbol;
 13
 14/**
 15 * 
 16 * @author tvc
 17 *
 18 * This test case tests all the primitive base-level behaviour of native types.
 19 */
 20public class PrimitivesTest extends AmbientTalkTest {
 21
 22	public static void main(String[] args) {
 23		junit.swingui.TestRunner.run(PrimitivesTest.class);
 24	}
 25	
 26	private NATText TXTambienttalk_ = NATText.atValue("ambienttalk");
 27	private NATText TXTcommas_ = NATText.atValue("one, two, three");
 28	
 29	public void testTextPrimitives() {
 30		try {
 31			// "ambienttalk".explode() => [a, m, b, i, e, n, t, t, a, l, k]
 32			ATTable exploded = TXTambienttalk_.base_explode();
 33			printedEquals(exploded, "[\"a\", \"m\", \"b\", \"i\", \"e\", \"n\", \"t\", \"t\", \"a\", \"l\", \"k\"]");
 34			
 35			// "one, two, three".split(", ") => [ "one", "two", "three" ]
 36			printedEquals(TXTcommas_.base_split(NATText.atValue(", ")), "[\"one\", \"two\", \"three\"]");
 37			
 38			// "ambienttalk".find: "[aeiou]" do: { |vowel| buff << vowel; nil } => buff = "aiea"
 39			final StringBuffer buff = new StringBuffer();
 40			TXTambienttalk_.base_find_do_(NATText.atValue("[aeiou]"), new NativeClosure(null) {
 41				public ATObject base_apply(ATTable arguments) throws InterpreterException {
 42					buff.append(arguments.base_at(NATNumber.ONE).asNativeText().javaValue);
 43					return Evaluator.getNil();
 44				}
 45			});
 46			assertEquals(buff.toString(), "aiea");
 47			
 48			// "ambienttalk".replace: "[aeiou]" by: { |vowel| vowel.toUpperCase() } => AmbIEnttAlk
 49			ATText replaced = TXTambienttalk_.base_replace_by_(NATText.atValue("[aeiou]"), new NativeClosure(null) {
 50				public ATObject base_apply(ATTable arguments) throws InterpreterException {
 51					return arguments.base_at(NATNumber.ONE).asNativeText().base_toUpperCase();
 52				}
 53			});
 54			printedEquals(replaced, "\"AmbIEnttAlk\"");
 55			
 56			// "A".toLowerCase() => "a"
 57			printedEquals(NATText.atValue("A").base_toLowerCase(), "\"a\"");
 58			
 59			// "ambienttalk".length => 11
 60			assertEquals(11, TXTambienttalk_.base_length().asNativeNumber().javaValue);
 61			
 62			// "ambient" + "talk" => "ambienttalk"
 63			assertEquals("ambienttalk", NATText.atValue("ambient").base__oppls_(NATText.atValue("talk")).asNativeText().javaValue);
 64			
 65			// "ambienttalk" <=> "ambienttalk" => 0
 66			assertEquals(NATNumber.ZERO, TXTambienttalk_.base__opltx__opeql__opgtx_(NATText.atValue("ambienttalk")));
 67			
 68			// "a" <=> "b" => -1
 69			assertEquals(NATNumber.MONE, NATText.atValue("a").base__opltx__opeql__opgtx_(NATText.atValue("b")));
 70			
 71			// "b" <=> "a" => 1
 72			assertEquals(NATNumber.ONE, NATText.atValue("b").base__opltx__opeql__opgtx_(NATText.atValue("a")));
 73			
 74			// "ambienttalk" ~= ".*tt.*" => true
 75			assertTrue(TXTambienttalk_.base__optil__opeql_(NATText.atValue(".*tt.*")).asNativeBoolean().javaValue);
 76			
 77			// "ambienttalk" ~= "java" => false
 78			assertFalse(TXTambienttalk_.base__optil__opeql_(NATText.atValue("java")).asNativeBoolean().javaValue);
 79		} catch (InterpreterException e) {
 80			fail(e.getMessage());
 81		}
 82	}
 83	
 84	public void testNumericPrimitives() {
 85		try {
 86			// 5.cos().inc()
 87			assertEquals(NATFraction.atValue(Math.cos(5)+1), NATNumber.atValue(5).base_cos().base_inc());
 88			
 89			// 2.expt(3).round()
 90			assertEquals(8, NATNumber.atValue(2).base_expt(NATNumber.atValue(3)).base_round().asNativeNumber().javaValue);
 91		
 92		    // 1 + 2 => 3
 93			assertEquals(3, NATNumber.ONE.base__oppls_(NATNumber.atValue(2)).asNativeNumber().javaValue);
 94		    // 1.1 + 2.2 => 3.3
 95			assertEquals(3.3, NATFraction.atValue(1.1).base__oppls_(NATFraction.atValue(2.2)).asNativeFraction().javaValue, 0.000001);
 96		    // 1.0 + 2 => 3.0
 97			assertEquals(3.0, NATFraction.atValue(1.0).base__oppls_(NATNumber.atValue(2)).asNativeFraction().javaValue, 0.000001);
 98		    // 1 + 2.0 => 3.0
 99			assertEquals(3.0, NATNumber.ONE.base__oppls_(NATFraction.atValue(2.0)).asNativeFraction().javaValue, 0.000001);
100
101		    // 1 - 2 => -1
102			assertEquals(-1, NATNumber.ONE.base__opmns_(NATNumber.atValue(2)).asNativeNumber().javaValue);
103		    // 1.1 - 2.2 => -1.1
104			assertEquals(-1.1, NATFraction.atValue(1.1).base__opmns_(NATFraction.atValue(2.2)).asNativeFraction().javaValue, 0.000001);
105		    // 1.0 - 2 => -1.0
106			assertEquals(-1.0, NATFraction.atValue(1.0).base__opmns_(NATNumber.atValue(2)).asNativeFraction().javaValue, 0.000001);
107		    // 1 - 2.0 => -1.0
108			assertEquals(-1.0, NATNumber.ONE.base__opmns_(NATFraction.atValue(2.0)).asNativeFraction().javaValue, 0.000001);
109			
110		    // 1 * 2 => 2
111			assertEquals(2, NATNumber.ONE.base__optms_(NATNumber.atValue(2)).asNativeNumber().javaValue);
112		    // 1.1 * 2.2
113			assertEquals(1.1 * 2.2, NATFraction.atValue(1.1).base__optms_(NATFraction.atValue(2.2)).asNativeFraction().javaValue, 0.000001);
114		    // 1.0 * 2 => 2.0
115			assertEquals(2.0, NATFraction.atValue(1.0).base__optms_(NATNumber.atValue(2)).asNativeFraction().javaValue, 0.000001);
116		    // 1 * 2.0 => 2.0
117			assertEquals(2.0, NATNumber.ONE.base__optms_(NATFraction.atValue(2.0)).asNativeFraction().javaValue, 0.000001);
118
119		    // 1 / 2 => 0.5
120			assertEquals(0.5, NATNumber.ONE.base__opdiv_(NATNumber.atValue(2)).asNativeFraction().javaValue, 0.0000001);
121		    // 1.1 / 2.2
122			assertEquals(1.1 / 2.2, NATFraction.atValue(1.1).base__opdiv_(NATFraction.atValue(2.2)).asNativeFraction().javaValue, 0.000001);
123		    // 1.0 / 2 => 0.5
124			assertEquals(0.5, NATFraction.atValue(1.0).base__opdiv_(NATNumber.atValue(2)).asNativeFraction().javaValue, 0.000001);
125		    // 1 / 2.0 => 0.5
126			assertEquals(0.5, NATNumber.ONE.base__opdiv_(NATFraction.atValue(2.0)).asNativeFraction().javaValue, 0.000001);
127			
128			// 1 < 2
129			assertTrue(NATNumber.ONE.base__opltx_(NATNumber.atValue(2)).asNativeBoolean().javaValue);
130			// 2.5 > 2
131			assertTrue(NATFraction.atValue(2.5).base__opgtx_(NATNumber.atValue(2)).asNativeBoolean().javaValue);
132             // 2.5 <= 2.5
133			assertTrue(NATFraction.atValue(2.5).base__opltx__opeql_(NATFraction.atValue(2.5)).asNativeBoolean().javaValue);
134             // 1 >= 1
135			assertTrue(NATNumber.ONE.base__opgtx__opeql_(NATNumber.ONE).asNativeBoolean().javaValue);
136			// 1.0 = 1
137			assertTrue(NATFraction.atValue(1.0).base__opeql_(NATNumber.ONE).asNativeBoolean().javaValue);
138			// 1 = 1.0
139			assertTrue(NATNumber.ONE.base__opeql_(NATFraction.atValue(1.0)).asNativeBoolean().javaValue);
140			// 1.1 != 1.0
141			assertTrue(NATFraction.atValue(1.1).base__opnot__opeql_(NATFraction.atValue(1.0)).asNativeBoolean().javaValue);
142             // ! 1.0 == 1
143			assertFalse(NATFraction.atValue(1.0).base__opeql__opeql_(NATNumber.ONE).asNativeBoolean().javaValue);
144			
145		} catch (InterpreterException e) {
146			fail(e.getMessage());
147		}
148	}
149	
150	public void testNumberPrimitives() {
151		try {
152			// 1.inc() => 2
153			assertEquals(2, NATNumber.ONE.base_inc().asNativeNumber().javaValue);
154			
155			// -1.abs() => 1
156			assertEquals(1, NATNumber.MONE.base_abs().asNativeNumber().javaValue);
157			
158			// 3.doTimes: { |i| buff << i; nil } => buff = 123
159			final StringBuffer buff = new StringBuffer();
160			NATNumber.atValue(3).base_doTimes_(new NativeClosure(null) {
161				public ATObject base_apply(ATTable args) throws InterpreterException {
162					buff.append(getNbr(args, 1));
163					return Evaluator.getNil();
164				}
165			});
166			assertEquals("123", buff.toString());
167			
168			// 3.to: 5 do: { |i| buff2 << i; nil } => buff2 = 34
169			final StringBuffer buff2 = new StringBuffer();
170			NATNumber.atValue(3).base_to_do_(NATNumber.atValue(5), new NativeClosure(null) {
171				public ATObject base_apply(ATTable args) throws InterpreterException {
172					buff2.append(getNbr(args, 1));
173					return Evaluator.getNil();
174				}
175			});
176			assertEquals("34", buff2.toString());
177			
178			// 50.to: 0 step: 10 do: { |i| buff3 << i; nil } => buff3 = 5040302010
179			final StringBuffer buff3 = new StringBuffer();
180			NATNumber.atValue(50).base_to_step_do_(NATNumber.atValue(0), NATNumber.atValue(10), new NativeClosure(null) {
181				public ATObject base_apply(ATTable args) throws InterpreterException {
182					buff3.append(getNbr(args, 1));
183					return Evaluator.getNil();
184				}
185			});
186			assertEquals("5040302010", buff3.toString());
187			
188			// 1 ** 4 => [1, 2, 3]
189			printedEquals(NATNumber.ONE.base__optms__optms_(NATNumber.atValue(4)), "[1, 2, 3]");
190			// 1 *** 4 => [1, 2, 3, 4]
191			printedEquals(NATNumber.ONE.base__optms__optms__optms_(NATNumber.atValue(4)), "[1, 2, 3, 4]");
192			// 4 ** 1 => [4, 3, 2]
193			printedEquals(NATNumber.atValue(4).base__optms__optms_(NATNumber.ONE), "[4, 3, 2]");
194			// 4 *** 1 => [4, 3, 2, 1]
195			printedEquals(NATNumber.atValue(4).base__optms__optms__optms_(NATNumber.ONE), "[4, 3, 2, 1]");
196			// -1 ** -1 => []
197			printedEquals(NATNumber.MONE.base__optms__optms_(NATNumber.MONE), "[]");
198			// -1 *** -1 => [-1]
199			printedEquals(NATNumber.MONE.base__optms__optms__optms_(NATNumber.MONE), "[-1]");
200			
201			// 1 ?? 5 => [1, 5[
202			double rand = NATNumber.ONE.base__opque__opque_(NATNumber.atValue(5)).asNativeFraction().javaValue;
203			assertTrue((1 <= rand) && (rand < 5));
204			
205			// 8 % 3 => 2
206			assertEquals(2, NATNumber.atValue(8).base__oprem_(NATNumber.atValue(3)).asNativeNumber().javaValue);
207			
208			// 9 /- 2 => 4
209			assertEquals(4, NATNumber.atValue(9).base__opdiv__opmns_(NATNumber.atValue(2)).asNativeNumber().javaValue);
210		} catch (InterpreterException e) {
211			fail(e.getMessage());
212		}
213	}
214
215	public void testFractionPrimitives() {
216		try {
217			// 1.4.round() => 1
218			assertEquals(1, NATFraction.atValue(1.4).base_round().asNativeNumber().javaValue);
219			// 1.8.round() => 2
220			assertEquals(2, NATFraction.atValue(1.8).base_round().asNativeNumber().javaValue);
221			// 1.5.round() => 2
222			assertEquals(2, NATFraction.atValue(1.5).base_round().asNativeNumber().javaValue);
223			
224			// 1.8.floor() => 1
225			assertEquals(1, NATFraction.atValue(1.8).base_floor().asNativeNumber().javaValue);
226			// 1.4.ceiling() => 2
227			assertEquals(2, NATFraction.atValue(1.4).base_ceiling().asNativeNumber().javaValue);
228		} catch (InterpreterException e) {
229			fail(e.getMessage());
230		}
231	}
232	
233	public void testClosurePrimitives() {
234		try {
235			// whileTrue
236			ATObject result = evalAndReturn("def i := 0; { i < 5 }.whileTrue: { i := i + 1 }; i");
237			assertEquals(5, result.asNativeNumber().javaValue);
238		} catch (XTypeMismatch e) {
239			fail(e.getMessage());
240		}
241	}
242	
243	public void testTablePrimitives() {
244		try {
245			ATTable vowels = evalAndReturn("[\"a\", \"e\", \"i\", \"o\", \"u\"]").asTable();
246
247			// vowels.length = 5
248			assertEquals(5, vowels.base_length().asNativeNumber().javaValue);
249			
250			// vowels.at(1) = "a"
251			assertEquals("a", vowels.base_at(NATNumber.ONE).asNativeText().javaValue);
252			
253			// vowels.atPut(1, "z")
254			vowels.base_atPut(NATNumber.ONE, NATText.atValue("z"));
255			assertEquals("z", vowels.base_at(NATNumber.ONE).asNativeText().javaValue);
256			
257			// vowels.isEmpty() => false
258			assertFalse(vowels.base_isEmpty().asNativeBoolean().javaValue);
259			
260			// each: ablock
261			evalAndCompareTo("def sum := 0; [1,2,3].each: { |i| sum := sum + i }; sum", "6");
262			
263			// map: ablock
264			evalAndCompareTo("[1,2,3].map: { |i| i + 1 }", "[2, 3, 4]");
265			
266			// with: init collect: ablock
267			evalAndCompareTo("[1,2,3].inject: 0 into: { |total, next| total + next }", "6");
268			
269			// filter: ablock
270			evalAndCompareTo("[1,2,3].filter: {|e| e != 2 }", "[1, 3]");
271
272			// find: ablock
273			evalAndCompareTo("[`a, `b, `c].find: { |e| e == `b }", "2");
274			evalAndCompareTo("[`a, `b, `c].find: { |e| e == `d }", Evaluator.getNil());
275			
276			// vowels.implode() => "zeiou"
277			assertEquals("zeiou", vowels.base_implode().asNativeText().javaValue);
278			
279             // vowels.join(",") => "z,e,i,o,u"
280			assertEquals("z,e,i,o,u", vowels.base_join(NATText.atValue(",")).asNativeText().javaValue);
281			
282            // [].implode() => ""
283			assertEquals("", NATTable.EMPTY.base_implode().asNativeText().javaValue);
284			
285            // [].join(",") => ""
286			assertEquals("", NATTable.EMPTY.base_join(NATText.atValue(",")).asNativeText().javaValue);
287			
288			// vowels.select(2,5).implode() => "eio"
289			assertEquals("eio", vowels.base_select(NATNumber.atValue(2), NATNumber.atValue(5)).base_implode().asNativeText().javaValue);
290		} catch (InterpreterException e) {
291			fail(e.getMessage());
292		}
293	}
294	
295	public void testBooleanPrimitives() {
296		try {
297			// (0 < 1).ifTrue: { 0 } => 0
298			assertEquals(NATNumber.ZERO, NATNumber.ZERO.base__opltx_(NATNumber.ONE).base_ifTrue_(new NativeClosure(null) {
299				public ATObject base_apply(ATTable args) {
300					return NATNumber.ZERO;
301				}
302			}));
303			
304			// (0 < 1).ifFalse: { 0 } => nil
305			assertEquals(Evaluator.getNil(), NATNumber.ZERO.base__opltx_(NATNumber.ONE).base_ifFalse_(new NativeClosure(null) {
306				public ATObject base_apply(ATTable args) {
307					return NATNumber.ZERO;
308				}
309			}));
310			
311			// false.and: { 1/0 } => false
312			try {
313				assertFalse(NATBoolean._FALSE_.base_and_(new NativeClosure(null) {
314					public ATObject base_apply(ATTable args) throws InterpreterException {
315						return NATNumber.ONE.base__opdiv_(NATNumber.ZERO);
316					}
317				}).asNativeBoolean().javaValue);
318			} catch (XIllegalArgument e) {
319				fail("short-circuit and: is broken.");
320			}
321			
322			// true.or: { 1/0 } => true
323			try {
324				assertTrue(NATBoolean._TRUE_.base_or_(new NativeClosure(null) {
325					public ATObject base_apply(ATTable args) throws InterpreterException {
326						return NATNumber.ONE.base__opdiv_(NATNumber.ZERO);
327					}
328				}).asNativeBoolean().javaValue);
329			} catch (XIllegalArgument e) {
330				fail("short-circuit or: is broken.");
331			}
332			
333			// false.or: { true } => true
334			assertTrue(NATBoolean._FALSE_.base_or_(new NativeClosure(null) {
335				public ATObject base_apply(ATTable args) throws InterpreterException {
336					return NATBoolean._TRUE_;
337				}
338			}).asNativeBoolean().javaValue);
339		} catch (InterpreterException e) {
340			fail(e.getMessage());
341		}
342	}
343	
344	public void testNilPrimitives() {
345		try {
346			NATNil nil = Evaluator.getNil();
347			// nil != nil => false
348			assertFalse(nil.base__opnot__opeql_(nil).asNativeBoolean().javaValue);
349			// nil == nil => true
350			assertTrue(nil.base__opeql__opeql_(nil).asNativeBoolean().javaValue);
351		    // object: {} != nil => true
352			ATObject obj = new NATObject();
353			assertTrue(obj.impl_invoke(obj, NATNil._NEQ_NAME_, NATTable.of(nil)).asNativeBoolean().javaValue);
354			// nil != object: {} => true
355			assertTrue(nil.base__opnot__opeql_(obj).asNativeBoolean().javaValue);
356		} catch (InterpreterException e) {
357			fail(e.getMessage());
358		}
359	}
360	
361}