| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). |
| 4 |
** All rights reserved. |
| 5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
| 6 |
** |
| 7 |
** This file is part of the test suite of the Qt Toolkit. |
| 8 |
** |
| 9 |
** $QT_BEGIN_LICENSE:LGPL$ |
| 10 |
** GNU Lesser General Public License Usage |
| 11 |
** This file may be used under the terms of the GNU Lesser General Public |
| 12 |
** License version 2.1 as published by the Free Software Foundation and |
| 13 |
** appearing in the file LICENSE.LGPL included in the packaging of this |
| 14 |
** file. Please review the following information to ensure the GNU Lesser |
| 15 |
** General Public License version 2.1 requirements will be met: |
| 16 |
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 17 |
** |
| 18 |
** In addition, as a special exception, Nokia gives you certain additional |
| 19 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
| 20 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 21 |
** |
| 22 |
** GNU General Public License Usage |
| 23 |
** Alternatively, this file may be used under the terms of the GNU General |
| 24 |
** Public License version 3.0 as published by the Free Software Foundation |
| 25 |
** and appearing in the file LICENSE.GPL included in the packaging of this |
| 26 |
** file. Please review the following information to ensure the GNU General |
| 27 |
** Public License version 3.0 requirements will be met: |
| 28 |
** http://www.gnu.org/copyleft/gpl.html. |
| 29 |
** |
| 30 |
** Other Usage |
| 31 |
** Alternatively, this file may be used in accordance with the terms and |
| 32 |
** conditions contained in a signed written agreement between you and Nokia. |
| 33 |
** |
| 34 |
** |
| 35 |
** |
| 36 |
** |
| 37 |
** |
| 38 |
** $QT_END_LICENSE$ |
| 39 |
** |
| 40 |
****************************************************************************/ |
| 41 |
|
| 42 |
#include <QtGui/QtGui> |
| 43 |
#include <QtTest/QtTest> |
| 44 |
|
| 45 |
#include <stddef.h> |
| 46 |
#include <exception> |
| 47 |
|
| 48 |
QT_USE_NAMESPACE |
| 49 |
|
| 50 |
// this test only works with |
| 51 |
// * GLIBC |
| 52 |
// * MSVC - only debug builds (we need the crtdbg.h helpers) |
| 53 |
// * SYMBIAN |
| 54 |
#if (defined(QT_NO_EXCEPTIONS) || (!defined(__GLIBC__) && !defined(Q_CC_MSVC) && !defined(Q_OS_SYMBIAN))) && !defined(Q_MOC_RUN) |
| 55 |
QTEST_NOOP_MAIN |
| 56 |
#else |
| 57 |
|
| 58 |
#include "oomsimulator.h" |
| 59 |
#if !defined(Q_OS_SYMBIAN) |
| 60 |
#include "3rdparty/memcheck.h" |
| 61 |
#endif |
| 62 |
|
| 63 |
class tst_ExceptionSafety_Objects: public QObject |
| 64 |
{ |
| 65 |
Q_OBJECT |
| 66 |
|
| 67 |
public slots: |
| 68 |
void initTestCase(); |
| 69 |
void cleanupTestCase(); |
| 70 |
|
| 71 |
private slots: |
| 72 |
void objects_data(); |
| 73 |
void objects(); |
| 74 |
|
| 75 |
void widgets_data(); |
| 76 |
void widgets(); |
| 77 |
|
| 78 |
void vector_data(); |
| 79 |
void vector(); |
| 80 |
|
| 81 |
void list_data(); |
| 82 |
void list(); |
| 83 |
|
| 84 |
void linkedList_data(); |
| 85 |
void linkedList(); |
| 86 |
|
| 87 |
private: |
| 88 |
static QtMsgHandler testMessageHandler; |
| 89 |
static void safeMessageHandler(QtMsgType, const char *); |
| 90 |
}; |
| 91 |
|
| 92 |
// helper structs to create an arbitrary widget |
| 93 |
struct AbstractTester |
| 94 |
{ |
| 95 |
virtual void operator()(QObject *parent) = 0; |
| 96 |
}; |
| 97 |
Q_DECLARE_METATYPE(AbstractTester *) |
| 98 |
|
| 99 |
typedef void (*TestFunction)(QObject*); |
| 100 |
Q_DECLARE_METATYPE(TestFunction) |
| 101 |
|
| 102 |
template <typename T> |
| 103 |
struct ObjectCreator : public AbstractTester |
| 104 |
{ |
| 105 |
void operator()(QObject *) |
| 106 |
{ |
| 107 |
QScopedPointer<T> ptr(new T); |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
struct BitArrayCreator : public AbstractTester |
| 112 |
{ |
| 113 |
void operator()(QObject *) |
| 114 |
{ QScopedPointer<QBitArray> bitArray(new QBitArray(100, true)); } |
| 115 |
}; |
| 116 |
|
| 117 |
struct ByteArrayMatcherCreator : public AbstractTester |
| 118 |
{ |
| 119 |
void operator()(QObject *) |
| 120 |
{ QScopedPointer<QByteArrayMatcher> ptr(new QByteArrayMatcher("ralf test",8)); } |
| 121 |
}; |
| 122 |
|
| 123 |
struct CryptographicHashCreator : public AbstractTester |
| 124 |
{ |
| 125 |
void operator()(QObject *) |
| 126 |
{ |
| 127 |
QScopedPointer<QCryptographicHash> ptr(new QCryptographicHash(QCryptographicHash::Sha1)); |
| 128 |
ptr->addData("ralf test",8); |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
struct DataStreamCreator : public AbstractTester |
| 133 |
{ |
| 134 |
void operator()(QObject *) |
| 135 |
{ |
| 136 |
QScopedPointer<QByteArray> arr(new QByteArray("hallo, test")); |
| 137 |
QScopedPointer<QDataStream> ptr(new QDataStream(arr.data(), QIODevice::ReadWrite)); |
| 138 |
ptr->writeBytes("ralf test",8); |
| 139 |
} |
| 140 |
}; |
| 141 |
|
| 142 |
struct DirCreator : public AbstractTester |
| 143 |
{ |
| 144 |
void operator()(QObject *) |
| 145 |
{ |
| 146 |
QDir::cleanPath("../////././"); |
| 147 |
QScopedPointer<QDir> ptr(new QDir(".")); |
| 148 |
while( ptr->cdUp() ) |
| 149 |
; // just going up |
| 150 |
ptr->count(); |
| 151 |
ptr->exists(ptr->path()); |
| 152 |
|
| 153 |
QStringList filters; |
| 154 |
filters << "*.cpp" << "*.cxx" << "*.cc"; |
| 155 |
ptr->setNameFilters(filters); |
| 156 |
} |
| 157 |
}; |
| 158 |
|
| 159 |
void tst_ExceptionSafety_Objects::objects_data() |
| 160 |
{ |
| 161 |
QTest::addColumn<AbstractTester *>("objectCreator"); |
| 162 |
|
| 163 |
#define NEWROW(T) QTest::newRow(#T) << static_cast<AbstractTester *>(new ObjectCreator<T >) |
| 164 |
NEWROW(QObject); |
| 165 |
NEWROW(QBuffer); |
| 166 |
NEWROW(QFile); |
| 167 |
NEWROW(QFSFileEngine); |
| 168 |
NEWROW(QProcess); |
| 169 |
NEWROW(QSettings); |
| 170 |
NEWROW(QThread); |
| 171 |
NEWROW(QThreadPool); |
| 172 |
NEWROW(QTranslator); |
| 173 |
|
| 174 |
#define NEWROW2(T, CREATOR) QTest::newRow(#T) << static_cast<AbstractTester *>(new CREATOR) |
| 175 |
NEWROW2(QBitArray, BitArrayCreator); |
| 176 |
NEWROW2(QByteArrayMatcher, ByteArrayMatcherCreator); |
| 177 |
NEWROW2(QCryptographicHash, CryptographicHashCreator); |
| 178 |
NEWROW2(QDataStream, DataStreamCreator); |
| 179 |
NEWROW2(QDir, DirCreator); |
| 180 |
} |
| 181 |
|
| 182 |
// create and destructs an object, and lets each and every allocation |
| 183 |
// during construction and destruction fail. |
| 184 |
template <typename T> |
| 185 |
static void doOOMTest(T &testFunc, QObject *parent, int start=0) |
| 186 |
{ |
| 187 |
int currentOOMIndex = start; |
| 188 |
bool caught = false; |
| 189 |
bool done = false; |
| 190 |
|
| 191 |
AllocFailer allocFailer(0); |
| 192 |
int allocCountBefore = allocFailer.currentAllocIndex(); |
| 193 |
|
| 194 |
do { |
| 195 |
allocFailer.reactivateAt(++currentOOMIndex); |
| 196 |
|
| 197 |
caught = false; |
| 198 |
|
| 199 |
try { |
| 200 |
testFunc(parent); |
| 201 |
} catch (const std::bad_alloc &) { |
| 202 |
caught = true; |
| 203 |
} catch (const std::exception &ex) { |
| 204 |
if (strcmp(ex.what(), "autotest swallow") != 0) |
| 205 |
throw; |
| 206 |
caught = true; |
| 207 |
} |
| 208 |
|
| 209 |
if (!caught) { |
| 210 |
void *buf = malloc(42); |
| 211 |
if (buf) { |
| 212 |
// we got memory here - oom test is over. |
| 213 |
free(buf); |
| 214 |
done = true; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// if we get a FAIL, stop executing now |
| 219 |
if (QTest::currentTestFailed()) |
| 220 |
done = true; |
| 221 |
|
| 222 |
//#define REALLY_VERBOSE |
| 223 |
#ifdef REALLY_VERBOSE |
| 224 |
fprintf(stderr, " OOM Index: %d\n", currentOOMIndex); |
| 225 |
#endif |
| 226 |
|
| 227 |
|
| 228 |
} while (caught || !done); |
| 229 |
|
| 230 |
allocFailer.deactivate(); |
| 231 |
|
| 232 |
//#define VERBOSE |
| 233 |
#ifdef VERBOSE |
| 234 |
fprintf(stderr, "OOM Test done, checked allocs: %d (range %d - %d)\n", currentOOMIndex, |
| 235 |
allocCountBefore, allocFailer.currentAllocIndex()); |
| 236 |
#else |
| 237 |
Q_UNUSED(allocCountBefore); |
| 238 |
#endif |
| 239 |
} |
| 240 |
|
| 241 |
static int alloc1Failed = 0; |
| 242 |
static int alloc2Failed = 0; |
| 243 |
static int alloc3Failed = 0; |
| 244 |
static int alloc4Failed = 0; |
| 245 |
static int malloc1Failed = 0; |
| 246 |
static int malloc2Failed = 0; |
| 247 |
|
| 248 |
// Tests that new, new[] and malloc() fail at least once during OOM testing. |
| 249 |
class SelfTestObject : public QObject |
| 250 |
{ |
| 251 |
public: |
| 252 |
SelfTestObject(QObject *parent = 0) |
| 253 |
: QObject(parent) |
| 254 |
{ |
| 255 |
try { delete new int; } catch (const std::bad_alloc &) { ++alloc1Failed; throw; } |
| 256 |
try { delete [] new double[5]; } catch (const std::bad_alloc &) { ++alloc2Failed; throw ;} |
| 257 |
void *buf = malloc(42); |
| 258 |
if (buf) |
| 259 |
free(buf); |
| 260 |
else |
| 261 |
++malloc1Failed; |
| 262 |
} |
| 263 |
|
| 264 |
~SelfTestObject() |
| 265 |
{ |
| 266 |
try { delete new int; } catch (const std::bad_alloc &) { ++alloc3Failed; } |
| 267 |
try { delete [] new double[5]; } catch (const std::bad_alloc &) { ++alloc4Failed; } |
| 268 |
void *buf = malloc(42); |
| 269 |
if (buf) |
| 270 |
free(buf); |
| 271 |
else |
| 272 |
++malloc2Failed = true; |
| 273 |
} |
| 274 |
}; |
| 275 |
|
| 276 |
QtMsgHandler tst_ExceptionSafety_Objects::testMessageHandler; |
| 277 |
|
| 278 |
void tst_ExceptionSafety_Objects::safeMessageHandler(QtMsgType type, const char *msg) |
| 279 |
{ |
| 280 |
// this temporarily suspends OOM testing while handling a message |
| 281 |
int currentIndex = mallocFailIndex; |
| 282 |
AllocFailer allocFailer(0); |
| 283 |
allocFailer.deactivate(); |
| 284 |
(*testMessageHandler)(type, msg); |
| 285 |
allocFailer.reactivateAt(currentIndex); |
| 286 |
} |
| 287 |
|
| 288 |
typedef void (*PVF)(); |
| 289 |
PVF defaultTerminate; |
| 290 |
void debugTerminate() |
| 291 |
{ |
| 292 |
// you can detect uncaught exceptions with a breakpoint in here |
| 293 |
(*defaultTerminate)(); |
| 294 |
} |
| 295 |
|
| 296 |
PVF defaultUnexpected; |
| 297 |
void debugUnexpected() |
| 298 |
{ |
| 299 |
// you can detect unexpected exceptions with a breakpoint in here |
| 300 |
(*defaultUnexpected)(); |
| 301 |
} |
| 302 |
|
| 303 |
void tst_ExceptionSafety_Objects::initTestCase() |
| 304 |
{ |
| 305 |
// set handlers for bad exception cases, you might want to step in and breakpoint the default handlers too |
| 306 |
defaultTerminate = std::set_terminate(&debugTerminate); |
| 307 |
defaultUnexpected = std::set_unexpected(&debugUnexpected); |
| 308 |
testMessageHandler = qInstallMsgHandler(safeMessageHandler); |
| 309 |
|
| 310 |
QVERIFY(AllocFailer::initialize()); |
| 311 |
|
| 312 |
// sanity check whether OOM simulation works |
| 313 |
AllocFailer allocFailer(0); |
| 314 |
|
| 315 |
// malloc fail index is 0 -> this malloc should fail. |
| 316 |
void *buf = malloc(42); |
| 317 |
allocFailer.deactivate(); |
| 318 |
QVERIFY(!buf); |
| 319 |
|
| 320 |
// malloc fail index is 1 - second malloc should fail. |
| 321 |
allocFailer.reactivateAt(1); |
| 322 |
buf = malloc(42); |
| 323 |
void *buf2 = malloc(42); |
| 324 |
allocFailer.deactivate(); |
| 325 |
|
| 326 |
QVERIFY(buf); |
| 327 |
free(buf); |
| 328 |
QVERIFY(!buf2); |
| 329 |
|
| 330 |
#ifdef Q_OS_SYMBIAN |
| 331 |
// temporary workaround for INC138398 |
| 332 |
std::new_handler nh_func = std::set_new_handler(0); |
| 333 |
(void) std::set_new_handler(nh_func); |
| 334 |
#endif |
| 335 |
|
| 336 |
ObjectCreator<SelfTestObject> *selfTest = new ObjectCreator<SelfTestObject>; |
| 337 |
doOOMTest(*selfTest, 0); |
| 338 |
delete selfTest; |
| 339 |
QCOMPARE(alloc1Failed, 1); |
| 340 |
QCOMPARE(alloc2Failed, 1); |
| 341 |
QCOMPARE(alloc3Failed, 2); |
| 342 |
QCOMPARE(alloc4Failed, 3); |
| 343 |
QCOMPARE(malloc1Failed, 1); |
| 344 |
QCOMPARE(malloc2Failed, 1); |
| 345 |
} |
| 346 |
|
| 347 |
void tst_ExceptionSafety_Objects::cleanupTestCase() |
| 348 |
{ |
| 349 |
qInstallMsgHandler(testMessageHandler); |
| 350 |
} |
| 351 |
|
| 352 |
void tst_ExceptionSafety_Objects::objects() |
| 353 |
{ |
| 354 |
QLatin1String tag = QLatin1String(QTest::currentDataTag()); |
| 355 |
if (tag == QLatin1String("QFile") |
| 356 |
|| tag == QLatin1String("QProcess") |
| 357 |
|| tag == QLatin1String("QSettings") |
| 358 |
|| tag == QLatin1String("QThread") |
| 359 |
|| tag == QLatin1String("QThreadPool")) |
| 360 |
QSKIP("This type of object is not currently strongly exception safe", SkipSingle); |
| 361 |
|
| 362 |
QFETCH(AbstractTester *, objectCreator); |
| 363 |
|
| 364 |
doOOMTest(*objectCreator, 0); |
| 365 |
|
| 366 |
delete objectCreator; |
| 367 |
} |
| 368 |
|
| 369 |
template <typename T> |
| 370 |
struct WidgetCreator : public AbstractTester |
| 371 |
{ |
| 372 |
void operator()(QObject *parent) |
| 373 |
{ |
| 374 |
if (parent && !parent->isWidgetType()) |
| 375 |
qFatal("%s: parent must be either null or a widget type", Q_FUNC_INFO); |
| 376 |
QScopedPointer<T> ptr(parent ? new T(static_cast<QWidget *>(parent)) : new T); |
| 377 |
} |
| 378 |
}; |
| 379 |
|
| 380 |
// QSizeGrip doesn't have a default constructor - always pass parent (even though it might be 0) |
| 381 |
template <> struct WidgetCreator<QSizeGrip> : public AbstractTester |
| 382 |
{ |
| 383 |
void operator()(QObject *parent) |
| 384 |
{ |
| 385 |
if (parent && !parent->isWidgetType()) |
| 386 |
qFatal("%s: parent must be either null or a widget type", Q_FUNC_INFO); |
| 387 |
QScopedPointer<QSizeGrip> ptr(new QSizeGrip(static_cast<QWidget *>(parent))); |
| 388 |
} |
| 389 |
}; |
| 390 |
|
| 391 |
// QDesktopWidget doesn't need a parent. |
| 392 |
template <> struct WidgetCreator<QDesktopWidget> : public AbstractTester |
| 393 |
{ |
| 394 |
void operator()(QObject *parent) |
| 395 |
{ |
| 396 |
if (parent && !parent->isWidgetType()) |
| 397 |
qFatal("%s: parent must be either null or a widget type", Q_FUNC_INFO); |
| 398 |
QScopedPointer<QDesktopWidget> ptr(new QDesktopWidget()); |
| 399 |
} |
| 400 |
}; |
| 401 |
void tst_ExceptionSafety_Objects::widgets_data() |
| 402 |
{ |
| 403 |
#ifdef Q_OS_SYMBIAN |
| 404 |
// Initialise the S60 rasteriser, which crashes if started while out of memory |
| 405 |
QImage image(20, 20, QImage::Format_RGB32); |
| 406 |
QPainter p(&image); |
| 407 |
p.drawText(0, 15, "foo"); |
| 408 |
#endif |
| 409 |
|
| 410 |
QTest::addColumn<AbstractTester *>("widgetCreator"); |
| 411 |
|
| 412 |
#undef NEWROW |
| 413 |
#define NEWROW(T) QTest::newRow(#T) << static_cast<AbstractTester *>(new WidgetCreator<T >) |
| 414 |
|
| 415 |
NEWROW(QWidget); |
| 416 |
|
| 417 |
NEWROW(QButtonGroup); |
| 418 |
NEWROW(QCheckBox); |
| 419 |
NEWROW(QColumnView); |
| 420 |
NEWROW(QComboBox); |
| 421 |
NEWROW(QCommandLinkButton); |
| 422 |
NEWROW(QDateEdit); |
| 423 |
NEWROW(QDateTimeEdit); |
| 424 |
NEWROW(QDesktopWidget); |
| 425 |
NEWROW(QDial); |
| 426 |
NEWROW(QDoubleSpinBox); |
| 427 |
NEWROW(QFocusFrame); |
| 428 |
NEWROW(QFontComboBox); |
| 429 |
NEWROW(QFrame); |
| 430 |
NEWROW(QGroupBox); |
| 431 |
NEWROW(QLabel); |
| 432 |
NEWROW(QLCDNumber); |
| 433 |
NEWROW(QLineEdit); |
| 434 |
NEWROW(QListView); |
| 435 |
NEWROW(QListWidget); |
| 436 |
NEWROW(QMainWindow); |
| 437 |
NEWROW(QMenu); |
| 438 |
NEWROW(QMenuBar); |
| 439 |
NEWROW(QPlainTextEdit); |
| 440 |
NEWROW(QProgressBar); |
| 441 |
NEWROW(QPushButton); |
| 442 |
NEWROW(QRadioButton); |
| 443 |
NEWROW(QScrollArea); |
| 444 |
NEWROW(QScrollBar); |
| 445 |
NEWROW(QSizeGrip); |
| 446 |
NEWROW(QSlider); |
| 447 |
NEWROW(QSpinBox); |
| 448 |
NEWROW(QSplitter); |
| 449 |
NEWROW(QStackedWidget); |
| 450 |
NEWROW(QStatusBar); |
| 451 |
NEWROW(QTabBar); |
| 452 |
NEWROW(QTableView); |
| 453 |
NEWROW(QTableWidget); |
| 454 |
NEWROW(QTabWidget); |
| 455 |
NEWROW(QTextBrowser); |
| 456 |
NEWROW(QTextEdit); |
| 457 |
NEWROW(QTimeEdit); |
| 458 |
NEWROW(QToolBar); |
| 459 |
NEWROW(QToolBox); |
| 460 |
NEWROW(QToolButton); |
| 461 |
NEWROW(QTreeView); |
| 462 |
NEWROW(QTreeWidget); |
| 463 |
NEWROW(QWorkspace); |
| 464 |
} |
| 465 |
|
| 466 |
void tst_ExceptionSafety_Objects::widgets() |
| 467 |
{ |
| 468 |
QLatin1String tag = QLatin1String(QTest::currentDataTag()); |
| 469 |
if (tag == QLatin1String("QColumnView") |
| 470 |
|| tag == QLatin1String("QComboBox") |
| 471 |
|| tag == QLatin1String("QCommandLinkButton") |
| 472 |
|| tag == QLatin1String("QDateEdit") |
| 473 |
|| tag == QLatin1String("QDateTimeEdit") |
| 474 |
|| tag == QLatin1String("QDesktopWidget") |
| 475 |
|| tag == QLatin1String("QDoubleSpinBox") |
| 476 |
|| tag == QLatin1String("QFontComboBox") |
| 477 |
|| tag == QLatin1String("QGroupBox") |
| 478 |
|| tag == QLatin1String("QLineEdit") |
| 479 |
|| tag == QLatin1String("QListView") |
| 480 |
|| tag == QLatin1String("QListWidget") |
| 481 |
|| tag == QLatin1String("QMainWindow") |
| 482 |
|| tag == QLatin1String("QMenu") |
| 483 |
|| tag == QLatin1String("QMenuBar") |
| 484 |
|| tag == QLatin1String("QPlainTextEdit") |
| 485 |
|| tag == QLatin1String("QProgressBar") |
| 486 |
|| tag == QLatin1String("QPushButton") |
| 487 |
|| tag == QLatin1String("QScrollArea") |
| 488 |
|| tag == QLatin1String("QSpinBox") |
| 489 |
|| tag == QLatin1String("QStackedWidget") |
| 490 |
|| tag == QLatin1String("QStatusBar") |
| 491 |
|| tag == QLatin1String("QTableView") |
| 492 |
|| tag == QLatin1String("QTableWidget") |
| 493 |
|| tag == QLatin1String("QTabWidget") |
| 494 |
|| tag == QLatin1String("QTextBrowser") |
| 495 |
|| tag == QLatin1String("QTextEdit") |
| 496 |
|| tag == QLatin1String("QTimeEdit") |
| 497 |
|| tag == QLatin1String("QToolBar") |
| 498 |
|| tag == QLatin1String("QToolBox") |
| 499 |
|| tag == QLatin1String("QTreeView") |
| 500 |
|| tag == QLatin1String("QTreeWidget") |
| 501 |
|| tag == QLatin1String("QWorkspace")) |
| 502 |
QSKIP("This type of widget is not currently strongly exception safe", SkipSingle); |
| 503 |
|
| 504 |
QFETCH(AbstractTester *, widgetCreator); |
| 505 |
|
| 506 |
doOOMTest(*widgetCreator, 0, 00000); |
| 507 |
|
| 508 |
QWidget parent; |
| 509 |
doOOMTest(*widgetCreator, &parent, 00000); |
| 510 |
|
| 511 |
delete widgetCreator; |
| 512 |
|
| 513 |
// if the test reaches here without crashing, we passed :) |
| 514 |
QVERIFY(true); |
| 515 |
} |
| 516 |
|
| 517 |
struct Integer |
| 518 |
{ |
| 519 |
Integer(int value = 42) |
| 520 |
: ptr(new int(value)) |
| 521 |
{ |
| 522 |
++instanceCount; |
| 523 |
} |
| 524 |
|
| 525 |
Integer(const Integer &other) |
| 526 |
: ptr(new int(*other.ptr)) |
| 527 |
{ |
| 528 |
++instanceCount; |
| 529 |
} |
| 530 |
|
| 531 |
Integer &operator=(const Integer &other) |
| 532 |
{ |
| 533 |
int *newPtr = new int(*other.ptr); |
| 534 |
delete ptr; |
| 535 |
ptr = newPtr; |
| 536 |
return *this; |
| 537 |
} |
| 538 |
|
| 539 |
~Integer() |
| 540 |
{ |
| 541 |
--instanceCount; |
| 542 |
delete ptr; |
| 543 |
} |
| 544 |
|
| 545 |
int value() const |
| 546 |
{ |
| 547 |
return *ptr; |
| 548 |
} |
| 549 |
|
| 550 |
int *ptr; |
| 551 |
static int instanceCount; |
| 552 |
}; |
| 553 |
|
| 554 |
int Integer::instanceCount = 0; |
| 555 |
|
| 556 |
struct IntegerMoveable |
| 557 |
{ |
| 558 |
IntegerMoveable(int value = 42) |
| 559 |
: val(value) |
| 560 |
{ |
| 561 |
delete new int; |
| 562 |
++instanceCount; |
| 563 |
} |
| 564 |
|
| 565 |
IntegerMoveable(const IntegerMoveable &other) |
| 566 |
: val(other.val) |
| 567 |
{ |
| 568 |
delete new int; |
| 569 |
++instanceCount; |
| 570 |
} |
| 571 |
|
| 572 |
IntegerMoveable &operator=(const IntegerMoveable &other) |
| 573 |
{ |
| 574 |
delete new int; |
| 575 |
val = other.val; |
| 576 |
return *this; |
| 577 |
} |
| 578 |
|
| 579 |
~IntegerMoveable() |
| 580 |
{ |
| 581 |
--instanceCount; |
| 582 |
} |
| 583 |
|
| 584 |
int value() const |
| 585 |
{ |
| 586 |
return val; |
| 587 |
} |
| 588 |
|
| 589 |
int val; |
| 590 |
static int instanceCount; |
| 591 |
}; |
| 592 |
|
| 593 |
int IntegerMoveable::instanceCount = 0; |
| 594 |
QT_BEGIN_NAMESPACE |
| 595 |
Q_DECLARE_TYPEINFO(IntegerMoveable, Q_MOVABLE_TYPE); |
| 596 |
QT_END_NAMESPACE |
| 597 |
|
| 598 |
template <typename T, template<typename> class Container> |
| 599 |
void containerInsertTest(QObject*) |
| 600 |
{ |
| 601 |
Container<T> container; |
| 602 |
|
| 603 |
// insert an item in an empty container |
| 604 |
try { |
| 605 |
container.insert(container.begin(), 41); |
| 606 |
} catch (...) { |
| 607 |
QVERIFY(container.isEmpty()); |
| 608 |
QCOMPARE(T::instanceCount, 0); |
| 609 |
return; |
| 610 |
} |
| 611 |
|
| 612 |
QCOMPARE(container.size(), 1); |
| 613 |
QCOMPARE(T::instanceCount, 1); |
| 614 |
|
| 615 |
// insert an item before another item |
| 616 |
try { |
| 617 |
container.insert(container.begin(), 42); |
| 618 |
} catch (...) { |
| 619 |
QCOMPARE(container.size(), 1); |
| 620 |
QCOMPARE(container.first().value(), 41); |
| 621 |
QCOMPARE(T::instanceCount, 1); |
| 622 |
return; |
| 623 |
} |
| 624 |
|
| 625 |
QCOMPARE(T::instanceCount, 2); |
| 626 |
|
| 627 |
// insert an item in between |
| 628 |
try { |
| 629 |
container.insert(container.begin() + 1, 43); |
| 630 |
} catch (...) { |
| 631 |
QCOMPARE(container.size(), 2); |
| 632 |
QCOMPARE(container.first().value(), 41); |
| 633 |
QCOMPARE((container.begin() + 1)->value(), 42); |
| 634 |
QCOMPARE(T::instanceCount, 2); |
| 635 |
return; |
| 636 |
} |
| 637 |
|
| 638 |
QCOMPARE(T::instanceCount, 3); |
| 639 |
} |
| 640 |
|
| 641 |
template <typename T, template<typename> class Container> |
| 642 |
void containerAppendTest(QObject*) |
| 643 |
{ |
| 644 |
Container<T> container; |
| 645 |
|
| 646 |
// append to an empty container |
| 647 |
try { |
| 648 |
container.append(42); |
| 649 |
} catch (...) { |
| 650 |
QCOMPARE(container.size(), 0); |
| 651 |
QCOMPARE(T::instanceCount, 0); |
| 652 |
return; |
| 653 |
} |
| 654 |
|
| 655 |
// append to a container with one item |
| 656 |
try { |
| 657 |
container.append(43); |
| 658 |
} catch (...) { |
| 659 |
QCOMPARE(container.size(), 1); |
| 660 |
QCOMPARE(container.first().value(), 42); |
| 661 |
QCOMPARE(T::instanceCount, 1); |
| 662 |
return; |
| 663 |
} |
| 664 |
|
| 665 |
Container<T> container2; |
| 666 |
|
| 667 |
try { |
| 668 |
container2.append(44); |
| 669 |
} catch (...) { |
| 670 |
// don't care |
| 671 |
return; |
| 672 |
} |
| 673 |
QCOMPARE(T::instanceCount, 3); |
| 674 |
|
| 675 |
// append another container with one item |
| 676 |
try { |
| 677 |
container += container2; |
| 678 |
} catch (...) { |
| 679 |
QCOMPARE(container.size(), 2); |
| 680 |
QCOMPARE(container.first().value(), 42); |
| 681 |
QCOMPARE((container.begin() + 1)->value(), 43); |
| 682 |
QCOMPARE(T::instanceCount, 3); |
| 683 |
return; |
| 684 |
} |
| 685 |
|
| 686 |
QCOMPARE(T::instanceCount, 4); |
| 687 |
} |
| 688 |
|
| 689 |
template <typename T, template<typename> class Container> |
| 690 |
void containerEraseTest(QObject*) |
| 691 |
{ |
| 692 |
Container<T> container; |
| 693 |
|
| 694 |
try { |
| 695 |
container.append(42); |
| 696 |
container.append(43); |
| 697 |
container.append(44); |
| 698 |
container.append(45); |
| 699 |
container.append(46); |
| 700 |
} catch (...) { |
| 701 |
// don't care |
| 702 |
return; |
| 703 |
} |
| 704 |
|
| 705 |
// sanity checks |
| 706 |
QCOMPARE(container.size(), 5); |
| 707 |
QCOMPARE(T::instanceCount, 5); |
| 708 |
|
| 709 |
// delete the first one |
| 710 |
try { |
| 711 |
container.erase(container.begin()); |
| 712 |
} catch (...) { |
| 713 |
QCOMPARE(container.size(), 5); |
| 714 |
QCOMPARE(container.first().value(), 42); |
| 715 |
QCOMPARE(T::instanceCount, 5); |
| 716 |
return; |
| 717 |
} |
| 718 |
|
| 719 |
QCOMPARE(container.size(), 4); |
| 720 |
QCOMPARE(container.first().value(), 43); |
| 721 |
QCOMPARE(T::instanceCount, 4); |
| 722 |
|
| 723 |
// delete the last one |
| 724 |
try { |
| 725 |
container.erase(container.end() - 1); |
| 726 |
} catch (...) { |
| 727 |
QCOMPARE(container.size(), 4); |
| 728 |
QCOMPARE(T::instanceCount, 4); |
| 729 |
return; |
| 730 |
} |
| 731 |
|
| 732 |
QCOMPARE(container.size(), 3); |
| 733 |
QCOMPARE(container.first().value(), 43); |
| 734 |
QCOMPARE((container.begin() + 1)->value(), 44); |
| 735 |
QCOMPARE((container.begin() + 2)->value(), 45); |
| 736 |
QCOMPARE(T::instanceCount, 3); |
| 737 |
|
| 738 |
// delete the middle one |
| 739 |
try { |
| 740 |
container.erase(container.begin() + 1); |
| 741 |
} catch (...) { |
| 742 |
QCOMPARE(container.size(), 3); |
| 743 |
QCOMPARE(container.first().value(), 43); |
| 744 |
QCOMPARE((container.begin() + 1)->value(), 44); |
| 745 |
QCOMPARE((container.begin() + 2)->value(), 45); |
| 746 |
QCOMPARE(T::instanceCount, 3); |
| 747 |
return; |
| 748 |
} |
| 749 |
|
| 750 |
QCOMPARE(container.size(), 2); |
| 751 |
QCOMPARE(container.first().value(), 43); |
| 752 |
QCOMPARE((container.begin() + 1)->value(), 45); |
| 753 |
QCOMPARE(T::instanceCount, 2); |
| 754 |
} |
| 755 |
|
| 756 |
template <template<typename T> class Container> |
| 757 |
static void containerData() |
| 758 |
{ |
| 759 |
QTest::addColumn<TestFunction>("testFunction"); |
| 760 |
|
| 761 |
QTest::newRow("insert static") << static_cast<TestFunction>(containerInsertTest<Integer, Container>); |
| 762 |
QTest::newRow("append static") << static_cast<TestFunction>(containerAppendTest<Integer, Container>); |
| 763 |
QTest::newRow("erase static") << static_cast<TestFunction>(containerEraseTest<Integer, Container>); |
| 764 |
QTest::newRow("insert moveable") << static_cast<TestFunction>(containerInsertTest<IntegerMoveable, Container>); |
| 765 |
QTest::newRow("append moveable") << static_cast<TestFunction>(containerAppendTest<IntegerMoveable, Container>); |
| 766 |
QTest::newRow("erase moveable") << static_cast<TestFunction>(containerEraseTest<IntegerMoveable, Container>); |
| 767 |
} |
| 768 |
|
| 769 |
void tst_ExceptionSafety_Objects::vector_data() |
| 770 |
{ |
| 771 |
containerData<QVector>(); |
| 772 |
} |
| 773 |
|
| 774 |
void tst_ExceptionSafety_Objects::vector() |
| 775 |
{ |
| 776 |
QFETCH(TestFunction, testFunction); |
| 777 |
|
| 778 |
if (QLatin1String(QTest::currentDataTag()) == QLatin1String("insert static") |
| 779 |
|| QLatin1String(QTest::currentDataTag()) == QLatin1String("insert moveable")) |
| 780 |
QSKIP("QVector::insert is currently not strongly exception safe", SkipSingle); |
| 781 |
|
| 782 |
doOOMTest(testFunction, 0); |
| 783 |
} |
| 784 |
|
| 785 |
void tst_ExceptionSafety_Objects::list_data() |
| 786 |
{ |
| 787 |
containerData<QList>(); |
| 788 |
} |
| 789 |
|
| 790 |
void tst_ExceptionSafety_Objects::list() |
| 791 |
{ |
| 792 |
QFETCH(TestFunction, testFunction); |
| 793 |
|
| 794 |
doOOMTest(testFunction, 0); |
| 795 |
} |
| 796 |
|
| 797 |
void tst_ExceptionSafety_Objects::linkedList_data() |
| 798 |
{ |
| 799 |
containerData<QLinkedList>(); |
| 800 |
} |
| 801 |
|
| 802 |
void tst_ExceptionSafety_Objects::linkedList() |
| 803 |
{ |
| 804 |
QFETCH(TestFunction, testFunction); |
| 805 |
|
| 806 |
doOOMTest(testFunction, 0); |
| 807 |
} |
| 808 |
|
| 809 |
QTEST_MAIN(tst_ExceptionSafety_Objects) |
| 810 |
#include "tst_exceptionsafety_objects.moc" |
| 811 |
#endif // QT_NO_EXCEPTIONS |