ivprogAssessment.js 6.1 KB

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