From 6eb0ef546a43ed721b59703e3b8646c9a41f20d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20K=C3=BCmmel?= Date: Wed, 10 Oct 2012 20:08:10 +0200 Subject: Add OOM unit test for QVarLengthArray Change-Id: Idf22ee6747aca8f0322e68b71f2c32e9ea562d4c Reviewed-by: Thiago Macieira Reviewed-by: Jason McDonald --- .../other/exceptionsafety/tst_exceptionsafety.cpp | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/auto/other/exceptionsafety/tst_exceptionsafety.cpp b/tests/auto/other/exceptionsafety/tst_exceptionsafety.cpp index 979a0db39f..98f1e98cd5 100644 --- a/tests/auto/other/exceptionsafety/tst_exceptionsafety.cpp +++ b/tests/auto/other/exceptionsafety/tst_exceptionsafety.cpp @@ -39,6 +39,13 @@ ** ****************************************************************************/ +#include "qplatformdefs.h" +#include + +void* internalMalloc(size_t bytes); +#define malloc internalMalloc +#include +#undef malloc #include @@ -59,6 +66,7 @@ private slots: void exceptionLinkedList(); // void exceptionEventLoop(); // void exceptionSignalSlot(); + void exceptionOOMQVarLengthArray(); #endif }; @@ -788,6 +796,50 @@ void tst_ExceptionSafety::exceptionSignalSlot() } #endif + +static bool outOfMemory = false; +void* internalMalloc(size_t bytes) { return outOfMemory ? 0 : malloc(bytes); } + +struct OutOfMemory +{ + OutOfMemory() { outOfMemory = true; } + ~OutOfMemory() { outOfMemory = false; } +}; + +void tst_ExceptionSafety::exceptionOOMQVarLengthArray() +{ +#ifdef QT_NO_EXCEPTIONS + // it will crash by design + Q_STATIC_ASSERT(false); +#else + QVarLengthArray arr0; + int minSize = arr0.capacity(); + + // constructor throws + bool success = false; + try { + OutOfMemory oom; + QVarLengthArray arr(minSize * 2); + } catch (const std::bad_alloc&) { + success = true; + } + QVERIFY(success); + + QVarLengthArray arr; + + // resize throws + success = false; + try { + OutOfMemory oom; + arr.resize(minSize * 2); + } catch(const std::bad_alloc&) { + arr.resize(1); + success = true; + } + QVERIFY(success); +#endif +} + #endif QTEST_MAIN(tst_ExceptionSafety) -- cgit v1.2.3