ivprogAssessment.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. import { Decimal } from 'decimal.js';
  8. import { Config } from "../util/config";
  9. export class IVProgAssessment {
  10. constructor (textCode, testCases, domConsole) {
  11. this.textCode = textCode;
  12. this.testCases = testCases;
  13. this.domConsole = domConsole;
  14. }
  15. runTest () {
  16. try {
  17. // try and show error messages through domconsole
  18. const parser = IVProgParser.createParser(this.textCode);
  19. const semantic = new SemanticAnalyser(parser.parseTree());
  20. const validTree = semantic.analyseTree();
  21. // loop test cases and show messages through domconsole
  22. const partialTests = this.testCases.map( (t, name) => {
  23. return this.partialEvaluateTestCase(new IVProgProcessor(validTree), t.input, t.output, name);
  24. });
  25. const testResult = partialTests.reduce((acc, curr) => acc.then(curr), Promise.resolve(0));
  26. return testResult.then(total => Promise.resolve(total / this.testCases.length))
  27. .catch(err => {
  28. this.domConsole.err("Erro durante a execução do programa");// try and show error messages through domconsole
  29. this.domConsole.err(err.message);
  30. return Promise.resolve(0);
  31. });
  32. } catch (error) {
  33. this.domConsole.err("Erro durante a execução do programa");// try and show error messages through domconsole
  34. this.domConsole.err(error.message);
  35. return Promise.resolve(0);
  36. }
  37. }
  38. evaluateTestCase (prog, inputList, outputList, name, accumulator) {
  39. const outerThis = this;
  40. const input = new InputTest(inputList);
  41. const output = new OutputTest();
  42. prog.registerInput(input);
  43. prog.registerOutput(output);
  44. const startTime = Date.now()
  45. return prog.interpretAST().then( _ => {
  46. const millis = Date.now() - startTime;
  47. if (input.inputList.length !== input.index) {
  48. outerThis.showErrorMessage('test_case_few_reads', name+1);
  49. outerThis.showMessage('test_case_duration', millis);
  50. return Promise.resolve(accumulator + 1 * (input.index/inputList.length));
  51. } else if (output.list.length < outputList.length) {
  52. outerThis.showErrorMessage('test_case_failed', name + 1, inputList.join(','),
  53. outputList.join(','), output.list.join(','));
  54. outerThis.showMessage('test_case_duration', millis);
  55. return Promise.resolve(accumulator + 1 * (output.list.length/outputList.length));
  56. } else if (output.list.length > outputList.length) {
  57. outerThis.showErrorMessage('test_case_failed', name + 1, inputList.join(','),
  58. outputList.join(','), output.list.join(','));
  59. outerThis.showMessage('test_case_duration', millis);
  60. return Promise.resolve(accumulator + 1 * (outputList.length/output.list.length));
  61. } else {
  62. const isOk = outerThis.checkOutput(output.list, outputList);
  63. if(!isOk) {
  64. outerThis.showErrorMessage('test_case_failed', name + 1, inputList.join(','),
  65. outputList.join(','), output.list.join(','));
  66. outerThis.showMessage('test_case_duration', millis);
  67. return Promise.resolve(accumulator);
  68. } else {
  69. outerThis.showMessage('test_case_success', name + 1);
  70. outerThis.showMessage('test_case_duration', millis);
  71. return Promise.resolve(accumulator + 1);
  72. }
  73. }
  74. }).catch( error => {
  75. outerThis.showErrorMessage('test_case_failed_exception', name + 1, 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. let castNumberA = parseFloat(outValue);
  86. if(!Number.isNaN(castNumberA)) {
  87. let castNumberB = parseFloat(bList[i]);
  88. if(Number.isNaN(castNumberB)) {
  89. return false;
  90. }
  91. castNumberA = new Decimal(castNumberA);
  92. castNumberB = new Decimal(castNumberB);
  93. const decimalPlaces = Math.min(castNumberB.dp(), Config.decimalPlaces);
  94. Decimal.set({ rounding: Decimal.ROUND_FLOOR});
  95. castNumberA = new Decimal(castNumberA.toFixed(decimalPlaces));
  96. castNumberB = new Decimal(castNumberB.toFixed(decimalPlaces));
  97. const aEqualsB = castNumberA.eq(castNumberB);
  98. Decimal.set({ rounding: Decimal.ROUND_HALF_UP});
  99. if (!aEqualsB) {
  100. return false;
  101. }
  102. } else if(outValue != bList[i]) {
  103. return false;
  104. }
  105. }
  106. return true;
  107. }
  108. showErrorMessage (errorID, ...args) {
  109. this.domConsole.err(LocalizedStrings.getError(errorID, args));
  110. }
  111. showMessage (msgID, ...args) {
  112. this.domConsole.info(LocalizedStrings.getMessage(msgID, args));
  113. }
  114. }