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