ReadCities() public method

public ReadCities ( string fileName ) : int
fileName string
return int
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to RoutePlanner (Version {0})", Assembly.GetExecutingAssembly().GetName().Version);
            var bern = new WayPoint("Bern",46.951081, 7.438637);
            var tripolis = new WayPoint("Tripolis",32.883333333333, 13.166666666667);
            var wayPoint = new WayPoint("Windisch", 47.479319847061966, 8.212966918945312);
            var City = new City("Bern", "Schweiz", 75000, 47.479319847061966, 8.212966918945312);
            Console.WriteLine(wayPoint);
            Console.WriteLine("Distance Bern to Tripolis: "+bern.Distance(tripolis)+"km");
            Cities cities = new Cities();
            cities.ReadCities(@"..\..\..\RoutePlannerTest\data\citiesTestDataLab2.txt");
            RoutesDijkstra routes = new RoutesDijkstra(cities);
            routes.ReadRoutes(@"..\..\..\RoutePlannerTest\data\linksTestDataLab3.txt");
            Cities citiesFail = new Cities();
            citiesFail.ReadCities(@"irgendeinfile.txt");

            Console.WriteLine("Starting parallel test......");
            Cities citiesP = new Cities();
            cities.ReadCities(@"..\..\..\RoutePlannerTest\data\citiesTestDataLab10.txt");

            int warmUpRounds = 100;
            int measureRounds = 20;
            Routes routesP = new RoutesFloydWarshall(cities);
            int count = routesP.ReadRoutes(@"..\..\..\RoutePlannerTest\data\linksTestDataLab10.txt");

            Console.WriteLine("doing warmup");
            for (int i = 0; i < warmUpRounds; i++)
            {
                List<Link> links = routesP.FindShortestRouteBetween("Lyon", "Berlin", TransportModes.Rail);
            }
            Stopwatch watch = new Stopwatch();
            watch.Start();
            Console.WriteLine("sequential mode: ");
            // test short routes in parallel mode
            routesP.ExecuteParallel = false;
            for (int i = 0; i < measureRounds; i++)
            {
                List<Link> links = routesP.FindShortestRouteBetween("Lyon", "Berlin", TransportModes.Rail);
            }
            watch.Stop();
            Console.WriteLine("Elapsed Time: " + watch.ElapsedMilliseconds + "ms");

            watch.Reset();
            watch.Start();
            Console.WriteLine("parallel mode: ");
            // test short routes in parallel mode
            routesP.ExecuteParallel = true;
            for (int i = 0; i < measureRounds; i++)
            {
                List<Link> links2 = routesP.FindShortestRouteBetween("Lyon", "Berlin", TransportModes.Rail);

            }
            watch.Stop();
            Console.WriteLine("Elapsed Time: " + watch.ElapsedMilliseconds + "ms");

            Console.WriteLine("Press any key to quit");
            Console.ReadKey();
        }
Esempio n. 2
0
        public void TestTask4FindRoutes()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);
            var expectedLinks = new List<Link>();
            expectedLinks.Add(new Link(new City("Zürich", "Switzerland", 7000, 1,2),
                                       new City("Aarau", "Switzerland", 7000, 1,2), 0));
            expectedLinks.Add(new Link(new City("Aarau", "Switzerland", 7000, 1, 2),
                                       new City("Liestal", "Switzerland", 7000, 1, 2), 0));
            expectedLinks.Add(new Link(new City("Liestal", "Switzerland", 7000, 1, 2),
                                       new City("Basel", "Switzerland", 7000, 1, 2), 0));

            var routes = new RoutesDijkstra(cities);
            routes.ReadRoutes(LinksTestFile);

            Assert.AreEqual(11, cities.Count);

            // test available cities
            var links = routes.FindShortestRouteBetween("Zürich", "Basel", TransportModes.Rail);

            Assert.IsNotNull(links);
            Assert.AreEqual(expectedLinks.Count, links.Count);

            for (var i = 0; i < links.Count; i++)
            {
                Assert.IsTrue(
                    (expectedLinks[i].FromCity.Name == links[i].FromCity.Name &&
                     expectedLinks[i].ToCity.Name == links[i].ToCity.Name) ||
                    (expectedLinks[i].FromCity.Name == links[i].ToCity.Name &&
                     expectedLinks[i].ToCity.Name == links[i].FromCity.Name));
            }

            links = routes.FindShortestRouteBetween("doesNotExist", "either", TransportModes.Rail);
            Assert.IsNull(links);
        }
