In the example below, assertions are used to check for the satisfying of both the pre- and post-conditions of a function that computes the square root of its argument, employing a method known as Newton iteration.
double sqrt( double arg ) {require ( arg >= 0 ); double r=arg, x=1, eps=0.0001; while( fabs(r - x) > eps ) { r=x; x=r-((r*r-arg)/(2*r)); } promise ( r - arg * arg <= eps ); return r; }
sqrt
Whereas Eiffel directly supports the use of assertions by allowing access to the value of an instance variable before the execution of a method through the keyword old, the C++ programmer must rely on explicit programming to be able to compare the state before an operation with the state after the operation.
class counter {public: counter(int n = 0) : _n(n) { require( n >= 0 ); promise( invariant() );
counter check initial state
} virtual void operator++() { require( true );empty pre-condition
hold();save the previous state
_n += 1; promise( _n == old_n + 1 && invariant() ); } int value() const { return _n; }no side effects
virtual bool invariant() { return value() >= 0; } protected: int _n; int old_n; virtual void hold() { old_n = n; } };
Assertions may also be used to check whether the
object is correctly initialized.
The pre-condition stated in the constructor
requires that the counter must start with
a value not less than zero.
In addition, the constructor checks whether
the class invariant, stated in the (virtual)
member function invariant, is satisfied.
Similarly, after checking whether the post-condition
of the
class bounded : public counter {public: bounded(int b = MAXINT) : counter(0), max(b) {} void operator++() { require( value() < max );
bounded to prevent overflow
counter::operator++(); } bool invariant() { return value() <= max && counter::invariant(); } private: int max; };
From a formal perspective, the use of assertions may be regarded as a way of augmenting the type system supported by object-oriented languages. More importantly, from a software engineering perspective, the use of assertions is a means to enforce contractual obligations.