ivprogAssessment.js 4.3 KB

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