summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorPeter Kümmel <syntheticpp@gmx.net>2012-10-10 20:08:10 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-11-06 05:33:28 +0100
commit6eb0ef546a43ed721b59703e3b8646c9a41f20d0 (patch)
tree52fea5279e9f296c76226e267a61968383ef7a31 /tests
parentf306ebc042ba42c53b304a070744fdb05b5ab6cd (diff)
Add OOM unit test for QVarLengthArray
Change-Id: Idf22ee6747aca8f0322e68b71f2c32e9ea562d4c Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Jason McDonald <macadder1@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/other/exceptionsafety/tst_exceptionsafety.cpp52
1 files changed, 52 insertions, 0 deletions
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 <stdlib.h>
+
+void* internalMalloc(size_t bytes);
+#define malloc internalMalloc
+#include <QVarLengthArray>
+#undef malloc
#include <QtTest/QtTest>
@@ -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<char> arr0;
+ int minSize = arr0.capacity();
+
+ // constructor throws
+ bool success = false;
+ try {
+ OutOfMemory oom;
+ QVarLengthArray<char> arr(minSize * 2);
+ } catch (const std::bad_alloc&) {
+ success = true;
+ }
+ QVERIFY(success);
+
+ QVarLengthArray<char> 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)