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