diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2017-04-13 21:13:52 -0700 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2017-11-08 09:14:03 +0000 |
commit | 19b0ce5daa31e2ffebfcf2701143742302f1deb4 (patch) | |
tree | 730cccd80947c60ee4872e16f71d4d48d906c354 /tests/auto/widgets | |
parent | 59c5f7bd9da63621887260fc45e6669282d71ecd (diff) |
Change almost all other uses of qrand() to QRandomGenerator
The vast majority is actually switched to QRandomGenerator::bounded(),
which gives a mostly uniform distribution over the [0, bound)
range. There are very few floating point cases left, as many of those
that did use floating point did not need to, after all. (I did leave
some that were too ugly for me to understand)
This commit also found a couple of calls to rand() instead of qrand().
This commit does not include changes to SSL code that continues to use
qrand() (job for someone else):
src/network/ssl/qsslkey_qt.cpp
src/network/ssl/qsslsocket_mac.cpp
tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp
Change-Id: Icd0e0d4b27cb4e5eb892fffd14b5285d43f4afbf
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests/auto/widgets')
6 files changed, 36 insertions, 56 deletions
diff --git a/tests/auto/widgets/dialogs/qfiledialog2/tst_qfiledialog2.cpp b/tests/auto/widgets/dialogs/qfiledialog2/tst_qfiledialog2.cpp index 09e070ac20..eafd4d7cea 100644 --- a/tests/auto/widgets/dialogs/qfiledialog2/tst_qfiledialog2.cpp +++ b/tests/auto/widgets/dialogs/qfiledialog2/tst_qfiledialog2.cpp @@ -48,6 +48,7 @@ #include <qlineedit.h> #include <qlayout.h> #include <qmenu.h> +#include <qrandom.h> #include "../../../../../src/widgets/dialogs/qsidebar_p.h" #include "../../../../../src/widgets/dialogs/qfilesystemmodel_p.h" #include "../../../../../src/widgets/dialogs/qfiledialog_p.h" @@ -1196,7 +1197,7 @@ void tst_QFileDialog2::QTBUG6558_showDirsOnly() { const QString tempPath = tempDir.path(); QDir dirTemp(tempPath); - const QString tempName = QLatin1String("showDirsOnly.") + QString::number(qrand()); + const QString tempName = QLatin1String("showDirsOnly.") + QString::number(QRandomGenerator::global()->generate()); dirTemp.mkdir(tempName); dirTemp.cd(tempName); QTRY_VERIFY(dirTemp.exists()); diff --git a/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp index 2abb6e3515..da15583d5d 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp @@ -32,6 +32,7 @@ #include <private/qgraphicsitem_p.h> #include <private/qgraphicsview_p.h> #include <private/qgraphicsscene_p.h> +#include <QRandomGenerator> #include <QStyleOptionGraphicsItem> #include <QAbstractTextDocumentLayout> #include <QBitmap> @@ -3595,12 +3596,12 @@ void tst_QGraphicsItem::group() QList<QGraphicsItem *> newItems; for (int i = 0; i < 100; ++i) { QGraphicsItem *item = scene.addRect(QRectF(-25, -25, 50, 50), QPen(Qt::black, 0), - QBrush(QColor(rand() % 255, rand() % 255, - rand() % 255, rand() % 255))); + QBrush(QColor(QRandomGenerator::global()->bounded(255), QRandomGenerator::global()->bounded(255), + QRandomGenerator::global()->bounded(255), QRandomGenerator::global()->bounded(255)))); newItems << item; - item->setPos(-1000 + rand() % 2000, - -1000 + rand() % 2000); - item->setTransform(QTransform().rotate(rand() % 90), true); + item->setPos(-1000 + QRandomGenerator::global()->bounded(2000), + -1000 + QRandomGenerator::global()->bounded(2000)); + item->setTransform(QTransform().rotate(QRandomGenerator::global()->bounded(90)), true); } view.fitInView(scene.itemsBoundingRect()); @@ -4143,8 +4144,8 @@ void tst_QGraphicsItem::ensureVisible() for (int x = -100; x < 100; x += 25) { for (int y = -100; y < 100; y += 25) { - int xmargin = rand() % 75; - int ymargin = rand() % 75; + int xmargin = QRandomGenerator::global()->bounded(75); + int ymargin = QRandomGenerator::global()->bounded(75); item->ensureVisible(x, y, 25, 25, xmargin, ymargin); QApplication::processEvents(); @@ -7137,7 +7138,7 @@ public: : QGraphicsRectItem(QRectF(-10, -10, 20, 20)) { setPen(QPen(Qt::black, 0)); - setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); + setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); } QTransform x; diff --git a/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp b/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp index 7615c5e821..968233438a 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp @@ -1507,10 +1507,10 @@ void tst_QGraphicsScene::mouseGrabberItem() QCOMPARE(scene.mouseGrabberItem(), topMostItem); // Geometrical changes should not affect the mouse grabber. - item->setZValue(rand() % 500); - item2->setZValue(rand() % 500); - item->setPos(rand() % 50000, rand() % 50000); - item2->setPos(rand() % 50000, rand() % 50000); + item->setZValue(QRandomGenerator::global()->bounded(500)); + item2->setZValue(QRandomGenerator::global()->bounded(500)); + item->setPos(QRandomGenerator::global()->bounded(50000), QRandomGenerator::global()->bounded(50000)); + item2->setPos(QRandomGenerator::global()->bounded(50000), QRandomGenerator::global()->bounded(50000)); } QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); @@ -3424,7 +3424,7 @@ void tst_QGraphicsScene::task139710_bspTreeCrash() // add 1000 more items - the BSP tree is now resized for (int i = 0; i < 1000; ++i) { QGraphicsRectItem *item = scene.addRect(QRectF(0, 0, 200, 200)); - item->setPos(qrand() % 10000, qrand() % 10000); + item->setPos(QRandomGenerator::global()->bounded(10000), QRandomGenerator::global()->bounded(10000)); } // trigger delayed item indexing for the first 1000 items @@ -3433,7 +3433,7 @@ void tst_QGraphicsScene::task139710_bspTreeCrash() // add 1000 more items - the BSP tree is now resized for (int i = 0; i < 1000; ++i) { QGraphicsRectItem *item = scene.addRect(QRectF(0, 0, 200, 200)); - item->setPos(qrand() % 10000, qrand() % 10000); + item->setPos(QRandomGenerator::global()->bounded(10000), QRandomGenerator::global()->bounded(10000)); } // get items from the BSP tree and use them. there was junk in the tree @@ -3543,15 +3543,15 @@ void tst_QGraphicsScene::sorting() QGraphicsRectItem *c_2_1 = new QGraphicsRectItem(0, 0, 30, 30, c_2); QGraphicsRectItem *c_2_1_1 = new QGraphicsRectItem(0, 0, 20, 20, c_2_1); QGraphicsRectItem *c_2_2 = new QGraphicsRectItem(0, 0, 30, 30, c_2); - t_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_1_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_1_1_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_1_2->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_2->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_2_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_2_1_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_2_2->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); + t_1->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_1->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_1_1->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_1_1_1->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_1_2->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_2->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_2_1->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_2_1_1->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_2_2->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); c_1->setPos(23, 18); c_1_1->setPos(24, 28); diff --git a/tests/auto/widgets/itemviews/qitemview/tst_qitemview.cpp b/tests/auto/widgets/itemviews/qitemview/tst_qitemview.cpp index 1fd7b31e22..f1239b2e7c 100644 --- a/tests/auto/widgets/itemviews/qitemview/tst_qitemview.cpp +++ b/tests/auto/widgets/itemviews/qitemview/tst_qitemview.cpp @@ -30,16 +30,6 @@ #include <QtTest/QtTest> #include <QtCore/QtCore> #include "viewstotest.cpp" -#include <stdlib.h> - -#if defined(Q_OS_UNIX) || defined(Q_OS_WIN) -#include <time.h> -#endif - -#if defined(Q_OS_WIN) -# define random rand -# define srandom srand -#endif /*! See viewstotest.cpp for instructions on how to have your view tested with these tests. @@ -410,13 +400,15 @@ void touch(QWidget *widget, Qt::KeyboardModifier modifier, Qt::Key keyPress){ int width = widget->width(); int height = widget->height(); for (int i = 0; i < 5; ++i) { - QTest::mouseClick(widget, Qt::LeftButton, modifier, QPoint(random() % width, random() % height)); - QTest::mouseDClick(widget, Qt::LeftButton, modifier, QPoint(random() % width, random() % height)); - QPoint press(random() % width, random() % height); - QPoint releasePoint(random() % width, random() % height); + QTest::mouseClick(widget, Qt::LeftButton, modifier, + QPoint(QRandomGenerator::global()->bounded(width), QRandomGenerator::global()->bounded(height))); + QTest::mouseDClick(widget, Qt::LeftButton, modifier, + QPoint(QRandomGenerator::global()->bounded(width), QRandomGenerator::global()->bounded(height))); + QPoint press(QRandomGenerator::global()->bounded(width), QRandomGenerator::global()->bounded(height)); + QPoint releasePoint(QRandomGenerator::global()->bounded(width), QRandomGenerator::global()->bounded(height)); QTest::mousePress(widget, Qt::LeftButton, modifier, press); QTest::mouseMove(widget, releasePoint); - if (random() % 1 == 0) + if (QRandomGenerator::global()->bounded(1) == 0) QTest::mouseRelease(widget, Qt::LeftButton, 0, releasePoint); else QTest::mouseRelease(widget, Qt::LeftButton, modifier, releasePoint); @@ -443,7 +435,6 @@ void tst_QItemView::spider() view->setModel(treeModel); view->show(); QVERIFY(QTest::qWaitForWindowActive(view)); - srandom(time(0)); touch(view->viewport(), Qt::NoModifier, Qt::Key_Left); touch(view->viewport(), Qt::ShiftModifier, Qt::Key_Enter); touch(view->viewport(), Qt::ControlModifier, Qt::Key_Backspace); diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp index 30fc927f7d..7d4024b556 100644 --- a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp +++ b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp @@ -50,6 +50,7 @@ #include <qcalendarwidget.h> #include <qmainwindow.h> #include <qdockwidget.h> +#include <qrandom.h> #include <qtoolbar.h> #include <qtoolbutton.h> #include <QtGui/qpaintengine.h> @@ -9826,7 +9827,7 @@ void tst_QWidget::grab() for (int row = 0; row < image.height(); ++row) { QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(row)); for (int col = 0; col < image.width(); ++col) - line[col] = qRgba(rand() & 255, row, col, opaque ? 255 : 127); + line[col] = qRgba(QRandomGenerator::global()->bounded(255), row, col, opaque ? 255 : 127); } QPalette pal = widget.palette(); diff --git a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp index 71969ec695..3d9620e75a 100644 --- a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp @@ -43,10 +43,6 @@ #include <private/qapplication_p.h> #include "qclipboard.h" -#ifdef Q_OS_MAC -#include <cstdlib> // For the random function. -#endif - #include <qlineedit.h> #include <private/qlineedit_p.h> #include <private/qwidgetlinecontrol_p.h> @@ -1949,17 +1945,7 @@ void tst_QLineEdit::noCursorBlinkWhenReadOnly() static void figureOutProperKey(Qt::Key &key, Qt::KeyboardModifiers &pressState) { #ifdef Q_OS_MAC - static bool tst_lineedit_randomized = false; - // Mac has 3 different ways of accomplishing this (same for moving to the back) - // So I guess we should just randomly do this for now. Which may get people mad, but if - // we fail at one point, it's just a matter of setting roll to the correct value - // instead of random. - - if (!tst_lineedit_randomized) { - tst_lineedit_randomized = true; - ::srandom(ulong(time(0))); - } - long roll = ::random() % 3; + long roll = QRandomGenerator::global()->bounded(3); switch (roll) { case 0: key = key == Qt::Key_Home ? Qt::Key_Up : Qt::Key_Down; |