Esempio n. 3
0
        public void TestRoutesReadLinks()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            Assert.AreEqual(7, new Routes(cities).ReadRoutes(LinksTestFile));
        }
Esempio n. 4
0
        public async Task TestFindShortestRouteBetweenAsync()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            var routes = new RoutesDijkstra(cities);
            routes.ReadRoutes(LinksTestFile);

            // do synchronous execution
            linksExpected = 
            routes.FindShortestRouteBetween("Basel", "Zürich", TransportModes.Rail);

            // do asynchronous execution
            linksActual =
             await routes.FindShortestRouteBetweenAsync("Basel", "Zürich", TransportModes.Rail);

            // not test the results
            Assert.IsNotNull(linksActual);
            Assert.AreEqual(linksExpected.Count, linksActual.Count);


            for (int i = 0; i < linksActual.Count; i++)
            {
                Assert.AreEqual(linksExpected[i].FromCity, linksActual[i].FromCity);
                Assert.AreEqual(linksExpected[i].ToCity, linksActual[i].ToCity);
            }
        }
Esempio n. 5
0
        public void TestTask1_FindRoutes()
        {
            Cities cities = new Cities();
            cities.ReadCities(@"citiesTestDataLab4.txt");
            List<Link> expectedLinks = new List<Link>();
            expectedLinks.Add(new Link(new City("Zürich", "Switzerland", 7000, 1, 2),
                                       new City("Aarau", "Switzerland", 7000, 1, 2), 0));
            expectedLinks.Add(new Link(new City("Aarau", "Switzerland", 7000, 1, 2),
                                       new City("Liestal", "Switzerland", 7000, 1, 2), 0));
            expectedLinks.Add(new Link(new City("Liestal", "Switzerland", 7000, 1, 2),
                                       new City("Basel", "Switzerland", 7000, 1, 2), 0));

            Routes routes = new RoutesFloydWarshall(cities);
            int count = routes.ReadRoutes(@"linksTestDataLab4.txt");

            Assert.AreEqual(11, cities.Count);

            // test available cities
            routes.ExecuteParallel = true;
            List<Link> links = routes.FindShortestRouteBetween("Zürich", "Basel", TransportModes.Rail);

            Assert.IsNotNull(links);
            Assert.AreEqual(expectedLinks.Count, links.Count);

            for (int i = 0; i < links.Count; i++)
            {
                Assert.AreEqual(expectedLinks[i].FromCity.Name, links[i].FromCity.Name);
                Assert.AreEqual(expectedLinks[i].ToCity.Name, links[i].ToCity.Name);
            }


            links = routes.FindShortestRouteBetween("doesNotExist", "either", TransportModes.Rail);
            Assert.IsNull(links);

        }
        public async Task TestFindShortestRouteBetweenAsyncProgress()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            var routes = new Routes(cities);
            routes.ReadRoutes(LinksTestFile);

            // do synchronous execution
            var linksExpected = routes.FindShortestRouteBetween("Basel", "Zürich", TransportMode.Rail);

            // do asynchronous execution
            var messages = new List<string>();
            var progress = new Progress<string>(msg => messages.Add(msg));
            var linksActual = await routes.FindShortestRouteBetweenAsync("Basel", "Zürich", TransportMode.Rail, progress);

            // let pending tasks execute
            await Task.Yield();

            // ensure that at least 5 progress calls are made
            Assert.IsTrue(messages.Distinct().Count()>=5, "Less than 5 distinct progress messages");

            // ensure that all progress messages end with " done"
            Assert.IsTrue(messages.All(m => m.EndsWith(" done")),
                string.Format("Progress message \"{0}\" does not end with \" done\"",
                    messages.FirstOrDefault(m => !m.EndsWith(" done"))));
        }
