C++/testing all the functions
Expert: vijayan - 11/16/2008
QuestionDear vijayan,
you were right, I wasn't compiling the right file! thanks :)
Do you believe that my test program tests all the functions in the list.h?
also what I did in the test for operator--, is that a good test? can you suggest a better test for both prefix and postfix (operator -- )
please see the link below:
http://rafb.net/p/Ur7jOR43.html
thanks again for your time :)
Answerthe basic idea of testing is to try and discover errors.
in general, you would get the best return on investment for your efforts if you try to cover the boundary conditions.
for example, if you want to test the function List<T>::insert, you should have test cases for
a. insert into an empty list
b. insert a new element to be the first element of the list
c. insert a new element to be the last element of the list
d. insert a new element to be somewhere in the middle of the list
another example. if you want to test the -- operator for an iterator
a. decrement an iterator at end (one beyond the last element)
b. decrement an iterator at the last element
c. decrement an iterator somewhere in the middle.
d. decrement an iterator to the second element of the list.
e. decrement, then increment. do you get back to the original?
etc.
another invaluable aid for detecting programming errors is to identify invariants and use assertions liberally.
for example, write a function to check the invariant for the List. eg:
template <typename T>
bool List<T>::is_ok() const
{
if( firstElement == 0 )
return lastElement == 0 ;
// non empty list
if( firstElement->prev != 0 ) return false ;
if( lastElement->next != 0 ) return false ;
return true ;
}
in members of the list eg. remove
template <typename T>
void List<T>::remove(Iterator& pos)
{
// on entry
assert( is_ok() ) ;
// modify list
// before exit
assert( is_ok() ) ;
}
you could also assert other invariants. eg. free
template <typename T>
void List<T>::free()
{
// on entry
assert( is_ok() ) ;
// free all elements
// before exit
assert( ( firstElement == 0 ) && ( lastElement == 0 ) ) ;
assert( is_ok() ) ;
}
these kind of constructs would do a lot of testing on their own.
here is an introductory tutorial on writing a test driver:
http://www.cs.umd.edu/class/fall2001/cmsc214/Tutorial/driver.html