NCBI C++ Toolkit Cross Reference

  C++/src/algo/tree/test/test_algo_tree.cpp


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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
/* $Id: test_algo_tree.cpp 33815 2007-05-04 17:18:18Z kazimird $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Anatoliy Kuznetov * * File Description: * TEST for: NCBI C++ core tree related API * */ #include <ncbi_pch.hpp> #include <corelib/ncbiapp.hpp> #include <corelib/ncbienv.hpp> #include <corelib/ncbireg.hpp> #include <corelib/ncbi_tree.hpp> #include <util/bitset/bitset_debug.hpp> #include <algo/tree/tree_algo.hpp> #include <util/bitset/ncbi_bitset.hpp> #include <algorithm> #include <common/test_assert.h> /* This header must go last */ // This is to use the ANSI C++ standard templates without the "std::" prefix // and to use NCBI C++ entities without the "ncbi::" prefix USING_NCBI_SCOPE; /// Test application for tree algorithms /// /// @internal /// class CAlgoTreeTestApplication : public CNcbiApplication { public: void Init(void); int Run(void); }; void CAlgoTreeTestApplication::Init(void) { // Set err.-posting and tracing to maximum SetDiagTrace(eDT_Enable); SetDiagPostFlag(eDPF_All); SetDiagPostLevel(eDiag_Info); } ETreeTraverseCode TestFunctor1(CTreeNode<int>& tr, int delta) { cout << tr.GetValue() << " :" << delta << endl; return eTreeTraverse; } ETreeTraverseCode TestFunctor2(CTreeNode<int>& tr) { cout << tr.GetValue() << endl; return eTreeTraverse; } bool TestFunctor3(int a, int b) { return (a == b); } static string s_IntToStr(int i) { return NStr::IntToString(i); } static void s_TEST_TreeOperations() { typedef CTreeNode<int> TTree; TTree* orig = new TTree(0); TTree* orig10 = orig->AddNode(10); TTree* orig11 = orig->AddNode(11); orig10->AddNode(20); orig10->AddNode(21); orig11->AddNode(22); orig11->AddNode(23); TTree* corr = new TTree(11); corr->AddNode(22); corr->AddNode(23); TTree* corr0 = corr->AddNode(0); TTree* corr10 = corr0->AddNode(10); corr10->AddNode(20); corr10->AddNode(21); TreeReRoot(*orig10); cout << "After rerooting original tree by node 10, " << endl; cout << "the original tree and correct tree are now "; if(TreeCompare(*orig10, *corr, TestFunctor3)) { cout << "the same." << endl; } else { cout << "different." << endl; } cout << endl; TreePrint(cout, *orig10, s_IntToStr, false); TreePrint(cout, *corr, s_IntToStr, false); cout << endl; /* TTree* corr2 = new TTree(11); corr2->AddNode(22); corr2->AddNode(23); corr2->AddNode(0); TTree* t = orig->DetachNode(orig10); cout << "After removing node 10 from this tree, " << endl; cout << "the original tree and correct tree are now "; if(TreeCompare(*orig11, *corr2, TestFunctor3)) { cout << "the same." << endl; } else { cout << "different." << endl; } cout << endl; TreePrint(cout, *orig11, s_IntToStr, false); TreePrint(cout, *t, s_IntToStr, false); TreePrint(cout, *corr2, s_IntToStr, false); cout << endl; */ delete orig->GetRoot(); delete corr->GetRoot(); } static void s_TEST_Tree() { typedef CTreeNode<int> TTree; TTree* tr = new TTree(0); TTree* tr10 = tr->AddNode(10); tr->AddNode(11); tr10->AddNode(20); tr10->AddNode(21); TTree* sr = new TTree(0); sr->AddNode(10); sr->AddNode(11); TTree* ur = new TTree(0); ur->AddNode(10); ur->AddNode(11); ur->AddNode(20); ur->AddNode(21); // TreePrint(cout, *tr, (IntConvType) NStr::IntToString); // TreeReRoot(*tr10); // TreePrint(cout, *tr10, (IntConvType) NStr::IntToString); cout << "Testing Breadth First Traversal" << endl; TreeBreadthFirstTraverse(*tr, TestFunctor2); cout << endl; cout << "Testing Depth First Traversal" << endl; TreeDepthFirstTraverse(*tr, TestFunctor1); cout << endl; cout << "Testing Tree Comparison" << endl; cout << "tr and tr10 are "; if(!TreeCompare(*tr, *tr10, TestFunctor3)) cout << "not "; cout << "the same." << endl; cout << "tr and tr are "; if(!TreeCompare(*tr, *tr, TestFunctor3)) cout << "not "; cout << "the same." << endl; cout << "tr and sr are "; if(!TreeCompare(*tr, *sr, TestFunctor3)) cout << "not "; cout << "the same." << endl; cout << "tr and ur are "; if(!TreeCompare(*tr, *ur, TestFunctor3)) cout << "not "; cout << "the same." << endl; cout << "sr and ur are "; if(!TreeCompare(*sr, *ur, TestFunctor3)) cout << "not "; cout << "the same." << endl; cout << endl; {{ unsigned int cnt; TTree::TNodeList_CI it = tr->SubNodeBegin(); TTree::TNodeList_CI it_end = tr->SubNodeEnd(); for (cnt = 0; it != it_end; ++it, ++cnt) { const TTree* t = *it; int v = t->GetValue(); assert(v == 10 || v == 11); } assert(cnt == 2); }} {{ TTree* tr2 = new TTree(*tr); unsigned int cnt; TTree::TNodeList_CI it = tr2->SubNodeBegin(); TTree::TNodeList_CI it_end = tr2->SubNodeEnd(); for (cnt = 0; it != it_end; ++it, ++cnt) { const TTree* t = *it; int v = t->GetValue(); assert(v == 10 || v == 11); } assert(cnt == 2); delete tr2; }} {{ TTree::TNodeList_I it = tr->SubNodeBegin(); TTree::TNodeList_I it_end = tr->SubNodeEnd(); for (; it != it_end; ++it) { TTree* t = *it; int v = t->GetValue(); if (v == 10) { tr->RemoveNode(t); break; } } }} TreeDepthFirstTraverse(*tr, TestFunctor1); cout << endl; {{ unsigned int cnt; TTree::TNodeList_CI it = tr->SubNodeBegin(); TTree::TNodeList_CI it_end = tr->SubNodeEnd(); for (cnt = 0; it != it_end; ++it, ++cnt) { const TTree* t = *it; int v = t->GetValue(); assert(v == 11); } assert(cnt == 1); }} delete tr; TTree* str = tr = new TTree(0); // // 0 - 2 // - 4 // - 3 // - 5 // - 6 // TTree* tr4 = tr->AddNode(2)->AddNode(4); tr = tr->AddNode(3); TTree* tr5 = tr->AddNode(5); TTree* tr6 = tr->AddNode(6); cout << "Test Tree: " << endl; TreeDepthFirstTraverse(*str, TestFunctor1); cout << endl; vector<const TTree*> trace_vec; TreeTraceToRoot(*tr6, trace_vec); assert(trace_vec.size() == 3); {{ cout << "Trace to root: "; ITERATE(vector<const TTree*>, it, trace_vec) { cout << (*it)->GetValue() << "; "; } cout << endl; }} const TTree* parent_node = TreeFindCommonParent(*tr4, *tr6); assert(tr4->IsParent(*parent_node)); assert(tr6->IsParent(*parent_node)); assert(!tr4->IsParent(*tr6)); assert(parent_node); cout << "parent: " << parent_node->GetValue() << endl; assert(parent_node->GetValue() == 0); parent_node = TreeFindCommonParent(*tr5, *tr6); assert(parent_node); assert(parent_node->GetValue() == 3); TreePrint(cout, *str, s_IntToStr); cout << endl; TreeReRoot(*tr5); TreePrint(cout, *tr5, s_IntToStr); tr5->MoveSubnodes(str); TreePrint(cout, *tr5, s_IntToStr); delete tr5; delete ur; delete sr; } struct IdValue { int id; IdValue() : id(0) {} IdValue(int v) : id(v) {} operator int() const { return id; } int GetId() const { return id; } }; static string s_IdValueToStr(const IdValue& idv) { return NStr::IntToString(idv.id); } static void s_TEST_IdTreeOperations() { cout << "--------------------- s_TEST_IdTreeOperations " << endl; typedef CTreeNode<IdValue> TTree; TTree* tr = new TTree(0); TTree* tr10 = tr->AddNode(10); TTree* tr11 = tr->AddNode(11); TTree* tr110 = tr10->AddNode(110); TTree* tr1100 = tr110->AddNode(1100); TreePrint(cout, *tr, s_IdValueToStr); bm::bvector<> bv; TreeMakeSubNodesSet(*tr, bv.inserter()); assert(bv.count() == 2); assert(bv[10]); assert(bv[11]); typedef vector<TTree*> TNodeList; TNodeList node_list; node_list.push_back(tr10); node_list.push_back(tr11); node_list.push_back(tr110); node_list.push_back(tr1100); TNodeList res_node_list; CTreeNonRedundantSet<TTree, bm::bvector<>, TNodeList> nr_func; nr_func(node_list, res_node_list); cout << "Non-redundant set:" << endl; ITERATE(TNodeList, it, res_node_list) { cout << (*it)->GetValue().GetId() << "; "; } cout << endl; assert(res_node_list.size() == 2); res_node_list.clear(); node_list.clear(); node_list.push_back(tr110); node_list.push_back(tr1100); CTreeMinimalSet<TTree, bm::bvector<>, TNodeList> min_func; min_func(node_list, res_node_list); cout << "Minimal set:" << endl; ITERATE(TNodeList, it, res_node_list) { cout << (*it)->GetValue().GetId() << "; "; } cout << endl; cout << "-----" << endl; assert(res_node_list.size() == 1); res_node_list.clear(); node_list.clear(); node_list.push_back(tr110); node_list.push_back(tr1100); node_list.push_back(tr11); min_func(node_list, res_node_list); cout << "Minimal set:" << endl; ITERATE(TNodeList, it, res_node_list) { cout << (*it)->GetValue().GetId() << "; "; } cout << endl; cout << "-----" << endl; assert(res_node_list.size() == 1); res_node_list.clear(); node_list.clear(); TNodeList node_list_a; TNodeList node_list_b; TNodeList node_list_c; node_list_a.push_back(tr10); node_list_a.push_back(tr11); node_list_a.push_back(tr110); node_list_a.push_back(tr1100); node_list_b.push_back(tr10); node_list_b.push_back(tr11); node_list_b.push_back(tr110); CTreeNodesAnd<TTree, bm::bvector<>, TNodeList> and_func; and_func(node_list_a, node_list_b, node_list_c); ITERATE(TNodeList, it, node_list_c) { cout << (*it)->GetValue().GetId() << "; "; } cout << endl; assert(node_list_c.size() == 3); node_list_c.clear(); CTreeNodesOr<TTree, bm::bvector<>, TNodeList> or_func; or_func(node_list_a, node_list_b, node_list_c); ITERATE(TNodeList, it, node_list_c) { cout << (*it)->GetValue().GetId() << "; "; } cout << endl; assert(node_list_c.size() == 4); delete tr; cout << "--------------------- s_TEST_IdTreeOperations ok" << endl; } static void s_TEST_IdTree() { typedef CTreePair<int, int>::TTreePair TTreePair; typedef CTreePair<int, int>::TPairTreeNode TTree; TTree* tr = new TTree(TTreePair(0, 0)); tr->AddNode(TTreePair(1, 10)); tr->AddNode(TTreePair(100, 110)); TTree* tr2 = tr->AddNode(TTreePair(2, 20)); tr2->AddNode(TTreePair(20, 21)); TTree* tr3 =tr2->AddNode(TTreePair(22, 22)); tr3->AddNode(TTreePair(222, 222)); {{ const TTree* tnd = tr->FindSubNode(100); assert(tnd); assert(tnd->GetValue().id == 100); assert(tnd->GetValue().value == 110); const TTree& tv = tnd->GetValue(); assert(tv.GetValue().value == 110); }} {{ list<int> npath; npath.push_back(2); npath.push_back(22); TTree::TConstNodeList res; tr->FindNodes(npath, &res); assert(!res.empty()); TTree::TConstNodeList::const_iterator it = res.begin(); const TTree* ftr = *it; assert(ftr->GetValue().value == 22); }} {{ list<int> npath; npath.push_back(2); npath.push_back(32); TTree::TConstNodeList res; tr->FindNodes(npath, &res); assert(res.empty()); }} {{ list<int> npath; npath.push_back(2); npath.push_back(22); npath.push_back(222); npath.push_back(100); const TTree* node = PairTreeTraceNode(*tr, npath); assert(node); cout << node->GetValue().id << " " << node->GetValue().value << endl; assert(node->GetValue().value == 110); node = PairTreeBackTraceNode(*tr3, 1); assert(node); cout << node->GetValue().id << " " << node->GetValue().value << endl; assert(node->GetValue().value == 10); }} {{ list<int> npath; npath.push_back(2); npath.push_back(22); npath.push_back(222); const TTree* node = PairTreeTraceNode(*tr, npath); assert(node); cout << node->GetValue().id << " " << node->GetValue().value << endl; assert(node->GetValue().value == 222); }} delete tr; } int CAlgoTreeTestApplication::Run(void) { // CExceptionReporterStream reporter(cerr); // CExceptionReporter::SetDefault(&reporter); // CExceptionReporter::EnableDefault(false); // CExceptionReporter::EnableDefault(true); // CExceptionReporter::SetDefault(0); /* CExceptionReporter::EnableDefault(true); cerr << endl; NCBI_REPORT_EXCEPTION( "****** default reporter (stream) ******",e); CExceptionReporter::SetDefault(0); cerr << endl; NCBI_REPORT_EXCEPTION( "****** default reporter (diag) ******",e); */ s_TEST_Tree(); s_TEST_TreeOperations(); s_TEST_IdTree(); s_TEST_IdTreeOperations(); return 0; } ///////////////////////////////// // APPLICATION OBJECT and MAIN // int main(int argc, const char* argv[] /*, const char* envp[]*/) { CAlgoTreeTestApplication theTestApplication; return theTestApplication.AppMain(argc, argv, 0 /*envp*/, eDS_ToMemory); }

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.