Click here to Skip to main content
11,634,631 members (68,476 online)
Click here to Skip to main content
Add your own
alternative version

Parsing Latitude and Longitude Information

, 21 Feb 2012 CPOL 50.3K 1.4K 72
Parses user input and extracts latitude and longitude information, taking into account the user's language and regional settings
using System;
using Geospatial;
using NUnit.Framework;
using System.Globalization;

namespace UnitTests
{
    [TestFixture]
    class AngleTest
    {
        private const double Delta = 0.000001;

        [Test]
        public void TestDerivedClasses()
        {
            var latitude = Latitude.FromRadians(0);
            var longitude = Longitude.FromRadians(0);
            var angle = Angle.FromRadians(0);

            Assert.IsFalse(latitude == longitude);
            Assert.IsTrue(latitude == angle);
            Assert.IsTrue(longitude == angle);
            Assert.IsTrue(angle == latitude);
            Assert.IsTrue(latitude.Equals(latitude));

            Assert.IsFalse(latitude < longitude);
            Assert.IsFalse(latitude <= longitude);
            Assert.IsFalse(latitude > longitude);
            Assert.IsFalse(latitude >= longitude);

            Assert.IsFalse(longitude < latitude);
            Assert.IsFalse(longitude <= latitude);
            Assert.IsFalse(longitude > latitude);
            Assert.IsFalse(longitude >= latitude);
        }

        [Test]
        public void TestCompareTo()
        {
            Assert.AreEqual(-1, Angle.FromRadians(0).CompareTo(Angle.FromRadians(1)));
            Assert.AreEqual(1, Angle.FromRadians(0).CompareTo(Angle.FromRadians(-1)));
            Assert.AreEqual(0, Angle.FromRadians(0).CompareTo(Angle.FromRadians(0)));

            Assert.AreEqual(1, Angle.FromRadians(0).CompareTo(Angle.FromRadians(double.NegativeInfinity)));
            Assert.AreEqual(1, Angle.FromRadians(0).CompareTo(null));
            Assert.AreEqual(1, Angle.FromRadians(0).CompareTo(Angle.FromRadians(double.NaN)));
            Assert.AreEqual(-1, Angle.FromRadians(0).CompareTo(Angle.FromRadians(double.PositiveInfinity)));
            Assert.AreEqual(-1, Angle.FromRadians(double.NegativeInfinity).CompareTo(Angle.FromRadians(0)));
            Assert.AreEqual(0, Angle.FromRadians(double.NaN).CompareTo(Angle.FromRadians(double.NaN)));

            Assert.IsTrue(Angle.FromRadians(0) < Angle.FromRadians(1));
            Assert.IsTrue(Angle.FromRadians(0) <= Angle.FromRadians(1));
            Assert.IsTrue(Angle.FromRadians(1) <= Angle.FromRadians(1));
            Assert.IsFalse(Angle.FromRadians(0) < Angle.FromRadians(0));
            Assert.IsFalse(Angle.FromRadians(1) < Angle.FromRadians(0));
            Assert.IsFalse(Angle.FromRadians(2) <= Angle.FromRadians(1));

            Assert.IsTrue(Angle.FromRadians(0) > Angle.FromRadians(-1));
            Assert.IsTrue(Angle.FromRadians(0) >= Angle.FromRadians(-1));
            Assert.IsTrue(Angle.FromRadians(-1) >= Angle.FromRadians(-1));
            Assert.IsFalse(Angle.FromRadians(0) > Angle.FromRadians(0));
            Assert.IsFalse(Angle.FromRadians(0) > Angle.FromRadians(1));
            Assert.IsFalse(Angle.FromRadians(0) >= Angle.FromRadians(1));

            Assert.IsFalse(Angle.FromRadians(0) < (Angle)null);
            Assert.IsFalse(Angle.FromRadians(0) <= (Angle)null);
            Assert.IsFalse(Angle.FromRadians(0) >= (Angle)null);
            Assert.IsFalse(Angle.FromRadians(0) > (Angle)null);
        }

        [Test]
        public void TestEquals()
        {
            Angle zero = Angle.FromRadians(0);
            Assert.IsTrue(Angle.FromRadians(0).Equals(zero));
            Assert.IsFalse(Angle.FromRadians(1).Equals(zero));

            object box = zero;
            Assert.IsTrue(zero.Equals(box));
            Assert.IsFalse(zero.Equals(null));
            Assert.IsTrue(Angle.FromRadians(0).Equals(box));

            Assert.IsTrue(Angle.FromRadians(0) == Angle.FromRadians(0));
            Assert.IsTrue((Angle)null == null);
            Assert.IsFalse(Angle.FromRadians(1) == Angle.FromRadians(0));
            Assert.IsFalse(Angle.FromRadians(1) == null);

            Assert.IsTrue(Angle.FromRadians(1) != Angle.FromRadians(0));
            Assert.IsTrue(Angle.FromRadians(1) != null);
            Assert.IsFalse(Angle.FromRadians(0) != Angle.FromRadians(0));
            Assert.IsFalse((Angle)null != null);
        }

        [Test]
        public void TestNegate()
        {
            Assert.Throws<ArgumentNullException>(() => Angle.Negate(null));

            Assert.AreEqual(-1, Angle.Negate(Angle.FromRadians(1)).Radians);
            Assert.AreEqual(1, Angle.Negate(Angle.FromRadians(-1)).Radians);
            Assert.AreEqual(0, Angle.Negate(Angle.FromRadians(0)).Radians);

            // Edge cases
            Assert.AreEqual(double.NaN, Angle.Negate(Angle.FromRadians(double.NaN)).Radians);
            Assert.AreEqual(double.PositiveInfinity, Angle.Negate(Angle.FromRadians(double.NegativeInfinity)).Radians);
            Assert.AreEqual(double.NegativeInfinity, Angle.Negate(Angle.FromRadians(double.PositiveInfinity)).Radians);
        }

