/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

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