123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import { IVProgParser } from "./../ast/ivprogParser";
- import { SemanticAnalyser } from "./../processor/semantic/semanticAnalyser";
- import { IVProgProcessor } from "./../processor/ivprogProcessor";
- import { InputTest } from "./../util/inputTest";
- import { OutputTest } from "./../util/outputTest";
- export class IVProgAssessment {
- constructor (textCode, testCases, domConsole) {
- this.textCode = textCode;
- this.testCases = testCases;
- this.domConsole = domConsole;
- }
- runTest () {
- const outerRef = this;
- return new Promise((resolve, _) => {
- try {
- // try and show error messages through domconsole
- const parser = IVProgParser.createParser(outerRef.textCode);
- const semantic = new SemanticAnalyser(parser.parseTree());
- const validTree = semantic.analyseTree();
- // loop test cases and show messages through domconsole
- const tests = outerRef.testCases.map( (t, name) => {
- return outerRef.evaluateTestCase(new IVProgProcessor(validTree), t.input, t.output, name);
- });
- Promise.all(tests).then(results => {
- const count = results.reduce((lastValue, nextValue) => lastValue + nextValue, 0);
- resolve(count / outerRef.testCases.length);
- }).catch(err => {
- outerRef.domConsole.err("Erro durante a execução do programa");// try and show error messages through domconsole
- outerRef.domConsole.err(err.message);
- resolve(0);
- })
- } catch (error) {
- outerRef.domConsole.err("Erro durante a execução do programa");// try and show error messages through domconsole
- outerRef.domConsole.err(error.message);
- resolve(0);
- }
- });
- }
- evaluateTestCase (prog, inputList, outputList, name) {
- const outerThis = this;
- return new Promise((resolve, reject) => {
- const input = new InputTest(inputList);
- const output = new OutputTest();
- prog.registerInput(input);
- prog.registerOutput(output);
- const startTime = Date.now()
- prog.interpretAST().then( _ => {
- const millis = Date.now() - startTime;
- if (input.inputList.length !== input.index) {
- outerThis.domConsole.err(`Caso de teste ${name + 1}: Falhou, ainda restam entradas!`);
- outerThis.domConsole.info(`Levou ${millis}ms`);
- resolve(1 * (input.index/inputList.length));
- } else if (output.list.length < outputList.length) {
- outerThis.domConsole.err(`Caso de teste ${name + 1}: Falhou <${inputList.join(", ")};${outputList.join(", ")};${output.list.join(", ")}>`);
- outerThis.domConsole.info(`Levou ${millis}ms`);
- resolve(1 * (output.list.length/outputList.length));
- } else if (output.list.length > outputList.length) {
- outerThis.domConsole.err(`Caso de teste ${name + 1}: Falhou <${inputList.join(", ")};${outputList.join(", ")};${output.list.join(", ")}>`);
- outerThis.domConsole.info(`Levou ${millis}ms`);
- resolve(1 * (outputList.length/output.list.length));
- } else {
- const isOk = outerThis.checkOutput(output.list, outputList);
- if(!isOk) {
- outerThis.domConsole.err(`Caso de teste ${name + 1}: Falhou <${inputList.join(", ")};${outputList.join(", ")};${output.list.join(", ")}>`);
- outerThis.domConsole.info(`Levou ${millis}ms`);
- resolve(0);
- } else {
- outerThis.domConsole.info(`Caso de teste ${name + 1}: OK!`);
- outerThis.domConsole.info(`Levou ${millis}ms`);
- resolve(1);
- }
- }
- }).catch( _ => resolve(0));
- })
- }
- checkOutput (aList, bList) {
- for (let i = 0; i < aList.length; i++) {
- const outValue = aList[i];
- if(outValue != bList[i]) {
- return false;
- }
- }
- return true;
- }
- }
|