1
0

ivprogAssessment.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { IVProgParser } from "./../ast/ivprogParser";
  2. import { SemanticAnalyser } from "./../processor/semantic/semanticAnalyser";
  3. import { IVProgProcessor } from "./../processor/ivprogProcessor";
  4. import { InputTest } from "./../util/inputTest";
  5. import { OutputTest } from "./../util/outputTest";
  6. export class IVProgAssessment {
  7. constructor (textCode, testCases, domConsole) {
  8. this.textCode = textCode;
  9. this.testCases = testCases;
  10. this.domConsole = domConsole;
  11. }
  12. runTest () {
  13. try {
  14. // try and show error messages through domconsole
  15. const parser = IVProgParser.createParser(this.textCode);
  16. const semantic = new SemanticAnalyser(parser.parseTree());
  17. const validTree = semantic.analyseTree();
  18. // loop test cases and show messages through domconsole
  19. const partialTests = this.testCases.map( (t, name) => {
  20. return this.partialEvaluateTestCase(new IVProgProcessor(validTree), t.input, t.output, name);
  21. });
  22. const testResult = partialTests.reduce((acc, curr) => acc.then(curr), Promise.resolve(0));
  23. return testResult.then(total => Promise.resolve(total / this.testCases.length))
  24. .catch(err => {
  25. this.domConsole.err("Erro durante a execução do programa");// try and show error messages through domconsole
  26. this.domConsole.err(err.message);
  27. return Promise.resolve(0);
  28. });
  29. } catch (error) {
  30. this.domConsole.err("Erro durante a execução do programa");// try and show error messages through domconsole
  31. this.domConsole.err(error.message);
  32. return Promise.resolve(0);
  33. }
  34. }
  35. evaluateTestCase (prog, inputList, outputList, name, accumulator) {
  36. const outerThis = this;
  37. const input = new InputTest(inputList);
  38. const output = new OutputTest();
  39. prog.registerInput(input);
  40. prog.registerOutput(output);
  41. const startTime = Date.now()
  42. return prog.interpretAST().then( _ => {
  43. const millis = Date.now() - startTime;
  44. if (input.inputList.length !== input.index) {
  45. outerThis.domConsole.err(`Caso de teste ${name + 1}: Falhou, ainda restam entradas!`);
  46. outerThis.domConsole.info(`Levou ${millis}ms`);
  47. return Promise.resolve(accumulator + 1 * (input.index/inputList.length));
  48. } else if (output.list.length < outputList.length) {
  49. outerThis.domConsole.err(`Caso de teste ${name + 1}: Falhou <${inputList.join(", ")};${outputList.join(", ")};${output.list.join(", ")}>`);
  50. outerThis.domConsole.info(`Levou ${millis}ms`);
  51. return Promise.resolve(accumulator + 1 * (output.list.length/outputList.length));
  52. } else if (output.list.length > outputList.length) {
  53. outerThis.domConsole.err(`Caso de teste ${name + 1}: Falhou <${inputList.join(", ")};${outputList.join(", ")};${output.list.join(", ")}>`);
  54. outerThis.domConsole.info(`Levou ${millis}ms`);
  55. return Promise.resolve(accumulator + 1 * (outputList.length/output.list.length));
  56. } else {
  57. const isOk = outerThis.checkOutput(output.list, outputList);
  58. if(!isOk) {
  59. outerThis.domConsole.err(`Caso de teste ${name + 1}: Falhou <${inputList.join(", ")};${outputList.join(", ")};${output.list.join(", ")}>`);
  60. outerThis.domConsole.info(`Levou ${millis}ms`);
  61. return Promise.resolve(accumulator);
  62. } else {
  63. outerThis.domConsole.info(`Caso de teste ${name + 1}: OK!`);
  64. outerThis.domConsole.info(`Levou ${millis}ms`);
  65. return Promise.resolve(accumulator + 1);
  66. }
  67. }
  68. }).catch( error => {
  69. this.domConsole.err(`Execução do caso de teste ${name + 1} falhou!`);// try and show error messages through domconsole
  70. this.domConsole.err(error.message);
  71. return Promise.resolve(accumulator);
  72. });
  73. }
  74. partialEvaluateTestCase (prog, inputList, outputList, name) {
  75. return this.evaluateTestCase.bind(this, prog, inputList, outputList, name);
  76. }
  77. checkOutput (aList, bList) {
  78. for (let i = 0; i < aList.length; i++) {
  79. const outValue = aList[i];
  80. if(outValue != bList[i]) {
  81. return false;
  82. }
  83. }
  84. return true;
  85. }
  86. }