iassignHelpers.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { setTestCases, getTestCases } from "../visualUI/functions";
  2. import { generate } from "../visualUI/code_generator";
  3. import { IVProgAssessment } from "../assessment/ivprogAssessment";
  4. import { TestConsole } from "./testConsole";
  5. import { parseLogs } from "./../services/userLog";
  6. function parseActivityData (data) {
  7. let algorithm_in_ilm = null;
  8. if (data.split('\n::algorithm::')[1]) {
  9. algorithm_in_ilm = data.split('\n::algorithm::')[1].split('\n::logs::')[0];
  10. const logs = data.split('\n::algorithm::')[1].split('\n::logs::')[1];
  11. if (logs != null) {
  12. parseLogs(logs);
  13. }
  14. }
  15. let content = JSON.parse(data.split('\n::algorithm::')[0]);
  16. content['algorithm_in_ilm'] = algorithm_in_ilm;
  17. return content;
  18. }
  19. export function prepareActivityToStudentHelper (ilm_cont) {
  20. const content = parseActivityData(ilm_cont);
  21. const testCases = content.testcases;
  22. setTestCases(testCases);
  23. return {
  24. settingsDataTypes: content.settings_data_types,
  25. settingsCommands: content.settings_commands,
  26. settingsFunctions: content.settings_functions,
  27. algorithmInIlm: content.algorithm_in_ilm
  28. }
  29. }
  30. export function autoEval (originalData, callback) {
  31. const code = generate();
  32. const original = parseActivityData(originalData);
  33. if (code == null) {
  34. return callback(-1);
  35. } else {
  36. if (!compareTestcases(original.testcases, getTestCases())) {
  37. return callback(-2);
  38. }
  39. const autoAssessment = new IVProgAssessment(code, getTestCases(), new TestConsole([]));
  40. autoAssessment.runTest().then( grade => callback(grade)).catch(err => console.log(err))
  41. }
  42. }
  43. function compareTestcases (original, student) {
  44. if (original.length != student.length) {
  45. return false;
  46. }
  47. for (let i = 0; i < original.length; ++i) {
  48. const elementO = original[i];
  49. const elementS = student[i];
  50. if(!compareArray(elementO.input, elementS.input)) {
  51. return false;
  52. }
  53. if(!compareArray(elementO.output, elementS.output)) {
  54. return false;
  55. }
  56. }
  57. }
  58. function compareArray (a, b) {
  59. for (let i = 0; i < a.length; ++i) {
  60. const elementA = a[i];
  61. const elementB = b[i];
  62. if (elementA != elementB) {
  63. return false;
  64. }
  65. }
  66. }