Esempio n. 7
0
        public void TestLoadAndRunDynamic()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            IRoutes routes = RoutesFactory.Create(cities);

            routes.ReadRoutes(LinksTestFile);

            Assert.AreEqual(11, cities.Count);

            // test available cities
            List<Link> links = routes.FindShortestRouteBetween("Zürich", "Basel", TransportModes.Rail);

            var expectedLinks = new List<Link>();
            expectedLinks.Add(new Link(new City("Zürich", "Switzerland", 7000, 1, 2),
                                       new City("Aarau", "Switzerland", 7000, 1, 2), 0));
            expectedLinks.Add(new Link(new City("Aarau", "Switzerland", 7000, 1, 2),
                                       new City("Liestal", "Switzerland", 7000, 1, 2), 0));
            expectedLinks.Add(new Link(new City("Liestal", "Switzerland", 7000, 1, 2),
                                       new City("Basel", "Switzerland", 7000, 1, 2), 0));

            Assert.IsNotNull(links);
            Assert.AreEqual(expectedLinks.Count, links.Count);

            for (int i = 0; i < links.Count; i++)
            {
                Assert.AreEqual(expectedLinks[i].FromCity.Name, links[i].FromCity.Name);
                Assert.AreEqual(expectedLinks[i].ToCity.Name, links[i].ToCity.Name);
            }

            links = routes.FindShortestRouteBetween("doesNotExist", "either", TransportModes.Rail);
            Assert.IsNull(links);
        }
Esempio n. 8
0
        public void TestTask2FindRoutes()
        {
            Cities cities = new Cities();
            cities.ReadCities(@"citiesTestDataLab11.txt");

            Assert.AreEqual(6372, cities.Count);

            List<Link> expectedLinks = new List<Link>();

            Routes routes = new RoutesFloydWarshall(cities);
            int count = routes.ReadRoutes(@"linksTestDataLab11.txt");
            Assert.AreEqual(112, count);

            // test available cities
            routes.ExecuteParallel = true;
            List<Link> links = routes.FindShortestRouteBetween("Lyon", "Berlin", TransportModes.Rail);
            Assert.AreEqual(13, links.Count);
            routes.ExecuteParallel = false;
            List<Link> links2 = routes.FindShortestRouteBetween("Lyon", "Berlin", TransportModes.Rail);

            Assert.IsNotNull(links);
            Assert.AreEqual(13, links.Count);
            Assert.IsNotNull(links2);
            Assert.AreEqual(13, links2.Count);
        }
Esempio n. 9
0
        public void TestRoutesReadLinks()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            var routes = new RoutesDijkstra(cities);

            var count = routes.ReadRoutes(LinksTestFile);
            Assert.AreEqual(7, count);
        }
Esempio n. 10
0
        public void TestLoadAndRunDynamicSecondImpl()
        {
            var defaultSettings = Assembly.GetAssembly(typeof(IRoutes))
                .GetType("Fhnw.Ecnf.RoutePlanner.RoutePlannerLib.Properties.Settings")
                .GetProperty("Default")
                .GetValue(null);
            defaultSettings.GetType().GetMethod("Reset").Invoke(defaultSettings, new object[] { });
            //defaultSettings.GetType().GetMethod("Reload").Invoke(defaultSettings, new object[] { });

            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            IRoutes routes = RoutesFactory.Create(cities, "Fhnw.Ecnf.RoutePlanner.RoutePlannerTest.TestRoutesValidConstructor");
            Assert.AreEqual(42, routes.ReadRoutes("nonsense"));

            IRoutes routes2 = RoutesFactory.Create(cities);
            Assert.AreNotEqual(routes.GetType(), routes2.GetType());

            try
            {
                try
                {
                    defaultSettings.GetType().GetProperty("RouteAlgorithm").SetValue(defaultSettings, "Fhnw.Ecnf.RoutePlanner.RoutePlannerTest.TestRoutesValidConstructor");
                }
                catch
                {
                    Assert.Fail("Make sure there is a String-property called \"RouteAlgorithm\" with User-scope.");
                }

                IRoutes routes3 = RoutesFactory.Create(cities);
                Assert.AreEqual(routes.GetType(), routes3.GetType());
            }
            finally
            {
                defaultSettings.GetType().GetMethod("Reset").Invoke(defaultSettings, new object[] { });
            }

            try
            {
                RoutesFactory.Create(cities, "Fhnw.Ecnf.RoutePlanner.RoutePlannerTest.Lab5Test.TestRoutesInvalidConstructor");
                Assert.Fail("Should throw a NotSupportedException, because it doesn't have the right constructor");
            }
            catch(NotSupportedException)
            {
            }

            try
            {
                RoutesFactory.Create(cities, "Fhnw.Ecnf.RoutePlanner.RoutePlannerTest.Lab5Test.TestRoutesNoInterface");
                Assert.Fail("Should throw a NotSupportedException, because IRoutes is not implemented");
            }
            catch (NotSupportedException)
            {
            }
        }
