#include "tree.h" #define BOOST_TEST_MODULE TreeTest #include #include namespace MosesTraining { namespace Syntax { namespace { // Test Tree<>::PreOrderIterator with a trivial, single-node tree. BOOST_AUTO_TEST_CASE(pre_order_1) { boost::scoped_ptr > root(new Tree(123)); Tree::PreOrderIterator p(*root); BOOST_REQUIRE(p != Tree::PreOrderIterator()); BOOST_REQUIRE(p->value() == 123); ++p; BOOST_REQUIRE(p == Tree::PreOrderIterator()); } // Test Tree<>::PreOrderIterator on this tree: (1 (2 3) (4) (5 6 (7 8))) BOOST_AUTO_TEST_CASE(pre_order_2) { boost::scoped_ptr > root(new Tree(1)); root->children().push_back(new Tree(2)); root->children()[0]->children().push_back(new Tree(3)); root->children().push_back(new Tree(4)); root->children().push_back(new Tree(5)); root->children()[2]->children().push_back(new Tree(6)); root->children()[2]->children().push_back(new Tree(7)); root->children()[2]->children()[1]->children().push_back(new Tree(8)); root->SetParents(); Tree::PreOrderIterator p(*root); Tree::PreOrderIterator end; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 1); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 2); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 3); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 4); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 5); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 6); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 7); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 8); ++p; BOOST_REQUIRE(p == end); } // Test Tree<>::ConstPreOrderIterator on this tree: (1 (2 (3 (4 (5) (6)))) (7)) BOOST_AUTO_TEST_CASE(const_pre_order_1) { boost::scoped_ptr > root(new Tree(1)); root->children().push_back(new Tree(2)); root->children()[0]->children().push_back(new Tree(3)); root->children()[0]->children()[0]->children().push_back(new Tree(4)); root->children()[0]->children()[0]->children()[0]->children().push_back( new Tree(5)); root->children()[0]->children()[0]->children()[0]->children().push_back( new Tree(6)); root->children().push_back(new Tree(7)); root->SetParents(); Tree::ConstPreOrderIterator p(*root); Tree::ConstPreOrderIterator end; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 1); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 2); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 3); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 4); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 5); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 6); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 7); ++p; BOOST_REQUIRE(p == end); } // Test Tree<>::LeafIterator with a trivial, single-node tree. BOOST_AUTO_TEST_CASE(leaf_1) { boost::scoped_ptr > root(new Tree(123)); Tree::LeafIterator p(*root); BOOST_REQUIRE(p != Tree::LeafIterator()); BOOST_REQUIRE(p->value() == 123); ++p; BOOST_REQUIRE(p == Tree::LeafIterator()); } // Test Tree<>::LeafIterator on this tree: (1 (2 3) (4) (5 6 (7 8))) BOOST_AUTO_TEST_CASE(leaf_2) { boost::scoped_ptr > root(new Tree(1)); root->children().push_back(new Tree(2)); root->children()[0]->children().push_back(new Tree(3)); root->children().push_back(new Tree(4)); root->children().push_back(new Tree(5)); root->children()[2]->children().push_back(new Tree(6)); root->children()[2]->children().push_back(new Tree(7)); root->children()[2]->children()[1]->children().push_back(new Tree(8)); root->SetParents(); Tree::LeafIterator p(*root); Tree::LeafIterator end; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 3); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 4); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 6); ++p; BOOST_REQUIRE(p != end); BOOST_REQUIRE(p->value() == 8); ++p; BOOST_REQUIRE(p == end); } } // namespace } // namespace Syntax } // namespace MosesTraining