public void TestFromValidDegrees() { const int runs = 10; for (int i = 0; i < runs; ++i) { var degreeLongitude = GenerateValidValue(GeographicalLocation.MAX_DEGREE_LONGITUDE); var degreeLatitude = GenerateValidValue(GeographicalLocation.MAX_DEGREE_LATITUDE); var radianLongitude = Calculator.DegreesToRadians(degreeLongitude); var radianLatitude = Calculator.DegreesToRadians(degreeLatitude); var location = GeographicalLocation.FromDegrees(degreeLongitude, degreeLatitude); Assert.IsTrue(location.DegreeLongitude == degreeLongitude, String.Format("Location degree longitude is not equal to the expected degree longitude! Expected: {0}, got: {1}.", degreeLongitude, location.DegreeLongitude)); Assert.IsTrue(location.DegreeLatitude == degreeLatitude, String.Format("Location degree latitude is not equal to the expected degree latitude! Expected: {0}, got: {1}.", degreeLatitude, location.DegreeLatitude)); Assert.IsTrue(location.RadianLongitude == radianLongitude, String.Format("Location radian longitude is not equal to the expected radian longitude! Expected: {0}, got: {1}.", radianLongitude, location.RadianLongitude)); Assert.IsTrue(location.RadianLatitude == radianLatitude, String.Format("Location radian latitude is not equal to the expected radian latitude! Expected: {0}, got: {1}.", radianLatitude, location.RadianLatitude)); } }
public void TestFromInvalidDegreeLatitude() { var degreeLongitude = GenerateValidValue(GeographicalLocation.MAX_DEGREE_LONGITUDE); var degreeLatitude = GenerateInvalidValue(GeographicalLocation.MAX_DEGREE_LATITUDE); var location = GeographicalLocation.FromDegrees(degreeLongitude, degreeLatitude); }
/// <summary> /// Method used to parse a single JSON object and return customer data. /// </summary> /// <param name="match">Regex match.</param> /// <returns>Customer data.</returns> /// <exception cref="IntercomTestException">Thrown if the JSON object is not properly formatted.</exception> private Customer ParseJsonObject(Match match) { if (ReferenceEquals(match, null)) { throw new IntercomTestException("JSON text is in invalid format!"); } var degreeLongitude = ReadDegreeLongitude(match); if (!GeographicalLocation.IsDegreeLongitudeValid(degreeLongitude)) { throw new IntercomTestException(String.Format("Invalid geographical longitude read: {0}!", degreeLongitude)); } var degreeLatitude = ReadDegreeLatitude(match); if (!GeographicalLocation.IsDegreeLatitudeValid(degreeLatitude)) { throw new IntercomTestException(String.Format("Invalid geographical latitude read: {0}!", degreeLatitude)); } var userId = ReadUserId(match); var customerName = ReadCustomerName(match); var location = GeographicalLocation.FromDegrees(degreeLongitude, degreeLatitude); return(new Customer(userId, customerName, location)); }
public void TestCustomerNameEmpty() { var userId = 0; var location = GeographicalLocation.FromDegrees(0.0d, 0.0d); string name = string.Empty; var customer = new Customer(userId, name, location); }
public void TestCustomerNameWhitespace() { const char whitespaceCharacter = ' '; var userId = 0; var location = GeographicalLocation.FromDegrees(0.0d, 0.0d); var random = new Random(Environment.TickCount); var numberOfWhitespaces = random.Next(1, 20); var name = new string(whitespaceCharacter, numberOfWhitespaces); var customer = new Customer(userId, name, location); }
public void TestCustomerValidInstantiation() { var userId = 0; var name = "John Doe"; var location = GeographicalLocation.FromDegrees(0.0d, 0.0d); var customer = new Customer(userId, name, location); Assert.AreNotEqual(customer, null, "IntercomTest.Customer class has not been instantiated, even though all parameters are valid!"); Assert.AreEqual(customer.UserId, userId, string.Format("IntercomTest.Customer class instance contains invalid data! Expected user ID: {0}, got {1}", userId, customer.UserId)); Assert.AreEqual(customer.Name, name.Trim(), string.Format("IntercomTest.Customer class instance contains invalid data! Expected name: {0}, got {1}", name, customer.Name)); Assert.AreEqual(customer.Location, location, "IntercomTest.Customer class instance contains invalid data. Location references do not match."); }
public void TestStaticCalculateDistance() { var fromDegreeLongitude = -6.257664d; var fromDegreeLatitude = 53.339428d; double[] toDegreeLongitudes = { -6.043701d, -10.27699d, -10.4240951d }; double[] toDegreeLatitudes = { 52.986375d, 51.92893d, 51.8856167d }; double[] expectedDistances = { 41.77d, 313.24d, 324.36d }; var fromLocation = GeographicalLocation.FromDegrees(fromDegreeLongitude, fromDegreeLatitude); for (int i = 0; i < toDegreeLongitudes.Length; ++i) { var toLocation = GeographicalLocation.FromDegrees(toDegreeLongitudes[i], toDegreeLatitudes[i]); var distance = GeographicalLocation.CalculateDistance(fromLocation, toLocation); Assert.IsTrue(distance - expectedDistances[i] < DISTANCE_EPSILON, string.Format("Wrong distance calculated! Expected: {0}, got: {1}.", expectedDistances[i], distance)); } }
static void Main(string[] args) { if (args.Length == 1) { if (string.IsNullOrWhiteSpace(args[0])) { PrintHelp(); } Console.WriteLine(); Console.WriteLine("Closing the program. Press any key to continue..."); Console.ReadKey(); return; } if (args.Length > 1) { PrintHelp(); Console.WriteLine(); Console.WriteLine("Closing the program. Press any key to continue..."); Console.ReadKey(); return; } try { var dublinOfficeLocation = GeographicalLocation.FromDegrees( ConfigurationReader.DublinOfficeDegreeLongitude, ConfigurationReader.DublinOfficeDegreeLatitude); var reader = CreateReader(); var customers1 = reader.ReadCustomers(); var customers2 = customers1?.Where(customer => dublinOfficeLocation.DistanceFrom(customer.Location) < ConfigurationReader.InvitationDistanceKilometers); var customers = customers2?.OrderBy(customer => customer.UserId); if (ReferenceEquals(customers, null)) { Console.WriteLine("An error has occurred! Customer info was note read."); return; } Console.WriteLine("Customers to invite:"); foreach (var customer in customers) { Console.WriteLine("Name: {0}, user ID: {1}, distance: {2:0.00} km", customer.Name, customer.UserId, customer.Location.DistanceFrom(dublinOfficeLocation)); } } catch (IntercomTestException ite) { Console.WriteLine(string.Format("An error has ocurred while trying to read user information! Detailed information: {0}", ite.IntercomTestErrorMessage)); if (ite.HasInnerException) { Console.WriteLine("Inner exception: " + ite.InnerException.Message); } Console.WriteLine(); PrintHelp(); } catch (ArgumentNullException ane) { Console.WriteLine("An internal erro has occurred!"); Console.WriteLine(ane.Message); Console.WriteLine(); PrintHelp(); } catch (ArgumentException ae) { Console.WriteLine("An internal erro has occurred!"); Console.WriteLine(ae.Message); Console.WriteLine(); PrintHelp(); } catch (DirectoryNotFoundException dnfe) { Console.WriteLine("Customer file directory not found!"); Console.WriteLine(dnfe.Message); Console.WriteLine(); PrintHelp(); } catch (FileNotFoundException fnfe) { Console.WriteLine("Customer file not found!"); Console.WriteLine(fnfe.Message); Console.WriteLine(); PrintHelp(); } catch (PathTooLongException ptle) { Console.WriteLine("Customer file path is too long!"); Console.WriteLine(ptle.Message); Console.WriteLine(); PrintHelp(); } catch (UnauthorizedAccessException uae) { Console.WriteLine("The current user does not have the perrmission to access the customer file" + "or the operation is not supported beacuse the file is read-only!"); Console.WriteLine(uae.Message); Console.WriteLine(); PrintHelp(); } catch (NotSupportedException nse) { Console.WriteLine("Reading the customer file is not supported!. This may happen path is in invalid format."); Console.WriteLine(nse.Message); Console.WriteLine(); PrintHelp(); } catch (SecurityException se) { Console.WriteLine("The current user does not have the perrmission to access the customer file!"); Console.WriteLine(se.Message); Console.WriteLine(); PrintHelp(); } finally { Console.WriteLine(); Console.WriteLine("Closing the program. Press any key to continue..."); Console.ReadKey(); } }