1
0
forked from Alepha/Alepha

Invariant gadget.

This commit is contained in:
2024-08-08 17:16:37 -04:00
parent 307175b616
commit 322bf65400
4 changed files with 110 additions and 0 deletions

64
Invariant.test/0.cc Normal file
View File

@ -0,0 +1,64 @@
static_assert( __cplusplus > 2023'00 );
#include "../Invariant.h"
#include <Alepha/Testing/TableTest.h>
#include <Alepha/Testing/test.h>
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!
};
};

View File

@ -0,0 +1 @@
unit_test( 0 )