Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

test-tree.cc

00001 /*
00002 *  Name:      test-tree.cc
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Test for tree
00005 *  Date:      $Date: 2003/10/06 12:45:11 $
00006 *  Revision:  $Revision: 1.2 $
00007 *
00008 *  Copyright (C) 2000-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
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 //  L O C A L   F U N C T I O N S
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   //  Tests iterator values.
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   //  Tests assign operator.
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   //  Next state:  tree has two nodes.
00085   //
00086   //      +-(3)
00087   //      |
00088   //  (1)-+
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   //  Next state:  tree has three nodes.
00097   //
00098   //      +-(3)
00099   //      |
00100   //  (1)-+
00101   //      |
00102   //      +-(4)
00103   //
00104   tTree1.insert (qtTree3);
00105   tTree1.insert (tTree1.begin(), qtTree4);
00106 
00107   TEST_MEMORY_STATUS;
00108   TEST_RETURN_CODE;
00109 
00110 }  // _TestMemory()
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     //  Next state:  tree has only one node.
00136     //
00137     //  (1)
00138     //
00139     TEST_NUMBERS (1, tTree.size());
00140     TEST_NUMBERS (1, qtSon2Tree->size());
00141 
00142     //
00143     //  Next state:  tree has one node.
00144     //
00145     //  (1)
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     //  Next state:  tree with one son.
00156     //
00157     //  (1)
00158     //   |
00159     //  (2)
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     //  Next state:  tree with three sons.
00171     //
00172     //      (1)
00173     //       |
00174     //   +---+---+
00175     //   |   |   |
00176     //  (2) (3) (4)
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     //  Next state:  tree with three sons and one grandson.
00187     //
00188     //      (1)
00189     //       |
00190     //   +---+---+
00191     //   |   |   |
00192     //  (2) (3) (4)
00193     //   |
00194     //  (5)
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     //  Checks node values.
00206     //
00207     //      (1)
00208     //       |
00209     //   +---+---+
00210     //   |   |   |
00211     //  (2) (3) (4)
00212     //   |
00213     //  (5)
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     //  Checks node values.
00225     //
00226     //      (1)
00227     //       |
00228     //   +---+---+
00229     //   |   |   |
00230     //  (2) (3) (4)
00231     //   |
00232     //  (5)
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     //  Tests copy constructor and assign operator.
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       //  Checks swap function.
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       //  Un-does swap.
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     //  Next state:  tree with three sons and one grandson.
00294     //
00295     //        (1)
00296     //         |
00297     //   +---+---+---+
00298     //   |   |   |   |
00299     //  (2) (3) (4) (6)
00300     //   |
00301     //  (5)
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     //  Checks node values using trees.
00315     //
00316     //        (1)
00317     //         |
00318     //   +---+---+---+
00319     //   |   |   |   |
00320     //  (2) (3) (4) (6)
00321     //   |
00322     //  (5)
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     //  Next state:  tree has only one node, but the pointers to the first child
00331     //               of the tree, are still alive and kicking.
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 }  // main()

Generated on Mon Oct 13 02:35:24 2003 for MPCL by doxygen1.2.18