File size: 4,375 Bytes
158b61b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
#include "tree.h"
#define BOOST_TEST_MODULE TreeTest
#include <boost/test/unit_test.hpp>
#include <boost/scoped_ptr.hpp>
namespace MosesTraining {
namespace Syntax {
namespace {
// Test Tree<>::PreOrderIterator with a trivial, single-node tree.
BOOST_AUTO_TEST_CASE(pre_order_1) {
boost::scoped_ptr<Tree<int> > root(new Tree<int>(123));
Tree<int>::PreOrderIterator p(*root);
BOOST_REQUIRE(p != Tree<int>::PreOrderIterator());
BOOST_REQUIRE(p->value() == 123);
++p;
BOOST_REQUIRE(p == Tree<int>::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<Tree<int> > root(new Tree<int>(1));
root->children().push_back(new Tree<int>(2));
root->children()[0]->children().push_back(new Tree<int>(3));
root->children().push_back(new Tree<int>(4));
root->children().push_back(new Tree<int>(5));
root->children()[2]->children().push_back(new Tree<int>(6));
root->children()[2]->children().push_back(new Tree<int>(7));
root->children()[2]->children()[1]->children().push_back(new Tree<int>(8));
root->SetParents();
Tree<int>::PreOrderIterator p(*root);
Tree<int>::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<Tree<int> > root(new Tree<int>(1));
root->children().push_back(new Tree<int>(2));
root->children()[0]->children().push_back(new Tree<int>(3));
root->children()[0]->children()[0]->children().push_back(new Tree<int>(4));
root->children()[0]->children()[0]->children()[0]->children().push_back(
new Tree<int>(5));
root->children()[0]->children()[0]->children()[0]->children().push_back(
new Tree<int>(6));
root->children().push_back(new Tree<int>(7));
root->SetParents();
Tree<int>::ConstPreOrderIterator p(*root);
Tree<int>::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<Tree<int> > root(new Tree<int>(123));
Tree<int>::LeafIterator p(*root);
BOOST_REQUIRE(p != Tree<int>::LeafIterator());
BOOST_REQUIRE(p->value() == 123);
++p;
BOOST_REQUIRE(p == Tree<int>::LeafIterator());
}
// Test Tree<>::LeafIterator on this tree: (1 (2 3) (4) (5 6 (7 8)))
BOOST_AUTO_TEST_CASE(leaf_2) {
boost::scoped_ptr<Tree<int> > root(new Tree<int>(1));
root->children().push_back(new Tree<int>(2));
root->children()[0]->children().push_back(new Tree<int>(3));
root->children().push_back(new Tree<int>(4));
root->children().push_back(new Tree<int>(5));
root->children()[2]->children().push_back(new Tree<int>(6));
root->children()[2]->children().push_back(new Tree<int>(7));
root->children()[2]->children()[1]->children().push_back(new Tree<int>(8));
root->SetParents();
Tree<int>::LeafIterator p(*root);
Tree<int>::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
|