Пример #1
0
        public void TestHandleLabelAndReturnLine_whenLabelNameIncorrect_thenThrowException()
        {
            var compilerModel = new CompilerModel(null, null, null);
            var line          = "l2!:test";
            var env           = new CompilerEnvironment();

            compilerModel.HandleLabelAndReturnLine(line, env);
        }
Пример #2
0
        public void TestHandleDirective_whenSetIncorrectStackSegment_thenThrowException()
        {
            var compilerModel = new CompilerModel(null, null, null);
            var line          = ".sx2";
            var env           = new CompilerEnvironment();

            compilerModel.HandleDirective(line, env);
        }
Пример #3
0
        public void TestHandleLabelAndReturnLine_whenLabelNameStartsWithNumber_thenThrowException()
        {
            var compilerModel = new CompilerModel(null, null, null);
            var line          = "2:test";
            var env           = new CompilerEnvironment();

            compilerModel.HandleLabelAndReturnLine(line, env);
        }
Пример #4
0
        public void TestHandleDirective_whenSetTooBigDataSegment_thenThrowException()
        {
            var compilerModel = new CompilerModel(null, null, null);
            var line          = ".d4";
            var env           = new CompilerEnvironment();

            compilerModel.HandleDirective(line, env);
        }
Пример #5
0
        public void TestHandleCommand_whenCommandNotFound_thenThrowException()
        {
            var env           = new CompilerEnvironment();
            var line          = "test arg1";
            var commands      = new Dictionary <string, CommandProcessorFactory.CommandProcessor>();
            var compilerModel = new CompilerModel(null, null, commands);

            compilerModel.HandleCommand(line, env);
        }
Пример #6
0
        public static CompilerController GetCompilerController(ICompilerControllerOutput output)
        {
            var compilerController = new CompilerController(output);

            var compilerModel = new CompilerModel(compilerController.CompilationComplete, compilerController.CompilationError);

            compilerController.SetCompiler(compilerModel);

            return(compilerController);
        }
Пример #7
0
        public void TestHandleDirective_whenGoToIncorrectAddress_thenThrowException()
        {
            var compilerModel = new CompilerModel(null, null, null);
            var line          = ".org 10x00";
            var env           = new CompilerEnvironment();

            env.CurrentAddress = 10;

            compilerModel.HandleDirective(line, env);
        }
Пример #8
0
        public void TestHandleLabelAndReturnLine_whenThereAreTwoSameLabels_thenThrowException()
        {
            var compilerModel = new CompilerModel(null, null, null);
            var line          = "l:test";
            var line2         = "l: test2";
            var env           = new CompilerEnvironment();

            compilerModel.HandleLabelAndReturnLine(line, env);
            compilerModel.HandleLabelAndReturnLine(line2, env);
        }
Пример #9
0
        public void TestHandleLabelAndReturnLine_whenThereIsNoLabel_thenReturnTheSameLine()
        {
            var compilerModel = new CompilerModel(null, null, null);
            var line          = "test";
            var env           = new CompilerEnvironment();

            var result = compilerModel.HandleLabelAndReturnLine(line, env);

            Assert.AreSame(result, line);
            Assert.AreEqual(env.GetLabelsCount(), 0);
        }
Пример #10
0
        public void TestHandleDirective_whenGoToAddress_thenAddressChanged()
        {
            var compilerModel = new CompilerModel(null, null, null);
            var line          = ".org 100";
            var env           = new CompilerEnvironment();

            env.CurrentAddress = 10;

            compilerModel.HandleDirective(line, env);

            Assert.AreEqual(env.CurrentAddress, 100);
        }
Пример #11
0
        public void TestHandleDirective_whenSetStackSegment_thenSegmentChanged()
        {
            var compilerModel = new CompilerModel(null, null, null);
            var line          = ".S2";
            var env           = new CompilerEnvironment();

            env.DefaultStackSegment = 1;

            compilerModel.HandleDirective(line, env);

            Assert.AreEqual(env.DefaultStackSegment, 2);
        }
