PageRenderTime 48ms CodeModel.GetById 20ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://ambienttalk.googlecode.com/
Java | 376 lines | 225 code | 60 blank | 91 comment | 1 complexity | 01ee6320497f229ed35e354a8c065fda 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			
 80			// "A".toNumber => 10
 81			assertEquals(NATNumber.atValue(Character.getNumericValue('A')), NATText.atValue("A").base_toNumber());
 82			
 83			// "ambienttalk".toNumber => XTypeMismatch, expected Character, given NATText
 84			try {
 85			  TXTambienttalk_.base_toNumber();
 86			} catch (XTypeMismatch e) {
 87			  assertEquals(Character.class, e.getExpectedType());
 88			  assertEquals(TXTambienttalk_, e.getFailedObject());
 89			}
 90		} catch (InterpreterException e) {
 91			fail(e.getMessage());
 92		}
 93	}
 94	
 95	public void testNumericPrimitives() {
 96		try {
 97			// 5.cos().inc()
 98			assertEquals(NATFraction.atValue(Math.cos(5)+1), NATNumber.atValue(5).base_cos().base_inc());
 99			
100			// 2.expt(3).round()
101			assertEquals(8, NATNumber.atValue(2).base_expt(NATNumber.atValue(3)).base_round().asNativeNumber().javaValue);
102		
103		    // 1 + 2 => 3
104			assertEquals(3, NATNumber.ONE.base__oppls_(NATNumber.atValue(2)).asNativeNumber().javaValue);
105		    // 1.1 + 2.2 => 3.3
106			assertEquals(3.3, NATFraction.atValue(1.1).base__oppls_(NATFraction.atValue(2.2)).asNativeFraction().javaValue, 0.000001);
107		    // 1.0 + 2 => 3.0
108			assertEquals(3.0, NATFraction.atValue(1.0).base__oppls_(NATNumber.atValue(2)).asNativeFraction().javaValue, 0.000001);
109		    // 1 + 2.0 => 3.0
110			assertEquals(3.0, NATNumber.ONE.base__oppls_(NATFraction.atValue(2.0)).asNativeFraction().javaValue, 0.000001);
111
112		    // 1 - 2 => -1
113			assertEquals(-1, NATNumber.ONE.base__opmns_(NATNumber.atValue(2)).asNativeNumber().javaValue);
114		    // 1.1 - 2.2 => -1.1
115			assertEquals(-1.1, NATFraction.atValue(1.1).base__opmns_(NATFraction.atValue(2.2)).asNativeFraction().javaValue, 0.000001);
116		    // 1.0 - 2 => -1.0
117			assertEquals(-1.0, NATFraction.atValue(1.0).base__opmns_(NATNumber.atValue(2)).asNativeFraction().javaValue, 0.000001);
118		    // 1 - 2.0 => -1.0
119			assertEquals(-1.0, NATNumber.ONE.base__opmns_(NATFraction.atValue(2.0)).asNativeFraction().javaValue, 0.000001);
120			
121		    // 1 * 2 => 2
122			assertEquals(2, NATNumber.ONE.base__optms_(NATNumber.atValue(2)).asNativeNumber().javaValue);
123		    // 1.1 * 2.2
124			assertEquals(1.1 * 2.2, NATFraction.atValue(1.1).base__optms_(NATFraction.atValue(2.2)).asNativeFraction().javaValue, 0.000001);
125		    // 1.0 * 2 => 2.0
126			assertEquals(2.0, NATFraction.atValue(1.0).base__optms_(NATNumber.atValue(2)).asNativeFraction().javaValue, 0.000001);
127		    // 1 * 2.0 => 2.0
128			assertEquals(2.0, NATNumber.ONE.base__optms_(NATFraction.atValue(2.0)).asNativeFraction().javaValue, 0.000001);
129
130		    // 1 / 2 => 0.5
131			assertEquals(0.5, NATNumber.ONE.base__opdiv_(NATNumber.atValue(2)).asNativeFraction().javaValue, 0.0000001);
132		    // 1.1 / 2.2
133			assertEquals(1.1 / 2.2, NATFraction.atValue(1.1).base__opdiv_(NATFraction.atValue(2.2)).asNativeFraction().javaValue, 0.000001);
134		    // 1.0 / 2 => 0.5
135			assertEquals(0.5, NATFraction.atValue(1.0).base__opdiv_(NATNumber.atValue(2)).asNativeFraction().javaValue, 0.000001);
136		    // 1 / 2.0 => 0.5
137			assertEquals(0.5, NATNumber.ONE.base__opdiv_(NATFraction.atValue(2.0)).asNativeFraction().javaValue, 0.000001);
138			
139			// 1 < 2
140			assertTrue(NATNumber.ONE.base__opltx_(NATNumber.atValue(2)).asNativeBoolean().javaValue);
141			// 2.5 > 2
142			assertTrue(NATFraction.atValue(2.5).base__opgtx_(NATNumber.atValue(2)).asNativeBoolean().javaValue);
143             // 2.5 <= 2.5
144			assertTrue(NATFraction.atValue(2.5).base__opltx__opeql_(NATFraction.atValue(2.5)).asNativeBoolean().javaValue);
145             // 1 >= 1
146			assertTrue(NATNumber.ONE.base__opgtx__opeql_(NATNumber.ONE).asNativeBoolean().javaValue);
147			// 1.0 = 1
148			assertTrue(NATFraction.atValue(1.0).base__opeql_(NATNumber.ONE).asNativeBoolean().javaValue);
149			// 1 = 1.0
150			assertTrue(NATNumber.ONE.base__opeql_(NATFraction.atValue(1.0)).asNativeBoolean().javaValue);
151			// 1.1 != 1.0
152			assertTrue(NATFraction.atValue(1.1).base__opnot__opeql_(NATFraction.atValue(1.0)).asNativeBoolean().javaValue);
153             // ! 1.0 == 1
154			assertFalse(NATFraction.atValue(1.0).base__opeql__opeql_(NATNumber.ONE).asNativeBoolean().javaValue);
155			
156		} catch (InterpreterException e) {
157			fail(e.getMessage());
158		}
159	}
160	
161	public void testNumberPrimitives() {
162		try {
163			// 1.inc() => 2
164			assertEquals(2, NATNumber.ONE.base_inc().asNativeNumber().javaValue);
165			
166			// -1.abs() => 1
167			assertEquals(1, NATNumber.MONE.base_abs().asNativeNumber().javaValue);
168			
169			// 3.doTimes: { |i| buff << i; nil } => buff = 123
170			final StringBuffer buff = new StringBuffer();
171			NATNumber.atValue(3).base_doTimes_(new NativeClosure(null) {
172				public ATObject base_apply(ATTable args) throws InterpreterException {
173					buff.append(getNbr(args, 1));
174					return Evaluator.getNil();
175				}
176			});
177			assertEquals("123", buff.toString());
178			
179			// 3.to: 5 do: { |i| buff2 << i; nil } => buff2 = 34
180			final StringBuffer buff2 = new StringBuffer();
181			NATNumber.atValue(3).base_to_do_(NATNumber.atValue(5), new NativeClosure(null) {
182				public ATObject base_apply(ATTable args) throws InterpreterException {
183					buff2.append(getNbr(args, 1));
184					return Evaluator.getNil();
185				}
186			});
187			assertEquals("34", buff2.toString());
188			
189			// 50.to: 0 step: 10 do: { |i| buff3 << i; nil } => buff3 = 5040302010
190			final StringBuffer buff3 = new StringBuffer();
191			NATNumber.atValue(50).base_to_step_do_(NATNumber.atValue(0), NATNumber.atValue(10), new NativeClosure(null) {
192				public ATObject base_apply(ATTable args) throws InterpreterException {
193					buff3.append(getNbr(args, 1));
194					return Evaluator.getNil();
195				}
196			});
197			assertEquals("5040302010", buff3.toString());
198			
199			// 1 ** 4 => [1, 2, 3]
200			printedEquals(NATNumber.ONE.base__optms__optms_(NATNumber.atValue(4)), "[1, 2, 3]");
201			// 1 *** 4 => [1, 2, 3, 4]
202			printedEquals(NATNumber.ONE.base__optms__optms__optms_(NATNumber.atValue(4)), "[1, 2, 3, 4]");
203			// 4 ** 1 => [4, 3, 2]
204			printedEquals(NATNumber.atValue(4).base__optms__optms_(NATNumber.ONE), "[4, 3, 2]");
205			// 4 *** 1 => [4, 3, 2, 1]
206			printedEquals(NATNumber.atValue(4).base__optms__optms__optms_(NATNumber.ONE), "[4, 3, 2, 1]");
207			// -1 ** -1 => []
208			printedEquals(NATNumber.MONE.base__optms__optms_(NATNumber.MONE), "[]");
209			// -1 *** -1 => [-1]
210			printedEquals(NATNumber.MONE.base__optms__optms__optms_(NATNumber.MONE), "[-1]");
211			
212			// 1 ?? 5 => [1, 5[
213			double rand = NATNumber.ONE.base__opque__opque_(NATNumber.atValue(5)).asNativeFraction().javaValue;
214			assertTrue((1 <= rand) && (rand < 5));
215			
216			// 8 % 3 => 2
217			assertEquals(2, NATNumber.atValue(8).base__oprem_(NATNumber.atValue(3)).asNativeNumber().javaValue);
218			
219			// 9 /- 2 => 4
220			assertEquals(4, NATNumber.atValue(9).base__opdiv__opmns_(NATNumber.atValue(2)).asNativeNumber().javaValue);
221			// 3.toText() => "3"
222			assertEquals(NATText.atValue("3"), NATNumber.atValue(3).base_toText());
223		} catch (InterpreterException e) {
224			fail(e.getMessage());
225		}
226	}
227
228	public void testFractionPrimitives() {
229		try {
230			// 1.4.round() => 1
231			assertEquals(1, NATFraction.atValue(1.4).base_round().asNativeNumber().javaValue);
232			// 1.8.round() => 2
233			assertEquals(2, NATFraction.atValue(1.8).base_round().asNativeNumber().javaValue);
234			// 1.5.round() => 2
235			assertEquals(2, NATFraction.atValue(1.5).base_round().asNativeNumber().javaValue);
236			
237			// 1.8.floor() => 1
238			assertEquals(1, NATFraction.atValue(1.8).base_floor().asNativeNumber().javaValue);
239			// 1.4.ceiling() => 2
240			assertEquals(2, NATFraction.atValue(1.4).base_ceiling().asNativeNumber().javaValue);
241			// (3.14).toText() => "3.14"
242			assertEquals(NATText.atValue("3.14"), NATFraction.atValue(3.14).base_toText());
243		} catch (InterpreterException e) {
244			fail(e.getMessage());
245		}
246	}
247	
248	public void testClosurePrimitives() {
249		try {
250			// whileTrue
251			ATObject result = evalAndReturn("def i := 0; { i < 5 }.whileTrue: { i := i + 1 }; i");
252			assertEquals(5, result.asNativeNumber().javaValue);
253		} catch (XTypeMismatch e) {
254			fail(e.getMessage());
255		}
256	}
257	
258	public void testTablePrimitives() {
259		try {
260			ATTable vowels = evalAndReturn("[\"a\", \"e\", \"i\", \"o\", \"u\"]").asTable();
261
262			// vowels.length = 5
263			assertEquals(5, vowels.base_length().asNativeNumber().javaValue);
264			
265			// vowels.at(1) = "a"
266			assertEquals("a", vowels.base_at(NATNumber.ONE).asNativeText().javaValue);
267			
268			// vowels.atPut(1, "z")
269			vowels.base_atPut(NATNumber.ONE, NATText.atValue("z"));
270			assertEquals("z", vowels.base_at(NATNumber.ONE).asNativeText().javaValue);
271			
272			// vowels.isEmpty() => false
273			assertFalse(vowels.base_isEmpty().asNativeBoolean().javaValue);
274			
275			// each: ablock
276			evalAndCompareTo("def sum := 0; [1,2,3].each: { |i| sum := sum + i }; sum", "6");
277			
278			// map: ablock
279			evalAndCompareTo("[1,2,3].map: { |i| i + 1 }", "[2, 3, 4]");
280			
281			// with: init collect: ablock
282			evalAndCompareTo("[1,2,3].inject: 0 into: { |total, next| total + next }", "6");
283			
284			// filter: ablock
285			evalAndCompareTo("[1,2,3].filter: {|e| e != 2 }", "[1, 3]");
286
287			// find: ablock
288			evalAndCompareTo("[`a, `b, `c].find: { |e| e == `b }", "2");
289			evalAndCompareTo("[`a, `b, `c].find: { |e| e == `d }", Evaluator.getNil());
290			
291			// vowels.implode() => "zeiou"
292			assertEquals("zeiou", vowels.base_implode().asNativeText().javaValue);
293			
294             // vowels.join(",") => "z,e,i,o,u"
295			assertEquals("z,e,i,o,u", vowels.base_join(NATText.atValue(",")).asNativeText().javaValue);
296			
297            // [].implode() => ""
298			assertEquals("", NATTable.EMPTY.base_implode().asNativeText().javaValue);
299			
300            // [].join(",") => ""
301			assertEquals("", NATTable.EMPTY.base_join(NATText.atValue(",")).asNativeText().javaValue);
302			
303			// vowels.select(2,5).implode() => "eio"
304			assertEquals("eio", vowels.base_select(NATNumber.atValue(2), NATNumber.atValue(5)).base_implode().asNativeText().javaValue);
305		} catch (InterpreterException e) {
306			fail(e.getMessage());
307		}
308	}
309	
310	public void testBooleanPrimitives() {
311		try {
312			// (0 < 1).ifTrue: { 0 } => 0
313			assertEquals(NATNumber.ZERO, NATNumber.ZERO.base__opltx_(NATNumber.ONE).base_ifTrue_(new NativeClosure(null) {
314				public ATObject base_apply(ATTable args) {
315					return NATNumber.ZERO;
316				}
317			}));
318			
319			// (0 < 1).ifFalse: { 0 } => nil
320			assertEquals(Evaluator.getNil(), NATNumber.ZERO.base__opltx_(NATNumber.ONE).base_ifFalse_(new NativeClosure(null) {
321				public ATObject base_apply(ATTable args) {
322					return NATNumber.ZERO;
323				}
324			}));
325			
326			// false.and: { 1/0 } => false
327			try {
328				assertFalse(NATBoolean._FALSE_.base_and_(new NativeClosure(null) {
329					public ATObject base_apply(ATTable args) throws InterpreterException {
330						return NATNumber.ONE.base__opdiv_(NATNumber.ZERO);
331					}
332				}).asNativeBoolean().javaValue);
333			} catch (XIllegalArgument e) {
334				fail("short-circuit and: is broken.");
335			}
336			
337			// true.or: { 1/0 } => true
338			try {
339				assertTrue(NATBoolean._TRUE_.base_or_(new NativeClosure(null) {
340					public ATObject base_apply(ATTable args) throws InterpreterException {
341						return NATNumber.ONE.base__opdiv_(NATNumber.ZERO);
342					}
343				}).asNativeBoolean().javaValue);
344			} catch (XIllegalArgument e) {
345				fail("short-circuit or: is broken.");
346			}
347			
348			// false.or: { true } => true
349			assertTrue(NATBoolean._FALSE_.base_or_(new NativeClosure(null) {
350				public ATObject base_apply(ATTable args) throws InterpreterException {
351					return NATBoolean._TRUE_;
352				}
353			}).asNativeBoolean().javaValue);
354		} catch (InterpreterException e) {
355			fail(e.getMessage());
356		}
357	}
358	
359	public void testNilPrimitives() {
360		try {
361			NATNil nil = Evaluator.getNil();
362			// nil != nil => false
363			assertFalse(nil.base__opnot__opeql_(nil).asNativeBoolean().javaValue);
364			// nil == nil => true
365			assertTrue(nil.base__opeql__opeql_(nil).asNativeBoolean().javaValue);
366		    // object: {} != nil => true
367			ATObject obj = new NATObject();
368			assertTrue(obj.impl_invoke(obj, NATNil._NEQ_NAME_, NATTable.of(nil)).asNativeBoolean().javaValue);
369			// nil != object: {} => true
370			assertTrue(nil.base__opnot__opeql_(obj).asNativeBoolean().javaValue);
371		} catch (InterpreterException e) {
372			fail(e.getMessage());
373		}
374	}
375	
376}