| | 1 | | using NUnit.Framework; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.TestTools; |
| | 7 | |
|
| | 8 | | public class KDTreeTest { |
| | 9 | | [Test] |
| 1 | 10 | | public void TestEmpty() { |
| 1 | 11 | | KDTree<Vector2> tree = new KDTree<Vector2>(new List<Vector2>(), (Vector2 point) => point); |
| 1 | 12 | | Assert.AreEqual(tree.NearestNeighbor(Vector2.zero), Vector2.zero); |
| 1 | 13 | | Assert.AreEqual(tree.NearestNeighbor(new Vector2(1, 1)), Vector2.zero); |
| 1 | 14 | | } |
| | 15 | |
|
| | 16 | | [Test] |
| 1 | 17 | | public void TestSingle() { |
| 1 | 18 | | KDTree<Vector2> tree = |
| 13 | 19 | | new KDTree<Vector2>(new List<Vector2> { new Vector2(1, 1) }, (Vector2 point) => point); |
| 1 | 20 | | Assert.AreEqual(tree.NearestNeighbor(Vector2.zero), new Vector2(1, 1)); |
| 1 | 21 | | Assert.AreEqual(tree.NearestNeighbor(new Vector2(1, 6)), new Vector2(1, 1)); |
| 1 | 22 | | Assert.AreEqual(tree.NearestNeighbor(new Vector2(3, 2)), new Vector2(1, 1)); |
| 1 | 23 | | Assert.AreEqual(tree.NearestNeighbor(new Vector2(9, 9)), new Vector2(1, 1)); |
| 1 | 24 | | } |
| | 25 | |
|
| | 26 | | [Test] |
| 1 | 27 | | public void TestNearestNeighborWithinPoints() { |
| 1 | 28 | | List<Vector2> points = new List<Vector2>(); |
| 35 | 29 | | for (int i = 0; i <= 10; ++i) { |
| 385 | 30 | | for (int j = 0; j <= 10; ++j) { |
| 121 | 31 | | points.Add(new Vector2(i, j)); |
| 121 | 32 | | } |
| 11 | 33 | | } |
| 1160 | 34 | | KDTree<Vector2> tree = new KDTree<Vector2>(points, (Vector2 point) => point); |
| 1 | 35 | | Assert.AreEqual(tree.NearestNeighbor(Vector2.zero), Vector2.zero); |
| 1 | 36 | | Assert.AreEqual(tree.NearestNeighbor(new Vector2(1, 6)), new Vector2(1, 6)); |
| 1 | 37 | | Assert.AreEqual(tree.NearestNeighbor(new Vector2(3, 2)), new Vector2(3, 2)); |
| 1 | 38 | | Assert.AreEqual(tree.NearestNeighbor(new Vector2(9, 9)), new Vector2(9, 9)); |
| 1 | 39 | | } |
| | 40 | |
|
| | 41 | | [Test] |
| 1 | 42 | | public void TestNearestNeighborAroundPoints() { |
| 1 | 43 | | List<Vector2> points = new List<Vector2>(); |
| 35 | 44 | | for (int i = 0; i <= 10; ++i) { |
| 385 | 45 | | for (int j = 0; j <= 10; ++j) { |
| 121 | 46 | | points.Add(new Vector2(i, j)); |
| 121 | 47 | | } |
| 11 | 48 | | } |
| 1240 | 49 | | KDTree<Vector2> tree = new KDTree<Vector2>(points, (Vector2 point) => point); |
| 1 | 50 | | Assert.AreEqual(tree.NearestNeighbor(Vector2.zero), Vector2.zero); |
| 1 | 51 | | Assert.AreEqual(tree.NearestNeighbor(new Vector2(1.4f, 1.2f)), new Vector2(1, 1)); |
| 1 | 52 | | Assert.AreEqual(tree.NearestNeighbor(new Vector2(2.7f, 2.2f)), new Vector2(3, 2)); |
| 1 | 53 | | Assert.AreEqual(tree.NearestNeighbor(new Vector2(9.05f, 8.61f)), new Vector2(9, 9)); |
| 1 | 54 | | } |
| | 55 | | } |