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

http://ambienttalk.googlecode.com/ · Java · 348 lines · 204 code · 59 blank · 85 comment · 1 complexity · 919a0fbd6065112ae040959ec3019519 MD5 · raw file

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