00001 #include "tree.h"
00002
00003 #define BOOST_TEST_MODULE TreeTest
00004 #include <boost/test/unit_test.hpp>
00005
00006 #include <boost/scoped_ptr.hpp>
00007
00008 namespace MosesTraining {
00009 namespace Syntax {
00010 namespace {
00011
00012
00013 BOOST_AUTO_TEST_CASE(pre_order_1) {
00014 boost::scoped_ptr<Tree<int> > root(new Tree<int>(123));
00015 Tree<int>::PreOrderIterator p(*root);
00016 BOOST_REQUIRE(p != Tree<int>::PreOrderIterator());
00017 BOOST_REQUIRE(p->value() == 123);
00018 ++p;
00019 BOOST_REQUIRE(p == Tree<int>::PreOrderIterator());
00020 }
00021
00022
00023 BOOST_AUTO_TEST_CASE(pre_order_2) {
00024 boost::scoped_ptr<Tree<int> > root(new Tree<int>(1));
00025 root->children().push_back(new Tree<int>(2));
00026 root->children()[0]->children().push_back(new Tree<int>(3));
00027 root->children().push_back(new Tree<int>(4));
00028 root->children().push_back(new Tree<int>(5));
00029 root->children()[2]->children().push_back(new Tree<int>(6));
00030 root->children()[2]->children().push_back(new Tree<int>(7));
00031 root->children()[2]->children()[1]->children().push_back(new Tree<int>(8));
00032 root->SetParents();
00033
00034 Tree<int>::PreOrderIterator p(*root);
00035 Tree<int>::PreOrderIterator end;
00036
00037 BOOST_REQUIRE(p != end);
00038 BOOST_REQUIRE(p->value() == 1);
00039 ++p;
00040 BOOST_REQUIRE(p != end);
00041 BOOST_REQUIRE(p->value() == 2);
00042 ++p;
00043 BOOST_REQUIRE(p != end);
00044 BOOST_REQUIRE(p->value() == 3);
00045 ++p;
00046 BOOST_REQUIRE(p != end);
00047 BOOST_REQUIRE(p->value() == 4);
00048 ++p;
00049 BOOST_REQUIRE(p != end);
00050 BOOST_REQUIRE(p->value() == 5);
00051 ++p;
00052 BOOST_REQUIRE(p != end);
00053 BOOST_REQUIRE(p->value() == 6);
00054 ++p;
00055 BOOST_REQUIRE(p != end);
00056 BOOST_REQUIRE(p->value() == 7);
00057 ++p;
00058 BOOST_REQUIRE(p != end);
00059 BOOST_REQUIRE(p->value() == 8);
00060 ++p;
00061 BOOST_REQUIRE(p == end);
00062 }
00063
00064
00065 BOOST_AUTO_TEST_CASE(const_pre_order_1) {
00066 boost::scoped_ptr<Tree<int> > root(new Tree<int>(1));
00067 root->children().push_back(new Tree<int>(2));
00068 root->children()[0]->children().push_back(new Tree<int>(3));
00069 root->children()[0]->children()[0]->children().push_back(new Tree<int>(4));
00070 root->children()[0]->children()[0]->children()[0]->children().push_back(
00071 new Tree<int>(5));
00072 root->children()[0]->children()[0]->children()[0]->children().push_back(
00073 new Tree<int>(6));
00074 root->children().push_back(new Tree<int>(7));
00075 root->SetParents();
00076
00077 Tree<int>::ConstPreOrderIterator p(*root);
00078 Tree<int>::ConstPreOrderIterator end;
00079
00080 BOOST_REQUIRE(p != end);
00081 BOOST_REQUIRE(p->value() == 1);
00082 ++p;
00083 BOOST_REQUIRE(p != end);
00084 BOOST_REQUIRE(p->value() == 2);
00085 ++p;
00086 BOOST_REQUIRE(p != end);
00087 BOOST_REQUIRE(p->value() == 3);
00088 ++p;
00089 BOOST_REQUIRE(p != end);
00090 BOOST_REQUIRE(p->value() == 4);
00091 ++p;
00092 BOOST_REQUIRE(p != end);
00093 BOOST_REQUIRE(p->value() == 5);
00094 ++p;
00095 BOOST_REQUIRE(p != end);
00096 BOOST_REQUIRE(p->value() == 6);
00097 ++p;
00098 BOOST_REQUIRE(p != end);
00099 BOOST_REQUIRE(p->value() == 7);
00100 ++p;
00101 BOOST_REQUIRE(p == end);
00102 }
00103
00104
00105 BOOST_AUTO_TEST_CASE(leaf_1) {
00106 boost::scoped_ptr<Tree<int> > root(new Tree<int>(123));
00107 Tree<int>::LeafIterator p(*root);
00108 BOOST_REQUIRE(p != Tree<int>::LeafIterator());
00109 BOOST_REQUIRE(p->value() == 123);
00110 ++p;
00111 BOOST_REQUIRE(p == Tree<int>::LeafIterator());
00112 }
00113
00114
00115 BOOST_AUTO_TEST_CASE(leaf_2) {
00116 boost::scoped_ptr<Tree<int> > root(new Tree<int>(1));
00117 root->children().push_back(new Tree<int>(2));
00118 root->children()[0]->children().push_back(new Tree<int>(3));
00119 root->children().push_back(new Tree<int>(4));
00120 root->children().push_back(new Tree<int>(5));
00121 root->children()[2]->children().push_back(new Tree<int>(6));
00122 root->children()[2]->children().push_back(new Tree<int>(7));
00123 root->children()[2]->children()[1]->children().push_back(new Tree<int>(8));
00124 root->SetParents();
00125
00126 Tree<int>::LeafIterator p(*root);
00127 Tree<int>::LeafIterator end;
00128
00129 BOOST_REQUIRE(p != end);
00130 BOOST_REQUIRE(p->value() == 3);
00131 ++p;
00132 BOOST_REQUIRE(p != end);
00133 BOOST_REQUIRE(p->value() == 4);
00134 ++p;
00135 BOOST_REQUIRE(p != end);
00136 BOOST_REQUIRE(p->value() == 6);
00137 ++p;
00138 BOOST_REQUIRE(p != end);
00139 BOOST_REQUIRE(p->value() == 8);
00140 ++p;
00141 BOOST_REQUIRE(p == end);
00142 }
00143
00144 }
00145 }
00146 }