00001 /* 00002 * This file is part of the Standard Portable Library (SPL). 00003 * 00004 * SPL is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * SPL is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with SPL. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 #include <spl/Debug.h> 00018 #include <spl/UnitTest.h> 00019 #include <spl/Int32.h> 00020 #include <spl/Log.h> 00021 #include <spl/collection/RedBlackTree.h> 00022 #include <spl/math/Math.h> 00023 00024 #ifdef DEBUG 00025 00026 void _RbTest1() 00027 { 00028 RedBlackTree<int, int> rbt; 00029 //rbt.Insert(1, 1); 00030 //UNIT_ASSERT("RbTest1 lookup present", rbt.Find(1) == 1); 00031 //UNIT_ASSERT("Contains key", rbt.ContainsKey(1)); 00032 //DEBUG_CLEAR_MEM_CHECK_POINTS(); 00033 //rbt.CheckMem(); 00034 //UNIT_ASSERT_MEM_NOTED("redblack tree one node"); 00035 //rbt.Remove(1); 00036 //UNIT_ASSERT("RbTest1 remove removed", rbt.Find(1) == 0); 00037 00038 //DEBUG_CLEAR_MEM_CHECK_POINTS(); 00039 //rbt.CheckMem(); 00040 //UNIT_ASSERT_MEM_NOTED("redblack tree one node"); 00041 00042 Log::SWriteOkFail( "redblack tree one node" ); 00043 } 00044 00045 void _RbTest2() 00046 { 00047 /* int x; 00048 Log::WriteCheck( "redblack tree 1 - 100 insert/remove" ); 00049 RedBlackTree<int, Int32> rbt; 00050 00051 for ( x = 0; x < 100; x++ ) 00052 { 00053 rbt.Insert(x, x); 00054 UNIT_ASSERT("lookup", rbt.Find(x) == x); 00055 } 00056 DEBUG_CLEAR_MEM_CHECK_POINTS(); 00057 rbt.CheckMem(); 00058 UNIT_ASSERT_MEM_NOTED("redblack tree 1 - 100 insert/remove"); 00059 for ( x = 0; x < 100; x++ ) 00060 { 00061 UNIT_ASSERT("lookup", rbt.Find(x) == x); 00062 UNIT_ASSERT("Contains key", rbt.ContainsKey(x)); 00063 } 00064 for ( x = 0; x < 100; x++ ) 00065 { 00066 rbt.Remove(x); 00067 } 00068 UNIT_ASSERT("Should be empty", rbt.Count() == 0); 00069 00070 DEBUG_CLEAR_MEM_CHECK_POINTS(); 00071 rbt.CheckMem(); 00072 UNIT_ASSERT_MEM_NOTED("redblack tree 1 - 100 insert/remove"); 00073 00074 Log::WriteOkFail( );*/ 00075 } 00076 00077 void _RbTest3() 00078 { 00079 /* int x; 00080 Log::WriteCheck( "redblack tree 100 - 0 insert/remove" ); 00081 RedBlackTree<int, Int32> rbt; 00082 00083 for ( x = 100; x >= 0; x-- ) 00084 { 00085 rbt.Insert(x, x); 00086 } 00087 for ( x = 100; x >= 0; x-- ) 00088 { 00089 UNIT_ASSERT("lookup", rbt.Find(x) == x); 00090 UNIT_ASSERT("Contains key", rbt.ContainsKey(x)); 00091 } 00092 for ( x = 100; x >= 0; x-- ) 00093 { 00094 rbt.Remove(x); 00095 } 00096 UNIT_ASSERT("Should be empty", rbt.Count() == 0); 00097 00098 DEBUG_CLEAR_MEM_CHECK_POINTS(); 00099 rbt.CheckMem(); 00100 UNIT_ASSERT_MEM_NOTED("redblack tree 100 - 0 insert/remove"); 00101 00102 Log::WriteOkFail( );*/ 00103 } 00104 00105 void _RbTest4() 00106 { 00107 /* int x; 00108 Log::WriteCheck( "redblack tree random" ); 00109 Math::InitRandom(); 00110 RedBlackTree<int, Int32> rbt; 00111 00112 int count = 0; 00113 for ( x = 0; x < 500; x++ ) 00114 { 00115 int k = (int)Math::RandomRange(300); 00116 if ( rbt.ContainsKey(k) ) 00117 { 00118 UNIT_ASSERT("Key value should be", rbt.Find(k) == k); 00119 } 00120 else 00121 { 00122 UNIT_ASSERT("Key value should not be", rbt.Find(k) == 0); 00123 rbt.Insert(k, k); 00124 count++; 00125 } 00126 } 00127 00128 DEBUG_CLEAR_MEM_CHECK_POINTS(); 00129 rbt.CheckMem(); 00130 UNIT_ASSERT_MEM_NOTED("redblack tree random"); 00131 for ( x = 0; x < 500; x++ ) 00132 { 00133 int k = (int)Math::RandomRange(300); 00134 rbt.Remove(k); 00135 } 00136 00137 DEBUG_CLEAR_MEM_CHECK_POINTS(); 00138 rbt.CheckMem(); 00139 UNIT_ASSERT_MEM_NOTED("redblack tree random"); 00140 00141 Log::WriteOkFail( );*/ 00142 } 00143 00144 void RedblacktreeTestHarness() 00145 { 00146 DEBUG_CLEAR_MEM_CHECK_POINTS(); 00147 UNIT_ASSERT_MEM_NOTED("redblack tree"); 00148 _RbTest1(); 00149 DEBUG_CLEAR_MEM_CHECK_POINTS(); 00150 UNIT_ASSERT_MEM_NOTED("redblack tree"); 00151 _RbTest2(); 00152 DEBUG_CLEAR_MEM_CHECK_POINTS(); 00153 UNIT_ASSERT_MEM_NOTED("redblack tree"); 00154 _RbTest3(); 00155 DEBUG_CLEAR_MEM_CHECK_POINTS(); 00156 UNIT_ASSERT_MEM_NOTED("redblack tree"); 00157 _RbTest4(); 00158 DEBUG_CLEAR_MEM_CHECK_POINTS(); 00159 UNIT_ASSERT_MEM_NOTED("redblack tree"); 00160 } 00161 00162 #endif