Esempio n. 11
0
        static void Main(string[] args)
        {
            var c = new City("Aarau", "Switzerland", 10, 1.1, 2.2);
            //Console.WriteLine(c.Location.GetType());

            var stream = new StringWriter();
            SimpleObjectWriter w = new SimpleObjectWriter(stream);
            w.Next(c);
            var objstr = stream.ToString();
            Console.Write(objstr + "\r\n");

            var stream2 = new StringReader(objstr);
            SimpleObjectReader r = new SimpleObjectReader(stream2);
            var city = r.Next() as City;
            Console.WriteLine(city.Name);
            Console.WriteLine(city.Country);
            Console.WriteLine(city.Population);
            Console.WriteLine(city.Location);
            Console.WriteLine(city.Location.Name);
            Console.WriteLine(city.Location.Latitude);
            Console.WriteLine(city.Location.Longitude);

            Console.Write("Welcome to RoutePlanner {0}\n", Assembly.GetExecutingAssembly().GetName().Version);
            var wayPoint = new WayPoint("Windisch", 47.479319847061966, 8.212966918945312);
            //Console.WriteLine("{0}: {1}/{2}", wayPoint.Name, wayPoint.Latitude, wayPoint.Longitude);
            Console.WriteLine(wayPoint);
            wayPoint.Name = "";
            Console.WriteLine(wayPoint);
            wayPoint.Name = null;
            Console.WriteLine(wayPoint);

            var cities = new Cities();
            Console.WriteLine(cities.ReadCities(@"data\citiesTestDataLab3.txt"));

            var routes = new Routes(cities);
            var count = routes.ReadRoutes(@"data\linksTestDataLab3.txt");

            var citiesError = new Cities();
            Console.WriteLine(cities.ReadCities(@"data\citiesTestDataLab253.txt"));

            Console.ReadKey();
        }
Esempio n. 12
0
        public void TestFindCitiesByTransportMode()
        {
            Cities cities = new Cities();
            cities.ReadCities(@"citiesTestDataLab3.txt");
            var routes = new Routes(cities);
            routes.ReadRoutes(@"linksTestDataLab3.txt");

            City[] citiesByMode = routes.FindCities(TransportMode.Rail);
            Assert.AreEqual(11, citiesByMode.Length);

            City[] emptyCitiesByMode = routes.FindCities(TransportMode.Bus);
            Assert.AreEqual(0, emptyCitiesByMode.Length);
        }
Esempio n. 13
0
        public void TestReadCitiesAndIndexer()
        {
            const int readCitiesExpected = 10;
            var cities = new Cities();

            Assert.AreEqual(readCitiesExpected, cities.ReadCities(CitiesTestFile));

            Assert.AreEqual(readCitiesExpected, cities.Count);

            // cities should be added to list
            Assert.AreEqual(readCitiesExpected, cities.ReadCities(CitiesTestFile));

            // total count should be doubled
            Assert.AreEqual(2*readCitiesExpected, cities.Count);

            //verify first and last city
            Assert.AreEqual("Mumbai", cities[0].Name);
            Assert.AreEqual("Jakarta", cities[9].Name);

            // check for invalid index
            Assert.IsNull(cities[100]);
        }
