⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stacktest.h

📁 This is the second part of that lab manual to teach you how to make real-time programme and how to d
💻 H
字号:
//: C02:StackTest.h
// From "Thinking in C++, 2nd Edition, Volume 2"
// by Bruce Eckel & Chuck Allison, (c) 2001 MindView, Inc.
// Available at www.BruceEckel.com.
#ifndef STACKTEST_H
#define STACKTEST_H
#include "Stack.h"
#include "../TestSuite/Test.h"
#include <iostream>
using namespace std;

class StackTest : public Test {
  enum {SIZE = 5};
  Stack<int> stk;
public:
  StackTest() : stk(SIZE){}
  void run(){
    testUnderflow();
    testPopulate();
    testOverflow();
    testPop();
    testBadSize();
  }
  void testBadSize(){
    try {
      Stack<int> s(0);
      fail_("Bad Size");
    }
    catch (StackError&) {
      succeed_();
    }
  }
  void testUnderflow(){
    test_(stk.size() == 0);
    try {
      stk.top();
      fail_("Underflow");
    }
    catch (StackError&) {
      succeed_();
    }
    try {
      stk.pop();
      fail_("Underflow");
    }
    catch (StackError&) {
      succeed_();
    }
  }
  void testPopulate(){
    try {
      for (int i = 0; i < SIZE; ++i)
        stk.push(i);
      succeed_();
    }
    catch (StackError&) {
      fail_("Populate");
    }
    test_(stk.size() == SIZE);
    test_(stk.top() == SIZE-1);
  }
  void testOverflow(){
    try {
      stk.push(SIZE);
      fail_("Overflow");
    }
    catch (StackError&) {
      succeed_();
    }
  }
  void testPop(){
    for (int i = 0; i < SIZE; ++i)
      test_(stk.pop() == SIZE-i-1);
    test_(stk.size() == 0);
  }
}; 
#endif // STACKTEST_H ///:~

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -