diff options
Diffstat (limited to 'test/unit/memory.h')
-rw-r--r-- | test/unit/memory.h | 84 |
1 files changed, 56 insertions, 28 deletions
diff --git a/test/unit/memory.h b/test/unit/memory.h index 73e4814e7..d15d1353f 100644 --- a/test/unit/memory.h +++ b/test/unit/memory.h @@ -19,6 +19,12 @@ ** TS_ASSERT_THROWS( foo(), bad_alloc ); ** ** The WithLimitedMemory destructor will re-establish the previous limit. + ** + ** This class does not exist in CVC4_MEMORY_LIMITING_DISABLED is defined. + ** This can be disabled for a variety of reasons. + ** If this is disabled, there will be a function: + ** void WarnWithLimitedMemoryDisabledReason() + ** uses TS_WARN to alert users that these tests are disabled. **/ #include <cxxtest/TestSuite.h> @@ -26,47 +32,52 @@ #ifndef __CVC4__TEST__MEMORY_H #define __CVC4__TEST__MEMORY_H -#include <sys/time.h> +#include <string> #include <sys/resource.h> +#include <sys/time.h> #include "base/cvc4_assert.h" +// Conditionally define CVC4_MEMORY_LIMITING_DISABLED. +#ifdef __APPLE__ +# define CVC4_MEMORY_LIMITING_DISABLED 1 +# define CVC4_MEMORY_LIMITING_DISABLED_REASON "setrlimit() is broken on Mac." +#else /* __APPLE__ */ +# if defined(__has_feature) +# if __has_feature(address_sanitizer) +// Tests cannot expect bad_alloc to be thrown due to limit memory using +// setrlimit when ASAN is enable. ASAN instead aborts on mmap failures. +# define CVC4_MEMORY_LIMITING_DISABLED 1 +# define CVC4_MEMORY_LIMITING_DISABLED_REASON "ASAN's mmap failures abort." +# endif /* __has_feature(address_sanitizer) */ +# endif /* defined(__has_feature) */ +#endif + + + namespace CVC4 { namespace test { -class WithLimitedMemory { - rlim_t d_prevAmount; +#ifdef CVC4_MEMORY_LIMITING_DISABLED - void remember() { - struct rlimit rlim; - TS_ASSERT_EQUALS(getrlimit(RLIMIT_AS, &rlim), 0); - d_prevAmount = rlim.rlim_cur; - } +inline void WarnWithLimitedMemoryDisabledReason() { + const std::string reason = std::string("WithLimitedMemory is disabled: ") + + std::string(CVC4_MEMORY_LIMITING_DISABLED_REASON); + TS_WARN(reason.c_str()); +} -public: +#else /* CVC4_MEMORY_LIMITING_DISABLED */ - WithLimitedMemory() { -#ifdef __APPLE__ - TS_FAIL("setrlimit() is broken on Mac, can't run memory tests."); - AlwaysAssert(false, - "setrlimit() is broken on Mac, can't run memory tests."); -#endif /* __APPLE__ */ - remember(); - } +class WithLimitedMemory { + public: + WithLimitedMemory() { remember(); } WithLimitedMemory(rlim_t amount) { -#ifdef __APPLE__ - TS_FAIL("setrlimit() is broken on Mac, can't run memory tests."); - AlwaysAssert(false, - "setrlimit() is broken on Mac, can't run memory tests."); -#endif /* __APPLE__ */ remember(); set(amount); } - ~WithLimitedMemory() { - set(d_prevAmount); - } + ~WithLimitedMemory() { set(d_prevAmount); } void set(rlim_t amount) { struct rlimit rlim; @@ -74,9 +85,26 @@ public: rlim.rlim_max = RLIM_INFINITY; TS_ASSERT_EQUALS(setrlimit(RLIMIT_AS, &rlim), 0); } -}; -}/* CVC4::test namespace */ -}/* CVC4 namespace */ + private: + void remember() { + struct rlimit rlim; + TS_ASSERT_EQUALS(getrlimit(RLIMIT_AS, &rlim), 0); + d_prevAmount = rlim.rlim_cur; + } + + rlim_t d_prevAmount; +}; /* class WithLimitedMemory */ + +#endif /* CVC4_MEMORY_LIMITING_DISABLED */ + +} /* CVC4::test namespace */ +} /* CVC4 namespace */ + + +// Remove CVC4_MEMORY_LIMITING_DISABLED_REASON if it is defined. +#ifdef CVC4_MEMORY_LIMITING_DISABLED_REASON +#undef CVC4_MEMORY_LIMITING_DISABLED_REASON +#endif /* CVC4_MEMORY_LIMITING_DISABLED_REASON */ #endif /* __CVC4__TEST__MEMORY_H */ |