ivprogAssessment.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. let success = 0;
  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. // loop test cases and show messages through domconsole
  20. for (let i = 0; i < this.testCases.length; i++) {
  21. const testCase = this.testCases[i];
  22. const input = new InputTest(testCase.input);
  23. const output = new OutputTest();
  24. processor.registerInput(input);
  25. processor.registerOutput(output);
  26. processor.interpretAST();
  27. if (input.inputList.length !== 0 ||
  28. output.list.length !== testCase.output.length) {
  29. this.domConsole.err(`Caso de teste ${i+1} falhou!`);
  30. } else {
  31. const isOk = this.checkOutput(output.list, testCase.output);
  32. if(!isOk) {
  33. this.domConsole.err(`Caso de teste ${i+1} falhou!`);
  34. } else {
  35. this.domConsole.info(`Caso de teste ${i+1} passou!`);
  36. success++;
  37. }
  38. }
  39. }
  40. } catch (error) {
  41. this.domConsole.err("Erro durante a execução do programa");// try and show error messages through domconsole
  42. this.domConsole.err(error.message);
  43. return 0;
  44. }
  45. const failed = this.testCases.length - success;
  46. if(failed === 0) {
  47. return 1;
  48. } else {
  49. return success / this.testCases.length;
  50. }
  51. }
  52. checkOutput (aList, bList) {
  53. for (let i = 0; i < aList.length; i++) {
  54. const outValue = aList[i];
  55. if(outValue !== bList[i]) {
  56. return false;
  57. }
  58. }
  59. return true;
  60. }
  61. }