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