Skip to content

Commit

Permalink
Adding unit tests
Browse files Browse the repository at this point in the history
Fixing defects uncovered by unit testing
Renaming IProvider.CoordinateSystem to IProvider.SpatialReference for consistency
  • Loading branch information
codekaizen committed Jul 29, 2007
1 parent c943fce commit c542f43
Show file tree
Hide file tree
Showing 16 changed files with 798 additions and 425 deletions.
42 changes: 21 additions & 21 deletions SharpMap.Tests/Geometries/BoundingBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace SharpMap.Tests.Geometries
public class BoundingBoxTests
{
[Test]
public void TestIntersection()
public void IntersectionTest()
{
}

[Test]
public void TestJoin()
public void JoinTest()
{
BoundingBox b1 = new BoundingBox(20, 30, 40, 50);
BoundingBox b2 = new BoundingBox(-20, 56, 70, 75);
Expand All @@ -29,7 +29,7 @@ public void TestJoin()
}

[Test]
public void TestExpandToInclude()
public void ExpandToIncludeTest()
{
BoundingBox b1 = new BoundingBox(20, 30, 40, 50);
BoundingBox b2 = new BoundingBox(-20, 56, 70, 75);
Expand All @@ -43,12 +43,12 @@ public void TestExpandToInclude()
}

[Test]
public void TestOffset()
public void OffsetTest()
{
}

[Test]
public void TestMetricsProperties()
public void MetricsPropertiesTest()
{
BoundingBox b1 = new BoundingBox(20, 30, 40, 55);
Assert.IsFalse(b1.IsEmpty);
Expand All @@ -72,29 +72,29 @@ public void TestMetricsProperties()
}

[Test]
public void TestToString()
public void ToStringTest()
{
BoundingBox b1 = new BoundingBox(20, 30, 40, 55);
Assert.AreEqual("[BoundingBox] Lower Left: (20.00, 30.00) Upper Right: (40.00, 55.00)", b1.ToString());
Assert.AreEqual("[BoundingBox] Empty", BoundingBox.Empty.ToString());
}

[Test]
public void TestGetHashCode()
public void GetHashCodeTest()
{
BoundingBox b1 = new BoundingBox(20, 30, 40, 55);
Assert.AreEqual(b1.GetHashCode(), (int)(b1.Left + b1.Bottom + b1.Right + b1.Top));
}

[Test]
public void TestGetArea()
public void GetAreaTest()
{
BoundingBox b1 = new BoundingBox(20, 30, 40, 55);
Assert.AreEqual(b1.GetArea(), 20 * 25);
}

[Test]
public void TestGetIntersectingArea()
public void GetIntersectingAreaTest()
{
BoundingBox b1 = new BoundingBox(20, 30, 40, 55);
BoundingBox b2 = new BoundingBox(20, 30, 40, 55);
Expand All @@ -106,28 +106,28 @@ public void TestGetIntersectingArea()
}

[Test]
public void TestDistance()
public void DistanceTest()
{
BoundingBox b1 = new BoundingBox(20, 30, 40, 55);
Assert.AreEqual(Math.Sqrt(200), b1.Distance(new BoundingBox(50, 65, 60, 75)));
}

[Test]
public void TestGetCentroid()
public void GetCentroidTest()
{
BoundingBox b1 = new BoundingBox(20, 30, 40, 55);
Assert.AreEqual(new Point(30, 42.5), b1.GetCentroid());
}

[Test]
public void TestGrowAndShrink()
public void GrowAndShrinkTest()
{
BoundingBox b1 = new BoundingBox(20, 30, 40, 55);
Assert.AreEqual(new BoundingBox(19, 29, 41, 56), b1.Grow(1));
}

[Test]
public void TestValueOperators()
public void ValueOperatorsTest()
{
BoundingBox b1 = new BoundingBox(20, 30, 40, 55);
BoundingBox b2 = new BoundingBox(20, 30, 40, 55);
Expand All @@ -140,7 +140,7 @@ public void TestValueOperators()
}

[Test]
public void TestEquality()
public void EqualityTest()
{
BoundingBox b1 = new BoundingBox(20, 30, 40, 55);
BoundingBox b2 = new BoundingBox(20, 30, 40, 55);
Expand All @@ -155,17 +155,17 @@ public void TestEquality()
}

[Test]
public void TestSplit()
public void SplitTest()
{
}

[Test]
public void TestBorders()
public void BordersTest()
{
}

[Test]
public void TestContains()
public void ContainsTest()
{
BoundingBox b1 = new BoundingBox(20, 30, 40, 55);
Assert.IsTrue(b1.Contains(new Point(30, 40)));
Expand All @@ -178,7 +178,7 @@ public void TestContains()
}

[Test]
public void TestIntersect()
public void IntersectTest()
{
//Test disjoint
BoundingBox b1 = new BoundingBox(0, 0, 10, 10);
Expand Down Expand Up @@ -226,17 +226,17 @@ public void TestIntersect()
}

[Test]
public void TestOverlaps()
public void OverlapsTest()
{
}

[Test]
public void TestTouches()
public void TouchesTest()
{
}

[Test]
public void TestWithin()
public void WithinTest()
{
}
}
Expand Down
5 changes: 4 additions & 1 deletion SharpMap.Tests/Indexing/BinaryTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
using System.Collections.Generic;
using System.Text;

using NUnit.Framework;

namespace SharpMap.Tests.IndexingTests
{
class BinaryTreeTests
[TestFixture]
public class BinaryTreeTests
{
}
}
121 changes: 121 additions & 0 deletions SharpMap.Tests/Indexing/RTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,132 @@
using System.Text;