Esempio n. 14
0
        public void TestDynamicWorld()
        {
            Cities cities = new Cities();

            cities.ReadCities("citiesTestDataLab2.txt");

            dynamic world = new World(cities);

            dynamic karachi = world.Karachi();
            Assert.AreEqual("Karachi", karachi.Name);

            string notFound = world.Entenhausen();
            Assert.AreEqual("The city <Entenhausen> does not exist!", notFound);
        }
 static void Main(string[] args)
 {
     Console.WriteLine("Welcome to RoutePlanner (Version {0})", Assembly.GetExecutingAssembly().GetName().Version);
     var wayPointTest = new WayPoint("Windisch", 47.479319847061966, 8.212966918945312);
     var wayPointBern = new WayPoint("Bern", 46.947346, 7.447742);
     var wayPointTripolis = new WayPoint("Tripolis", 32.885460, 13.190739);
     Console.WriteLine(wayPointTest.ToString());
     Console.WriteLine("Distance between Bern and Tripoli: " + wayPointBern.Distance(wayPointTripolis) + " km");
     var citiesTest = new Cities();
     //citiesTest.ReadCities(@"citiesTestDataLab2.txt");
     citiesTest.ReadCities("citiesTestDataLab2.txt");
     //Console.WriteLine("{0}: {1}/{2}", wayPoint.Name, wayPoint.Latitude, wayPoint.Longitude);
     Console.ReadKey();
 }
Esempio n. 16
0
        public void TestTask1FindCityInCities()
        {
            var expectedCity = "Zürich";
            Cities cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            var notFound = cities.FindCity("noCity");
            Assert.IsNull(notFound);
            var found = cities.FindCity(expectedCity);
            Assert.AreEqual(expectedCity, found.Name);

            // should work case insensitive
            found = cities.FindCity("züRicH");
            Assert.AreEqual(expectedCity, found.Name);
        }
Esempio n. 17
0
        public void TestRouteParallelSpeed()
        {
            var cities = new Cities();
            cities.ReadCities("citiesTestDataLab12.txt");
            var routes = new Routes(cities);
            routes.ReadRoutes("linksTestDataLab12.txt");

            //warmup
            routes.FindAllShortestRoutesParallel();

            //execute and measure time
            var timeA = DateTime.Now;
            List<List<Link>> allRoutesSerial = routes.FindAllShortestRoutes();
            var timeB = DateTime.Now;
            List<List<Link>> allRoutesParallel = routes.FindAllShortestRoutesParallel();
            var timeC = DateTime.Now;
            var oldAffinity = Process.GetCurrentProcess().ProcessorAffinity;
            Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)1;
            routes.FindAllShortestRoutesParallel();
            Process.GetCurrentProcess().ProcessorAffinity = oldAffinity;
            var timeD = DateTime.Now;

            var factor = (timeC - timeB).TotalSeconds / (timeB - timeA).TotalSeconds;
            var slowdown = (timeD - timeC).TotalSeconds / (timeB - timeA).TotalSeconds;

            Trace.WriteLine($"Factor: {factor:F2}, Slowdown: {slowdown:F2}");

            //parallel execution on a single core shouldn't be much slower
            Assert.IsTrue(slowdown < 1.1);

            switch (Environment.ProcessorCount)
            {
                case 1:
                    break;
                case 2:
                    //expect at least 25% reduction with 2 cores
                    Assert.IsTrue(factor < 0.75);
                    break;
                case 3:
                    //expect at least 35% reduction with 3 cores
                    Assert.IsTrue(factor < 0.65);
                    break;
                default:
                    //expect at least 45% reduction with 4+ cores
                    Assert.IsTrue(factor < 0.55);
                    break;
            }
        }
