author | sl@SLION-WIN7.fritz.box |
Fri, 15 Jun 2012 03:10:57 +0200 | |
changeset 0 | bde4ae8d615e |
permissions | -rw-r--r-- |
sl@0 | 1 |
//#include <list> |
sl@0 | 2 |
#include <stdexcept> |
sl@0 | 3 |
#include <stdio.h> |
sl@0 | 4 |
|
sl@0 | 5 |
using namespace std; |
sl@0 | 6 |
|
sl@0 | 7 |
struct BigStruct |
sl@0 | 8 |
{ |
sl@0 | 9 |
char _data[4096]; |
sl@0 | 10 |
}; |
sl@0 | 11 |
|
sl@0 | 12 |
void bad_alloc_test() |
sl@0 | 13 |
{ |
sl@0 | 14 |
typedef allocator<BigStruct> BigStructAllocType; |
sl@0 | 15 |
BigStructAllocType bigStructAlloc; |
sl@0 | 16 |
|
sl@0 | 17 |
try { |
sl@0 | 18 |
//Lets try to allocate almost 4096 Go (on most of the platforms) of memory: |
sl@0 | 19 |
BigStructAllocType::pointer pbigStruct = bigStructAlloc.allocate(1024 * 1024 * 1024); |
sl@0 | 20 |
|
sl@0 | 21 |
// CPPUNIT_ASSERT( pbigStruct != 0 && "Allocation failed but no exception thrown" ); |
sl@0 | 22 |
} |
sl@0 | 23 |
catch (bad_alloc const&) { |
sl@0 | 24 |
printf( "Ok\n" ); |
sl@0 | 25 |
} |
sl@0 | 26 |
catch (...) { |
sl@0 | 27 |
//We shouldn't be there: |
sl@0 | 28 |
// CPPUNIT_ASSERT( false && "Not bad_alloc exception thrown." ); |
sl@0 | 29 |
} |
sl@0 | 30 |
} |
sl@0 | 31 |
|
sl@0 | 32 |
void bad_alloc_test1() |
sl@0 | 33 |
{ |
sl@0 | 34 |
try { |
sl@0 | 35 |
allocator<BigStruct> all; |
sl@0 | 36 |
BigStruct *bs = all.allocate(1024*1024*1024); |
sl@0 | 37 |
|
sl@0 | 38 |
// throw bad_alloc(); |
sl@0 | 39 |
} |
sl@0 | 40 |
catch ( bad_alloc const & ) { |
sl@0 | 41 |
printf( "I am here\n" ); |
sl@0 | 42 |
} |
sl@0 | 43 |
catch ( ... ) { |
sl@0 | 44 |
} |
sl@0 | 45 |
} |
sl@0 | 46 |
|
sl@0 | 47 |
int main() |
sl@0 | 48 |
{ |
sl@0 | 49 |
bad_alloc_test(); |
sl@0 | 50 |
#if 0 |
sl@0 | 51 |
try { |
sl@0 | 52 |
throw bad_alloc(); |
sl@0 | 53 |
} |
sl@0 | 54 |
catch ( bad_alloc& ) { |
sl@0 | 55 |
} |
sl@0 | 56 |
catch ( ... ) { |
sl@0 | 57 |
} |
sl@0 | 58 |
#endif |
sl@0 | 59 |
return 0; |
sl@0 | 60 |
} |