1
0

ivprogAssessment.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. export class IVProgAssessment {
  7. constructor (textCode, testCases, domConsole) {
  8. this.textCode = textCode;
  9. this.testCases = testCases;
  10. this.domConsole = domConsole;
  11. }
  12. runTest () {
  13. return new Promise((resolve, _) => {
  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 processor = new IVProgProcessor(semantic.analyseTree());
  19. const fun = this.partialBindTestCase(this.evaluateTestCase, processor);
  20. // loop test cases and show messages through domconsole
  21. const tests = this.testCases.map( t => fun(t.input, t.output));
  22. Promise.all(tests).then(results => {
  23. const count = results.reduce((p, n) => {
  24. if(n) {
  25. return p + 1;
  26. } else {
  27. return p + 0;
  28. }
  29. },0);
  30. const failed = this.testCases.length - count;
  31. if(failed === 0) {
  32. resolve(1);
  33. } else {
  34. resolve(count / this.testCases.length);
  35. }
  36. }).catch(err => {
  37. this.domConsole.err("Erro durante a execução do programa");// try and show error messages through domconsole
  38. this.domConsole.err(err.message);
  39. resolve(0);
  40. })
  41. } catch (error) {
  42. this.domConsole.err("Erro durante a execução do programa");// try and show error messages through domconsole
  43. this.domConsole.err(error.message);
  44. resolve(0);
  45. return;
  46. }
  47. });
  48. }
  49. evaluateTestCase (prog, inputList, outputList) {
  50. return new Promise((resolve, _) => {
  51. const input = new InputTest(inputList);
  52. const output = new OutputTest();
  53. prog.registerInput(input);
  54. prog.registerOutput(output);
  55. prog.interpretAST().then( _ => {
  56. if (input.inputList.length !== 0 ||
  57. output.list.length !== outputList.length) {
  58. this.domConsole.err(`Caso de teste ${i + 1} falhou!`);
  59. } else {
  60. const isOk = this.checkOutput(output.list, outputList);
  61. if(!isOk) {
  62. this.domConsole.err(`Caso de teste ${i + 1} falhou!`);
  63. resolve(false);
  64. } else {
  65. this.domConsole.info(`Caso de teste ${i + 1} passou!`);
  66. resolve(true);
  67. }
  68. }
  69. })
  70. });
  71. }
  72. partialBindTestCase (fun, param) {
  73. return (i, o) => fun(param, i, o);
  74. }
  75. checkOutput (aList, bList) {
  76. for (let i = 0; i < aList.length; i++) {
  77. const outValue = aList[i];
  78. if(outValue !== bList[i]) {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. }