test_constructors_al_linkable.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. import unittest
  2. import sys
  3. import os
  4. from utils import execute, kill, run_file, run_barebone
  5. def extend_commands(commands):
  6. # Initially we communicate with the compilation_manager to add the bytecodes
  7. pre = [
  8. '3',
  9. '"upload"',
  10. '"test.o"',
  11. '"MD5_HASH"',
  12. 'true', # use NEW interface for constructors
  13. '"funcdef"', '"main"', '0', # Main function
  14. ]
  15. # We only have to define "main" for now
  16. post = [
  17. 'true',
  18. '"main"',
  19. 'true',
  20. 'false',
  21. ]
  22. return pre + commands + post
  23. class TestConstructorsActionLanguageLinkable(unittest.TestCase):
  24. def test_constructors_simple(self):
  25. commands = [
  26. '"output"',
  27. '"const"', 'true',
  28. 'true',
  29. '"return"',
  30. 'true',
  31. '"const"', 'true',
  32. 'false',
  33. ]
  34. commands = extend_commands(commands)
  35. self.assertTrue(run_barebone(commands, ["True"], None, link=["test.o"]))
  36. def test_constructors_if_else_true(self):
  37. commands = [
  38. '"if"', # If
  39. '"const"', 'true', # Condition
  40. '"output"', # True-part
  41. '"const"', 'true',
  42. 'false', # Has next
  43. 'true', # Has else
  44. '"output"', # False-part
  45. '"const"', 'false',
  46. 'false', # Has next
  47. 'true', # Has next
  48. '"return"', # Return
  49. 'true', # Has value
  50. '"const"', "true",
  51. 'false',
  52. ]
  53. commands = extend_commands(commands)
  54. self.assertTrue(run_barebone(commands, ["True"], None, link=["test.o"]))
  55. def test_constructors_if_else_false(self):
  56. commands = [
  57. '"if"', # If
  58. '"const"', 'false', # Condition
  59. '"output"', # True-part
  60. '"const"', 'true',
  61. 'false', # Has next
  62. 'true', # Has else
  63. '"output"', # False-part
  64. '"const"', 'false',
  65. 'false', # Has next
  66. 'true', # Has next
  67. '"return"', # Return
  68. 'true', # Has value
  69. '"const"', "true",
  70. 'false',
  71. ]
  72. commands = extend_commands(commands)
  73. self.assertTrue(run_barebone(commands, ["False"], None, link=["test.o"]))
  74. def test_constructors_if_true(self):
  75. commands = [
  76. '"if"', # If
  77. '"const"', 'true', # Condition
  78. '"output"', # True-part
  79. '"const"', 'true',
  80. 'false', # Has next
  81. 'false', # Has else
  82. 'true', # Has next
  83. '"return"', # Return
  84. 'true', # Has value
  85. '"const"', "true",
  86. 'false',
  87. ]
  88. commands = extend_commands(commands)
  89. self.assertTrue(run_barebone(commands, ["True"], None, link=["test.o"]))
  90. def test_constructors_if_false(self):
  91. commands = [
  92. '"if"', # If
  93. '"const"', 'false', # Condition
  94. '"output"', # True-part
  95. '"const"', 'true',
  96. 'false', # Has next
  97. 'false', # Has else
  98. 'true', # Has next
  99. '"return"', # Return
  100. 'true', # Has value
  101. '"const"', "true",
  102. 'false',
  103. ]
  104. commands = extend_commands(commands)
  105. self.assertTrue(run_barebone(commands, [None], None, timeout=True, link=["test.o"]))
  106. """
  107. def test_constructors_addition(self):
  108. commands = [('"output"',
  109. ('"call"',
  110. ('"deref"', '"primitives/integer_addition"'),
  111. '2',
  112. ('"const"', '1'),
  113. ('"const"', '5'),
  114. 'false',
  115. ),
  116. 'true',
  117. ('"return"', 'true', '"const"', 'true'),
  118. )
  119. ]
  120. self.assertTrue(run_barebone(flatten(commands), ["6"], 1))
  121. """
  122. def test_constructors_while_false(self):
  123. commands = [
  124. '"while"', # While
  125. '"const"', 'false', # Condition
  126. '"output"', # True-part
  127. '"const"', 'true',
  128. 'false', # Has next
  129. 'true', # Has next
  130. '"output"', # Output false
  131. '"const"', 'false',
  132. 'true', # Has next
  133. '"return"', # Return
  134. 'true', # Has value
  135. '"const"', "true",
  136. 'false',
  137. ]
  138. commands = extend_commands(commands)
  139. self.assertTrue(run_barebone(commands, ["False"], None, link=["test.o"]))
  140. def test_constructors_while_true(self):
  141. commands = [
  142. '"while"', # While
  143. '"const"', 'true', # Condition
  144. '"output"', # True-part
  145. '"const"', 'true',
  146. 'false', # Has next
  147. 'true', # Has next
  148. '"output"', # False-part
  149. '"const"', 'false',
  150. 'true', # Has next
  151. '"return"', # Return
  152. 'true', # Has value
  153. '"const"', "true",
  154. 'false',
  155. ]
  156. commands = extend_commands(commands)
  157. self.assertTrue(run_barebone(commands, ["True", "True", "True", "True"], None, link=["test.o"]))
  158. def test_constructors_declare_and_assign(self):
  159. commands = [
  160. '"declare"',
  161. 1,
  162. 'true',
  163. '"assign"',
  164. '"resolve"', 1,
  165. '"const"', '5',
  166. 'true',
  167. '"output"',
  168. '"access"', '"resolve"', 1,
  169. 'true',
  170. '"return"',
  171. 'true',
  172. '"const"', "true",
  173. 'false',
  174. ]
  175. commands = extend_commands(commands)
  176. self.assertTrue(run_barebone(commands, ["5"], None, link=["test.o"]))
  177. """
  178. def test_constructors_output_input(self):
  179. commands = [
  180. '"output"',
  181. '"input"',
  182. 'true',
  183. '"return"',
  184. 'true',
  185. '"const"', "true",
  186. 'false',
  187. ]
  188. commands = extend_commands(commands)
  189. self.assertTrue(run_barebone(commands + ['123456'], ["123456"], None, link=["test.o"]))
  190. """
  191. def test_constructors_global_and_assign(self):
  192. commands = [
  193. '"global"',
  194. '"my_global"',
  195. 'true',
  196. '"assign"',
  197. '"resolve"', '"my_global"',
  198. '"const"', '5',
  199. 'true',
  200. '"output"',
  201. '"access"', '"resolve"', '"my_global"',
  202. 'true',
  203. '"return"',
  204. 'true',
  205. '"const"', "true",
  206. 'false',
  207. ]
  208. commands = extend_commands(commands)
  209. self.assertTrue(run_barebone(commands, ["5"], None, link=["test.o"]))
  210. """
  211. def test_constructors_funcdecl(self):
  212. commands = ['"global"',
  213. '"my_global"',
  214. 'true',
  215. '"funcdef"', '"my_global"',
  216. '2',
  217. 2,
  218. 3,
  219. '"return"', 'true',
  220. '"call"',
  221. '"deref"', '"primitives/integer_addition"',
  222. '2',
  223. '"access"', '"resolve"', 2,
  224. '"access"', '"resolve"', 3,
  225. 'false',
  226. 'true',
  227. '"output"',
  228. '"call"',
  229. '"access"', '"resolve"', '"my_global"',
  230. '2',
  231. '"input"',
  232. '"input"',
  233. 'false',
  234. 'true',
  235. '"return"', 'true',
  236. '"const"', 'true',
  237. ]
  238. self.assertTrue(run_barebone(flatten(commands) + ['2', '5'], ["7"], 1))
  239. def test_constructors_fibonacci(self):
  240. commands = ['"global"',
  241. '"fibonacci"',
  242. 'true',
  243. '"funcdef"', '"fibonacci"',
  244. '1',
  245. 2,
  246. '"if"',
  247. '"call"',
  248. '"deref"', '"primitives/integer_lte"',
  249. '2',
  250. '"access"', '"resolve"', 2,
  251. '"const"', '2',
  252. 'false',
  253. '"return"', 'true', '"const"', '1',
  254. 'true',
  255. '"return"', 'true',
  256. '"call"',
  257. '"deref"', '"primitives/integer_addition"',
  258. '2',
  259. '"call"',
  260. '"access"', '"resolve"', '"fibonacci"',
  261. '1',
  262. '"call"',
  263. '"deref"', '"primitives/integer_subtraction"',
  264. '2',
  265. '"access"', '"resolve"', 2,
  266. '"const"', '1',
  267. 'false',
  268. 'false',
  269. '"call"',
  270. '"access"', '"resolve"', '"fibonacci"',
  271. '1',
  272. '"call"',
  273. '"deref"', '"primitives/integer_subtraction"',
  274. '2',
  275. '"access"', '"resolve"', 2,
  276. '"const"', '2',
  277. 'false',
  278. 'false',
  279. 'false',
  280. 'false',
  281. 'true',
  282. '"while"',
  283. '"const"', 'true',
  284. '"output"',
  285. '"call"',
  286. '"access"', '"resolve"', '"fibonacci"',
  287. '1',
  288. '"input"',
  289. 'false',
  290. 'false',
  291. 'false',
  292. ]
  293. self.assertTrue(run_barebone(flatten(commands) + ['1', '2', '3', '4', '5', '6', '7', '8'], ['1', '1', '2', '3', '5', '8', '13', '21'], 1))
  294. def test_constructors_continue(self):
  295. commands = ['"while"',
  296. '"const"', 'true',
  297. '"output"', '"const"', '1',
  298. 'true',
  299. '"if"',
  300. '"const"', 'true',
  301. '"continue"',
  302. 'false',
  303. 'true',
  304. '"output"', '"const"', '2',
  305. 'false',
  306. 'true',
  307. '"output"', '"const"', '3',
  308. 'true',
  309. '"return"', 'true',
  310. '"const"', 'true',
  311. ]
  312. self.assertTrue(run_barebone(flatten(commands), ['1', '1', '1', '1', '1'], 1))
  313. def test_constructors_break(self):
  314. commands = ['"while"',
  315. '"const"', 'true',
  316. '"output"', '"const"', '1',
  317. 'true',
  318. '"if"',
  319. '"const"', 'true',
  320. '"break"',
  321. 'false',
  322. 'true',
  323. '"output"', '"const"', '2',
  324. 'false',
  325. 'true',
  326. '"output"', '"const"', '3',
  327. 'true',
  328. '"return"', 'true',
  329. '"const"', 'true',
  330. ]
  331. self.assertTrue(run_barebone(flatten(commands), ['1', '3'], 1))
  332. """