Пример #12
0
        public void TestHandleLabelAndReturnLine_whenThereIsLabel_thenReturnLineWithoutLabelAndAddLabelToEnv()
        {
            var compilerModel = new CompilerModel(null, null, null);
            var line          = "la2_-s:test";
            var env           = new CompilerEnvironment();
            int address       = 67;

            env.CurrentAddress = 67;

            var result = compilerModel.HandleLabelAndReturnLine(line, env);

            Assert.IsTrue(result.Equals("test"));
            Assert.AreEqual(env.GetLabelsCount(), 1);
            Assert.AreEqual(env.GetLabelAddress("la2_-s"), address);
        }
Пример #13
0
        public void TestPrepareCode()
        {
            var inputs = new List <string>()
            {
                "123",
                "123\n456",
                "123\n\n456",
                " 123\n456 \n  789  ",
                "123;test\n456",
                "123  ;test\n456",
                "123\n;test\n456"
            };
            var outputs = new List <List <string> >()
            {
                new List <string> {
                    "123"
                },
                new List <string> {
                    "123", "456"
                },
                new List <string> {
                    "123", "", "456"
                },
                new List <string> {
                    "123", "456", "789"
                },
                new List <string> {
                    "123", "456"
                },
                new List <string> {
                    "123", "456"
                },
                new List <string> {
                    "123", "", "456"
                }
            };
            var compilerModel = new CompilerModel(null, null, null);

            for (int i = 0; i < inputs.Count; i++)
            {
                var result = compilerModel.PrepareCode(inputs[i]);
                Assert.AreEqual(result.Count, outputs[i].Count);
                for (int j = 0; j < result.Count; j++)
                {
                    Assert.AreEqual(result[j], outputs[i][j]);
                }
            }
        }
Пример #14
0
        public void TestHandleCommand_whenCommandWithoutArgs_thenArgumentsEmpty()
        {
            var env      = new CompilerEnvironment();
            var line     = "tEsT";
            var commands = new Dictionary <string, CommandProcessorFactory.CommandProcessor> {
                {
                    "test",
                    (args, innerEnv) => {
                        Assert.AreSame(innerEnv, env);
                        Assert.AreEqual(args.Length, 0);
                    }
                }
            };
            var compilerModel = new CompilerModel(null, null, commands);

            compilerModel.HandleCommand(line, env);
        }
