我創建了一個類和一個驗證程序,所以我想使用驗證程序的斷言進行一些測試。這就是我說的:驗證程序斷言失敗
類藥物
class Medicine{
private:
int ID;
string name;
public:
Medicine(){
this->ID=0;
this->name="";
}
Medicine(int ID, string name, float concentration, int quantity){
this->ID=ID;
this->name=name;
}
~Medicine(){};
//inline get methods
int getID(){
return ID;
}
string& getName(){
return name;
}
//inline set methods
void setID(int newID){
this->ID = newID;
}
void setName(string newName){
this->name = newName;
}
};
exceptions.h
#include <string>
using namespace std;
#include <string>
using namespace std;
class MyException
{
public:
MyException(string msg):message(msg){}
const string& getMessage() const {return message;}
private:
string message;
};
class ValidatorException: public MyException
{
public:
ValidatorException(string msg): MyException(msg){}
};
class RepositoryException: public MyException
{
public:
RepositoryException(string msg): MyException(msg){}
};
的medicineValidator
#include "exceptions.h"
#include "medicine.h"
class MedicineValidator{
public:
void validate(Medicine& m) throw (ValidatorException);
};
#include "medicineValidator.h"
void MedicineValidator::validate(Medicine& m) throw (ValidatorException){
string message="";
if(m.getID()<1){
message+="The ID should be positive and >0!";
}
if(m.getName()==""){
message+="The name field is empty, and it shouldn't!";
}
if(m.getConcentration()<1){
message+="The concentration should be positive(>0)!";
}
if(m.getQuantity()<1){
message+="The quantity should be positive(>0)!";
}
}
算法,測試驗證:
void testValidator(){
Medicine* m = new Medicine(1,"para",30,40);
MedicineValidator* medValidator = new MedicineValidator();
medValidator->validate(*m);
m->setID(-2); //the ID should not be < 1 so it should catch the exception but the test fails
try{
medValidator->validate(*m);
assert(false);
}
catch(ValidatorException& ex){
assert(true);
}
delete m;
}
我在做什麼錯?我只是看不到錯誤......也許你可以。 :)當我運行程序,這是我得到:「斷言失敗:錯誤,文件.. \ src/utils /../域/ testMedicine.h,行32
此應用程序已請求運行時終止它在一個不尋常的方式。 請與應用程序的支持團隊聯繫獲取更多信息。「
可能,而不是隻是傾倒所有代碼... – 2013-04-26 15:31:04
究竟是什麼問題? – piokuc 2013-04-26 15:31:05
@DanielDaranas我還能說些什麼?當我運行程序時,它說「斷言失敗......」。測試失敗。 – user1849859 2013-04-26 15:32:55