Esempio n. 18
0
        public void TestTask2FiredEvents()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            var routes = new RoutesDijkstra(cities);

            // test available cities
            routes.RouteRequestEvent += TestForCorrectEventArgsWithFoundCities;
            routes.FindShortestRouteBetween("Bern", "Zürich", TransportModes.Rail);

            // test not existing cities
            routes.RouteRequestEvent -= TestForCorrectEventArgsWithFoundCities;
            routes.RouteRequestEvent += TestForCorrectEventArgsWithNotFoundCities;
            routes.FindShortestRouteBetween("doesNotExist", "either", TransportModes.Rail);
        }
Esempio n. 19
0
        public void TestFindCitiesByTransportMode()
        {
            Cities cities = new Cities();
            cities.ReadCities(@"citiesTestDataLab3.txt");
            var routes = new RoutesDijkstra(cities);
            routes.ReadRoutes(@"linksTestDataLab3.txt");

            City[] citiesByMode = routes.FindCities(TransportModes.Rail);

            Assert.AreEqual(11, citiesByMode.Count());

            // there must be no cities
            City[] emptyCitiesByMode = routes.FindCities(TransportModes.Bus);
            Assert.AreEqual(0, emptyCitiesByMode.Count());

        }
        static void Main(string[] args)
        {
            //Lab1 Aufgabe 1
            Console.WriteLine("Welcome to RoutePlanner (Version " + Assembly.GetExecutingAssembly().GetName().Version + ")");

            //Lab1 Aufgabe 2d
            var wayPoint = new WayPoint("Windisch", 47.479319847061966, 8.212966918945312);
            Console.WriteLine("{0}: {1}/{2}", wayPoint.Name, wayPoint.Latitude, wayPoint.Longitude);
            Console.WriteLine(wayPoint);

            //Lab2 Aufgabe 1a
            Console.WriteLine(wayPoint);
            var wayPoint1 = new WayPoint("", 47.479319847061966, 8.212966918945312);
            var wayPoint2 = new WayPoint(null, 47.479319847061966, 8.212966918945312);
            Console.WriteLine(wayPoint1);
            Console.WriteLine(wayPoint2);

            //Lab2 Aufgabe 1b
            var bern = new WayPoint("Bern", 46.948342, 7.442935);
            var tripolis = new WayPoint("Tripolis", 32.808858, 13.098922);
            Console.WriteLine(bern.Distance(tripolis));
            Console.WriteLine(tripolis.Distance(bern));

            //Lab2 Aufgabe 2b - FunktionsTest - Funktioniert
            Cities c = new Cities();
            c.ReadCities("citiesTestDataLab2.txt");
            Console.WriteLine("Test: {0}", c[5].Location.Latitude);

            var target = new WayPoint("Windisch", 0.564, 0.646);
            Console.WriteLine(target.ToString() + " vs. " + "WayPoint: Windisch 0.56/0.65");

            //Lab3 Aufgabe 2
            var reqWatch = new RouteRequestWatcher();

            var routeCities = new Cities();
            routeCities.ReadCities("citiesTestDataLab2.txt");

            var routes = new Routes(routeCities);

            routes.RouteRequested += reqWatch.LogRouteRequests;

            routes.FindShortestRouteBetween("mumbai", "buenos aires", TransportMode.Rail);
            routes.FindShortestRouteBetween("dilli", "mumbai", TransportMode.Rail);
            routes.FindShortestRouteBetween("mumbai", "buenos aires", TransportMode.Rail);

            Console.ReadLine();
        }
Esempio n. 21
0
        public void TestFindNeighbours()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            var loc = cities[0].Location;

            var neighbors = cities.FindNeighbours(loc, 2000);

            Assert.AreEqual(4, neighbors.Count);

            //verifies if the correct cities were found
            Assert.IsNotNull(neighbors.First(c=>c.Name == "Mumbai"));
            Assert.IsNotNull(neighbors.First(c => c.Name == "Karachi"));
            Assert.IsNotNull(neighbors.First(c => c.Name == "Dhaka"));
            Assert.IsNotNull(neighbors.First(c => c.Name == "Dilli"));
        }
