PageRenderTime 61ms CodeModel.GetById 2ms app.highlight 54ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2-build190607/test/edu/vub/at/objects/natives/PrimitivesTest.java

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