1
0

ivprogAssessment.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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( v => curr(v)), 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( _ => Promise.resolve(accumulator));
  69. }
  70. partialEvaluateTestCase (prog, inputList, outputList, name) {
  71. let partial = (accumulator) => this.evaluateTestCase(prog, inputList, outputList, name, accumulator)
  72. partial = partial.bind(this);
  73. return partial;
  74. }
  75. checkOutput (aList, bList) {
  76. for (let i = 0; i < aList.length; i++) {
  77. const outValue = aList[i];
  78. if(outValue != bList[i]) {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. }