extension.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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, readSync } from 'fs';
  11. // import { request } from 'http';
  12. const SRC = join(dirname(dirname(dirname(__filename))), 'src', 'test');
  13. const ARENA = join(SRC, 'arena');
  14. suite("Extension Tests", function () {
  15. /**
  16. * Recursive remove
  17. */
  18. function recRmdir(path: string){
  19. readdirSync(path).forEach(name => {
  20. let cPath = join(path, name);
  21. try{
  22. unlinkSync(cPath);
  23. }
  24. catch(err){
  25. recRmdir(cPath);
  26. }
  27. });
  28. rmdirSync(path);
  29. }
  30. function writeFile(path: string, content: string){
  31. let currentFd = openSync(path, 'w');
  32. writeSync(currentFd, content);
  33. closeSync(currentFd);
  34. }
  35. /**
  36. * core::newArena
  37. */
  38. test("newArena", function(){
  39. let path = join(ARENA, "testNew");
  40. if (existsSync(path)){
  41. recRmdir(path);
  42. }
  43. assert.equal(existsSync(path), false);
  44. newArena(path);
  45. assert.equal(existsSync(join(path, 'sol.cpp')), true);
  46. assert.equal(existsSync(join(path, ATTIC)), true);
  47. assert.equal(existsSync(join(path, ATTIC, 'checker')), true);
  48. assert.equal(existsSync(join(path, TESTCASES)), true);
  49. recRmdir(path);
  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. recRmdir(path);
  65. });
  66. /**
  67. * core::testcasesName
  68. */
  69. test("testcasesName", function(){
  70. let path = join(ARENA, 'exampleContest', 'A');
  71. let result = testcasesName(path);
  72. let target = ["0", "1", "2"];
  73. // TODO: 010
  74. // I want to compare `result` & `target`
  75. target.forEach(name => {assert.notEqual(result.findIndex(tname => { return tname === name; }), -1);});
  76. result.forEach(name => {assert.notEqual(target.findIndex(tname => { return tname === name; }), -1);});
  77. });
  78. /**
  79. * core::newProblem
  80. */
  81. test("newProblemFromId", function(){
  82. let problemId = 'testProblemFromId';
  83. let path = join(ARENA, problemId);
  84. assert.equal(existsSync(path), false);
  85. newProblemFromId(path, 'personal', problemId);
  86. assert.equal(existsSync(join(path, 'sol.cpp')), true);
  87. assert.equal(existsSync(join(path, ATTIC)), true);
  88. assert.equal(existsSync(join(path, TESTCASES)), true);
  89. assert.equal(readdirSync(join(path, TESTCASES)).length, 6);
  90. recRmdir(path);
  91. });
  92. /**
  93. * core::newProblem
  94. */
  95. test("newContestFromId", function(){
  96. let contestId = 'testContestFromId';
  97. let path = join(ARENA, contestId);
  98. assert.equal(existsSync(path), false);
  99. newContestFromId(path, 'personal', 5);
  100. assert.equal(readdirSync(path).length, 5);
  101. recRmdir(path);
  102. });
  103. /**
  104. * core::timedRun
  105. *
  106. * Test running one single test cases, and receiving all different veredicts
  107. */
  108. test("timedRunOk", function() {
  109. let exampleContest = join(ARENA, 'exampleContest');
  110. let problem = join(exampleContest, 'A');
  111. let testcaseId = '0';
  112. let result: TestcaseResult = timedRun(problem, testcaseId);
  113. assert.equal(result.status, Veredict.OK);
  114. });
  115. test("timedRunWA", function() {
  116. let exampleContest = join(ARENA, 'exampleContest');
  117. let problem = join(exampleContest, 'B');
  118. let testcaseId = '0';
  119. let result: TestcaseResult = timedRun(problem, testcaseId);
  120. assert.equal(result.status, Veredict.WA);
  121. });
  122. test("timedRunRTE", function() {
  123. let exampleContest = join(ARENA, 'exampleContest');
  124. let problem = join(exampleContest, 'C');
  125. let testcaseId = '0';
  126. let result: TestcaseResult = timedRun(problem, testcaseId);
  127. assert.equal(result.status, Veredict.RTE);
  128. });
  129. test("timedRunTLE", function() {
  130. let exampleContest = join(ARENA, 'exampleContest');
  131. let problem = join(exampleContest, 'D');
  132. let testcaseId = '0';
  133. let result: TestcaseResult = timedRun(problem, testcaseId, 100);
  134. assert.equal(result.status, Veredict.TLE);
  135. });
  136. /**
  137. * core::testSolution
  138. *
  139. * Test running one single test cases, and receiving all different veredicts
  140. */
  141. test("testSolutionOK", function() {
  142. let exampleContest = join(ARENA, 'exampleContest');
  143. let problem = join(exampleContest, 'A');
  144. let result: TestcaseResult = testSolution(problem);
  145. assert.equal(result.status, Veredict.OK);
  146. });
  147. test("testSolutionCE", function() {
  148. let exampleContest = join(ARENA, 'exampleContest');
  149. let problem = join(exampleContest, 'E');
  150. let result: TestcaseResult = testSolution(problem);
  151. assert.equal(result.status, Veredict.CE);
  152. });
  153. /**
  154. * core::stressSolution
  155. */
  156. test("stressSolutionOK", function() {
  157. let path = join(ARENA, 'testStressOK');
  158. if (existsSync(path)){
  159. recRmdir(path);
  160. }
  161. assert.equal(existsSync(path), false);
  162. newArena(path);
  163. upgradeArena(path);
  164. // populate sol.cpp
  165. writeFile(join(path, "sol.cpp"),
  166. `#include <iostream>\n` +
  167. `\n` +
  168. `using namespace std;\n` +
  169. `\n` +
  170. `int main(){\n` +
  171. ` int n; cin >> n;\n` +
  172. ` cout << n + 2 << endl;\n` +
  173. ` return 0;\n` +
  174. `}\n`
  175. );
  176. // populate brute.cpp
  177. writeFile(join(path, "brute.cpp"),
  178. `#include <iostream>\n` +
  179. `\n` +
  180. `using namespace std;\n` +
  181. `\n` +
  182. `int main(){\n` +
  183. ` int n; cin >> n;\n` +
  184. ` cout << n + 2 << endl;\n` +
  185. ` return 0;\n` +
  186. `}\n`
  187. );
  188. // populate gen.py
  189. writeFile(join(path, ATTIC, 'gen.py'),
  190. `import random\n` +
  191. `print(random.randint(0, 99))\n`
  192. );
  193. let result = stressSolution(path);
  194. assert.equal(result.status, Veredict.OK);
  195. recRmdir(path);
  196. });
  197. test("stressSolutionWA", function() {
  198. let path = join(ARENA, 'testStressWA');
  199. if (existsSync(path)){
  200. recRmdir(path);
  201. }
  202. assert.equal(existsSync(path), false);
  203. newArena(path);
  204. upgradeArena(path);
  205. // populate sol.cpp
  206. writeFile(join(path, "sol.cpp"),
  207. `#include <iostream>\n` +
  208. `\n` +
  209. `using namespace std;\n` +
  210. `\n` +
  211. `int main(){\n` +
  212. ` int n; cin >> n;\n` +
  213. ` cout << n + 3 << endl;\n` +
  214. ` return 0;\n` +
  215. `}\n`
  216. );
  217. // populate brute.cpp
  218. writeFile(join(path, "brute.cpp"),
  219. `#include <iostream>\n` +
  220. `\n` +
  221. `using namespace std;\n` +
  222. `\n` +
  223. `int main(){\n` +
  224. ` int n; cin >> n;\n` +
  225. ` cout << n + 2 << endl;\n` +
  226. ` return 0;\n` +
  227. `}\n`
  228. );
  229. // populate gen.py
  230. writeFile(join(path, ATTIC, 'gen.py'),
  231. `import random\n` +
  232. `print(random.randint(0, 99))\n`
  233. );
  234. let result = stressSolution(path);
  235. assert.equal(result.status, Veredict.WA);
  236. recRmdir(path);
  237. });
  238. function readFile(path: string){
  239. let fd = openSync(path, "r");
  240. let buffer = new Buffer(1 << 20);
  241. readSync(fd, buffer, 0, 1 << 20, 0);
  242. let answer = buffer.toString();
  243. return answer;
  244. }
  245. test("downloading", async function(){
  246. let request = require('request');
  247. // let statusCode = undefined;
  248. await request('http://codeforces.com/contest/1081/problem/E', function(error: any, response: any, body: any){
  249. // let x = request('http://localhost:8000/libros', function(error: any, response: any, body: any) {
  250. // console.log(">>>" + response.statusCode);
  251. // statusCode = response.statusCode;
  252. writeFile(join(SRC, "codeforces.html"), body);
  253. });
  254. // assert.equal(statusCode, 200);
  255. });
  256. test("parsing", function(){
  257. let html: string = readFile(join(__dirname, "codeforces.html"));
  258. let pos = 0;
  259. while (true){
  260. pos = html.indexOf('<div class="title">Input</div>', pos);
  261. if (pos === -1){
  262. break;
  263. }
  264. let begin_pre = html.indexOf('<pre>', pos);
  265. let end_pre = html.indexOf('</pre>', pos);
  266. let inputTestcase = html.substring(begin_pre + 5, end_pre);
  267. while (inputTestcase.indexOf('<br />') !== -1){
  268. inputTestcase = inputTestcase.replace('<br />', '\n');
  269. }
  270. pos = html.indexOf('<div class="title">Output</div>', pos);
  271. begin_pre = html.indexOf('<pre>', pos);
  272. end_pre = html.indexOf('</pre>', pos);
  273. let outputTestcase = html.substring(begin_pre + 5, end_pre);
  274. while (outputTestcase.indexOf('<br />') !== -1){
  275. outputTestcase = outputTestcase.replace('<br />', '\n');
  276. }
  277. }
  278. });
  279. test("parsingLocalhost", function(){
  280. let request = require('request');
  281. request('http://localhost:8000/libros', function (error: any, response: any, body: any) {
  282. // console.log('error:', error);
  283. // console.log('statusCode:', response && response.statusCode);
  284. // console.log('body:', body);
  285. });
  286. });
  287. });