        [Test]
        public void TestRadians()
        {
            Assert.AreEqual(Math.PI, Angle.FromDegrees(180).Radians, Delta);
            Assert.AreEqual(-Math.PI, Angle.FromRadians(-Math.PI).Radians, Delta);
            Assert.AreEqual(0, Angle.FromRadians(0).Radians, Delta);
            Assert.AreEqual(0.0, Angle.FromRadians(-0.0).Radians);
        }

        [Test]
        public void TestDegrees()
        {
            // Test basic constructors
            Assert.AreEqual(180, Angle.FromRadians(Math.PI).TotalDegrees, Delta);
            Assert.AreEqual(0, Angle.FromDegrees(0).TotalDegrees, Delta);
            Assert.AreEqual(0, Angle.FromDegrees(0).Radians, Delta);

            // Test degrees minutes
            Assert.AreEqual(0.1, Angle.FromDegrees(0, 6).TotalDegrees, Delta);
            Assert.AreEqual(1, Angle.FromDegrees(0, 60).TotalDegrees, Delta);
            Assert.AreEqual(0, Angle.FromDegrees(0, 60).Minutes, Delta);
            Assert.AreEqual(1, Angle.FromDegrees(1, 0).TotalDegrees, Delta);
            Assert.AreEqual(-1.1, Angle.FromDegrees(-1, -6).TotalDegrees, Delta);

            // Test degrees minutes seconds
            Assert.AreEqual(0.01, Angle.FromDegrees(0, 0, 36).TotalDegrees, Delta);
            Assert.AreEqual(1, Angle.FromDegrees(0, 0, 60).Minutes, Delta);
            Assert.AreEqual(0, Angle.FromDegrees(0, 0, 60).Seconds, Delta);
            Assert.AreEqual(0.1, Angle.FromDegrees(0, 6, 0).TotalDegrees, Delta);
            Assert.AreEqual(1, Angle.FromDegrees(1, 0, 0).TotalDegrees, Delta);
            Assert.AreEqual(-1.01, Angle.FromDegrees(-1, 0, -36).TotalDegrees, Delta);

            // Make sure the rounding works
            Assert.AreEqual(180, Angle.FromDegrees(180).Degrees, Delta);
            Assert.AreEqual(170, Angle.FromDegrees(170.5).Degrees, Delta);
            Assert.AreEqual(-170, Angle.FromDegrees(-170.5).Degrees, Delta);
        }

        [Test]
        public void TestMinutes()
        {
            Assert.AreEqual(0, Angle.FromDegrees(0).TotalMinutes, Delta);
            Assert.AreEqual(0, Angle.FromDegrees(0).Minutes, Delta);
            Assert.AreEqual(150.6, Angle.FromDegrees(2.51).TotalMinutes, Delta);
            Assert.AreEqual(30, Angle.FromDegrees(2.51).Minutes, Delta);
            Assert.AreEqual(-30, Angle.FromDegrees(-2.51).Minutes, Delta);
        }

        [Test]
        public void TestSeconds()
        {
            Assert.AreEqual(0, Angle.FromDegrees(0).TotalSeconds, Delta);
            Assert.AreEqual(0, Angle.FromDegrees(0).Seconds, Delta);
            Assert.AreEqual(9039.6, Angle.FromDegrees(2.511).TotalSeconds, Delta);
            Assert.AreEqual(39.6, Angle.FromDegrees(2.511).Seconds, Delta);
            Assert.AreEqual(-39.6, Angle.FromDegrees(-2.511).Seconds, Delta);
        }

        [Test]
        public void TestToString()
        {
            Angle angle = Angle.FromDegrees(0.1);
            Assert.AreEqual("0,1\u00B0", angle.ToString("D", CultureInfo.GetCultureInfo("es-ES")));
            Assert.AreEqual("0.1\u00B0", angle.ToString("D", CultureInfo.InvariantCulture));
            Assert.AreEqual("0.100\u00B0", angle.ToString("D3", CultureInfo.InvariantCulture));
            Assert.AreEqual("0\u00B0 06\u2032 00\u2033", angle.ToString(null, null));

            Assert.AreEqual("-1\u00B0 00.6\u2032", Angle.FromDegrees(-1.01).ToString("DM", CultureInfo.InvariantCulture));
            Assert.AreEqual("-1\u00B0 00\u2032 36\u2033", Angle.FromDegrees(-1.01).ToString("DMS", CultureInfo.InvariantCulture));

            Assert.Throws<ArgumentException>(() => angle.ToString("ISO", null)); // ISO doesn't define a format for an angle
            Assert.Throws<ArgumentException>(() => angle.ToString("invalid", null));
        }

        [Test]
        public void TestRounding()
        {
            Angle angle = Angle.FromDegrees(15.45);
            Assert.AreEqual(15.45, angle.TotalDegrees);
            Assert.AreEqual(15, angle.Degrees);
            Assert.AreEqual(27, angle.Minutes);
            Assert.AreEqual((15 * 60) + 27, angle.TotalMinutes);
            Assert.AreEqual(0, angle.Seconds);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Samuel Cragg
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150728.1 | Last Updated 21 Feb 2012
Article Copyright 2011 by Samuel Cragg
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid