iassignHelpers.js 2.0 KB

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