Esempio n. 22
0
        public void TestTaskReadRoutes()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);
            var expectedLinks = new List<Link>();
            expectedLinks.Add(new Link(new City("Zürich", "Switzerland", 7000, 1, 2),
                                       new City("Aarau", "Switzerland", 7000, 1, 2), 0));
            expectedLinks.Add(new Link(new City("Aarau", "Switzerland", 7000, 1, 2),
                                       new City("Liestal", "Switzerland", 7000, 1, 2), 0));
            expectedLinks.Add(new Link(new City("Liestal", "Switzerland", 7000, 1, 2),
                                       new City("Basel", "Switzerland", 7000, 1, 2), 0));

            var routes = new RoutesDijkstra(cities);
            var count = routes.ReadRoutes(LinksTestFile);

            Assert.AreEqual(10, count);
            Assert.AreEqual(10, routes.Count);
        }
Esempio n. 23
0
        public void TestRequestWatcher()
        {
            var reqWatch = new RouteRequestWatcher();

            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            var routes = new RoutesDijkstra(cities);

            routes.RouteRequestEvent += reqWatch.LogRouteRequests;
            routes.FindShortestRouteBetween("Bern", "Zürich", TransportModes.Rail);
            routes.FindShortestRouteBetween("Bern", "Zürich", TransportModes.Rail);
            routes.FindShortestRouteBetween("Basel", "Bern", TransportModes.Rail);

            Assert.AreEqual(reqWatch.GetCityRequests("Zürich"), 2);
            Assert.AreEqual(reqWatch.GetCityRequests("Bern"), 1);
            Assert.AreEqual(reqWatch.GetCityRequests("Basel"), 0);
        }
Esempio n. 24
0
        public void TestCorrectIndexingOfCities()
        {
            const int readCitiesExpected = 10;
            var cities = new Cities();

            Assert.AreEqual(readCitiesExpected, cities.ReadCities(CitiesTestFile));

            City from = cities.FindCity("Mumbai");
            City to = cities.FindCity("Istanbul");
            List<City> foundCities = cities.FindCitiesBetween(from, to);

            // verify that Index property is initialized
            int i = 0;
            foreach (var city in foundCities)
            {
                Assert.AreEqual(i, city.Index);
                i++;
            }
        }
Esempio n. 25
0
        public void TestRequestWatcher()
        {
            var reqWatch = new RouteRequestWatcher();

            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            var routes = new Routes(cities);

            routes.RouteRequested += reqWatch.LogRouteRequests;

            routes.FindShortestRouteBetween("Bern", "Zürich", TransportMode.Rail);
            routes.FindShortestRouteBetween("Bern", "Zürich", TransportMode.Rail);
            routes.FindShortestRouteBetween("Basel", "Bern", TransportMode.Rail);

            Assert.AreEqual(reqWatch.GetCityRequests(cities["Zürich"]), 2);
            Assert.AreEqual(reqWatch.GetCityRequests(cities["Bern"]), 1);
            Assert.AreEqual(reqWatch.GetCityRequests(cities["Basel"]), 0);
            Assert.AreEqual(reqWatch.GetCityRequests(cities["Lausanne"]), 0);
        }
Esempio n. 26
0
        public void TestLoadAndRunDynamicClassicImpl()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            IRoutes routes = RoutesFactory.Create(cities);

            routes.ReadRoutes(LinksTestFile);

            Assert.AreEqual(28, cities.Count);

            // test available cities
            List<Link> links = routes.FindShortestRouteBetween("Zürich", "Basel", TransportMode.Rail);

            var expectedLinks = new List<Link>();
            expectedLinks.Add(new Link(new City("Zürich", "Switzerland", 7000, 1, 2),
                                       new City("Aarau", "Switzerland", 7000, 1, 2), 0));
            expectedLinks.Add(new Link(new City("Aarau", "Switzerland", 7000, 1, 2),
                                       new City("Liestal", "Switzerland", 7000, 1, 2), 0));
            expectedLinks.Add(new Link(new City("Liestal", "Switzerland", 7000, 1, 2),
                                       new City("Basel", "Switzerland", 7000, 1, 2), 0));

            Assert.IsNotNull(links);
            Assert.AreEqual(expectedLinks.Count, links.Count);

            for (int i = 0; i < links.Count; i++)
                Assert.IsTrue(
                    (expectedLinks[i].FromCity.Name == links[i].FromCity.Name &&
                     expectedLinks[i].ToCity.Name == links[i].ToCity.Name) ||
                    (expectedLinks[i].FromCity.Name == links[i].ToCity.Name &&
                     expectedLinks[i].ToCity.Name == links[i].FromCity.Name));

            try
            {
                links = routes.FindShortestRouteBetween("doesNotExist", "either", TransportMode.Rail);
                Assert.Fail("Should throw a KeyNotFoundException");
            }
            catch(KeyNotFoundException)
            {
            }
        }
