From e2bc0fac6b4e2164a991128b8a73399ab9acb902 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Sat, 19 Mar 2022 19:50:30 -0400 Subject: [PATCH] add test demonstrating apply does not enumerate all leaves --- gtsam/discrete/DecisionTree-inl.h | 1 - gtsam/discrete/tests/testDecisionTree.cpp | 14 ++++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/gtsam/discrete/DecisionTree-inl.h b/gtsam/discrete/DecisionTree-inl.h index 3f82ce9a6..0ebfc86bc 100644 --- a/gtsam/discrete/DecisionTree-inl.h +++ b/gtsam/discrete/DecisionTree-inl.h @@ -786,7 +786,6 @@ namespace gtsam { template DecisionTree DecisionTree::apply( const UnaryAssignment& op) const { - std::cout << "Calling the correct apply" << std::endl; // It is unclear what should happen if tree is empty: if (empty()) { throw std::runtime_error( diff --git a/gtsam/discrete/tests/testDecisionTree.cpp b/gtsam/discrete/tests/testDecisionTree.cpp index 935d433c6..f234905e3 100644 --- a/gtsam/discrete/tests/testDecisionTree.cpp +++ b/gtsam/discrete/tests/testDecisionTree.cpp @@ -462,7 +462,7 @@ TEST(DecisionTree, ApplyWithAssignment) { DecisionTree probTree( keys, "0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08"); - double threshold = 0.035; + double threshold = 0.045; // We test pruning one tree by indexing into another. auto pruner = [&](const Assignment& choices, const int& x) { @@ -475,8 +475,18 @@ TEST(DecisionTree, ApplyWithAssignment) { }; DT prunedTree = tree.apply(pruner); - DT expectedTree(keys, "0 0 0 4 5 6 7 8"); + DT expectedTree(keys, "0 0 0 0 5 6 7 8"); EXPECT(assert_equal(expectedTree, prunedTree)); + + size_t count = 0; + auto counter = [&](const Assignment& choices, const int& x) { + count += 1; + return x; + }; + DT prunedTree2 = prunedTree.apply(counter); + + // Check if apply doesn't enumerate all leaves. + EXPECT_LONGS_EQUAL(5, count); } /* ************************************************************************* */