1
0

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 list_joiner = LocalizedStrings.getUI("text_join_assessment_outputs");
  12. const StringTypes = line_i18n.StringTypes;
  13. export class IVProgAssessment {
  14. constructor (textCode, testCases, domConsole) {
  15. this.textCode = textCode;
  16. this.testCases = testCases;
  17. this.domConsole = domConsole;
  18. }
  19. runTest () {
  20. const outerRef = this
  21. try {
  22. const validTree = SemanticAnalyser.analyseFromSource(this.textCode);
  23. // loop test cases and show messages through domconsole
  24. const partialTests = this.testCases.map( (t, name) => {
  25. return outerRef.partialEvaluateTestCase(new IVProgProcessor(validTree), t.input, t.output, name);
  26. });
  27. const testResult = partialTests.reduce((acc, curr) => acc.then(curr), Promise.resolve(0));
  28. return testResult.then(function (total) {
  29. const grade = total / outerRef.testCases.length;
  30. const channel = grade == 1 ? DOMConsole.INFO : DOMConsole.ERR;
  31. outerRef.writeToConsole(channel, StringTypes.MESSAGE, "test_suite_grade", (grade * 100).toFixed(2));
  32. return Promise.resolve(grade)
  33. }).catch(err => {
  34. outerRef.domConsole.err("Erro inesperado durante o cálculo da nota.");// try and show error messages through domconsole
  35. outerRef.domConsole.err(err.message);
  36. return Promise.resolve(0);
  37. });
  38. } catch (error) {
  39. outerRef.domConsole.err("Erro inesperado durante a execução do programa");// try and show error messages through domconsole
  40. outerRef.domConsole.err(error.message);
  41. return Promise.resolve(0);
  42. }
  43. }
  44. evaluateTestCase (prog, inputList, expectedOutputs, name, accumulator) {
  45. const outerThis = this;
  46. const input = new InputTest(inputList);
  47. const output = new OutputTest();
  48. prog.registerInput(input);
  49. prog.registerOutput(output);
  50. const startTime = Date.now()
  51. return prog.interpretAST().then( _ => {
  52. const millis = Date.now() - startTime;
  53. if (input.inputList.length !== input.index) {
  54. outerThis.showErrorMessage('test_case_few_reads', name+1);
  55. outerThis.showInfoMessage('test_case_duration', millis);
  56. return Promise.resolve(accumulator);
  57. } else if (output.list.length != expectedOutputs.length) {
  58. outerThis.showErrorMessage('test_case_failed', name + 1, inputList.join(list_joiner),
  59. expectedOutputs.join(list_joiner), output.list.join(list_joiner));
  60. outerThis.showInfoMessage('test_case_duration', millis);
  61. // must check for a partial match of the generated output
  62. const numMatchedOutputs = output.list.reduce((acc, actualOutput, index) => {
  63. if(outerThis.checkOutputValues(actualOutput, expectedOutputs[index])) {
  64. return acc + 1;
  65. } else {
  66. return acc;
  67. }
  68. }, 0);
  69. const maxLength = Math.max(expectedOutputs.length, output.list.length);
  70. return Promise.resolve(accumulator + (numMatchedOutputs/maxLength));
  71. } else {
  72. const isOk = outerThis.checkOutputLists(output.list, expectedOutputs);
  73. if(!isOk) {
  74. outerThis.showErrorMessage('test_case_failed', name + 1, inputList.join(list_joiner),
  75. expectedOutputs.join(list_joiner), output.list.join(list_joiner));
  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. }