[mlpack-svn] r13236 - mlpack/trunk/src/mlpack/tests
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Jul 16 11:42:17 EDT 2012
Author: rcurtin
Date: 2012-07-16 11:42:16 -0400 (Mon, 16 Jul 2012)
New Revision: 13236
Modified:
mlpack/trunk/src/mlpack/tests/det_test.cpp
Log:
Clean up test. Don't use 'new'/'delete'. Fix wrong index for TestSplitData.
Modified: mlpack/trunk/src/mlpack/tests/det_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/det_test.cpp 2012-07-16 15:41:51 UTC (rev 13235)
+++ mlpack/trunk/src/mlpack/tests/det_test.cpp 2012-07-16 15:42:16 UTC (rev 13236)
@@ -33,38 +33,28 @@
BOOST_AUTO_TEST_CASE(TestGetMaxMinVals)
{
- DTree<>* testDTree = new DTree<>();
+ MatType test_data(3, 5);
- MatType test_data(3,5);
-
test_data << 4 << 5 << 7 << 3 << 5 << arma::endr
<< 5 << 0 << 1 << 7 << 1 << arma::endr
<< 5 << 6 << 7 << 1 << 8 << arma::endr;
- VecType* max_vals = new VecType();
- VecType* min_vals = new VecType();
+ DTree<> tree(test_data);
- testDTree->GetMaxMinVals_(&test_data, max_vals, min_vals);
-
- BOOST_REQUIRE((*max_vals)[0] == 7);
- BOOST_REQUIRE((*min_vals)[0] == 3);
- BOOST_REQUIRE((*max_vals)[1] == 7);
- BOOST_REQUIRE((*min_vals)[1] == 0);
- BOOST_REQUIRE((*max_vals)[2] == 8);
- BOOST_REQUIRE((*min_vals)[2] == 1);
-
- delete testDTree;
+ BOOST_REQUIRE_EQUAL(tree.maxVals[0], 7);
+ BOOST_REQUIRE_EQUAL(tree.minVals[0], 3);
+ BOOST_REQUIRE_EQUAL(tree.maxVals[1], 7);
+ BOOST_REQUIRE_EQUAL(tree.minVals[1], 0);
+ BOOST_REQUIRE_EQUAL(tree.maxVals[2], 8);
+ BOOST_REQUIRE_EQUAL(tree.minVals[2], 1);
}
BOOST_AUTO_TEST_CASE(TestComputeNodeError)
{
- VecType* max_vals = new VecType(3);
- VecType* min_vals = new VecType(3);
+ arma::vec maxVals("7 7 8");
+ arma::vec minVals("3 0 1");
- *max_vals << 7 << 7 << 8;
- *min_vals << 3 << 0 << 1;
-
- DTree<>* testDTree = new DTree<>(max_vals, min_vals, 5);
+ DTree<>* testDTree = new DTree<>(maxVals, minVals, 5);
double true_node_error = -1.0 * exp(-log(4.0) - log(7.0) - log(7.0));
BOOST_REQUIRE_CLOSE((double) testDTree->error_, true_node_error, 1e-10);
@@ -82,24 +72,19 @@
BOOST_AUTO_TEST_CASE(TestWithinRange)
{
- VecType* max_vals = new VecType(3);
- VecType* min_vals = new VecType(3);
+ arma::vec maxVals("7 7 8");
+ arma::vec minVals("3 0 1");
- *max_vals << 7 << 7 << 8;
- *min_vals << 3 << 0 << 1;
+ DTree<> testDTree(maxVals, minVals, 5);
- DTree<>* testDTree = new DTree<>(max_vals, min_vals, 5);
-
VecType test_query(3);
test_query << 4.5 << 2.5 << 2;
- BOOST_REQUIRE(testDTree->WithinRange_(&test_query));
+ BOOST_REQUIRE(testDTree.WithinRange_(&test_query));
test_query << 8.5 << 2.5 << 2;
- BOOST_REQUIRE(!testDTree->WithinRange_(&test_query));
-
- delete testDTree;
+ BOOST_REQUIRE(!testDTree.WithinRange_(&test_query));
}
BOOST_AUTO_TEST_CASE(TestFindSplit)
@@ -110,7 +95,7 @@
<< 5 << 0 << 1 << 7 << 1 << arma::endr
<< 5 << 6 << 7 << 1 << 8 << arma::endr;
- DTree<>* testDTree = new DTree<>(&test_data);
+ DTree<> testDTree(test_data);
size_t ob_dim, true_dim;
double true_left_error, ob_left_error, true_right_error, ob_right_error,
@@ -123,7 +108,7 @@
true_right_error = -1.0 * exp(2 * log(3.0 / 5.0) - (log(7.0) + log(4.0) +
log(2.5)));
- BOOST_REQUIRE(testDTree->FindSplit(test_data, ob_dim, ob_split, ob_left_error,
+ BOOST_REQUIRE(testDTree.FindSplit(test_data, ob_dim, ob_split, ob_left_error,
ob_right_error, 2, 1));
BOOST_REQUIRE(true_dim == ob_dim);
@@ -131,19 +116,17 @@
BOOST_REQUIRE_CLOSE(true_left_error, ob_left_error, 1e-10);
BOOST_REQUIRE_CLOSE(true_right_error, ob_right_error, 1e-10);
-
- delete testDTree;
}
BOOST_AUTO_TEST_CASE(TestSplitData)
{
- MatType test_data(3,5);
+ MatType test_data(3, 5);
test_data << 4 << 5 << 7 << 3 << 5 << arma::endr
<< 5 << 0 << 1 << 7 << 1 << arma::endr
<< 5 << 6 << 7 << 1 << 8 << arma::endr;
- DTree<>* testDTree = new DTree<>(&test_data);
+ DTree<> testDTree(test_data);
arma::Col<size_t> o_test(5);
o_test << 1 << 2 << 3 << 4 << 5;
@@ -151,25 +134,23 @@
size_t split_dim = 2;
double true_split_val = 5.5;
- size_t splitInd = testDTree->SplitData(test_data, split_dim, true_split_val,
+ size_t splitInd = testDTree.SplitData(test_data, split_dim, true_split_val,
o_test);
- BOOST_REQUIRE_EQUAL(splitInd, 1);
+ BOOST_REQUIRE_EQUAL(splitInd, 2); // 2 points on left side.
BOOST_REQUIRE_EQUAL(o_test[0], 1);
BOOST_REQUIRE_EQUAL(o_test[1], 4);
BOOST_REQUIRE_EQUAL(o_test[2], 3);
BOOST_REQUIRE_EQUAL(o_test[3], 2);
BOOST_REQUIRE_EQUAL(o_test[4], 5);
-
- delete testDTree;
}
// the public functions
BOOST_AUTO_TEST_CASE(TestGrow)
{
- MatType test_data(3,5);
+ MatType test_data(3, 5);
test_data << 4 << 5 << 7 << 3 << 5 << arma::endr
<< 5 << 0 << 1 << 7 << 1 << arma::endr
@@ -188,8 +169,8 @@
rl_error = -1.0 * exp(2 * log(1.0 / 5.0) - (log(0.5) + log(4.0) + log(2.5)));
rr_error = -1.0 * exp(2 * log(2.0 / 5.0) - (log(6.5) + log(4.0) + log(2.5)));
- DTree<>* testDTree = new DTree<>(&test_data);
- long double alpha = testDTree->Grow(&test_data, &o_test, false, 2, 1);
+ DTree<> testDTree(test_data);
+ long double alpha = testDTree.Grow(&test_data, &o_test, false, 2, 1);
BOOST_REQUIRE_EQUAL(o_test[0], 0);
BOOST_REQUIRE_EQUAL(o_test[1], 3);
@@ -198,26 +179,26 @@
BOOST_REQUIRE_EQUAL(o_test[4], 4);
// test the structure of the tree
- BOOST_REQUIRE(testDTree->left()->left() == NULL);
- BOOST_REQUIRE(testDTree->left()->right() == NULL);
- BOOST_REQUIRE(testDTree->right()->left()->left() == NULL);
- BOOST_REQUIRE(testDTree->right()->left()->right() == NULL);
- BOOST_REQUIRE(testDTree->right()->right()->left() == NULL);
- BOOST_REQUIRE(testDTree->right()->right()->right() == NULL);
+ BOOST_REQUIRE(testDTree.left()->left() == NULL);
+ BOOST_REQUIRE(testDTree.left()->right() == NULL);
+ BOOST_REQUIRE(testDTree.right()->left()->left() == NULL);
+ BOOST_REQUIRE(testDTree.right()->left()->right() == NULL);
+ BOOST_REQUIRE(testDTree.right()->right()->left() == NULL);
+ BOOST_REQUIRE(testDTree.right()->right()->right() == NULL);
- BOOST_REQUIRE(testDTree->subtree_leaves() == 3);
+ BOOST_REQUIRE(testDTree.subtree_leaves() == 3);
- BOOST_REQUIRE(testDTree->split_dim() == 2);
- BOOST_REQUIRE_CLOSE(testDTree->split_value(), 5.5, 1e-5);
- BOOST_REQUIRE(testDTree->right()->split_dim() == 1);
- BOOST_REQUIRE_CLOSE(testDTree->right()->split_value(), 0.5, 1e-5);
+ BOOST_REQUIRE(testDTree.split_dim() == 2);
+ BOOST_REQUIRE_CLOSE(testDTree.split_value(), 5.5, 1e-5);
+ BOOST_REQUIRE(testDTree.right()->split_dim() == 1);
+ BOOST_REQUIRE_CLOSE(testDTree.right()->split_value(), 0.5, 1e-5);
// test node errors for every node
- BOOST_REQUIRE_CLOSE(testDTree->error_, root_error, 1e-10);
- BOOST_REQUIRE_CLOSE(testDTree->left()->error_, l_error, 1e-10);
- BOOST_REQUIRE_CLOSE(testDTree->right()->error_, r_error, 1e-10);
- BOOST_REQUIRE_CLOSE(testDTree->right()->left()->error_, rl_error, 1e-10);
- BOOST_REQUIRE_CLOSE(testDTree->right()->right()->error_, rr_error, 1e-10);
+ BOOST_REQUIRE_CLOSE(testDTree.error_, root_error, 1e-10);
+ BOOST_REQUIRE_CLOSE(testDTree.left()->error_, l_error, 1e-10);
+ BOOST_REQUIRE_CLOSE(testDTree.right()->error_, r_error, 1e-10);
+ BOOST_REQUIRE_CLOSE(testDTree.right()->left()->error_, rl_error, 1e-10);
+ BOOST_REQUIRE_CLOSE(testDTree.right()->right()->error_, rr_error, 1e-10);
// test alpha
@@ -226,13 +207,11 @@
r_alpha = r_error - (rl_error + rr_error);
BOOST_REQUIRE_CLOSE(alpha, min(root_alpha, r_alpha), 1e-10);
-
- delete testDTree;
}
BOOST_AUTO_TEST_CASE(TestPruneAndUpdate)
{
- MatType test_data(3,5);
+ MatType test_data(3, 5);
test_data << 4 << 5 << 7 << 3 << 5 << arma::endr
<< 5 << 0 << 1 << 7 << 1 << arma::endr
@@ -240,26 +219,24 @@
arma::Col<size_t> o_test(5);
o_test << 0 << 1 << 2 << 3 << 4;
- DTree<>* testDTree = new DTree<>(&test_data);
- long double alpha = testDTree->Grow(&test_data, &o_test, false, 2, 1);
- alpha = testDTree->PruneAndUpdate(alpha, false);
+ DTree<> testDTree(test_data);
+ long double alpha = testDTree.Grow(&test_data, &o_test, false, 2, 1);
+ alpha = testDTree.PruneAndUpdate(alpha, false);
BOOST_REQUIRE_CLOSE(alpha, numeric_limits<long double>::max(), 1e-10);
- BOOST_REQUIRE(testDTree->subtree_leaves() == 1);
+ BOOST_REQUIRE(testDTree.subtree_leaves() == 1);
long double root_error = -1.0 * exp(-log(4.0) - log(7.0) - log(7.0));
- BOOST_REQUIRE_CLOSE(testDTree->error(), root_error, 1e-10);
- BOOST_REQUIRE_CLOSE(testDTree->subtree_leaves_error(), root_error, 1e-10);
- BOOST_REQUIRE(testDTree->left() == NULL);
- BOOST_REQUIRE(testDTree->right() == NULL);
-
- delete testDTree;
+ BOOST_REQUIRE_CLOSE(testDTree.error(), root_error, 1e-10);
+ BOOST_REQUIRE_CLOSE(testDTree.subtree_leaves_error(), root_error, 1e-10);
+ BOOST_REQUIRE(testDTree.left() == NULL);
+ BOOST_REQUIRE(testDTree.right() == NULL);
}
BOOST_AUTO_TEST_CASE(TestComputeValue)
{
- MatType test_data(3,5);
+ MatType test_data(3, 5);
test_data << 4 << 5 << 7 << 3 << 5 << arma::endr
<< 5 << 0 << 1 << 7 << 1 << arma::endr
@@ -275,34 +252,32 @@
arma::Col<size_t> o_test(5);
o_test << 0 << 1 << 2 << 3 << 4;
- DTree<>* testDTree = new DTree<>(&test_data);
- long double alpha = testDTree->Grow(&test_data, &o_test, false, 2, 1);
+ DTree<> testDTree(test_data);
+ long double alpha = testDTree.Grow(&test_data, &o_test, false, 2, 1);
long double d1, d2, d3;
d1 = (2.0 / 5.0) / exp(log(4.0) + log(7.0) + log(4.5));
d2 = (1.0 / 5.0) / exp(log(4.0) + log(0.5) + log(2.5));
d3 = (2.0 / 5.0) / exp(log(4.0) + log(6.5) + log(2.5));
- BOOST_REQUIRE_CLOSE(d1, testDTree->ComputeValue(&q1), 1e-10);
- BOOST_REQUIRE_CLOSE(d2, testDTree->ComputeValue(&q2), 1e-10);
- BOOST_REQUIRE_CLOSE(d3, testDTree->ComputeValue(&q3), 1e-10);
- BOOST_REQUIRE_CLOSE((long double) 0.0, testDTree->ComputeValue(&q4), 1e-10);
+ BOOST_REQUIRE_CLOSE(d1, testDTree.ComputeValue(&q1), 1e-10);
+ BOOST_REQUIRE_CLOSE(d2, testDTree.ComputeValue(&q2), 1e-10);
+ BOOST_REQUIRE_CLOSE(d3, testDTree.ComputeValue(&q3), 1e-10);
+ BOOST_REQUIRE_CLOSE((long double) 0.0, testDTree.ComputeValue(&q4), 1e-10);
- alpha = testDTree->PruneAndUpdate(alpha, false);
+ alpha = testDTree.PruneAndUpdate(alpha, false);
long double d = 1.0 / exp(log(4.0) + log(7.0) + log(7.0));
- BOOST_REQUIRE_CLOSE(d, testDTree->ComputeValue(&q1), 1e-10);
- BOOST_REQUIRE_CLOSE(d, testDTree->ComputeValue(&q2), 1e-10);
- BOOST_REQUIRE_CLOSE(d, testDTree->ComputeValue(&q3), 1e-10);
- BOOST_REQUIRE_CLOSE((long double) 0.0, testDTree->ComputeValue(&q4), 1e-10);
-
- delete testDTree;
+ BOOST_REQUIRE_CLOSE(d, testDTree.ComputeValue(&q1), 1e-10);
+ BOOST_REQUIRE_CLOSE(d, testDTree.ComputeValue(&q2), 1e-10);
+ BOOST_REQUIRE_CLOSE(d, testDTree.ComputeValue(&q3), 1e-10);
+ BOOST_REQUIRE_CLOSE((long double) 0.0, testDTree.ComputeValue(&q4), 1e-10);
}
BOOST_AUTO_TEST_CASE(TestVariableImportance)
{
- MatType test_data(3,5);
+ MatType test_data(3, 5);
test_data << 4 << 5 << 7 << 3 << 5 << arma::endr
<< 5 << 0 << 1 << 7 << 1 << arma::endr
@@ -321,21 +296,19 @@
arma::Col<size_t> o_test(5);
o_test << 0 << 1 << 2 << 3 << 4;
- DTree<>* testDTree = new DTree<>(&test_data);
- testDTree->Grow(&test_data, &o_test, false, 2, 1);
+ DTree<> testDTree(test_data);
+ testDTree.Grow(&test_data, &o_test, false, 2, 1);
arma::vec imps(3);
imps.zeros();
- testDTree->ComputeVariableImportance(&imps);
+ testDTree.ComputeVariableImportance(&imps);
BOOST_REQUIRE_CLOSE((double) 0.0, imps[0], 1e-10);
BOOST_REQUIRE_CLOSE((double) (r_error - (rl_error + rr_error)), imps[1],
1e-10);
BOOST_REQUIRE_CLOSE((double) (root_error - (l_error + r_error)), imps[2],
1e-10);
-
- delete testDTree;
}
/**
More information about the mlpack-svn
mailing list