123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- //
- // Note: This example test is leveraging the Mocha test framework.
- // Please refer to their documentation on https://mochajs.org/ for help.
- //
- // The module 'assert' provides assertion methods from node
- import * as assert from 'assert';
- import { dirname, join } from 'path';
- import { timedRun, testcasesName, testSolution, newArena, ATTIC, TESTCASES, upgradeArena, stressSolution, newProblemFromId, newContestFromId } from '../core';
- import { TestcaseResult, Veredict } from '../types';
- import { rmdirSync, existsSync, readdirSync, unlinkSync, openSync, writeSync, closeSync } from 'fs';
- const SRC = join(dirname(dirname(dirname(__filename))), 'src', 'test');
- const ARENA = join(SRC, 'arena');
- suite("Extension Tests", function () {
- /**
- * Recursive remove
- */
- function recRmdir(path: string){
- readdirSync(path).forEach(name => {
- let cPath = join(path, name);
- try{
- unlinkSync(cPath);
- }
- catch(err){
- recRmdir(cPath);
- }
- });
- rmdirSync(path);
- }
- function writeFile(path: string, content: string){
- let currentFd = openSync(path, 'w');
- writeSync(currentFd, content);
- closeSync(currentFd);
- }
- // Defines a Mocha unit test
- test("learnjs", function() {
- });
- /**
- * core::newArena
- */
- test("newArena", function(){
- let path = join(ARENA, "testNew");
- if (existsSync(path)){
- recRmdir(path);
- }
- assert.equal(existsSync(path), false);
- newArena(path);
- assert.equal(existsSync(join(path, ATTIC)), true);
- assert.equal(existsSync(join(path, TESTCASES)), true);
- assert.equal(existsSync(join(path, 'sol.cpp')), true);
- });
- /**
- * core::upgradeArena
- */
- test("upgradeArena", function(){
- let path = join(ARENA, "testUpgrade");
- if (existsSync(path)){
- recRmdir(path);
- }
- assert.equal(existsSync(path), false);
- newArena(path);
- upgradeArena(path);
- assert.equal(existsSync(join(path, ATTIC, 'gen.py')), true);
- assert.equal(existsSync(join(path, 'brute.cpp')), true);
- });
- /**
- * core::testcasesName
- */
- test("testcasesName", function(){
- let path = join(ARENA, 'exampleContest', 'A');
- let result = testcasesName(path);
- let target = ["0", "1", "2"];
- // TODO: How to check if two arrays are equal
- // I want to compare `result` & `target`
- target.forEach(name => {assert.notEqual(result.findIndex(tname => { return tname === name; }), -1);});
- result.forEach(name => {assert.notEqual(target.findIndex(tname => { return tname === name; }), -1);});
- });
- /**
- * core::newProblem
- */
- test("newProblemFromId", function(){
- let path = join(ARENA);
- let problemId = 'testProblemFromId';
- newProblemFromId(join(path, problemId), 'personal', problemId);
- assert.equal(existsSync(join(path, problemId, 'sol.cpp')), true);
- assert.equal(existsSync(join(path, problemId, ATTIC)), true);
- assert.equal(existsSync(join(path, problemId, TESTCASES)), true);
- assert.equal(readdirSync(join(path, problemId, TESTCASES)).length, 6);
- });
- /**
- * core::newProblem
- */
- test("newContestFromId", function(){
- let path = join(ARENA);
- let contestId = 'testContestFromId';
- newContestFromId(join(path, contestId), 'personal', 5);
- assert.equal(readdirSync(join(path, contestId)).length, 5);
- });
- /**
- * core::timedRun
- *
- * Test running one single test cases, and receiving all different veredicts
- */
- test("timedRunOk", function() {
- let exampleContest = join(ARENA, 'exampleContest');
- let problem = join(exampleContest, 'A');
- let testcaseId = '0';
- let result: TestcaseResult = timedRun(problem, testcaseId);
- assert.equal(result.status, Veredict.OK);
- });
- test("timedRunWA", function() {
- let exampleContest = join(ARENA, 'exampleContest');
- let problem = join(exampleContest, 'B');
- let testcaseId = '0';
- let result: TestcaseResult = timedRun(problem, testcaseId);
- assert.equal(result.status, Veredict.WA);
- });
- test("timedRunRTE", function() {
- let exampleContest = join(ARENA, 'exampleContest');
- let problem = join(exampleContest, 'C');
- let testcaseId = '0';
- let result: TestcaseResult = timedRun(problem, testcaseId);
- assert.equal(result.status, Veredict.RTE);
- });
- test("timedRunTLE", function() {
- let exampleContest = join(ARENA, 'exampleContest');
- let problem = join(exampleContest, 'D');
- let testcaseId = '0';
- let result: TestcaseResult = timedRun(problem, testcaseId, 100);
- assert.equal(result.status, Veredict.TLE);
- });
- /**
- * core::testSolution
- *
- * Test running one single test cases, and receiving all different veredicts
- */
- test("testSolutionOK", function() {
- let exampleContest = join(ARENA, 'exampleContest');
- let problem = join(exampleContest, 'A');
- let result: TestcaseResult = testSolution(problem);
- assert.equal(result.status, Veredict.OK);
- });
- test("testSolutionCE", function() {
- let exampleContest = join(ARENA, 'exampleContest');
- let problem = join(exampleContest, 'E');
- let result: TestcaseResult = testSolution(problem);
- assert.equal(result.status, Veredict.CE);
- });
- /**
- * core::stressSolution
- */
- test("stressSolutionOK", function() {
- let path = join(ARENA, 'testStressOK');
- if (existsSync(path)){
- recRmdir(path);
- }
- assert.equal(existsSync(path), false);
- newArena(path);
- upgradeArena(path);
- // populate sol.cpp
- writeFile(join(path, "sol.cpp"),
- `#include <iostream>\n` +
- `\n` +
- `using namespace std;\n` +
- `\n` +
- `int main(){\n` +
- ` int n; cin >> n;\n` +
- ` cout << n + 2 << endl;\n` +
- ` return 0;\n` +
- `}\n`
- );
- // populate brute.cpp
- writeFile(join(path, "brute.cpp"),
- `#include <iostream>\n` +
- `\n` +
- `using namespace std;\n` +
- `\n` +
- `int main(){\n` +
- ` int n; cin >> n;\n` +
- ` cout << n + 2 << endl;\n` +
- ` return 0;\n` +
- `}\n`
- );
- // populate gen.py
- writeFile(join(path, ATTIC, 'gen.py'),
- `import random\n` +
- `print(random.randint(0, 99))\n`
- );
- let result = stressSolution(path);
- assert.equal(result.status, Veredict.OK);
- });
- test("stressSolutionWA", function() {
- let path = join(ARENA, 'testStressWA');
- if (existsSync(path)){
- recRmdir(path);
- }
- assert.equal(existsSync(path), false);
- newArena(path);
- upgradeArena(path);
- // populate sol.cpp
- writeFile(join(path, "sol.cpp"),
- `#include <iostream>\n` +
- `\n` +
- `using namespace std;\n` +
- `\n` +
- `int main(){\n` +
- ` int n; cin >> n;\n` +
- ` cout << n + 3 << endl;\n` +
- ` return 0;\n` +
- `}\n`
- );
- // populate brute.cpp
- writeFile(join(path, "brute.cpp"),
- `#include <iostream>\n` +
- `\n` +
- `using namespace std;\n` +
- `\n` +
- `int main(){\n` +
- ` int n; cin >> n;\n` +
- ` cout << n + 2 << endl;\n` +
- ` return 0;\n` +
- `}\n`
- );
- // populate gen.py
- writeFile(join(path, ATTIC, 'gen.py'),
- `import random\n` +
- `print(random.randint(0, 99))\n`
- );
- let result = stressSolution(path);
- assert.equal(result.status, Veredict.WA);
- });
- });
|