using NUnit.Framework;
using SharpMap.Indexing.RTree;
using SharpMap.Geometries;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace SharpMap.Tests.IndexingTests
{
[TestFixture]
public class RTreeTests
{
[Test]
public void CreateRTreeTest()
{
DynamicRTree<int> rTree = new DynamicRTree<int>(new GuttmanQuadraticInsert<int>(),
new GuttmanQuadraticSplit<int>(), new DynamicRTreeBalanceHeuristic());

rTree.Dispose();
}

[Test]
public void InsertTest()
{
DynamicRTree<int> rTree = new DynamicRTree<int>(new GuttmanQuadraticInsert<int>(),
new GuttmanQuadraticSplit<int>(), new DynamicRTreeBalanceHeuristic());

addEntries(rTree);

Assert.AreEqual(new BoundingBox(-100, -100, 5928.57523425, 3252.50803582), rTree.Root.BoundingBox);

rTree.Dispose();
}

[Test]
public void SearchTest()
{
DynamicRTree<int> rTree = new DynamicRTree<int>(new GuttmanQuadraticInsert<int>(),
new GuttmanQuadraticSplit<int>(), new DynamicRTreeBalanceHeuristic());

addEntries(rTree);

List<RTreeIndexEntry<int>> resultsList = new List<RTreeIndexEntry<int>>();

resultsList.AddRange(rTree.Search(new BoundingBox(-100, -100, 5928.57523425, 3252.50803582)));
Assert.AreEqual(8, resultsList.Count);
resultsList.Clear();

resultsList.AddRange(rTree.Search(new BoundingBox(0, 0, 100, 100)));
Assert.AreEqual(6, resultsList.Count);
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 1; }));
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 2; }));
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 3; }));
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 6; }));
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 7; }));
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 8; }));
resultsList.Clear();

resultsList.AddRange(rTree.Search(new BoundingBox(1500, 1500, 1500, 1500)));
Assert.AreEqual(2, resultsList.Count);
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 4; }));
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 5; }));
resultsList.Clear();

resultsList.AddRange(rTree.Search(new BoundingBox(100, 100, 100, 100)));
Assert.AreEqual(4, resultsList.Count);
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 1; }));
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 2; }));
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 7; }));
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 8; }));
resultsList.Clear();

addRandomEntries(rTree);
resultsList.AddRange(rTree.Search(new BoundingBox(100, 100, 100, 100)));
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 1; }));
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 2; }));
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 7; }));
Assert.IsTrue(resultsList.Exists(delegate(RTreeIndexEntry<int> match) { return match.Value == 8; }));
resultsList.Clear();

rTree.Dispose();
}

[Test]
[Ignore("Saving index broken")]
public void SaveIndexTest()
{
DynamicRTree<int> rTree = new DynamicRTree<int>(new GuttmanQuadraticInsert<int>(),
new GuttmanQuadraticSplit<int>(), new DynamicRTreeBalanceHeuristic());

addRandomEntries(rTree);
MemoryStream s = new MemoryStream();
rTree.SaveIndex(s);
rTree.Dispose();

s.Position = 0;
DynamicRTree<int> rTree2 = DynamicRTree<int>.FromStream(s);
List<RTreeIndexEntry<int>> results = new List<RTreeIndexEntry<int>>();
results.AddRange(rTree2.Search(rTree2.Root.BoundingBox));
Assert.AreEqual(99990, results.Count);
}

private static void addEntries(DynamicRTree<int> rTree)
{
rTree.Insert(new RTreeIndexEntry<int>(1, new BoundingBox(0, 0, 100, 100)));
rTree.Insert(new RTreeIndexEntry<int>(2, new BoundingBox(50, 50, 150, 150)));
rTree.Insert(new RTreeIndexEntry<int>(3, new BoundingBox(-100, -100, 0, 0)));
rTree.Insert(new RTreeIndexEntry<int>(4, new BoundingBox(1000, 1000, 2000, 2000)));
rTree.Insert(new RTreeIndexEntry<int>(5, new BoundingBox(346.23975, 424.5720832, 5928.57523425, 3252.50803582)));
rTree.Insert(new RTreeIndexEntry<int>(6, new BoundingBox(0, 0, 0, 0)));
rTree.Insert(new RTreeIndexEntry<int>(7, new BoundingBox(100, 100, 100, 100)));
rTree.Insert(new RTreeIndexEntry<int>(8, new BoundingBox(0, 0, 100, 100)));
}

private static void addRandomEntries(DynamicRTree<int> rTree)
{
Random rnd = new Random();

for (int i = 10; i < 100000; i++)
{
double xMin = rnd.NextDouble() * (rnd.Next(0, 1) == 1 ? -1 : 1) * rnd.Next();
double xMax = rnd.NextDouble() * (rnd.Next(0, 1) == 1 ? -1 : 1) * rnd.Next();
double yMin = rnd.NextDouble() * (rnd.Next(0, 1) == 1 ? -1 : 1) * rnd.Next();
double yMax = rnd.NextDouble() * (rnd.Next(0, 1) == 1 ? -1 : 1) * rnd.Next();

BoundingBox bounds = new BoundingBox(xMin, yMin, xMax, yMax);
rTree.Insert(new RTreeIndexEntry<int>(i, bounds));
}
}
}
}
12 changes: 12 additions & 0 deletions SharpMap.Tests/Indexing/SelfOptimizingSpatialIndexTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;

namespace SharpMap.Tests.Indexing
{
[TestFixture]
public class SelfOptimizingSpatialIndexTests
{
}
}
Loading

0 comments on commit c542f43

Please sign in to comment.