00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <spl/Debug.h>
00018
00019 #if defined(DEBUG) || defined(_DEBUG)
00020
00021 #include <spl/Int32.h>
00022 #include <spl/Log.h>
00023 #include <spl/collection/Hashtable.h>
00024
00025 static void _THashTest1()
00026 {
00027 Hashtable<int, int> ht;
00028 ht.ValidateMem();
00029 ht.Set(1, 1);
00030 ht.ValidateMem();
00031 DEBUG_CLEAR_MEM_CHECK_POINTS();
00032 ht.CheckMem();
00033 UNIT_ASSERT_MEM_NOTED("THashtable 1.1");
00034
00035 {
00036 RefCountPtr<Vector<int> > vec = ht.Values();
00037 DEBUG_CLEAR_MEM_CHECK_POINTS();
00038 ht.CheckMem();
00039 vec.CheckMem();
00040 UNIT_ASSERT_MEM_NOTED("THashtable 1.2");
00041 }
00042
00043 ht.Clear();
00044 DEBUG_CLEAR_MEM_CHECK_POINTS();
00045 ht.CheckMem();
00046 UNIT_ASSERT_MEM_NOTED("THashtable 1.3");
00047 Log::SWriteOkFail( "Hashtable test 1" );
00048 }
00049
00050 static void _THashTest1a()
00051 {
00052 Hashtable<int, int> ht;
00053
00054 ht.Set(0, 99);
00055 UNIT_ASSERT("THash set", ht.Get(0) == 99);
00056 UNIT_ASSERT("THash cleared", ht.Count() == 1);
00057
00058 DEBUG_CLEAR_MEM_CHECK_POINTS();
00059 ht.CheckMem();
00060 UNIT_ASSERT_MEM_NOTED("THashtable 1a.1");
00061
00062 ht.Remove(0);
00063 UNIT_ASSERT("ht count", ht.Count() == 0);
00064 DEBUG_CLEAR_MEM_CHECK_POINTS();
00065 ht.CheckMem();
00066 UNIT_ASSERT_MEM_NOTED("THashtable 1a.2");
00067
00068 DEBUG_CLEAR_MEM_CHECK_POINTS();
00069 ht.CheckMem();
00070 UNIT_ASSERT_MEM_NOTED("THashtable 1a.3");
00071 Log::SWriteOkFail( "Hashtable test 1a" );
00072 }
00073
00074 static void _THashTest2()
00075 {
00076 int x;
00077 Hashtable<int, int> ht;
00078
00079 for ( x = 0; x < 100; x++ )
00080 {
00081 ht.Set(x, x);
00082 UNIT_ASSERT("THash set", ht.Get(x) == x);
00083 }
00084 DEBUG_CLEAR_MEM_CHECK_POINTS();
00085 ht.CheckMem();
00086 UNIT_ASSERT_MEM_NOTED("THashtable 2.1");
00087
00088 for ( x = 0; x < 100; x++ )
00089 {
00090 ht.Remove(x);
00091 UNIT_ASSERT("ht count", ht.Count() == 99 - x);
00092 DEBUG_CLEAR_MEM_CHECK_POINTS();
00093 ht.CheckMem();
00094 UNIT_ASSERT_MEM_NOTED("THashtable 2.2");
00095 }
00096 UNIT_ASSERT("THash cleared", ht.Count() == 0);
00097
00098 DEBUG_CLEAR_MEM_CHECK_POINTS();
00099 ht.CheckMem();
00100 UNIT_ASSERT_MEM_NOTED("THashtable 2.3");
00101 Log::SWriteOkFail( "Hashtable test 2" );
00102 }
00103
00104 static void _THashTest3()
00105 {
00106 int x;
00107 Hashtable<int, int> ht;
00108
00109 for ( x = 100; x >= 0; x-- )
00110 {
00111 ht.Set(x, x);
00112 UNIT_ASSERT("THash set", ht.Get(x) == x);
00113 }
00114 DEBUG_CLEAR_MEM_CHECK_POINTS();
00115 ht.CheckMem();
00116 UNIT_ASSERT_MEM_NOTED("THashtable 3.1");
00117
00118 for ( x = 100; x >= 0; x-- )
00119 {
00120 ht.Remove(x);
00121 DEBUG_CLEAR_MEM_CHECK_POINTS();
00122 ht.CheckMem();
00123 UNIT_ASSERT_MEM_NOTED("THashtable 3.2");
00124 }
00125 UNIT_ASSERT("THash cleared", ht.Count() == 0);
00126
00127 DEBUG_CLEAR_MEM_CHECK_POINTS();
00128 ht.CheckMem();
00129 UNIT_ASSERT_MEM_NOTED("THashtable 3.3");
00130 Log::SWriteOkFail( "Hashtable test 3" );
00131 }
00132
00133 static void _THashTest4()
00134 {
00135 int x;
00136 Math::InitRandom();
00137 Hashtable<int, int> ht;
00138
00139 for ( x = 100; x >= 0; x-- )
00140 {
00141 int k = Math::RandomRange(5000);
00142 ht.Set(k, k);
00143 }
00144 DEBUG_CLEAR_MEM_CHECK_POINTS();
00145 ht.CheckMem();
00146 UNIT_ASSERT_MEM_NOTED("THashtable 4.1");
00147
00148 for ( x = 100; x >= 0; x-- )
00149 {
00150 int k = Math::RandomRange(5000);
00151 ht.Remove(k);
00152 DEBUG_CLEAR_MEM_CHECK_POINTS();
00153 ht.CheckMem();
00154 UNIT_ASSERT_MEM_NOTED("THashtable 4.2");
00155 }
00156 ht.Clear();
00157 UNIT_ASSERT("THash cleared", ht.Count() == 0);
00158
00159 DEBUG_CLEAR_MEM_CHECK_POINTS();
00160 ht.CheckMem();
00161 UNIT_ASSERT_MEM_NOTED("THashtable 4.3");
00162 Log::SWriteOkFail( "Hashtable test 4" );
00163 }
00164
00165 static void _THashTest5()
00166 {
00167 int x;
00168 Hashtable<String, int> ht;
00169
00170 for ( x = 500; x >= 0; x-- )
00171 {
00172 int k = Math::RandomRange(500);
00173 ht.Set(*Int32::ToString(k), k);
00174 UNIT_ASSERT("THash set", ht.Get(*Int32::ToString(k)) == k);
00175 }
00176 DEBUG_CLEAR_MEM_CHECK_POINTS();
00177 ht.CheckMem();
00178 UNIT_ASSERT_MEM_NOTED("THashtable 5.1");
00179
00180 for ( x = 200; x >= 0; x-- )
00181 {
00182 int k = Math::RandomRange(500);
00183 ht.Remove(*Int32::ToString(k));
00184 DEBUG_CLEAR_MEM_CHECK_POINTS();
00185 ht.CheckMem();
00186 UNIT_ASSERT_MEM_NOTED("THashtable 5.2");
00187 }
00188 ht.Clear();
00189 UNIT_ASSERT("THash cleared", ht.Count() == 0);
00190
00191 DEBUG_CLEAR_MEM_CHECK_POINTS();
00192 ht.CheckMem();
00193 UNIT_ASSERT_MEM_NOTED("THashtable 5.3");
00194 Log::SWriteOkFail( "Hashtable test 5" );
00195 }
00196
00197 static void _THashTest6()
00198 {
00199 Hashtable<String, int> ht;
00200
00201 ht.Set("0", 0);
00202 int count = 0;
00203
00204 foreach(v,ht)
00205 {
00206 count++;
00207 UNIT_ASSERT("hashtable foreach", v.Current() == 0);
00208 }
00209 UNIT_ASSERT("foreach count", count == 1);
00210
00211 ht.Set("1", 1);
00212 count = 0;
00213
00214 UNIT_ASSERT("0", ht.ContainsKey("0"));
00215 UNIT_ASSERT("1", ht.ContainsKey("1"));
00216
00217 UNIT_ASSERT("0", ht.Get("0") == 0);
00218 UNIT_ASSERT("1", ht.Get("1") == 1);
00219
00220 {
00221 Hashtable<String, int>::Iterator itt = ht.Begin();
00222 while(itt.Next())
00223 {
00224 count++;
00225 UNIT_ASSERT("hashtable itter", itt.Current() == 0 || itt.Current() == 1);
00226 }
00227 UNIT_ASSERT("foreach count", count == 2);
00228 }
00229
00230 {
00231 count = 0;
00232 foreach(v2,ht)
00233 {
00234 count++;
00235 UNIT_ASSERT("hashtable foreach 2", v2.Current() == 0 || v2.Current() == 1);
00236 }
00237 UNIT_ASSERT("foreach count", count == 2);
00238 }
00239
00240 ht.Clear();
00241 UNIT_ASSERT("THash cleared", ht.Count() == 0);
00242
00243 DEBUG_CLEAR_MEM_CHECK_POINTS();
00244 ht.CheckMem();
00245 UNIT_ASSERT_MEM_NOTED("THashtable 5.3");
00246 Log::SWriteOkFail( "Hashtable test 6" );
00247 }
00248
00249 void THashtableTest( )
00250 {
00251 _THashTest1();
00252 DEBUG_CLEAR_MEM_CHECK_POINTS();
00253 UNIT_ASSERT_MEM_NOTED("Hashtable A");
00254
00255 _THashTest1a();
00256 DEBUG_CLEAR_MEM_CHECK_POINTS();
00257 UNIT_ASSERT_MEM_NOTED("Hashtable A.1");
00258
00259 _THashTest2();
00260 DEBUG_CLEAR_MEM_CHECK_POINTS();
00261 UNIT_ASSERT_MEM_NOTED("Hashtable B");
00262
00263 _THashTest3();
00264 DEBUG_CLEAR_MEM_CHECK_POINTS();
00265 UNIT_ASSERT_MEM_NOTED("Hashtable C");
00266
00267 _THashTest4();
00268 DEBUG_CLEAR_MEM_CHECK_POINTS();
00269 UNIT_ASSERT_MEM_NOTED("Hashtable D");
00270
00271 _THashTest5();
00272 DEBUG_CLEAR_MEM_CHECK_POINTS();
00273 UNIT_ASSERT_MEM_NOTED("Hashtable D");
00274
00275 _THashTest6();
00276 DEBUG_CLEAR_MEM_CHECK_POINTS();
00277 UNIT_ASSERT_MEM_NOTED("Hashtable E");
00278 }
00279
00280 #endif