Пример #15
0
        public void TestHandleCommand_whenCommandCorrect_thenArgumentsPassed()
        {
            var env      = new CompilerEnvironment();
            var line     = "tEsT arg1  , arg2 ";
            var commands = new Dictionary <string, CommandProcessorFactory.CommandProcessor> {
                {
                    "test",
                    (args, innerEnv) => {
                        Assert.AreSame(innerEnv, env);
                        Assert.AreEqual(args.Length, 2);
                        Assert.IsTrue(args[0].Equals("arg1"));
                        Assert.IsTrue(args[1].Equals("arg2"));
                    }
                }
            };
            var compilerModel = new CompilerModel(null, null, commands);

            compilerModel.HandleCommand(line, env);
        }
        public IActionResult Index(CompilerModel model)
        {
            /*
             * SlackerCompile testRunner = new SlackerCompile();
             * testRunner.MyExe = @"C:\myTeacher.exe";
             * testRunner.MyCode = "using System;public class Program{static void Main(){int myInput = 1000;int mySum = 0;for (int i = myInput - 1; i > 0; i--) { if (i % 3 == 0 || i % 5 == 0) mySum = mySum + i; }Console.WriteLine(mySum);";
             * testRunner.MyArgs = "";
             * testRunner.CompileItResult = testRunner.CompileIt(testRunner.MyCode, testRunner.MyExe);
             * testRunner.RunItResult = testRunner.RunIt(testRunner.MyExe,testRunner.MyArgs);
             */
            //If we list all the natural numbers below 10 that are multiples of 3 or 5,
            //we get 3, 5, 6 and 9.The sum of these multiples is 23.
            //Find the sum of all the multiples of 3 or 5 below 1000.

            var myId = TempData["myProblem"];
            int toIntId;

            int.TryParse(myId.ToString(), out toIntId);

            var teachcode = new TeacherCodeModel().GetFromDatabase(toIntId);

            /*
             * var myTeacherCode = _context.TeacherCode
             * .SingleOrDefault(t => t.Id == toIntId); //grabs teacher row where id matches
             * if (myTeacherCode == null) return Content("BLEEEECH");
             */

            //TeacherCodeModel myTeacherCode = (TeacherCodeModel)TempData["myProblem"];


            List <string> testList = new List <string> {
                "1", "2"
            };
            // string testStudentCode = "using System;public class Program { static void Main() { int myInput = 1000; int mySum = 0; for (int i = myInput - 1; i > 0; i--) { if (i % 3 == 0 || i % 5 == 0) mySum = mySum + i; } Console.WriteLine(mySum); } }";
            //string testTeacherCode = "using System;public class sum{public static void Main(){int sum = 0;for (int i = 1; i <= 999; i++){if (i % 3 == 0 || i % 5 == 0) { sum += i; }}Console.WriteLine(sum);}}";
            //Tester myTester = new Tester(@"C:\inetpub\myTeacher.exe", "myTeacher.exe",testTeacherCode, @"C:\inetpub\myStudent.exe", "myStudent.exe",testStudentCode, testList);
            //Tester myTester = new Tester(myTeacherCode.CodePath + myTeacherCode.CodeExe, myTeacherCode.CodeExe, myTeacherCode.CodeSolution, @"C:\myStudent.exe", "myStudent.exe", model.inputCode, testList);
            Tester myTester = new Tester(teachcode.CodePath + teachcode.CodeExe, teachcode.CodeExe, teachcode.CodeSolution, @"C:\inetpub\myStudent.exe", "myStudent.exe", model.inputCode, testList);

            //return Content(teachcode.CodeSolution);
            // return Content(model.inputCode);
            //return Content(myTeacherCode.CodeSolution);
            bool myTestResult = myTester.TestIt();

            if (myTestResult == true)
            {
                ViewBag.Message = "Success!";
            }
            else
            {
                ViewBag.Message = "Failure!";
            }
            //ViewBag.Message = myTestResult.ToString();
            TempData["myProblem"] = TempData["myProblem"];
            CompilerModel myModel = new CompilerModel();

            if (myTester.myOutputs != null)
            {
                ViewBag.myOutputs = myTester.myOutputs;
            }
            else
            {
                ViewBag.myOutputs = new List <string> {
                }
            };

            return(View(myModel));

            /*
             *
             * //return Content(myTestResult.ToString());
             *
             * //sending inputs into the prog.
             * //testRunner.CompileIt
             * //testRunner.RunIt
             *
             * //take try catch code here, put in compiler class.
             *
             * SlackerCompile myRunner = new SlackerCompile();
             * myRunner.MyExe = "myGuy.exe";
             * myRunner.MyPath = @"C:\"+myRunner.MyExe;
             * string test = myRunner.MyExe;
             * // myRunner.MyCode = "public class MyClass{ static void Main(string[] args){System.Console.WriteLine("Hello World");  System.Console.WriteLine("testtesttest"); }}";
             * myRunner.MyCode = model.inputCode;
             * Console.WriteLine("Starting compilation");
             *
             * myRunner.CompileItResult = myRunner.CompileIt(myRunner.MyPath, myRunner.MyExe, myRunner.MyCode);
             *
             *
             * List<Diagnostic> myResult = myRunner.CompileItResult;
             *
             * try
             * {
             *
             *  List<string> myOutput = myRunner.RunIt(myRunner.MyPath, "");//TODO: Have it use current working directory
             *
             *
             *  myRunner.RunItResult = myOutput;
             *
             *  string StringOutput = "";
             *
             *  foreach (string myString in myOutput) StringOutput += myString;
             *  model.outputCode = StringOutput;
             *  var myModel = model;
             * return View(myModel);
             *  return Content(StringOutput);
             * }
             *
             * catch {
             *
             *  string StringResult = "";
             *  foreach (Diagnostic diagnostic in myResult)
             *  {
             *      StringResult += diagnostic.ToString();
             *
             *  }
             *
             *  model.outputCode = StringResult;
             *  var myModel = model;
             *  return View(myModel);
             *  return Content(StringResult);
             *
             * }
             *
             *
             *
             *
             *
             *
             *
             * //myRunner.MyCode = "public class MyClass{ static void Main(string[] args){System.Console.WriteLine(\"Hello World\"); System.Console.ReadLine(); System.Console.WriteLine(\"testtesttest\"); }}";
             *
             *
             *
             *
             *      //TODO: Have it use current working directory
             *
             *
             *
             * return View();
             */
        }
    }
Пример #17
0
 public void SetCompiler(CompilerModel compilerModel)
 {
     _compilerModel = compilerModel;
 }