防御性编程之二

1,测试程序+编码 快于 直接编码
一个简单的测试案例
#include <iostream>
using namespace std;

class MyDate
{
public:
    MyDate(int y,int m,int d)
    {
       year=y;
       month=m;
       date=d;
    }
    int getYear(){ return year;}
    int getMonth(){ return month;}
    int getDate(){ return date;}
private:
    int year,month,date;
};

int nPass=0,nFail=0;
void test(bool t)
{
    if(t) nPass++;
    else nFail++;
}

int main()
{
    MyDate d(1985,12,20);

    //加入测试代码
    test(d.getYear()==1985);
    test(d.getMonth()==11);
    test(d.getDate()==20);

    //返回测试结果
    cout<<"Passed:"<<nPass<<", Failed:"<<nFail<<endl;

    return 0;
}

你可能感兴趣的:(编程)