extension.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // Note: This example test is leveraging the Mocha test framework.
  3. // Please refer to their documentation on https://mochajs.org/ for help.
  4. //
  5. // The module 'assert' provides assertion methods from node
  6. import * as assert from 'assert';
  7. import { dirname, join } from 'path';
  8. import { timedRun, testcasesName, testSolution, newArena, ATTIC, TESTCASES, upgradeArena, stressSolution, newProblemFromId, newContestFromId } from '../core';
  9. import { TestcaseResult, Veredict } from '../types';
  10. import { rmdirSync, existsSync, readdirSync, unlinkSync, openSync, writeSync, closeSync } from 'fs';
  11. const SRC = join(dirname(dirname(dirname(__filename))), 'src', 'test');
  12. const ARENA = join(SRC, 'arena');
  13. suite("Extension Tests", function () {
  14. /**
  15. * Recursive remove
  16. */
  17. function recRmdir(path: string){
  18. readdirSync(path).forEach(name => {
  19. let cPath = join(path, name);
  20. try{
  21. unlinkSync(cPath);
  22. }
  23. catch(err){
  24. recRmdir(cPath);
  25. }
  26. });
  27. rmdirSync(path);
  28. }
  29. function writeFile(path: string, content: string){
  30. let currentFd = openSync(path, 'w');
  31. writeSync(currentFd, content);
  32. closeSync(currentFd);
  33. }
  34. // Defines a Mocha unit test
  35. test("learnjs", function() {
  36. });
  37. /**
  38. * core::newArena
  39. */
  40. test("newArena", function(){
  41. let path = join(ARENA, "testNew");
  42. if (existsSync(path)){
  43. recRmdir(path);
  44. }
  45. assert.equal(existsSync(path), false);
  46. newArena(path);
  47. assert.equal(existsSync(join(path, ATTIC)), true);
  48. assert.equal(existsSync(join(path, TESTCASES)), true);
  49. assert.equal(existsSync(join(path, 'sol.cpp')), true);
  50. });
  51. /**
  52. * core::upgradeArena
  53. */
  54. test("upgradeArena", function(){
  55. let path = join(ARENA, "testUpgrade");
  56. if (existsSync(path)){
  57. recRmdir(path);
  58. }
  59. assert.equal(existsSync(path), false);
  60. newArena(path);
  61. upgradeArena(path);
  62. assert.equal(existsSync(join(path, ATTIC, 'gen.py')), true);
  63. assert.equal(existsSync(join(path, 'brute.cpp')), true);
  64. });
  65. /**
  66. * core::testcasesName
  67. */
  68. test("testcasesName", function(){
  69. let path = join(ARENA, 'exampleContest', 'A');
  70. let result = testcasesName(path);
  71. let target = ["0", "1", "2"];
  72. // TODO: How to check if two arrays are equal
  73. // I want to compare `result` & `target`
  74. target.forEach(name => {assert.notEqual(result.findIndex(tname => { return tname === name; }), -1);});
  75. result.forEach(name => {assert.notEqual(target.findIndex(tname => { return tname === name; }), -1);});
  76. });
  77. /**
  78. * core::newProblem
  79. */
  80. test("newProblemFromId", function(){
  81. let path = join(ARENA);
  82. let problemId = 'testProblemFromId';
  83. newProblemFromId(join(path, problemId), 'personal', problemId);
  84. assert.equal(existsSync(join(path, problemId, 'sol.cpp')), true);
  85. assert.equal(existsSync(join(path, problemId, ATTIC)), true);
  86. assert.equal(existsSync(join(path, problemId, TESTCASES)), true);
  87. assert.equal(readdirSync(join(path, problemId, TESTCASES)).length, 6);
  88. });
  89. /**
  90. * core::newProblem
  91. */
  92. test("newContestFromId", function(){
  93. let path = join(ARENA);
  94. let contestId = 'testContestFromId';
  95. newContestFromId(join(path, contestId), 'personal', 5);
  96. assert.equal(readdirSync(join(path, contestId)).length, 5);
  97. });
  98. /**
  99. * core::timedRun
  100. *
  101. * Test running one single test cases, and receiving all different veredicts
  102. */
  103. test("timedRunOk", function() {
  104. let exampleContest = join(ARENA, 'exampleContest');
  105. let problem = join(exampleContest, 'A');
  106. let testcaseId = '0';
  107. let result: TestcaseResult = timedRun(problem, testcaseId);
  108. assert.equal(result.status, Veredict.OK);
  109. });
  110. test("timedRunWA", function() {
  111. let exampleContest = join(ARENA, 'exampleContest');
  112. let problem = join(exampleContest, 'B');
  113. let testcaseId = '0';
  114. let result: TestcaseResult = timedRun(problem, testcaseId);
  115. assert.equal(result.status, Veredict.WA);
  116. });
  117. test("timedRunRTE", function() {
  118. let exampleContest = join(ARENA, 'exampleContest');
  119. let problem = join(exampleContest, 'C');
  120. let testcaseId = '0';
  121. let result: TestcaseResult = timedRun(problem, testcaseId);
  122. assert.equal(result.status, Veredict.RTE);
  123. });
  124. test("timedRunTLE", function() {
  125. let exampleContest = join(ARENA, 'exampleContest');
  126. let problem = join(exampleContest, 'D');
  127. let testcaseId = '0';
  128. let result: TestcaseResult = timedRun(problem, testcaseId, 100);
  129. assert.equal(result.status, Veredict.TLE);
  130. });
  131. /**
  132. * core::testSolution
  133. *
  134. * Test running one single test cases, and receiving all different veredicts
  135. */
  136. test("testSolutionOK", function() {
  137. let exampleContest = join(ARENA, 'exampleContest');
  138. let problem = join(exampleContest, 'A');
  139. let result: TestcaseResult = testSolution(problem);
  140. assert.equal(result.status, Veredict.OK);
  141. });
  142. test("testSolutionCE", function() {
  143. let exampleContest = join(ARENA, 'exampleContest');
  144. let problem = join(exampleContest, 'E');
  145. let result: TestcaseResult = testSolution(problem);
  146. assert.equal(result.status, Veredict.CE);
  147. });
  148. /**
  149. * core::stressSolution
  150. */
  151. test("stressSolutionOK", function() {
  152. let path = join(ARENA, 'testStressOK');
  153. if (existsSync(path)){
  154. recRmdir(path);
  155. }
  156. assert.equal(existsSync(path), false);
  157. newArena(path);
  158. upgradeArena(path);
  159. // populate sol.cpp
  160. writeFile(join(path, "sol.cpp"),
  161. `#include <iostream>\n` +
  162. `\n` +
  163. `using namespace std;\n` +
  164. `\n` +
  165. `int main(){\n` +
  166. ` int n; cin >> n;\n` +
  167. ` cout << n + 2 << endl;\n` +
  168. ` return 0;\n` +
  169. `}\n`
  170. );
  171. // populate brute.cpp
  172. writeFile(join(path, "brute.cpp"),
  173. `#include <iostream>\n` +
  174. `\n` +
  175. `using namespace std;\n` +
  176. `\n` +
  177. `int main(){\n` +
  178. ` int n; cin >> n;\n` +
  179. ` cout << n + 2 << endl;\n` +
  180. ` return 0;\n` +
  181. `}\n`
  182. );
  183. // populate gen.py
  184. writeFile(join(path, ATTIC, 'gen.py'),
  185. `import random\n` +
  186. `print(random.randint(0, 99))\n`
  187. );
  188. let result = stressSolution(path);
  189. assert.equal(result.status, Veredict.OK);
  190. });
  191. test("stressSolutionWA", function() {
  192. let path = join(ARENA, 'testStressWA');
  193. if (existsSync(path)){
  194. recRmdir(path);
  195. }
  196. assert.equal(existsSync(path), false);
  197. newArena(path);
  198. upgradeArena(path);
  199. // populate sol.cpp
  200. writeFile(join(path, "sol.cpp"),
  201. `#include <iostream>\n` +
  202. `\n` +
  203. `using namespace std;\n` +
  204. `\n` +
  205. `int main(){\n` +
  206. ` int n; cin >> n;\n` +
  207. ` cout << n + 3 << endl;\n` +
  208. ` return 0;\n` +
  209. `}\n`
  210. );
  211. // populate brute.cpp
  212. writeFile(join(path, "brute.cpp"),
  213. `#include <iostream>\n` +
  214. `\n` +
  215. `using namespace std;\n` +
  216. `\n` +
  217. `int main(){\n` +
  218. ` int n; cin >> n;\n` +
  219. ` cout << n + 2 << endl;\n` +
  220. ` return 0;\n` +
  221. `}\n`
  222. );
  223. // populate gen.py
  224. writeFile(join(path, ATTIC, 'gen.py'),
  225. `import random\n` +
  226. `print(random.randint(0, 99))\n`
  227. );
  228. let result = stressSolution(path);
  229. assert.equal(result.status, Veredict.WA);
  230. });
  231. });