Skip to content
This repository was archived by the owner on Jun 22, 2023. It is now read-only.

Commit c9cef8b

Browse files
committed
Added unit tests #24
Some modifications to build scripts and projects
1 parent 8803609 commit c9cef8b

18 files changed

+797
-25
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using GeoAPI.DataStructures;
2+
using NUnit.Framework;
3+
4+
namespace GeoAPI.Tests.DataStructures
5+
{
6+
[TestFixtureAttribute]
7+
public class IntervalTest
8+
{
9+
[TestAttribute]
10+
public void TestIntersectsBasic()
11+
{
12+
Assert.IsTrue(Interval.Create(5, 10).Overlaps(Interval.Create(7, 12)));
13+
Assert.IsTrue(Interval.Create(7, 12).Overlaps(Interval.Create(5, 10)));
14+
Assert.IsTrue(!Interval.Create(5, 10).Overlaps(Interval.Create(11, 12)));
15+
Assert.IsTrue(!Interval.Create(11, 12).Overlaps(Interval.Create(5, 10)));
16+
Assert.IsTrue(Interval.Create(5, 10).Overlaps(Interval.Create(10, 12)));
17+
Assert.IsTrue(Interval.Create(10, 12).Overlaps(Interval.Create(5, 10)));
18+
}
19+
20+
[TestAttribute]
21+
public void TestIntersectsZeroWidthInterval()
22+
{
23+
Assert.IsTrue(Interval.Create(10).Overlaps(Interval.Create(7, 12)));
24+
Assert.IsTrue(Interval.Create(7, 12).Overlaps(Interval.Create(10)));
25+
Assert.IsTrue(!Interval.Create(10).Overlaps(Interval.Create(11, 12)));
26+
Assert.IsTrue(!Interval.Create(11, 12).Overlaps(Interval.Create(10)));
27+
Assert.IsTrue(Interval.Create(10).Overlaps(Interval.Create(10, 12)));
28+
Assert.IsTrue(Interval.Create(10, 12).Overlaps(Interval.Create(10)));
29+
}
30+
31+
[TestAttribute]
32+
public void TestCopyConstructor()
33+
{
34+
Assert.IsTrue(IntervalsAreEqual(Interval.Create(3, 4), Interval.Create(3, 4)));
35+
Assert.IsTrue(IntervalsAreEqual(Interval.Create(3, 4), Interval.Create(Interval.Create(3, 4))));
36+
}
37+
38+
[TestAttribute]
39+
public void TestCentre()
40+
{
41+
Assert.AreEqual(6.5, Interval.Create(4, 9).Centre, 1E-10);
42+
}
43+
44+
[TestAttribute]
45+
public void TestExpandToInclude()
46+
{
47+
var expected = Interval.Create(3, 8);
48+
var actual = Interval.Create(3, 4);
49+
actual = actual.ExpandedByInterval(Interval.Create(7, 8));
50+
51+
Assert.AreEqual(expected.Min, actual.Min);
52+
Assert.AreEqual(expected.Max, actual.Max);
53+
54+
expected = Interval.Create(3, 7);
55+
actual = Interval.Create(3, 7);
56+
actual = actual.ExpandedByInterval(Interval.Create(4, 5));
57+
58+
Assert.AreEqual(expected.Min, actual.Min);
59+
Assert.AreEqual(expected.Max, actual.Max);
60+
61+
expected = Interval.Create(3, 8);
62+
actual = Interval.Create(3, 7);
63+
actual = actual.ExpandedByInterval(Interval.Create(4, 8));
64+
65+
Assert.AreEqual(expected.Min, actual.Min);
66+
Assert.AreEqual(expected.Max, actual.Max);
67+
}
68+
69+
// Added a method for comparing intervals, because the Equals method had not been overriden on Interval
70+
private static bool IntervalsAreEqual(Interval expected, Interval actual)
71+
{
72+
return expected.Min == actual.Min && expected.Max == actual.Max && expected.Width == actual.Width;
73+
}
74+
}
75+
}

