需要被測試的功能是:C++如何測試函數使其拋出一個錯誤?
int hire(Payroll * p) throw(out_of_range, logic_error)
{
// error if no holes left
if (staffcount == MAX_EMPLOYEES)
throw (out_of_range("Hire: Too many employees"));
// spin down holes looking for a hole.
for (int i = 0; i < MAX_EMPLOYEES; ++i) {
Payroll *current = staff[i].get(); // get the pointer
if (current == 0) { // it is empty
appay newpay(p); // convert *Payrollto auto_ptr
staff[i] =newpay;
staffcount++; // one more staff
return i; // return index
} else {
// do nothing. Hole is filled
}
}
// should never get here
throw (logic_error("no holes, but count ok")); }
我可以拋出一個out_of_range
錯誤來測試它,但我想不出任何logic_error
的。
這是我的主要測試out_of_range
:
try {
for (int i = 0; i<11; i++){
hr.hire(new Payroll("Prog M. Er", 55757575));
hr.showAllStaff(" after hires");
}
} catch (out_of_range e) {
cout << "Out of range error: " << e.what() << endl;
cout << "DEBUG: carry on processing - line 177 was tested\n";
}
如何編寫此功能的測試logic_error任何幫助將不勝感激! 謝謝。
A
C++中的異常規範有些破碎:http://www.gotw.ca/publications/mill22.htm – 2009-12-15 17:51:50
看起來像是您將MAX_EMPLOYEES設置爲2並嘗試僱用第三名員工。我不認爲這應該是一個邏輯錯誤,但更多的是溢出。 – 2009-12-15 18:08:25
這是真的托馬斯。我發現很難找到這個邏輯錯誤。我需要爲這個函數創建一個測試,在這裏我拋出錯誤(兩種類型)。 讓我知道如果你能想到別的。謝謝。 Adri – Adri 2009-12-15 18:52:53