static_assert( __cplusplus > 2023'00 ); #include "../Invariant.h" #include #include static auto init= Alepha::Utility::enroll <=[] { using namespace Alepha::Testing::exports; "Invariant basic test"_test <=[] { struct ItIsFine {}; struct FakeVector { int size= 0; int capacity= 0; int *data= nullptr; bool invariant() const { return true and ( capacity >= size ) and ( capacity < 10 ) and ( capacity == 0 or data != nullptr ) ; } void push_back( const int x ) { Alepha::Invariant inv{ this }; if( size == capacity ) { // Not fully exception safe, but just // ignore that... const int newCapacity= capacity * 2 + 1; int *const data2= new int [ newCapacity ]; if( capacity > 8 ) throw ItIsFine{}; std::copy( data, data + size, data2 ); capacity= newCapacity; delete [] data; data= data2; } assert( size < capacity ); data[ size++ ]= x; } }; FakeVector v; for( int i= 0; i < 4; ++i ) try { v.push_back( i ); } catch( const ItIsFine & ) {} // It's fine! }; };