GeoAPI.Tests/GeoAPI.Tests.csproj

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{A2CC5645-37A7-4762-ACC4-6C8AA18B1574}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>GeoAPI.Tests</RootNamespace>
11+
<AssemblyName>GeoAPI.Tests</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="nunit.framework, Version=3.6.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
34+
<HintPath>..\packages\NUnit.3.6.0\lib\net45\nunit.framework.dll</HintPath>
35+
<Private>True</Private>
36+
</Reference>
37+
<Reference Include="System" />
38+
</ItemGroup>
39+
<ItemGroup>
40+
<Compile Include="Geometries\CoordinateTest.cs" />
41+
<Compile Include="Geometries\EnvelopeTest.cs" />
42+
<Compile Include="DataStructures\IntervalTest.cs" />
43+
<Compile Include="Properties\AssemblyInfo.cs" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<ProjectReference Include="..\GeoAPI\GeoAPI.csproj">
47+
<Project>{ffb69466-79de-466a-ada7-5c47c5c5ca3a}</Project>
48+
<Name>GeoAPI</Name>
49+
</ProjectReference>
50+
</ItemGroup>
51+
<ItemGroup>
52+
<None Include="packages.config" />
53+
</ItemGroup>
54+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
55+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
56+
Other similar extension points exist, see Microsoft.Common.targets.
57+
<Target Name="BeforeBuild">
58+
</Target>
59+
<Target Name="AfterBuild">
60+
</Target>
61+
-->
62+
</Project>
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
using System;
2+
using GeoAPI.Geometries;
3+
using NUnit.Framework;
4+
5+
namespace GeoAPI.Tests.Geometries
6+
{
7+
public class CoordinateTest
8+
{
9+
[TestAttribute]
10+
public void TestConstructor3D()
11+
{
12+
Coordinate c = new Coordinate(350.2, 4566.8, 5266.3);
13+
Assert.AreEqual(c.X, 350.2);
14+
Assert.AreEqual(c.Y, 4566.8);
15+
Assert.AreEqual(c.Z, 5266.3);
16+
}
17+
18+
[TestAttribute]
19+
public void TestConstructor2D()
20+
{
21+
Coordinate c = new Coordinate(350.2, 4566.8);
22+
Assert.AreEqual(c.X, 350.2);
23+
Assert.AreEqual(c.Y, 4566.8);
24+
Assert.AreEqual(c.Z, Coordinate.NullOrdinate);
25+
}
26+
27+
[TestAttribute]
28+
public void TestDefaultConstructor()
29+
{
30+
Coordinate c = new Coordinate();
31+
Assert.AreEqual(c.X, 0.0);
32+
Assert.AreEqual(c.Y, 0.0);
33+
Assert.AreEqual(c.Z, Coordinate.NullOrdinate);
34+
}
35+
36+
[TestAttribute]
37+
public void TestCopyConstructor3D()
38+
{
39+
Coordinate orig = new Coordinate(350.2, 4566.8, 5266.3);
40+
Coordinate c = new Coordinate(orig);
41+
Assert.AreEqual(c.X, 350.2);
42+
Assert.AreEqual(c.Y, 4566.8);
43+
Assert.AreEqual(c.Z, 5266.3);
44+
}
45+
46+
[TestAttribute]
47+
public void TestSetCoordinate()
48+
{
49+
Coordinate orig = new Coordinate(350.2, 4566.8, 5266.3);
50+
Coordinate c = new Coordinate { CoordinateValue = orig };
51+
Assert.AreEqual(c.X, 350.2);
52+
Assert.AreEqual(c.Y, 4566.8);
53+
Assert.AreEqual(c.Z, 5266.3);
54+
}
55+
56+
[TestAttribute]
57+
public void TestGetOrdinate()
58+
{
59+
Coordinate c = new Coordinate(350.2, 4566.8, 5266.3);
60+
Assert.AreEqual(c[Ordinate.X], 350.2);
61+
Assert.AreEqual(c[Ordinate.Y], 4566.8);
62+
Assert.AreEqual(c[Ordinate.Z], 5266.3);
63+
}
64+
65+
[TestAttribute]
66+
public void TestSetOrdinate()
67+
{
68+
Coordinate c = new Coordinate();
69+
c[Ordinate.X] = 111;
70+
c[Ordinate.Y] = 222;
71+
c[Ordinate.Z] = 333;
72+
Assert.AreEqual(c[Ordinate.X], 111.0);
73+
Assert.AreEqual(c[Ordinate.Y], 222.0);
74+
Assert.AreEqual(c[Ordinate.Z], 333.0);
75+
}
76+
77+
[TestAttribute]
78+
public void TestEquals()
79+
{
80+
Coordinate c1 = new Coordinate(1, 2, 3);
81+
const string s = "Not a coordinate";
82+
Assert.IsFalse(c1.Equals(s));
83+
84+
Coordinate c2 = new Coordinate(1, 2, 3);
85+
Assert.IsTrue(c1.Equals2D(c2));
86+
87+
Coordinate c3 = new Coordinate(1, 22, 3);
88+
Assert.IsFalse(c1.Equals2D(c3));
89+
}
90+
91+
[TestAttribute]
92+
public void TestEquals2D()
93+
{
94+
Coordinate c1 = new Coordinate(1, 2, 3);
95+
Coordinate c2 = new Coordinate(1, 2, 3);
96+
Assert.IsTrue(c1.Equals2D(c2));
97+
98+
Coordinate c3 = new Coordinate(1, 22, 3);
99+
Assert.IsFalse(c1.Equals2D(c3));
100+
}
101+
102+
[TestAttribute]
103+
public void TestEquals3D()
104+
{
105+
Coordinate c1 = new Coordinate(1, 2, 3);
106+
Coordinate c2 = new Coordinate(1, 2, 3);
107+
Assert.IsTrue(c1.Equals3D(c2));
108+
109+
Coordinate c3 = new Coordinate(1, 22, 3);
110+
Assert.IsFalse(c1.Equals3D(c3));
111+
}
112+
113+
[TestAttribute]
114+
public void TestEquals2DWithinTolerance()
115+
{
116+
Coordinate c = new Coordinate(100.0, 200.0, 50.0);
117+
Coordinate aBitOff = new Coordinate(100.1, 200.1, 50.0);
118+
Assert.IsTrue(c.Equals2D(aBitOff, 0.2));
119+
}
120+
121+
[TestAttribute]
122+
public void TestEqualsInZ()
123+
{
124+
125+
Coordinate c = new Coordinate(100.0, 200.0, 50.0);
126+
Coordinate withSameZ = new Coordinate(100.1, 200.1, 50.1);
127+
Assert.IsTrue(c.EqualInZ(withSameZ, 0.2));
128+
}
129+
130+
[TestAttribute]
131+
public void TestCompareTo()
132+
{
133+
Coordinate lowest = new Coordinate(10.0, 100.0, 50.0);
134+
Coordinate highest = new Coordinate(20.0, 100.0, 50.0);
135+
Coordinate equalToHighest = new Coordinate(20.0, 100.0, 50.0);
136+
Coordinate higherStill = new Coordinate(20.0, 200.0, 50.0);
137+
138+
Assert.AreEqual(-1, lowest.CompareTo(highest));
139+
Assert.AreEqual(1, highest.CompareTo(lowest));
140+
Assert.AreEqual(-1, highest.CompareTo(higherStill));
141+
Assert.AreEqual(0, highest.CompareTo(equalToHighest));
142+
}
143+
144+
[TestAttribute]
145+
public void TestToString()
146+
{
147+
const string expectedResult = "(100, 200, 50)";
148+
String actualResult = new Coordinate(100, 200, 50).ToString();
149+
Assert.AreEqual(expectedResult, actualResult);
150+
}
151+
152+
[TestAttribute]
153+
public void TestClone()
154+
{
155+
Coordinate c = new Coordinate(100.0, 200.0, 50.0);
156+
Coordinate clone = (Coordinate)c.Clone();
157+
Assert.IsTrue(c.Equals3D(clone));
158+
}
159+
160+
[TestAttribute]
161+
public void TestDistance()
162+
{
163+
Coordinate coord1 = new Coordinate(0.0, 0.0, 0.0);
164+
Coordinate coord2 = new Coordinate(100.0, 200.0, 50.0);
165+
double distance = coord1.Distance(coord2);
166+
Assert.AreEqual(distance, 223.60679774997897, 0.00001);
167+
}
168+
169+
[TestAttribute]
170+
public void TestDistance3D()
171+
{
172+
Coordinate coord1 = new Coordinate(0.0, 0.0, 0.0);
173+
Coordinate coord2 = new Coordinate(100.0, 200.0, 50.0);
174+
double distance = coord1.Distance3D(coord2);
175+
Assert.AreEqual(distance, 229.128784747792, 0.000001);
176+
}
177+
178+
[TestAttribute]
179+
public void TestSettingOrdinateValuesViaIndexer()
180+
{
181+
var c = new Coordinate();
182+
Assert.DoesNotThrow(() => c[Ordinate.X] = 1);
183+
Assert.AreEqual(1d, c.X);
184+
Assert.AreEqual(c.X, c[Ordinate.X]);
185+
186+
Assert.DoesNotThrow(() => c[Ordinate.Y] = 2);
187+
Assert.AreEqual(2d, c.Y);
188+
Assert.AreEqual(c.Y, c[Ordinate.Y]);
189+
190+
Assert.DoesNotThrow(() => c[Ordinate.Z] = 3);
191+
Assert.AreEqual(3d, c.Z);
192+
Assert.AreEqual(c.Z, c[Ordinate.Z]);
193+
194+
Assert.Throws<ArgumentOutOfRangeException>(() => c[Ordinate.M] = 4);
195+
}
196+
}
197+
}

0 commit comments

Comments
 (0)