The function that needs to be tested is:
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")); }
I am able to test it by throwing an out_of_range
error, but I can't think of any logic_error
.
Here is my test in the main for 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";
}
Any help on how to write a logic_error test for this function would be greatly appreciated! Thank you.
A