[mlpack-git] master: Add tests for spill tree. (061ee92)
gitdub at mlpack.org
gitdub at mlpack.org
Thu Aug 18 13:39:54 EDT 2016
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/0f4b25acd6aaa14294c044874ba6cc0751712baa...0a19d07bd39e6223991976474bc79671ba8aa0f0
>---------------------------------------------------------------
commit 061ee9219258c84e45217ae95bdadb7ea1fafe1d
Author: MarcosPividori <marcos.pividori at gmail.com>
Date: Sat Jul 16 12:50:31 2016 -0300
Add tests for spill tree.
>---------------------------------------------------------------
061ee9219258c84e45217ae95bdadb7ea1fafe1d
src/mlpack/tests/CMakeLists.txt | 1 +
src/mlpack/tests/spill_tree_test.cpp | 122 +++++++++++++++++++++++++++++++++++
2 files changed, 123 insertions(+)
diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt
index 7db9026..1de4903 100644
--- a/src/mlpack/tests/CMakeLists.txt
+++ b/src/mlpack/tests/CMakeLists.txt
@@ -74,6 +74,7 @@ add_executable(mlpack_test
sort_policy_test.cpp
sparse_autoencoder_test.cpp
sparse_coding_test.cpp
+ spill_tree_test.cpp
split_data_test.cpp
termination_policy_test.cpp
tree_test.cpp
diff --git a/src/mlpack/tests/spill_tree_test.cpp b/src/mlpack/tests/spill_tree_test.cpp
new file mode 100644
index 0000000..5deaed0
--- /dev/null
+++ b/src/mlpack/tests/spill_tree_test.cpp
@@ -0,0 +1,122 @@
+/**
+ * @file spill_tree_test.cpp
+ * @author Marcos Pividori
+ *
+ * Tests for the SpillTree class. This should ensure that the class works
+ * correctly and that subsequent changes don't break anything.
+ */
+
+#include <mlpack/core.hpp>
+#include <mlpack/core/tree/spill_tree.hpp>
+#include <boost/test/unit_test.hpp>
+#include <stack>
+
+using namespace mlpack;
+using namespace mlpack::tree;
+using namespace mlpack::metric;
+
+BOOST_AUTO_TEST_SUITE(SpillTreeTest);
+
+/**
+ * Test to make sure the tree contains the correct number of points after
+ * it is constructed. Also, it checks some invariants in the relation between
+ * parent and child nodes.
+ */
+BOOST_AUTO_TEST_CASE(SpillTreeConstructionCountTest)
+{
+ arma::mat dataset;
+ dataset.randu(3, 1000); // 1000 points in 3 dimensions.
+
+ typedef SPTree<EuclideanDistance, EmptyStatistic, arma::mat> TreeType;
+
+ // When overlapping buffer is 0, there shouldn't be repeated points.
+ TreeType tree1(dataset, 0);
+ TreeType tree2 = tree1;
+
+ BOOST_REQUIRE_EQUAL(tree1.NumDescendants(), 1000);
+ BOOST_REQUIRE_EQUAL(tree2.NumDescendants(), 1000);
+
+ // When overlapping buffer is greater than 0, it is possible to have repeated
+ // points. So, let's check node by node, that the number of descendants
+ // equals to the addition of the number of descendants of child nodes.
+ TreeType tree3(dataset, 0.5);
+
+ std::stack<TreeType*> nodes;
+ nodes.push(&tree3);
+ while (!nodes.empty())
+ {
+ TreeType* node = nodes.top();
+ nodes.pop();
+
+ size_t numDesc = node->NumPoints();
+
+ if (node->Left())
+ {
+ nodes.push(node->Left());
+ numDesc += node->Left()->NumDescendants();
+ }
+
+ if (node->Right())
+ {
+ nodes.push(node->Right());
+ numDesc += node->Right()->NumDescendants();
+ }
+
+ if (node->IsLeaf())
+ BOOST_REQUIRE_EQUAL(node->NumPoints(), node->NumDescendants());
+ else
+ BOOST_REQUIRE_EQUAL(node->NumPoints(), 0);
+
+ BOOST_REQUIRE_EQUAL(node->NumDescendants(), numDesc);
+ }
+}
+
+/**
+ * Test to check that parents and children are set correctly.
+ */
+BOOST_AUTO_TEST_CASE(SpillTreeConstructionParentTest)
+{
+ arma::mat dataset;
+ dataset.randu(3, 1000); // 1000 points in 3 dimensions.
+
+ typedef SPTree<EuclideanDistance, EmptyStatistic, arma::mat> TreeType;
+
+ TreeType tree(dataset, 0.5);
+
+ std::stack<TreeType*> nodes;
+ nodes.push(&tree);
+ while (!nodes.empty())
+ {
+ TreeType* node = nodes.top();
+ nodes.pop();
+
+ if (node->Left())
+ {
+ nodes.push(node->Left());
+ BOOST_REQUIRE_EQUAL(node, node->Left()->Parent());
+ }
+
+ if (node->Right())
+ {
+ nodes.push(node->Right());
+ BOOST_REQUIRE_EQUAL(node, node->Right()->Parent());
+ }
+ }
+}
+
+/**
+ * Simple test for the move constructor.
+ */
+BOOST_AUTO_TEST_CASE(SpillTreeMoveDatasetTest)
+{
+ arma::mat dataset = arma::randu<arma::mat>(3, 1000);
+ typedef SPTree<EuclideanDistance, EmptyStatistic, arma::mat> TreeType;
+
+ TreeType tree(std::move(dataset));
+
+ BOOST_REQUIRE_EQUAL(dataset.n_elem, 0);
+ BOOST_REQUIRE_EQUAL(tree.Dataset().n_rows, 3);
+ BOOST_REQUIRE_EQUAL(tree.Dataset().n_cols, 1000);
+}
+
+BOOST_AUTO_TEST_SUITE_END();
More information about the mlpack-git
mailing list