Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
6.7 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1999-2000 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // ListTest.cpp
  7. //
  8. // Description:
  9. // Main file for the application used to test CList
  10. //
  11. // Documentation:
  12. // No documention for the test harness.
  13. //
  14. // Maintained By:
  15. // Vij Vasu (Vvasu) 08-MAR-2000
  16. //
  17. //////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////
  19. // Include Files
  20. //////////////////////////////////////////////////////////////////////////////
  21. #if DBG==1 || defined( _DEBUG )
  22. #define DEBUG
  23. #endif
  24. #include <windows.h>
  25. HINSTANCE g_hInstance;
  26. LPVOID g_GlobalMemoryList = NULL; // Global memory tracking list
  27. // For the debugging macros.
  28. #include "debug.h"
  29. // For printf
  30. #include <stdio.h>
  31. // For CList
  32. #include "CList.h"
  33. // Globals
  34. int g_nPrintDepth = 1;
  35. DEFINE_MODULE( "ListTest" )
  36. // Test class
  37. class CTestClass
  38. {
  39. public:
  40. static int ms_nObjectNo;
  41. int m_nId;
  42. CTestClass()
  43. {
  44. ++ms_nObjectNo;
  45. m_nId = ms_nObjectNo;
  46. wprintf( L"%*cConstructing CTestClass object %d\n", g_nPrintDepth, L' ', ms_nObjectNo );
  47. }
  48. CTestClass( const CTestClass & src )
  49. {
  50. ++ms_nObjectNo;
  51. m_nId = src.m_nId;
  52. wprintf( L"%*cConstructing CTestClass object %d\n", g_nPrintDepth, L' ', ms_nObjectNo );
  53. }
  54. ~CTestClass()
  55. {
  56. wprintf( L"%*cDestroying CTestClass object %d\n", g_nPrintDepth, L' ', ms_nObjectNo );
  57. --ms_nObjectNo;
  58. }
  59. private:
  60. const CTestClass & operator=( const CTestClass & );
  61. }; //*** class CTestClass
  62. int CTestClass::ms_nObjectNo = 0;
  63. void PrintId( CList< CTestClass >::CIterator & iter )
  64. {
  65. wprintf( L"%d", iter->m_nId );
  66. }
  67. void PrintId( CList< CTestClass * >::CIterator & iter )
  68. {
  69. wprintf( L"%d", (*iter)->m_nId );
  70. }
  71. void PrintId( CList< int >::CIterator & iter )
  72. {
  73. wprintf( L"%d", *iter );
  74. }
  75. template< class t_Ty > void PrintForwardList( CList<t_Ty> & l )
  76. {
  77. g_nPrintDepth += 4;
  78. wprintf( L"%*cPrinting forward list element ids...\n", g_nPrintDepth, L' ' );
  79. CList<t_Ty>::CIterator s = l.CiBegin();
  80. CList<t_Ty>::CIterator e = l.CiEnd();
  81. wprintf( L"%*c+ ", g_nPrintDepth, L' ' );
  82. while( s != e )
  83. {
  84. PrintId( s );
  85. wprintf( L" " );
  86. ++s;
  87. }
  88. wprintf( L" +\n" );
  89. g_nPrintDepth -= 4;
  90. }
  91. template< class t_Ty > void PrintReverseList( CList<t_Ty> & l )
  92. {
  93. g_nPrintDepth += 4;
  94. wprintf( L"%*cPrinting reverse list element ids...\n", g_nPrintDepth, L' ' );
  95. CList<t_Ty>::CIterator s = l.CiEnd();
  96. CList<t_Ty>::CIterator e = l.CiEnd();
  97. wprintf( L"%*c+ ", g_nPrintDepth, L' ' );
  98. --s;
  99. while( s != e )
  100. {
  101. PrintId( s );
  102. wprintf( L" " );
  103. --s;
  104. }
  105. wprintf( L"+\n" );
  106. g_nPrintDepth -= 4;
  107. }
  108. template< class t_Ty > void CheckSize( CList<t_Ty> & l, int size )
  109. {
  110. if ( l.CGetSize() != size )
  111. {
  112. wprintf( L"%*cERROR: The list should be %d. It is %d.\n", g_nPrintDepth, L' ', size, l.CGetSize() );
  113. throw L"List size";
  114. }
  115. }
  116. // Test a list of integers
  117. template< class t_Ty > void TestList( t_Ty array[], int arrSize )
  118. {
  119. int idx;
  120. wprintf( L"%*c|-------------------------------------------------------------------|\n", g_nPrintDepth, L' ' );
  121. g_nPrintDepth += 4;
  122. wprintf( L"\n%*cConstructing empty list.\n", g_nPrintDepth, L' ' );
  123. CList<t_Ty> l;
  124. CheckSize( l, 0 );
  125. {
  126. CList<t_Ty>::CIterator b = l.CiBegin();
  127. CList<t_Ty>::CIterator e = l.CiEnd();
  128. wprintf( L"\n%*cChecking if beginning and end of empty list are the same... ", g_nPrintDepth, L' ' );
  129. if ( ( b == e ) && !( b != e ) )
  130. {
  131. wprintf( L"Passed\n" );
  132. }
  133. else
  134. {
  135. wprintf( L"Failed\n" );
  136. throw L"Empty list iterator";
  137. }
  138. }
  139. wprintf( L"\n%*cPrinting empty list...\n", g_nPrintDepth, L' ' );
  140. PrintForwardList( l );
  141. PrintReverseList( l );
  142. {
  143. wprintf( L"\n%*cAdding one element to list\n", g_nPrintDepth, L' ' );
  144. l.Append( array[0] );
  145. CheckSize( l, 1 );
  146. PrintForwardList( l );
  147. PrintReverseList( l );
  148. }
  149. {
  150. wprintf( L"\n%*cAdding %d more elements\n", g_nPrintDepth, L' ', arrSize - 1 );
  151. CList<t_Ty>::CIterator iter = l.CiBegin();
  152. for ( idx = 1; idx < arrSize; )
  153. {
  154. l.InsertAfter( iter, array[idx] );
  155. ++iter;
  156. ++idx;
  157. CheckSize( l, idx );
  158. }
  159. PrintForwardList( l );
  160. PrintReverseList( l );
  161. }
  162. {
  163. wprintf( L"\n%*cDeleting elements\n", g_nPrintDepth, L' ', arrSize - 1 );
  164. CList<t_Ty>::CIterator iter = l.CiBegin();
  165. CList<t_Ty>::CIterator end = l.CiEnd();
  166. idx = arrSize;
  167. while( iter != end )
  168. {
  169. --idx;
  170. l.DeleteAndMoveToNext( iter );
  171. CheckSize( l, idx );
  172. }
  173. PrintForwardList( l );
  174. PrintReverseList( l );
  175. }
  176. g_nPrintDepth -= 4;
  177. wprintf( L"%*c|-------------------------------------------------------------------|\n", g_nPrintDepth, L' ' );
  178. }
  179. int __cdecl wmain( void )
  180. {
  181. int nRetVal = 0;
  182. g_hInstance = GetModuleHandle( NULL );
  183. TraceInitializeProcess( NULL, NULL );
  184. TraceCreateMemoryList( g_GlobalMemoryList );
  185. g_tfModule = mtfMEMORYLEAKS;
  186. try
  187. {
  188. {
  189. wprintf( L"%*cTesting CTestClass list.\n", g_nPrintDepth, L' ' );
  190. CTestClass arr[ 4 ];
  191. TestList< CTestClass >( arr, 4 );
  192. }
  193. {
  194. wprintf( L"\n%*cTesting CTestClass pointer list.\n", g_nPrintDepth, L' ' );
  195. CTestClass * arr[ 5 ];
  196. int idx;
  197. for ( idx = 0; idx < 5; ++idx )
  198. arr[idx] = new CTestClass;
  199. TestList< CTestClass * >( arr, 5 );
  200. for ( idx = 0; idx < 5; ++idx )
  201. delete arr[idx];
  202. }
  203. {
  204. int arr[] = { 1, 2, 3, 4 };
  205. wprintf( L"\n%*cTesting int list.\n", g_nPrintDepth, L' ' );
  206. TestList< int >( arr, sizeof( arr ) / sizeof( arr[0] ) );
  207. }
  208. }
  209. catch( WCHAR * pszTestName )
  210. {
  211. wprintf( L"Test '%s' failed.\n", pszTestName );
  212. }
  213. catch( ... )
  214. {
  215. wprintf( L"Caught an unknown exception.\n" );
  216. nRetVal = 1;
  217. }
  218. TraceTerminateMemoryList( g_GlobalMemoryList );
  219. TraceTerminateProcess( NULL, NULL );
  220. return nRetVal;
  221. }