00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <iostream>
00027 #include <mpcl/test.h>
00028 #include <mpcl/text/string.hh>
00029 #include <mpcl/util/collection/general_tree.hh>
00030 #include <typeinfo>
00031
00032
00033 using mpcl::text::Format;
00034
00035
00036
00037
00038
00039
00040 template <typename TIntTree>
00041 static int _TestMemory (void)
00042 {
00043
00044 typedef
00045 typename TIntTree::QTTree
00046 QTIntTree;
00047
00048 TIntTree tTree1;
00049 TIntTree tTree2;
00050 QTIntTree qtTree3 (new TIntTree (3));
00051 QTIntTree qtTree4 (new TIntTree (4));
00052
00053 TEST_INIT (Format ( "tests for class '%s' and its copying capabilities" ,
00054 typeid (TIntTree).name() ).c_str());
00055
00056 TEST_NUMBERS (true, ( tTree1.begin() == tTree2.begin() ));
00057 TEST_NUMBERS (true, ( tTree2.begin() == tTree1.begin() ));
00058 TEST_NUMBERS (true, ( tTree1.begin() == tTree1.end() ));
00059 TEST_NUMBERS (true, ( tTree2.begin() == tTree2.end() ));
00060 TEST_NUMBERS (0, tTree1.size());
00061 TEST_NUMBERS (0, tTree2.size());
00062
00063
00064
00065
00066 tTree1.setRoot (1);
00067 TEST_NUMBERS (true, ( tTree1.begin() != tTree1.end() ));
00068 TEST_NUMBERS (true, ( ++tTree1.begin() == tTree1.end() ));
00069 TEST_NUMBERS (true, ( tTree1.begin()++ != tTree1.end() ));
00070 TEST_NUMBERS (1, tTree1.size());
00071 TEST_NUMBERS (0, tTree2.size());
00072
00073
00074
00075
00076 tTree2 = tTree1;
00077 TEST_NUMBERS (true, ( tTree2.begin() != tTree2.end() ));
00078 TEST_NUMBERS (true, ( ++tTree2.begin() == tTree2.end() ));
00079 TEST_NUMBERS (true, ( tTree2.begin()++ != tTree2.end() ));
00080 TEST_NUMBERS (1, tTree1.size());
00081 TEST_NUMBERS (1, tTree2.size());
00082
00083
00084
00085
00086
00087
00088
00089
00090 tTree1.insert (qtTree3);
00091 TEST_NUMBERS (true, ( *(tTree1.begin()) == 1 ));
00092 TEST_NUMBERS (true, ( *(++tTree1.begin()) == 3 ));
00093 TEST_NUMBERS (true, ( ++(++tTree1.begin()) == tTree1.end() ));
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 tTree1.insert (qtTree3);
00105 tTree1.insert (tTree1.begin(), qtTree4);
00106
00107 TEST_MEMORY_STATUS;
00108 TEST_RETURN_CODE;
00109
00110 }
00111
00112
00114 int main (void)
00115 {
00116
00117 using std::swap;
00118
00119 typedef
00120 mpcl::util::collection::TGeneralTree<int>
00121 TIntTree;
00122
00123 TEST_INIT ("tests for class 'TGeneralTree'");
00124 try
00125 {
00126 TIntTree::iterator tIterator;
00127 TIntTree::side_iterator tSideIterator;
00128 TIntTree tTree (1);
00129 TIntTree::QTTree qtSon2Tree (new TIntTree (2));
00130 TIntTree::QTTree qtSon3Tree (new TIntTree (3));
00131 TIntTree::QTTree qtSon4Tree (new TIntTree (4));
00132 TIntTree::QTTree qtGrandSon5Tree (new TIntTree (5));
00133
00134
00135
00136
00137
00138
00139 TEST_NUMBERS (1, tTree.size());
00140 TEST_NUMBERS (1, qtSon2Tree->size());
00141
00142
00143
00144
00145
00146
00147 tTree.setRoot (1);
00148 TEST_NUMBERS (tTree.getRoot(), 1);
00149 TEST_NUMBERS (tTree.size(), 1);
00150 tTree.setRoot (2);
00151 TEST_NUMBERS (tTree.getRoot(), 2);
00152 TEST_NUMBERS (tTree.size(), 1);
00153
00154
00155
00156
00157
00158
00159
00160
00161 tTree.setRoot (1);
00162 qtSon2Tree->setRoot (2);
00163 tTree.insert (qtSon2Tree);
00164 TEST_NUMBERS (1, tTree.getRoot());
00165 TEST_NUMBERS (2, tTree.size());
00166 TEST_NUMBERS (1, qtSon2Tree->size());
00167 TEST_NUMBERS (1, qtSon2Tree->height());
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 tTree.insert (qtSon3Tree);
00179 tTree.insert (qtSon4Tree);
00180 TEST_NUMBERS (4, tTree.size());
00181 TEST_NUMBERS (2, tTree.height());
00182 TEST_NUMBERS (1, qtSon2Tree->size());
00183 TEST_NUMBERS (1, qtSon2Tree->height());
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 qtSon2Tree->insert (qtGrandSon5Tree);
00197 TEST_NUMBERS (5, tTree.size());
00198 TEST_NUMBERS (3, tTree.height());
00199 TEST_NUMBERS (2, qtSon2Tree->size());
00200 TEST_NUMBERS (2, qtSon2Tree->height());
00201 TEST_NUMBERS (1, qtGrandSon5Tree->size());
00202 TEST_NUMBERS (1, qtGrandSon5Tree->height());
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 tIterator = tTree.begin();
00216 TEST_NUMBERS (1, *(tIterator++));
00217 TEST_NUMBERS (2, *(tIterator++));
00218 TEST_NUMBERS (5, *(tIterator++));
00219 TEST_NUMBERS (3, *(tIterator++));
00220 TEST_NUMBERS (4, *(tIterator++));
00221 TEST_NUMBERS (true, ( tIterator == tTree.end() ));
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 tIterator = tTree.begin();
00235 tSideIterator = ++tTree.begin();
00236 TEST_NUMBERS (1, *(tIterator++));
00237 TEST_NUMBERS (2, *(tSideIterator++));
00238 TEST_NUMBERS (3, *(tSideIterator++));
00239 TEST_NUMBERS (4, *(tSideIterator++));
00240 TEST_NUMBERS (2, *(tIterator++));
00241 TEST_NUMBERS (5, *(tIterator++));
00242 TEST_NUMBERS (3, *(tIterator++));
00243 TEST_NUMBERS (true, ( tTree.begin() != tTree.end() ));
00244 TEST_NUMBERS (true, ( tTree.beginChild() != tTree.endChild() ));
00245 TEST_NUMBERS (true, ( tTree.endChild() == tTree.end() ));
00246 TEST_NUMBERS (true, ( tSideIterator == tTree.end() ));
00247 TEST_NUMBERS (true, ( tSideIterator != tTree.begin() ));
00248
00249
00250
00251
00252 {
00253 TIntTree tTreeCopy (tTree);
00254
00255 tIterator = tTreeCopy.begin();
00256 TEST_NUMBERS (1, *(tIterator++));
00257 TEST_NUMBERS (2, *(tIterator++));
00258 TEST_NUMBERS (5, *(tIterator++));
00259 TEST_NUMBERS (3, *(tIterator++));
00260 TEST_NUMBERS (4, *(tIterator++));
00261 TEST_NUMBERS (true, ( tIterator == tTreeCopy.end() ));
00262
00263 tTreeCopy = tTree;
00264 tIterator = tTreeCopy.begin();
00265 TEST_NUMBERS (1, *(tIterator++));
00266 TEST_NUMBERS (2, *(tIterator++));
00267 TEST_NUMBERS (5, *(tIterator++));
00268 TEST_NUMBERS (3, *(tIterator++));
00269 TEST_NUMBERS (4, *(tIterator++));
00270 TEST_NUMBERS (true, ( tIterator == tTreeCopy.end() ));
00271
00272
00273
00274
00275 tTreeCopy.clear();
00276 swap (tTree, tTreeCopy);
00277 TEST_NUMBERS (true, tTree.empty());
00278 TEST_NUMBERS (false, tTreeCopy.empty());
00279 TEST_NUMBERS (0, tTree.size());
00280 TEST_NUMBERS (5, tTreeCopy.size());
00281
00282
00283
00284
00285 swap (tTree, tTreeCopy);
00286 TEST_NUMBERS (false, tTree.empty());
00287 TEST_NUMBERS (true, tTreeCopy.empty());
00288 TEST_NUMBERS (5, tTree.size());
00289 TEST_NUMBERS (0, tTreeCopy.size());
00290 }
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 tTree.insert (6);
00304 tIterator = tTree.begin();
00305 TEST_NUMBERS (1, *(tIterator++));
00306 TEST_NUMBERS (2, *(tIterator++));
00307 TEST_NUMBERS (5, *(tIterator++));
00308 TEST_NUMBERS (3, *(tIterator++));
00309 TEST_NUMBERS (4, *(tIterator++));
00310 TEST_NUMBERS (6, *(tIterator++));
00311 TEST_NUMBERS (true, ( tIterator == tTree.end() ));
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 TEST_NUMBERS (2, tTree.leftChild().getRoot());
00325 TEST_NUMBERS (3, tTree.leftChild().rightSibling().getRoot());
00326 TEST_NUMBERS (4, tTree.leftChild().rightSibling().rightSibling().getRoot());
00327 TEST_NUMBERS (5, tTree.leftChild().leftChild().getRoot());
00328
00329
00330
00331
00332
00333 tTree.erase (tTree.beginChild(), tTree.endChild());
00334 TEST_NUMBERS (1, tTree.size());
00335 TEST_NUMBERS (1, tTree.height());
00336 TEST_NUMBERS (2, qtSon2Tree->size());
00337 TEST_NUMBERS (2, qtSon2Tree->height());
00338 TEST_NUMBERS (1, qtGrandSon5Tree->size());
00339 TEST_NUMBERS (1, qtGrandSon5Tree->height());
00340
00341 TEST_FUNCTION (_TestMemory<TIntTree>());
00342 }
00343 catch (const mpcl::TException& rktEXCEPTION)
00344 {
00345 std::cerr << rktEXCEPTION << std::endl;
00346 TEST_FAIL;
00347 }
00348 TEST_MEMORY_STATUS;
00349 TEST_RETURN_CODE;
00350
00351 }