ivprogAssessment.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { Decimal } from 'decimal.js';
  2. import line_i18n from 'line-i18n'
  3. import { SemanticAnalyser } from "./../processor/semantic/semanticAnalyser";
  4. import { IVProgProcessor } from "./../processor/ivprogProcessor";
  5. import { InputTest } from "./../util/inputTest";
  6. import { OutputTest } from "./../util/outputTest";
  7. import { DOMConsole} from "./../io/domConsole";
  8. import * as LocalizedStringsService from "../services/localizedStringsService";
  9. import { Config } from "../util/config";
  10. const LocalizedStrings = LocalizedStringsService.getInstance();
  11. const StringTypes = line_i18n.StringTypes;
  12. export class IVProgAssessment {
  13. constructor (textCode, testCases, domConsole) {
  14. this.textCode = textCode;
  15. this.testCases = testCases;
  16. this.domConsole = domConsole;
  17. }
  18. runTest () {
  19. try {
  20. const validTree = SemanticAnalyser.analyseFromSource(this.textCode);
  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 => {
  27. const grade = total / this.testCases.length;
  28. const channel = grade == 1 ? DOMConsole.INFO : DOMConsole.ERR;
  29. this.writeToConsole(channel, StringTypes.MESSAGE, "test_suite_grade", grade * 100);
  30. return Promise.resolve(grade)
  31. }).catch(err => {
  32. this.domConsole.err("Erro inesperado durante o cálculo da nota.");// try and show error messages through domconsole
  33. this.domConsole.err(err.message);
  34. return Promise.resolve(0);
  35. });
  36. } catch (error) {
  37. this.domConsole.err("Erro inesperado durante a execução do programa");// try and show error messages through domconsole
  38. this.domConsole.err(error.message);
  39. return Promise.resolve(0);
  40. }
  41. }
  42. evaluateTestCase (prog, inputList, expectedOutputs, name, accumulator) {
  43. const outerThis = this;
  44. const input = new InputTest(inputList);
  45. const output = new OutputTest();
  46. prog.registerInput(input);
  47. prog.registerOutput(output);
  48. const startTime = Date.now()
  49. return prog.interpretAST().then( _ => {
  50. const millis = Date.now() - startTime;
  51. if (input.inputList.length !== input.index) {
  52. outerThis.showErrorMessage('test_case_few_reads', name+1);
  53. outerThis.showInfoMessage('test_case_duration', millis);
  54. return Promise.resolve(accumulator + (input.index/inputList.length));
  55. } else if (output.list.length != expectedOutputs.length) {
  56. outerThis.showErrorMessage('test_case_failed', name + 1, inputList.join(','),
  57. expectedOutputs.join(','), output.list.join(','));
  58. outerThis.showInfoMessage('test_case_duration', millis);
  59. // must check for a partial match of the generated output
  60. const numMatchedOutputs = output.list.reduce((acc, actualOutput, index) => {
  61. if(outerThis.checkOutputValues(actualOutput, expectedOutputs[index])) {
  62. return acc + 1;
  63. } else {
  64. return acc;
  65. }
  66. }, 0);
  67. const maxLength = Math.max(expectedOutputs.length, output.list.length);
  68. return Promise.resolve(accumulator + (numMatchedOutputs/maxLength));
  69. } else {
  70. const isOk = outerThis.checkOutputLists(output.list, expectedOutputs);
  71. if(!isOk) {
  72. console.log("not ok.");
  73. outerThis.showErrorMessage('test_case_failed', name + 1, inputList.join(','),
  74. expectedOutputs.join(','), output.list.join(','));
  75. outerThis.showInfoMessage('test_case_duration', millis);
  76. return Promise.resolve(accumulator);
  77. } else {
  78. outerThis.showInfoMessage('test_case_success', name + 1);
  79. outerThis.showInfoMessage('test_case_duration', millis);
  80. return Promise.resolve(accumulator + 1);
  81. }
  82. }
  83. }).catch( error => {
  84. outerThis.showErrorMessage('test_case_failed_exception', name + 1, error.message);
  85. return Promise.resolve(accumulator);
  86. });
  87. }
  88. partialEvaluateTestCase (prog, inputList, expectedOutputs, name) {
  89. return this.evaluateTestCase.bind(this, prog, inputList, expectedOutputs, name);
  90. }
  91. checkOutputLists (actualOutputs, expectedOutputs) {
  92. for (let i = 0; i < actualOutputs.length; i++) {
  93. const outValue = actualOutputs[i];
  94. const expectedValue = expectedOutputs[i];
  95. if(!this.checkOutputValues(outValue, expectedValue)) {
  96. return false;
  97. }
  98. }
  99. return true;
  100. }
  101. checkOutputValues (actualValue, expectedValue) {
  102. let castNumberA = parseFloat(actualValue);
  103. if(!Number.isNaN(castNumberA)) {
  104. let castNumberB = parseFloat(expectedValue);
  105. if(Number.isNaN(castNumberB)) {
  106. return false;
  107. }
  108. castNumberA = new Decimal(castNumberA);
  109. castNumberB = new Decimal(castNumberB);
  110. const decimalPlaces = Math.min(castNumberB.dp(), Config.decimalPlaces);
  111. castNumberA = new Decimal(castNumberA.toFixed(decimalPlaces, Decimal.ROUND_FLOOR));
  112. castNumberB = new Decimal(castNumberB.toFixed(decimalPlaces, Decimal.ROUND_FLOOR));
  113. const aEqualsB = castNumberA.eq(castNumberB);
  114. if (!aEqualsB) {
  115. return false;
  116. }
  117. } else if(actualValue != expectedValue) {
  118. return false;
  119. }
  120. return true;
  121. }
  122. showErrorMessage (errorID, ...args) {
  123. this.domConsole.err(LocalizedStrings.getError(errorID, args));
  124. }
  125. showInfoMessage (msgID, ...args) {
  126. this.domConsole.info(LocalizedStrings.getMessage(msgID, args));
  127. }
  128. writeToConsole (channel, msgType, msgID, ...args) {
  129. let msg = LocalizedStrings.getString(msgID, msgType);
  130. msg = LocalizedStrings.processString(msg, args);
  131. switch(channel) {
  132. case DOMConsole.ERR: {
  133. this.domConsole.err(msg);
  134. break;
  135. }
  136. case DOMConsole.INFO: {
  137. this.domConsole.info(msg);
  138. break;
  139. }
  140. case DOMConsole.USER: {
  141. this.domConsole.write(msg);
  142. break;
  143. }
  144. }
  145. }
  146. }