0001 /****************************************************************************************** 0002 * Test of list 0003 ******************************************************************************************/ 0004 #include "list_test.h" 0005 0006 Rank testID = 0; //测试编号 0007 0008 /****************************************************************************************** 0009 * 随机生成长度为n的列表(其中可能包含重复节点) 0010 ******************************************************************************************/ 0011 template <typename T> //元素类型 0012 void randomList ( List<T> & list, Rank n ) { //创建长度为n的列表,其元素随机取自[0, 4n) 0013 ListNodePosi<T> p = 0014 ( rand() % 2 ) 0015 ? list.insertAsLast ( rand() % ( T ) ( n * 4 ) ) 0016 : list.insertAsFirst ( rand() % ( T ) ( n * 4 ) ); 0017 for ( Rank i = 1; i < n; i++ ) 0018 p = rand() % 2 0019 ? list.insert ( rand() % ( T ) ( n * 4 ), p ) 0020 : list.insert ( p, rand() % ( T ) ( n * 4 ) ); 0021 } 0022 0023 /****************************************************************************************** 0024 * 测试列表 0025 ******************************************************************************************/ 0026 #define PRINT(x) { print(x); crc(x); checkOrder(x); } 0027 template <typename T> void testList( Rank testSize ) { 0028 printf("\n ==== Test %2d. Generate a random list with %d elements\n", testID++, testSize); 0029 List<T> L; randomList(L, testSize); PRINT(L); 0030 printf("\n ==== Test %2d. Sort it\n", testID++); 0031 L.sort(); PRINT(L); 0032 0033 printf("\n ==== Test %2d. Generate two sorted lists each with %d random elements\n", testID++, testSize); 0034 List<T> Lx; randomList(Lx, testSize); PRINT(Lx); Lx.sort(); PRINT(Lx); 0035 List<T> Ly; randomList(Ly, testSize); PRINT(Ly); Ly.sort(); PRINT(Ly); 0036 0037 printf("\n ==== Test %2d. Merge them into a single sorted list\n", testID++); 0038 Lx.merge(Ly); PRINT(Lx); PRINT(Ly); 0039 // 0040 printf ( "\n ==== Test %2d. Generate two lists each of size %d by random insertions\n", testID++, testSize ); 0041 List<T> La; randomList ( La, testSize ); PRINT ( La ); 0042 List<T> Lb; randomList ( Lb, testSize ); PRINT ( Lb ); 0043 // 0044 printf ( "\n ==== Test %2d. Call list members by Rank (with high complexity)\n", testID++ ); 0045 for ( Rank i = 0; i < La.size(); i++ ) print ( La[i]->data ); printf ( "\n" ); 0046 for ( Rank i = 0; i < Lb.size(); i++ ) print ( Lb[i]->data ); printf ( "\n" ); 0047 printf ( "\n ==== Test %2d. Concatenation\n", testID++ ); PRINT ( La ); PRINT ( Lb ); 0048 while ( 0 < Lb.size() ) La.insertAsLast ( Lb.remove ( Lb.first() ) ); PRINT ( La ); PRINT ( Lb ); 0049 printf ( "\n ==== Test %2d. Increase\n", testID++ ); PRINT ( La ); 0050 increase ( La ); PRINT ( La ); 0051 printf ( "\n ==== Test %2d. Lowpass (with high complexity) on\n", testID++ ); PRINT ( La ); 0052 Rank i = La.size(); while ( 0 < --i ) { La[i-1]->data += La[i]->data; La[i-1]->data >>= 1; } PRINT ( La ); //call-by-rank, clear but slow 0053 printf ( "\n ==== Test %2d. reverse\n", testID++ ); 0054 La.reverse(); PRINT ( La ); 0055 printf ( "\n ==== Test %2d. Copy all\n", testID++ ); PRINT ( La ); 0056 List<T> Le ( La ); PRINT ( Le ); 0057 Rank r = dice( La.size() / 4 ), n = dice( La.size() * 3 / 4, La.size() ) - r; 0058 printf( "\n ==== Test %2d. Copy [%d, %d)\n", testID++, r, r + n ); 0059 List<T> Ld( La, r, n ); PRINT( Ld ); 0060 printf ( "\n ==== Test %2d. Trim by random deletions\n", testID++ ); PRINT ( Ld ); 0061 while ( testSize / 4 < Ld.size() ) { 0062 Rank N = dice( Ld.size() ); printf ( "removing L[%d]=", N ); 0063 ListNodePosi<T> p = Ld[N]; print ( p->data ); printf ( " ...\n" ); //call-by-rank, clear but slow 0064 Ld.remove ( p ); PRINT ( Ld ); 0065 } 0066 printf ( "\n ==== Test %2d. Copy\n", testID++ ); PRINT ( La ); 0067 List<T> Lf ( La ); PRINT ( Lf ); 0068 printf ( "\n ==== Test %2d. FIND in\n", testID++ ); PRINT ( Lf ); 0069 for ( Rank i = 0; i <= testSize * 2; i++ ) { //逐一测试[0, 2n]中的所有可能 0070 ListNodePosi<T> p = Lf.find ( ( T ) i ); printf ( "Looking for " ); print ( ( T ) i ); printf ( ": " ); 0071 if ( p ) { printf ( "found with" ); print ( p->data ); } 0072 else printf ( "not found" ); 0073 printf ( "\n" ); 0074 } //正确的结构应该是大致(n+1次)失败、(n次)成功相间 0075 printf ( "\n ==== Test %2d. Sort\n", testID++ ); PRINT ( La ); 0076 La.sort(); PRINT ( La ); 0077 printf ( "\n ==== Test %2d. SEARCH in\n", testID++ ); PRINT ( La ); 0078 for ( Rank i = 0; i <= testSize * 2; i++ ) { //逐一测试[0, 2n]中的所有可能 0079 ListNodePosi<T> p = La.search ( ( T ) i ); printf ( "Looking for " ); print ( ( T ) i ); printf ( ": " ); 0080 printf( ( La.valid( p ) && ( (T)i == p->data ) ) ? "found at" : "failed at" ); 0081 La.valid( p ) ? print( p->data ) : print( "header" ); 0082 printf ( "\n" ); 0083 } //正确的结构应该是大致(n+1次)失败、(n次)成功相间 0084 printf ( "\n ==== Test %2d. Remove redundancy in\n", testID++ ); PRINT ( La ); 0085 printf ( "%d node(s) removed\n", La.uniquify() ); PRINT ( La ); La.reverse(); PRINT ( La ); 0086 printf ( "\n ==== Test %2d. Remove redundancy in\n", testID++ ); PRINT ( Le ); 0087 printf ( "%d node(s) removed\n", Le.deduplicate() ); PRINT ( Le ); 0088 printf ( "\n ==== Test %2d. Sort\n", testID++ ); PRINT ( Le ); 0089 Le.sort(); PRINT ( Le ); 0090 return; 0091 } 0092 0093 /****************************************************************************************** 0094 * 测试列表 0095 ******************************************************************************************/ 0096 int main ( int argc, char* argv[] ) { 0097 if ( 2 > argc ) { printf ( "Usage: %s <size of test>\a\a\n", argv[0] ); return 1; } 0098 srand( (unsigned int)time( NULL ) ); //随机种子 0099 //srand( 31415926 ); //固定种子(假种子,调试用) 0100 testList<int>( atoi( argv[1] ) ); //元素类型可以在这里任意选择 0101 return 0; 0102 }