Esempio n. 27
0
        public async Task TestFindShortestRouteBetweenAsynchProgress()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            var routes = new RoutesDijkstra(cities);
            routes.ReadRoutes(LinksTestFile);


            var progress = new Progress<string> (ProgressReport);
            // do asynchronous execution
            linksActual =
             await routes.GoFindShortestRouteBetween("Basel", "Zürich", TransportModes.Rail, progress);

            // the following assert has to be made after the routine routine returns 
            // assert, that in minimum 5 progress calls are made
            Assert.IsTrue(progCount >= 5, "less than 5 progress calls");

            // See comment in ProgressReport method
            Assert.IsFalse(doneMissing, String.Format("the progress message <{0}>does not contain <done>", failingMessage));
        }
Esempio n. 28
0
        public void TestTask1FindCityInCities()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            try
            {
                var x=cities["noCity"];
                Assert.Fail("Indexer Cities[string] should throw a KeyNotFoundException when the supplied City cannot be found.");
            }
            catch (KeyNotFoundException)
            {
            }

            Assert.AreEqual("Zürich", cities["Zürich"].Name);

            // should be case insensitive
            Assert.AreEqual("Zürich", cities["züRicH"].Name);

            // should be case insensitive, even in "weird" cultures,
            // see http://www.moserware.com/2008/02/does-your-code-pass-turkey-test.html
            var previousCulture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
            Assert.AreEqual("Zürich", cities["züRicH"].Name);
            Assert.AreEqual("Zürich", cities["züRIcH"].Name);
            Thread.CurrentThread.CurrentCulture = previousCulture;

            // should be picky about spaces
            try
            {
                var x=cities["züRicH "];
                Assert.Fail("Indexer Cities[string] should be picky about leading/trailing spaces.");
            }
            catch (KeyNotFoundException)
            {
            }
        }
Esempio n. 29
0
        public void TestTask3_CompareAlgorithms()
        {
            Cities cities = new Cities();

            cities.ReadCities(@"citiesTestDataLab11.txt");
            Assert.AreEqual(6372, cities.Count);

            Routes routes = new RoutesDijkstra(cities);
            long dijkstraTime = FindRoutes(routes);

            routes = new RoutesFloydWarshall(cities);
            routes.ExecuteParallel = false;
            long floydWarshallTime = FindRoutes(routes);

            // the sequentiel floydWarshal should be slower
            Assert.IsTrue(floydWarshallTime > dijkstraTime, "FloydWarshal should be slower");

        }
Esempio n. 30
0
        public void TestTask5_CompareParallelSequential()
        {
            Cities cities = new Cities();

            cities.ReadCities(@"citiesTestDataLab11.txt");
            Assert.AreEqual(6372, cities.Count);

            Routes routes = new RoutesFloydWarshall(cities);
            routes.ExecuteParallel = true;
            long floydWarshallParallelTime = FindRoutes(routes);

            routes = new RoutesFloydWarshall(cities);
            routes.ExecuteParallel = false;
            long floydWarshallTime = FindRoutes(routes);

            // the sequentiel floydWarshal should be slower
            Assert.IsTrue(floydWarshallTime > floydWarshallParallelTime, "FloydWarshal parallel should be faster than sequential");


        }