include/Tests/NTreeTests.hpp

Go to the documentation of this file.
00001 #ifndef hpp_NTreeTests_hpp
00002 #define hpp_NTreeTests_hpp
00003 
00004 // We need NTree declaration
00005 #include "../Tree/NTree.hpp"
00006 // We need test vectors
00007 #include "TestVectors.hpp"
00008 
00009 namespace Tests
00010 {
00011     struct NTreeTest
00012     {
00014         enum
00015         {
00016             Root = 0,
00017             A = 1,
00018             B = 2,
00019             C = 3,
00020             AA = 11,
00021             AB = 12,
00022             AC = 13,
00023             AAA = 111,
00024             BA = 21,
00025             BC = 22,
00026             CA = 31,
00027             Number = 11,
00028             Sum = 227,
00029         };
00030 
00031 
00033         struct RefCount
00034         {
00035             static int count;
00036             int value;
00037             
00038             RefCount(int val) : value(val)  
00039             { 
00040                 RefCount::count ++; 
00041             }
00042 
00043             RefCount(const RefCount & a) : value(a.value) { RefCount::count++; }
00044 
00045             void Count(int & val) const { val += value; }
00046             ~RefCount() 
00047             { 
00048                 RefCount::count --; 
00049             }
00050         };
00051 
00053         struct Count
00054         {
00055             int count;
00056             int apply(const RefCount & ref) { ref.Count(count); return 1; }
00057             Count() : count(0) {}
00058         };
00059         
00060         typedef ::Tree::NTree<RefCount>  NRefTree;
00061         
00062 
00064         bool refCheck(NRefTree & xRef)
00065         {
00066             return xRef.getRootNode() ? xRef.getRootNode()->getData().count == Number : false; 
00067         }
00068 
00070         bool countNodes(NRefTree & xRef)
00071         {
00072             NRefTree::Node * root = xRef.getRootNode();
00073             if (!root) return false;
00074             
00075             Count countObj;
00076             root->applyOnChildrenData(countObj, &Count::apply, true);
00077 
00078             return countObj.count == Sum;
00079         }
00080 
00082         bool testMethod1(NRefTree & xRef)
00083         {
00084             NRefTree::Node * root = xRef.getRootNode();
00085             if (!root) return false;
00086 
00087             // Root mustn't have a parent method
00088             if (root->parentNode()) return false;
00089 
00090             // Last child is not null, and should be C
00091             if (!root->lastChild() || root->lastChild()->getData().value != C) return false;
00092 
00093             // Check coherency of data
00094             if (!root->firstChild() || root->firstChild()->getData().value != A || root->firstChild()->nextNode()->getData().value != B || root->firstChild()->parentNode() != root) return false;
00095 
00096             // Test child at index method
00097             if (!root->firstChild()->childAtIndex(2) || root->firstChild()->childAtIndex(2)->getData().value != AC) return false;
00098             
00099             // Ok, it's good
00100             return true;
00101         }
00102 
00104         struct FindAAA 
00105         {
00106             int count;
00107             int findAAA(const RefCount & data) { return data.value == AAA; }
00108             int printList(const NRefTree::Node * node) { count += node? node->getData().value : 0; return 1; }
00109 
00110             FindAAA() : count(0) {}
00111         };
00112         
00114         bool testMethod2(NRefTree & xRef) 
00115         {
00116             NRefTree::Node * root = xRef.getRootNode(), * aaa = 0;
00117             if (!root) return false;
00118 
00119             FindAAA xF;
00120             // This should find any node
00121             if (root->findChild(xF, &FindAAA::findAAA, false)) return false;
00122             // However, this one should return a node with AAA 
00123             if (!(aaa = root->findChild(xF, &FindAAA::findAAA, true)) || aaa->getData().value != AAA) return false;
00124 
00125             // Then apply a method on all first level children
00126             if (!root->applyOnChildrenNode(xF, &FindAAA::printList, false) || xF.count != (A + B + C)) return false;
00127             // Then apply a method on all children
00128             xF.count = 0;
00129             if (!root->applyOnChildrenNode(xF, &FindAAA::printList, true) || xF.count != Sum) return false;
00130 
00131             return true;
00132         }
00133 
00135         bool testMethod3(NRefTree & xRef)
00136         {
00137             NRefTree::Node * root = xRef.getRootNode(), * node = 0;
00138             if (!root) return false;
00139 
00140             node = root->firstChild();
00141             if (!node) return false;
00142 
00143             // Try to delete middle child (AB)
00144             if (!node->deleteChildAtIndex(1)) return false;
00145             // Check deletion
00146             if (!node->firstChild() || node->firstChild()->getData().value != AA || !node->firstChild()->nextNode() || node->firstChild()->nextNode()->getData().value != AC) return false;
00147 
00148             // Try to delete last child of AA (AAA)
00149             if (!node->firstChild()->deleteChildAtIndex(0)) return false;
00150             // Check deletion
00151             if (node->firstChild()->firstChild()) return false;
00152 
00153             // Now delete first child in a list (AA)
00154             if (!node->deleteChildAtIndex(0)) return false;
00155 
00156             // Check the remaining node is ok
00157             if (!node->firstChild() || node->firstChild()->getData().value != AC || node->firstChild()->nextNode()) return false;
00158 
00159             return true;
00160         }
00161 
00163         bool addSomeData(NRefTree & xRef) throw()
00164         {
00165             try
00166             {
00167                 NRefTree::Node * root = xRef.getRootNode();
00168                 if (!root) return false;
00169 
00170                 // Create the nodes
00171                 NRefTree::Node * a = new NRefTree::Node(RefCount(A), root);
00172                 NRefTree::Node * b = new NRefTree::Node(RefCount(B), root);
00173                 NRefTree::Node * c = new NRefTree::Node(RefCount(C), root);
00174 
00175                 NRefTree::Node * aa = new NRefTree::Node(RefCount(AA), a);
00176                 NRefTree::Node * ab = new NRefTree::Node(RefCount(AB), a);
00177                 NRefTree::Node * ac = new NRefTree::Node(RefCount(AC), a);
00178 
00179                 NRefTree::Node * aaa = new NRefTree::Node(RefCount(AAA), aa);
00180 
00181                 NRefTree::Node * ba = new NRefTree::Node(RefCount(BA));
00182                 NRefTree::Node * bc = new NRefTree::Node(RefCount(BC));
00183 
00184                 NRefTree::Node * ca = new NRefTree::Node(RefCount(CA), a);
00185 
00186                 // Link them
00187                 root->appendChild(a);
00188                 root->appendChild(b);
00189                 root->appendChild(c);
00190 
00191                 a->appendChild(aa);
00192                 a->appendChild(ab);
00193                 a->appendChild(ac);
00194 
00195                 aa->appendChild(aaa);
00196 
00197                 b->appendChild(ba);
00198                 b->appendChild(bc);
00199                 
00200                 c->appendChild(ca);
00201             }
00202             catch (...)
00203             {
00204                 return false;
00205             }
00206             return true;
00207         }
00208 
00210         struct TestDeletion
00211         {
00212             bool checkDeletion() { return RefCount::count == 0; }
00213         };
00214 
00215 
00216 
00217         NRefTree xTree;
00218         NTreeTest() : xTree(Root) {}        
00219 
00220 
00221     };
00222 
00223     void createNTreeTests(Test::Vector & xVector, Bstrlib::String & results)
00224     {
00225         typedef Bstrlib::String String; 
00226         xVector.addUnitToTest(Test::MakeLineDelimiter("NTree testing"));
00227         {
00228             NTreeTest xTT;
00229             xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Creating the tree and adding data" ), xTT, &NTreeTest::addSomeData, byRef(xTT.xTree)));
00230             xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Checking node accessibility" ), xTT, &NTreeTest::countNodes, byRef(xTT.xTree)));
00231             xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Checking node creation count" ), xTT, &NTreeTest::refCheck, byRef(xTT.xTree)));
00232             xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Checking Node's access methods" ), xTT, &NTreeTest::testMethod1, byRef(xTT.xTree)));
00233             xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Checking Node's find and apply methods" ), xTT, &NTreeTest::testMethod2, byRef(xTT.xTree)));
00234             xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Checking Node deletion methods" ), xTT, &NTreeTest::testMethod3, byRef(xTT.xTree)));
00235             xVector.testLastInsertedVectors(results);
00236         }
00237     }
00238 }
00239 
00240 
00241 #endif
00242 

(C) An X-Ryl669 project 2007

This document describes Unlimited Zooming Interface source code. UZI stands for Unlimited Zooming Interface, and source code license is