Source code of Windows XP (NT5)
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.

244 lines
8.3 KiB

  1. #if _MSC_VER > 1000
  2. #pragma once
  3. #endif // _MSC_VER > 1000
  4. template< class TSortedAssociativeContainer, const size_t c_uiNodes>
  5. class CSortedAssociativeContainerTester
  6. {
  7. protected:
  8. typedef TSortedAssociativeContainer TTest;
  9. typedef block< pair< TTest::key_type, TTest::value_type::second_type>, c_uiNodes> TNodes;
  10. struct SAssureOrdered:
  11. public unary_function< TTest::value_type, bool>
  12. {
  13. TNodes::value_type m_LstVal;
  14. TTest::value_compare m_Cmp;
  15. size_t m_uiNodes;
  16. SAssureOrdered( const TTest::value_compare& Cmp): m_Cmp( Cmp),
  17. m_uiNodes( 0)
  18. { }
  19. result_type operator()( const argument_type& Arg)
  20. {
  21. result_type Ret( false);
  22. if( 1!= ++m_uiNodes)
  23. Ret= m_Cmp( Arg, m_LstVal);
  24. m_LstVal.first= Arg.first;
  25. m_LstVal.second= Arg.second;
  26. return Ret;
  27. }
  28. };
  29. struct SAssureUnOrdered:
  30. public unary_function< TTest::value_type, bool>
  31. {
  32. TNodes::value_type m_LstVal;
  33. TTest::value_compare m_Cmp;
  34. size_t m_uiNodes;
  35. SAssureUnOrdered( const TTest::value_compare& Cmp): m_Cmp( Cmp),
  36. m_uiNodes( 0)
  37. { }
  38. result_type operator()( const argument_type& Arg)
  39. {
  40. result_type Ret( false);
  41. if( 1!= ++m_uiNodes)
  42. Ret= !m_Cmp( Arg, m_LstVal);
  43. m_LstVal.first= Arg.first;
  44. m_LstVal.second= Arg.second;
  45. return Ret;
  46. }
  47. };
  48. protected:
  49. TTest m_Ctr;
  50. size_t m_uiPerm;
  51. TNodes m_Nodes;
  52. struct TestFailure
  53. {
  54. size_t m_uiPerm;
  55. TestFailure( size_t uiPerm): m_uiPerm( uiPerm) { }
  56. };
  57. public:
  58. CSortedAssociativeContainerTester( const TTest& X= TTest()): m_Ctr( X), m_uiPerm( 0)
  59. {
  60. TTest::key_type Key( 0);
  61. // TTest::mapped_type Val( c_uiNodes- 1);
  62. TTest::value_type::second_type Val( c_uiNodes- 1);
  63. TNodes::iterator itCur( m_Nodes.begin());
  64. while( itCur!= m_Nodes.end())
  65. {
  66. *itCur= TNodes::value_type( Key, Val);
  67. ++itCur;
  68. ++Key;
  69. --Val;
  70. }
  71. }
  72. void Test()
  73. {
  74. do
  75. {
  76. //try
  77. {
  78. TTest NewCtr( m_Ctr);
  79. if( !NewCtr.empty())
  80. throw TestFailure( m_uiPerm);
  81. if( NewCtr.size()!= 0)
  82. throw TestFailure( m_uiPerm);
  83. if( NewCtr.begin()!= NewCtr.end())
  84. throw TestFailure( m_uiPerm);
  85. if( NewCtr.rbegin()!= NewCtr.rend())
  86. throw TestFailure( m_uiPerm);
  87. if( !NewCtr.valid())
  88. throw TestFailure( m_uiPerm);
  89. TNodes::iterator itCur( m_Nodes.begin());
  90. while( itCur!= m_Nodes.end())
  91. {
  92. NewCtr.insert( *itCur);
  93. ++itCur;
  94. if( !NewCtr.valid())
  95. throw TestFailure( m_uiPerm);
  96. if( NewCtr.size()!= itCur- m_Nodes.begin())
  97. throw TestFailure( m_uiPerm);
  98. {
  99. TTest::difference_type Dist( 0);
  100. TTest::iterator f( NewCtr.begin());
  101. TTest::iterator l( NewCtr.end());
  102. for( ; f!= l; ++f)
  103. ++Dist;
  104. if( NewCtr.size()!= Dist)
  105. throw TestFailure( m_uiPerm);
  106. for( ; Dist!= 0; --Dist)
  107. --f;
  108. if( NewCtr.begin()!= f)
  109. throw TestFailure( m_uiPerm);
  110. }
  111. {
  112. TTest::difference_type Dist( 0);
  113. TTest::reverse_iterator f( NewCtr.rbegin());
  114. TTest::reverse_iterator l( NewCtr.rend());
  115. for( ; f!= l; ++f)
  116. ++Dist;
  117. if( NewCtr.size()!= Dist)
  118. throw TestFailure( m_uiPerm);
  119. for( ; Dist!= 0; --Dist)
  120. --f;
  121. if( NewCtr.rbegin()!= f)
  122. throw TestFailure( m_uiPerm);
  123. }
  124. if( NewCtr.end()!= find_if( NewCtr.begin(), NewCtr.end(),
  125. SAssureOrdered( NewCtr.value_comp())))
  126. throw TestFailure( m_uiPerm);
  127. if( NewCtr.rend()!= find_if( NewCtr.rbegin(), NewCtr.rend(),
  128. SAssureUnOrdered( NewCtr.value_comp())))
  129. throw TestFailure( m_uiPerm);
  130. TNodes::iterator itCur2( m_Nodes.begin());
  131. while( itCur2!= itCur)
  132. {
  133. if( NewCtr.end()== NewCtr.find( itCur2->first))
  134. throw TestFailure( m_uiPerm);
  135. ++itCur2;
  136. }
  137. TTest NotherCtr( NewCtr);
  138. if( !NotherCtr.valid())
  139. throw TestFailure( m_uiPerm);
  140. itCur2= m_Nodes.begin();
  141. while( itCur2!= itCur)
  142. {
  143. if( 1!= NotherCtr.erase( itCur2->first))
  144. throw TestFailure( m_uiPerm);
  145. if( !NotherCtr.valid())
  146. throw TestFailure( m_uiPerm);
  147. ++itCur2;
  148. {
  149. TTest::difference_type Dist( 0);
  150. TTest::iterator f( NotherCtr.begin());
  151. TTest::iterator l( NotherCtr.end());
  152. for( ; f!= l; ++f)
  153. ++Dist;
  154. if( NotherCtr.size()!= Dist)
  155. throw TestFailure( m_uiPerm);
  156. for( ; Dist!= 0; --Dist)
  157. --f;
  158. if( NotherCtr.begin()!= f)
  159. throw TestFailure( m_uiPerm);
  160. }
  161. {
  162. TTest::difference_type Dist( 0);
  163. TTest::reverse_iterator f( NotherCtr.rbegin());
  164. TTest::reverse_iterator l( NotherCtr.rend());
  165. for( ; f!= l; ++f)
  166. ++Dist;
  167. if( NotherCtr.size()!= Dist)
  168. throw TestFailure( m_uiPerm);
  169. for( ; Dist!= 0; --Dist)
  170. --f;
  171. if( NotherCtr.rbegin()!= f)
  172. throw TestFailure( m_uiPerm);
  173. }
  174. if( NotherCtr.end()!= find_if( NotherCtr.begin(), NotherCtr.end(),
  175. SAssureOrdered( NotherCtr.value_comp())))
  176. throw TestFailure( m_uiPerm);
  177. if( NotherCtr.rend()!= find_if( NotherCtr.rbegin(), NotherCtr.rend(),
  178. SAssureUnOrdered( NotherCtr.value_comp())))
  179. throw TestFailure( m_uiPerm);
  180. TNodes::iterator itCur3( itCur2);
  181. while( itCur3!= itCur)
  182. {
  183. if( NotherCtr.end()== NotherCtr.find( itCur3->first))
  184. throw TestFailure( m_uiPerm);
  185. ++itCur3;
  186. }
  187. }
  188. if( !NotherCtr.empty())
  189. throw TestFailure( m_uiPerm);
  190. }
  191. }
  192. /*catch( TestFailure TF)
  193. { assert( false); }
  194. catch( bad_alloc BA)
  195. { assert( false); }
  196. catch( ... )
  197. { assert( false); }*/
  198. ++m_uiPerm;
  199. } while( false /*next_permutation( m_Nodes.begin(), m_Nodes.end(),
  200. m_Ctr.value_comp())*/);
  201. }
  202. };