From 0736c37064a4e22c81f2190d8e2375c78b238944 Mon Sep 17 00:00:00 2001 From: Harald Fernengel Date: Tue, 16 Jun 2009 16:37:53 +0200 Subject: add object exception test as well --- .../tst_exceptionsafety_objects.cpp | 62 ++++++++++++++++++---- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/tests/auto/exceptionsafety_objects/tst_exceptionsafety_objects.cpp b/tests/auto/exceptionsafety_objects/tst_exceptionsafety_objects.cpp index 31c8f0a..3c85306 100644 --- a/tests/auto/exceptionsafety_objects/tst_exceptionsafety_objects.cpp +++ b/tests/auto/exceptionsafety_objects/tst_exceptionsafety_objects.cpp @@ -59,36 +59,80 @@ public slots: void initTestCase(); private slots: + void objects_data(); + void objects(); + void widgets_data(); void widgets(); }; void tst_ExceptionSafetyObjects::initTestCase() { + // sanity check whether OOM simulation works + AllocFailActivator allocFailActivator; + mallocFailIndex = 0; + + // malloc fail index is 0 -> this malloc should fail. + void *buf = malloc(42); + QVERIFY(!buf); } // helper structs to create an arbitrary widget -struct AbstractWidgetCreator +struct AbstractObjectCreator { - virtual QWidget *create(QWidget *parent) = 0; + virtual QObject *create(QObject *parent) = 0; }; -Q_DECLARE_METATYPE(AbstractWidgetCreator *) +Q_DECLARE_METATYPE(AbstractObjectCreator *) template -struct WidgetCreator : public AbstractWidgetCreator +struct ObjectCreator : public AbstractObjectCreator { - QWidget *create(QWidget *parent) + QObject *create(QObject *parent) { return parent ? new T(parent) : new T; } }; +void tst_ExceptionSafetyObjects::objects_data() +{ + QTest::addColumn("objectCreator"); + +#define NEWROW(T) QTest::newRow(#T) << static_cast(new ObjectCreator) + NEWROW(QObject); +} + +void tst_ExceptionSafetyObjects::objects() +{ + QFETCH(AbstractObjectCreator *, objectCreator); + + AllocFailActivator allocFailActivator; + int currentOOMIndex = 0; + do { + mallocFailIndex = ++currentOOMIndex; + + try { + QScopedPointer ptr(objectCreator->create(0)); + } catch (const std::bad_alloc &) { + } + } while (mallocFailIndex <= 0); +} + +template +struct WidgetCreator : public AbstractObjectCreator +{ + QObject *create(QObject *parent) + { + return parent ? new T(static_cast(parent)) : new T; + } +}; + void tst_ExceptionSafetyObjects::widgets_data() { - QTest::addColumn("widgetCreator"); + QTest::addColumn("widgetCreator"); -#define NEWROW(T) QTest::newRow(#T) << static_cast(new WidgetCreator) +#undef NEWROW +#define NEWROW(T) QTest::newRow(#T) << static_cast(new WidgetCreator) NEWROW(QWidget); NEWROW(QPushButton); NEWROW(QLabel); @@ -96,7 +140,7 @@ void tst_ExceptionSafetyObjects::widgets_data() void tst_ExceptionSafetyObjects::widgets() { - QFETCH(AbstractWidgetCreator *, widgetCreator); + QFETCH(AbstractObjectCreator *, widgetCreator); mallocCount = freeCount = 0; @@ -111,7 +155,7 @@ void tst_ExceptionSafetyObjects::widgets() // first, create without a parent try { - QScopedPointer ptr(widgetCreator->create(0)); + QScopedPointer ptr(widgetCreator->create(0)); // QScopedPointer deletes the widget again here. } catch (const std::bad_alloc &) { // ignore all std::bad_alloc - note: valgrind should show no leaks -- cgit v0.12