diff options
author | Harald Fernengel <harald@trolltech.com> | 2009-06-16 14:37:53 (GMT) |
---|---|---|
committer | Harald Fernengel <harald@trolltech.com> | 2009-06-16 14:37:53 (GMT) |
commit | 0736c37064a4e22c81f2190d8e2375c78b238944 (patch) | |
tree | 516feeeba73f469b12ab2fc20438aa48c8a0c226 /tests/auto | |
parent | afd45f48c2c87196f4f18256bfb326aa076af4d6 (diff) | |
download | Qt-0736c37064a4e22c81f2190d8e2375c78b238944.zip Qt-0736c37064a4e22c81f2190d8e2375c78b238944.tar.gz Qt-0736c37064a4e22c81f2190d8e2375c78b238944.tar.bz2 |
add object exception test as well
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/exceptionsafety_objects/tst_exceptionsafety_objects.cpp | 62 |
1 files 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 <typename T> -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<AbstractObjectCreator *>("objectCreator"); + +#define NEWROW(T) QTest::newRow(#T) << static_cast<AbstractObjectCreator *>(new ObjectCreator<T >) + NEWROW(QObject); +} + +void tst_ExceptionSafetyObjects::objects() +{ + QFETCH(AbstractObjectCreator *, objectCreator); + + AllocFailActivator allocFailActivator; + int currentOOMIndex = 0; + do { + mallocFailIndex = ++currentOOMIndex; + + try { + QScopedPointer<QObject> ptr(objectCreator->create(0)); + } catch (const std::bad_alloc &) { + } + } while (mallocFailIndex <= 0); +} + +template <typename T> +struct WidgetCreator : public AbstractObjectCreator +{ + QObject *create(QObject *parent) + { + return parent ? new T(static_cast<QWidget *>(parent)) : new T; + } +}; + void tst_ExceptionSafetyObjects::widgets_data() { - QTest::addColumn<AbstractWidgetCreator *>("widgetCreator"); + QTest::addColumn<AbstractObjectCreator *>("widgetCreator"); -#define NEWROW(T) QTest::newRow(#T) << static_cast<AbstractWidgetCreator *>(new WidgetCreator<T >) +#undef NEWROW +#define NEWROW(T) QTest::newRow(#T) << static_cast<AbstractObjectCreator *>(new WidgetCreator<T >) 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<QWidget> ptr(widgetCreator->create(0)); + QScopedPointer<QObject> 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 |