ivprogAssessment.js 4.3 KB

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