/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

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