diff options
author | Steve Huston <shuston@riverace.com> | 1999-04-21 21:35:29 +0000 |
---|---|---|
committer | Steve Huston <shuston@riverace.com> | 1999-04-21 21:35:29 +0000 |
commit | 868e82cc6f4af5560504028300ab07f755fd6a5f (patch) | |
tree | a15036cf9d3806f86522573beb694ed56c168228 /tests | |
parent | 9f5ce505f8a5fcbcb0233fc3a54bec6bc6627ac0 (diff) | |
download | ATCD-868e82cc6f4af5560504028300ab07f755fd6a5f.tar.gz |
Added New_Fail_Test, which checks correct behavior when heap space is exhausted.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile | 1 | ||||
-rw-r--r-- | tests/New_Fail_Test.cpp | 133 |
2 files changed, 134 insertions, 0 deletions
diff --git a/tests/Makefile b/tests/Makefile index 1349f1ecea3..d20d50aa29d 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -36,6 +36,7 @@ BIN = Aio_Platform_Test \ Message_Block_Test \ Message_Queue_Test \ Message_Queue_Notifications_Test \ + New_Fail_Test \ Notify_Performance_Test \ Process_Mutex_Test \ Process_Strategy_Test \ diff --git a/tests/New_Fail_Test.cpp b/tests/New_Fail_Test.cpp new file mode 100644 index 00000000000..bf1949df41e --- /dev/null +++ b/tests/New_Fail_Test.cpp @@ -0,0 +1,133 @@ +// $Id$ +// +// ============================================================================ +// +// = LIBRARY +// tests +// +// = FILENAME +// New_Fail_Test.cpp +// +// = DESCRIPTION +// Checks to be sure that a failed ACE_NEW[_RETURN] doesn't end up throwing +// an exception up to the caller. +// +// = AUTHOR +// Steve Huston +// +// ============================================================================ + +#include "test_config.h" +#include "ace/Log_Msg.h" +#include "ace/OS.h" + +ACE_RCSID(tests, New_Fail_Test, "$Id$") + +#if defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 +USELIB("..\ace\aced.lib"); +//--------------------------------------------------------------------------- +#endif /* defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 */ + + +// This test allocates all of the heap memory, forcing 'new' to fail because +// of a lack of memory. The ACE_NEW macros should prevent an exception from +// being thrown past the ACE_NEW. If this test doesn't wipe out on an alloc +// exception, it passes. +// +// If it doesn't ever fail an allocation, there's a warning that something is +// wrong. The allocated memory is always freed to avoid masking a leak +// somewhere else in the test. +static const int BIG_BLOCK = 1024*1024; // 1MB +static const int MAX_ALLOCS_IN_TEST = 4096; // about 4GB max in the test + + +static void try_ace_new (char **p) +{ + ACE_NEW (*p, char[BIG_BLOCK]); + return; +} + + +static char * try_ace_new_return (void) +{ + char *p; + ACE_NEW_RETURN (p, char[BIG_BLOCK], 0); + return p; +} + + +int +main (int, ASYS_TCHAR *[]) +{ + ACE_START_TEST (ASYS_TEXT ("New_Fail_Test")); + int status = 0; + +#if defined (__SUNPRO_CC) && !defined (ACE_HAS_EXCEPTIONS) + ACE_DEBUG ((LM_NOTICE, "Out-of-memory will throw an unhandled exception\n")); +#else + + char *blocks[MAX_ALLOCS_IN_TEST]; + int i; + + try + { + // First part: test ACE_NEW + for (i = 0; i < MAX_ALLOCS_IN_TEST; i++) + { + try_ace_new (&blocks[i]); + if (blocks[i] == 0) + break; + } + if (i == MAX_ALLOCS_IN_TEST) + { + ACE_ERROR((LM_WARNING, + "Test didn't exhaust all available memory\n")); + --i; // Back up to valid pointer for deleting + } + else + { + ACE_ASSERT (blocks[i] == 0); + ACE_ASSERT (errno == ENOMEM); + ACE_DEBUG((LM_DEBUG, "ACE_NEW failed properly at block %d\n", i)); + } + + // Free the memory to try ACE_NEW_RETURN + while (i >= 0) + delete [] blocks[i--]; + + // Second part: test ACE_NEW_RETURN + for (i = 0; i < MAX_ALLOCS_IN_TEST; i++) + { + blocks[i] = try_ace_new_return (); + if (blocks[i] == 0) + break; + } + if (i == MAX_ALLOCS_IN_TEST) + { + ACE_ERROR((LM_WARNING, + "Test didn't exhaust all available memory\n")); + --i; // Back up to valid pointer + } + else + { + ACE_ASSERT (blocks[i] == 0); + ACE_ASSERT (errno == ENOMEM); + ACE_DEBUG((LM_DEBUG, "ACE_NEW_RETURN failed properly at block %d\n", + i)); + } + while (i >= 0) + delete [] blocks[i--]; + } + + catch (...) + { + ACE_ERROR((LM_ERROR, + "Caught exception during test; ACE_bad_alloc not defined correctly.\n")); + status = 1; // Mark test failure + } + +#endif /* __SUNPRO_CC && !ACE_HAS_EXCEPTIONS */ + + ACE_END_TEST; + return status; +} |