0001 /****************************************************************************************** 0002 * Test of stack 0003 ******************************************************************************************/ 0004 #include "stack_test.h" 0005 0006 int testID = 0; //测试编号 0007 0008 /****************************************************************************************** 0009 * 测试栈 0010 ******************************************************************************************/ 0011 template <typename T> //元素类型 0012 void testStack ( int n ) { 0013 Stack<T> S; 0014 printf ( "\n ==== Test %2d. Growing stack\n", testID++ ); 0015 while ( S.size() < n ) { 0016 if ( S.empty() || ( 30 < ( rand() % 100 ) ) ) { //70%概率入栈 0017 T e = (T) dice ( 2 * n ); 0018 printf ( "pushing " ); print ( e ); printf ( " ...\n" ); 0019 S.push ( e ); 0020 } else { //30%概率出栈 0021 printf ( "popping with ... " ); print ( S.pop() ); printf ( "\n" ); 0022 } 0023 print ( S ); 0024 } 0025 printf ( "\n ==== Test %2d. Shrinking stack\n", testID++ ); 0026 while ( !S.empty() ) { 0027 if ( 70 < dice ( 100 ) ) { //70%概率入栈 0028 T e = (T) dice ( 2 * n ); 0029 printf ( "pushing " ); print ( e ); printf ( " ...\n" ); 0030 S.push ( e ); 0031 } else { //70%概率出栈 0032 printf ( "popping with ... " ); print ( S.pop() ); printf ( "\n" ); 0033 } 0034 print ( S ); 0035 } 0036 } 0037 0038 0039 /****************************************************************************************** 0040 * 测试栈 0041 ******************************************************************************************/ 0042 int main ( int argc, char* argv[] ) { 0043 if ( 2 > argc ) { printf ( "Usage: %s <size of test>\a\a\n", argv[0] ); return 1; } 0044 srand ( ( unsigned int ) time ( NULL ) ); //设置随机种子 0045 testStack<int> ( atoi ( argv[1] ) ); //元素类型可以在这里任意选择 0046 return 0; 0047 }