ivprogAssessment.js 3.8 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. const outerRef = this;
  14. return new Promise((resolve, _) => {
  15. try {
  16. // try and show error messages through domconsole
  17. const parser = IVProgParser.createParser(outerRef.textCode);
  18. const semantic = new SemanticAnalyser(parser.parseTree());
  19. const validTree = semantic.analyseTree();
  20. // loop test cases and show messages through domconsole
  21. const tests = outerRef.testCases.map( (t, name) => {
  22. return outerRef.evaluateTestCase(new IVProgProcessor(validTree), t.input, t.output, name);
  23. });
  24. Promise.all(tests).then(results => {
  25. const count = results.reduce((lastValue, nextValue) => lastValue + nextValue, 0);
  26. resolve(count / outerRef.testCases.length);
  27. }).catch(err => {
  28. outerRef.domConsole.err("Erro durante a execução do programa");// try and show error messages through domconsole
  29. outerRef.domConsole.err(err.message);
  30. resolve(0);
  31. })
  32. } catch (error) {
  33. outerRef.domConsole.err("Erro durante a execução do programa");// try and show error messages through domconsole
  34. outerRef.domConsole.err(error.message);
  35. resolve(0);
  36. }
  37. });
  38. }
  39. evaluateTestCase (prog, inputList, outputList, name) {
  40. const outerThis = this;
  41. return new Promise((resolve, reject) => {
  42. const input = new InputTest(inputList);
  43. const output = new OutputTest();
  44. prog.registerInput(input);
  45. prog.registerOutput(output);
  46. const startTime = Date.now()
  47. prog.interpretAST().then( _ => {
  48. const millis = Date.now() - startTime;
  49. if (input.inputList.length !== input.index) {
  50. outerThis.domConsole.err(`Caso de teste ${name + 1}: Falhou, ainda restam entradas!`);
  51. outerThis.domConsole.info(`Levou ${millis}ms`);
  52. resolve(1 * (input.index/inputList.length));
  53. } else if (output.list.length < outputList.length) {
  54. outerThis.domConsole.err(`Caso de teste ${name + 1}: Falhou <${inputList.join(", ")};${outputList.join(", ")};${output.list.join(", ")}>`);
  55. outerThis.domConsole.info(`Levou ${millis}ms`);
  56. resolve(1 * (output.list.length/outputList.length));
  57. } else if (output.list.length > outputList.length) {
  58. outerThis.domConsole.err(`Caso de teste ${name + 1}: Falhou <${inputList.join(", ")};${outputList.join(", ")};${output.list.join(", ")}>`);
  59. outerThis.domConsole.info(`Levou ${millis}ms`);
  60. resolve(1 * (outputList.length/output.list.length));
  61. } else {
  62. const isOk = outerThis.checkOutput(output.list, outputList);
  63. if(!isOk) {
  64. outerThis.domConsole.err(`Caso de teste ${name + 1}: Falhou <${inputList.join(", ")};${outputList.join(", ")};${output.list.join(", ")}>`);
  65. outerThis.domConsole.info(`Levou ${millis}ms`);
  66. resolve(0);
  67. } else {
  68. outerThis.domConsole.info(`Caso de teste ${name + 1}: OK!`);
  69. outerThis.domConsole.info(`Levou ${millis}ms`);
  70. resolve(1);
  71. }
  72. }
  73. }).catch( _ => resolve(0));
  74. })
  75. }
  76. checkOutput (aList, bList) {
  77. for (let i = 0; i < aList.length; i++) {
  78. const outValue = aList[i];
  79. if(outValue != bList[i]) {
  80. return false;
  81. }
  82. }
  83. return true;
  84. }
  85. }