codebox.test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import { CodeBox } from "../dist/main";
  2. /**
  3. * A utility class used to concatenate the output of the CodeBox
  4. */
  5. class OutputJoiner {
  6. constructor(joinChar = "|") {
  7. this.out = "";
  8. this.JOIN_CHAR = joinChar;
  9. }
  10. push(newOutput) {
  11. if (this.out != "") { this.out += this.JOIN_CHAR; }
  12. this.out += newOutput;
  13. }
  14. }
  15. test("Initialize an empty CodeBox", () => {
  16. function emptyCodeBoxError() {
  17. new CodeBox();
  18. }
  19. expect(emptyCodeBoxError).toThrow(/No room for the fish to survive/gm);
  20. });
  21. test("Initialize a nonempty CodeBox", () => {
  22. const cb = new CodeBox("11+n;");
  23. expect(cb.maxBoxHeight).toBe(1);
  24. expect(cb.maxBoxWidth).toBe(5);
  25. expect(cb.stacks[0].values()).toEqual([]);
  26. });
  27. test("Test simple output", () => {
  28. const output = new OutputJoiner();
  29. const cb = new CodeBox("11+n;", [], {
  30. outputCallback: output.push.bind(output),
  31. });
  32. cb.run();
  33. expect(output.out).toBe("2");
  34. });
  35. test("Test simple output with initial stack", () => {
  36. const output = new OutputJoiner();
  37. const cb = new CodeBox("+n;", [1,3], {
  38. outputCallback: output.push.bind(output),
  39. });
  40. cb.run();
  41. expect(output.out).toBe("4");
  42. });
  43. test("Test all arithmetic operators", () => {
  44. const output = new OutputJoiner();
  45. const cb = new CodeBox("11+n94-n22*n93,nf3%9a)n9a(n99=n;", [], {
  46. outputCallback: output.push.bind(output),
  47. });
  48. cb.run();
  49. expect(output.out).toBe("2|5|4|3|0|1|1");
  50. });
  51. test("Test all redirects", () => {
  52. const output = new OutputJoiner();
  53. const cb = new CodeBox(`v33*>n;
  54. >ff*^`, [], {
  55. outputCallback: output.push.bind(output),
  56. });
  57. cb.run();
  58. expect(output.out).toBe("225");
  59. });
  60. test("Test out of bounds movement", () => {
  61. const output = new OutputJoiner();
  62. const cb = new CodeBox("<;n+33", [], {
  63. outputCallback: output.push.bind(output),
  64. });
  65. cb.run();
  66. expect(output.out).toBe("6");
  67. });
  68. test("Test all mirror", () => {
  69. const output = new OutputJoiner();
  70. const cb = new CodeBox(`v ;
  71. \\!aa|/+
  72. !
  73. n
  74. n
  75. 5
  76. _`, [], {
  77. outputCallback: output.push.bind(output),
  78. });
  79. cb.run();
  80. expect(output.out).toBe("20|5|5");
  81. });
  82. test("Test more complex mirror test", () => {
  83. const output = new OutputJoiner("");
  84. const cb = new CodeBox(`>11+nv
  85. /o*84<
  86. \\72-n\\
  87. /o*84<
  88. \\47*n\\
  89. /o*84<
  90. \\94,n\\
  91. /o*84<
  92. \\f4%n;`, [], {
  93. outputCallback: output.push.bind(output),
  94. });
  95. cb.run();
  96. expect(output.out).toBe("2 5 28 2.25 3");
  97. });
  98. test("Test dive and rise", () => {
  99. const output = new OutputJoiner("");
  100. const cb = new CodeBox(`"Hello, world!"r>Ool?u!|;`, [], {
  101. outputCallback: output.push.bind(output),
  102. });
  103. cb.run();
  104. expect(output.out).toBe("Hello, world!");
  105. });
  106. test("Test stack splitting", () => {
  107. const cb = new CodeBox(`abcdef0983[D3[DII;`);
  108. cb.run();
  109. expect(cb.stacks[0].values()).toEqual([10,11,12]);
  110. expect(cb.stacks[1].values()).toEqual([13,14,15]);
  111. expect(cb.stacks[2].values()).toEqual([0,9,8]);
  112. });
  113. test("Test stack splitting and collapsing", () => {
  114. const cb = new CodeBox(`abcdef0983[D3[DIID]I];`);
  115. cb.run();
  116. expect(cb.stacks[0].values()).toEqual([10,11,12,13,14,15,0,9,8]);
  117. });
  118. test("Test printing with stack splitting and collapsing", () => {
  119. const output = new OutputJoiner("");
  120. const cb = new CodeBox(`"Hello "r0["World"rDv
  121. ov!?l <
  122. I
  123. o>l?!;`, [], {
  124. outputCallback: output.push.bind(output),
  125. });
  126. cb.run();
  127. expect(output.out).toBe("Hello World");
  128. });
  129. test("Test printing of string in initial stack", () => {
  130. const output = new OutputJoiner("");
  131. const cb = new CodeBox(` r\\
  132. o;!?l<`, '"Howdy doody"', {
  133. outputCallback: output.push.bind(output),
  134. });
  135. cb.run();
  136. expect(output.out).toBe("Howdy doody");
  137. });
  138. test("Test printing using vertical fisherman", () => {
  139. const output = new OutputJoiner("");
  140. const cb = new CodeBox(`"hi"\`
  141. \`roo!/<
  142. "
  143. e
  144. y
  145. b
  146. "
  147. ;oooo\``, [], {
  148. outputCallback: output.push.bind(output),
  149. });
  150. cb.run();
  151. expect(output.out).toBe("hi bye");
  152. });
  153. test("Test time variables: seconds", () => {
  154. const output = new OutputJoiner("");
  155. const cb = new CodeBox(`sn;`, [], {
  156. outputCallback: output.push.bind(output),
  157. });
  158. cb.run();
  159. expect(output.out).toBe((new Date()).getUTCSeconds().toString());
  160. });
  161. test("Test time variables: minutes", () => {
  162. const output = new OutputJoiner("");
  163. const cb = new CodeBox(`mn;`, [], {
  164. outputCallback: output.push.bind(output),
  165. });
  166. cb.run();
  167. expect(output.out).toBe((new Date()).getUTCMinutes().toString());
  168. });
  169. test("Test time variables: hours", () => {
  170. const output = new OutputJoiner("");
  171. const cb = new CodeBox(`hn;`, [], {
  172. outputCallback: output.push.bind(output),
  173. });
  174. cb.run();
  175. expect(output.out).toBe((new Date()).getUTCHours().toString());
  176. });
  177. test("Test single-stack register", () => {
  178. const cb = new CodeBox(`a&;`);
  179. cb.run();
  180. expect(cb.stacks[0].values()).toEqual([]);
  181. expect(cb.stacks[0].getRegisterValue()).toBe(10);
  182. cb.parseCodeBox("a&0&;");
  183. cb.reset();
  184. cb.run();
  185. expect(cb.stacks[0].values()).toEqual([0,10]);
  186. });
  187. test("Test multi-stack register", () => {
  188. const cb = new CodeBox(`a&0[f&D&;`);
  189. cb.run();
  190. expect(cb.stacks[0].values()).toEqual([10]);
  191. expect(cb.stacks[0].getRegisterValue()).toBe(null);
  192. expect(cb.stacks[1].values()).toEqual([]);
  193. expect(cb.stacks[1].getRegisterValue()).toBe(15);
  194. });
  195. test("Test push value from CodeBox", () => {
  196. const output = new OutputJoiner("|");
  197. const cb = new CodeBox(`c22g\\
  198. /g32/
  199. \\nnn;
  200. 2`, [], {
  201. outputCallback: output.push.bind(output),
  202. });
  203. cb.run();
  204. expect(output.out).toBe("50|110|12");
  205. });
  206. test("Test push out-of-bounds value from CodeBox", () => {
  207. const output = new OutputJoiner("|");
  208. const cb = new CodeBox(`5fgn;`, [], {
  209. outputCallback: output.push.bind(output),
  210. });
  211. cb.run();
  212. expect(output.out).toBe("0");
  213. });
  214. test("Test function call and return", () => {
  215. const output = new OutputJoiner("");
  216. const cb = new CodeBox(`<;C10
  217. v"It works!"
  218. <oR!?l`, [], {
  219. outputCallback: output.push.bind(output),
  220. });
  221. cb.run();
  222. expect(output.out).toBe("It works!");
  223. });
  224. test("Test - factorial of ten", () => {
  225. const output = new OutputJoiner("");
  226. const cb = new CodeBox(` :?\\~11>*l1\\
  227. -1:/ ;n\\?- /`, [10], {
  228. outputCallback: output.push.bind(output),
  229. });
  230. cb.run();
  231. expect(output.out).toBe("3628800");
  232. });
  233. test("Test - quine", () => {
  234. const output = new OutputJoiner("");
  235. const cb = new CodeBox(`"r00gol?!;40.`, [], {
  236. outputCallback: output.push.bind(output),
  237. });
  238. cb.run();
  239. expect(output.out).toBe('"r00gol?!;40.');
  240. });