Exemple #1
0
        public void Test_Run_Sample_01_04_Battery()
        {
            var myRobot = new Robot(new Battery());
            var request = new CleaningRequest();

            var input = "{ \"map\": [[ \"S\", \"S\", \"S\", \"S\" ], [ \"S\", \"S\", \"C\", \"S\" ],[ \"S\", \"S\", \"S\", \"S\" ],[ \"S\", \"null\", \"S\", \"S\" ]],\"start\": {\"X\": 3, \"Y\": 0, \"facing\": \"N\" }, \"commands\": [ \"TL\", \"A\", \"C\", \"A\", \"C\", \"TR\", \"A\", \"C\" ],\"battery\":  80 }";

            request = JsonConvert.DeserializeObject <CleaningRequest>(input);
            var result = myRobot.Run(request);

            Assert.AreEqual(result.Battery, 54);
        }
Exemple #2
0
        public void Test_Run_Sample_02_00_Basic()
        {
            var myRobot = new Robot(new Battery());
            var request = new CleaningRequest();

            var input = "{  \"map\": [    [\"S\", \"S\", \"S\", \"S\"],    [\"S\", \"S\", \"C\", \"S\"],    [\"S\", \"S\", \"S\", \"S\"],    [\"S\", \"null\", \"S\", \"S\"]  ],  \"start\": {\"X\": 3, \"Y\": 1, \"facing\": \"S\"},  \"commands\": [ \"TR\",\"A\",\"C\",\"A\",\"C\",\"TR\",\"A\",\"C\"],  \"battery\": 1094}";

            request = JsonConvert.DeserializeObject <CleaningRequest>(input);
            var result = myRobot.Run(request);

            Assert.IsNotNull(result);
        }
Exemple #3
0
        public void Test_Run_Sample_01_05_Map_Not_Valid()
        {
            var myRobot = new Robot(new Battery());
            var request = new CleaningRequest();

            var input = "{ \"map\": [[ \"S\", \"S\", \"S\", \"S\" ], [ \"S\", \"S\", \"C\", \"S\" ],[ \"S\", \"S\", \"S\", \"S\" ],[ \"S\", \"null\", \"S\", \"S\" ]],\"start\": {\"X\": 3, \"Y\": 0, \"facing\": \"N\" }, \"commands\": [ \"TL\", \"A\", \"C\", \"A\", \"C\", \"TR\", \"A\", \"C\" ],\"battery\":  80 }";

            request = JsonConvert.DeserializeObject <CleaningRequest>(input);

            request.Map[0] = new[] { "a", "b", "A", "A" };

            var result = myRobot.Run(request);

            Assert.IsNull(result);
        }
Exemple #4
0
 public IHttpActionResult Post(CleaningRequest request)
 {
     return(Ok(robot.Run(request)));
 }
Exemple #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("=======================================================================");
            Console.WriteLine("====================CLEANING ROBOT V1.O ===============================");
            Console.WriteLine("=============== CARLOS VALDERRAMA ([email protected])=============");
            Console.WriteLine();

            if (args.Length < 2)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("You must execute this program passing 2 arguments. Example:");
                Console.WriteLine("cleaning_robot source.json result.json");
                Console.WriteLine("where: source.json is the input file to execute the Simulation and result.json is the output file to generate the results.");
                Console.ForegroundColor = ConsoleColor.White;
                return;
            }


            var input_path  = args[0];
            var output_path = args[1];

            if (!File.Exists(input_path))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"Input file {input_path} does not exist");
                Console.ForegroundColor = ConsoleColor.White;
                return;
            }

            try
            {
                var myRobot = new Robot(new Battery());
                var request = new CleaningRequest();

                using (var source = new StreamReader(args[0]))
                {
                    var input = source.ReadToEnd();
                    request = JsonConvert.DeserializeObject <CleaningRequest>(input);
                }

                Console.WriteLine("Executing commands");
                var result = myRobot.Run(request);

                Console.WriteLine("Generating output file");
                using (var dest = new StreamWriter(args[1]))
                {
                    var output = JsonConvert.SerializeObject(result, Newtonsoft.Json.Formatting.Indented);
                    dest.Write(output);
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"Unhandled Exception executing program: {ex.Message}");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine($"Hint: Review your input file. Possible malformed .JSON File");
            }
            finally
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("End of Execution");
            }
        }