From 61e3aa2663955e1b7dfd0f528ae2bf4f752f6a09 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Fri, 22 Nov 2013 17:02:09 +0000 Subject: [PATCH] Test BinarySpaceTree::FurthestPointDistance(). --- src/mlpack/tests/tree_test.cpp | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/mlpack/tests/tree_test.cpp b/src/mlpack/tests/tree_test.cpp index 0a726a05b5..023cf5ef1a 100644 --- a/src/mlpack/tests/tree_test.cpp +++ b/src/mlpack/tests/tree_test.cpp @@ -11,6 +11,8 @@ #include #include +#include + #include #include "old_boost_test_definitions.hpp" @@ -1582,6 +1584,52 @@ BOOST_AUTO_TEST_CASE(FurthestDescendantDistanceTest) BOOST_REQUIRE_CLOSE(twoPoint.FurthestDescendantDistance(), sqrt(2.0), 1e-5); } +// Ensure that FurthestPointDistance() works. +BOOST_AUTO_TEST_CASE(FurthestPointDistanceTest) +{ + arma::mat dataset; + dataset.randu(5, 100); + + BinarySpaceTree > tree(dataset); + + // Now, check each node. + std::queue >*> nodeQueue; + nodeQueue.push(&tree); + + while (!nodeQueue.empty()) + { + BinarySpaceTree >* node = nodeQueue.front(); + nodeQueue.pop(); + + if (node->NumChildren() == 0) + BOOST_REQUIRE_EQUAL(node->FurthestPointDistance(), 0.0); + else + { + // Get centroid. + arma::vec centroid; + node->Centroid(centroid); + + double maxDist = 0.0; + for (size_t i = 0; i < node->NumPoints(); ++i) + { + const double dist = metric::EuclideanDistance::Evaluate(centroid, + dataset.col(node->Point(i))); + if (dist > maxDist) + maxDist = dist; + } + + // We don't require an exact value because FurthestPointDistance() can + // just bound the value instead of returning the exact value. + BOOST_REQUIRE_LE(maxDist, node->FurthestPointDistance()); + + if (node->Left()) + nodeQueue.push(node->Left()); + if (node->Right()) + nodeQueue.push(node->Right()); + } + } +} + // Forward declaration of methods we need for the next test. template bool CheckPointBounds(TreeType* node, const MatType& data);