Beispiel #1
0
 private void AssertException(TimelineClientImpl client, RuntimeException ce)
 {
     NUnit.Framework.Assert.IsTrue("Handler exception for reason other than retry: " +
                                   ce.ToString(), ce.Message.Contains("Connection retries limit exceeded"));
     // we would expect this exception here, check if the client has retried
     NUnit.Framework.Assert.IsTrue("Retry filter didn't perform any retries! ", client
                                   .connectionRetry.GetRetired());
 }
        /// <summary>
        /// Compiles and runs the code on Pico VM
        /// </summary>
        /// <param name="studentAssignment">StudentAssignment, requires its Solution and its TestResults to be loaded</param>
        /// <param name="code">User source code</param>
        /// <returns></returns>
        private List <Error> CompileAndRunUserCode(StudentAssignment studentAssignment, string code, bool returnExpectedOutputError = true)
        {
            Assembler a = new Assembler();

            a.LoadFromString(code);
            if (!a.Process())
            {
                return(a.Errors);
            }

            var hex = a.ExportToHex();

            // get tests
            var variant = _context.AssignmentVariants.Single(av => av.AssignmentID == studentAssignment.AssignmentID && av.Index == studentAssignment.AssignmentVariantIndex);
            var tests   = _context.AssignmentTests.Where(at => at.AssignmentVariantID == variant.ID);

            // mark as testing, clear all past tests
            studentAssignment.Solution.LastTestedTime = DateTime.Now;
            if (studentAssignment.Solution.AssignmentTestResults != null)
            {
                studentAssignment.Solution.AssignmentTestResults.Clear();
            }
            _context.Update(studentAssignment.Solution);
            _context.SaveChanges();

            var errors = new List <Error>();

            foreach (var test in tests)
            {
                ushort[] givenInput = test.GivenInput != null
                    ? test.GivenInput.Trim().Split(' ').Select(ushort.Parse).ToArray()
                    : new ushort[0];

                ushort[] expectedOutput = test.ExpectedOutput != null
                    ? test.ExpectedOutput.Trim().Split(' ').Select(ushort.Parse).ToArray()
                    : new ushort[0];

                VirtualMachine vm = new VirtualMachine(givenInput, expectedOutput);
                vm.LoadFromLines(hex);
                RuntimeException e = vm.Run();

                // save test result
                var testResult = new AssignmentTestResult()
                {
                    AssignmentTestID = test.ID,
                    SolutionID       = studentAssignment.Solution.ID,
                    CreatedTime      = DateTime.Now,
                    CalculatedOutput = (vm.Processor.IODevice as TestingIODevice).CalculatedOutput,
                };
                if (e == null || e is NormalTerminationRuntimeException)
                {
                    testResult.IsSuccess = true;
                }
                else
                {
                    if (returnExpectedOutputError)
                    {
                        errors.Add(new Error()
                        {
                            ID = 666, Description = e.ToString()
                        });
                    }
                }
                _context.AssignmentTestResults.Add(testResult);
            }
            _context.SaveChanges();
            return(errors.Count > 0 ? errors : null);
        }