tests/benchmarks/qrect/main.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 // This file contains benchmarks for QRect/QRectF functions.
       
    42 
       
    43 #include <QDebug>
       
    44 #include <qtest.h>
       
    45 
       
    46 class tst_qrect : public QObject
       
    47 {
       
    48     Q_OBJECT
       
    49 private slots:
       
    50     // QRect functions:
       
    51     void contains_point_data();
       
    52     void contains_point();
       
    53     void contains_rect_data();
       
    54     void contains_rect();
       
    55     void intersects_data();
       
    56     void intersects();
       
    57     void intersected_data();
       
    58     void intersected();
       
    59     void united_data();
       
    60     void united();
       
    61 
       
    62     // QRectF functions:
       
    63     void contains_point_f_data();
       
    64     void contains_point_f();
       
    65     void contains_rect_f_data();
       
    66     void contains_rect_f();
       
    67     void intersects_f_data();
       
    68     void intersects_f();
       
    69     void intersected_f_data();
       
    70     void intersected_f();
       
    71     void united_f_data();
       
    72     void united_f();
       
    73 };
       
    74 
       
    75 struct RectRectCombination
       
    76 {
       
    77     QString tag;
       
    78     qreal x1, y1, w1, h1, x2, y2, w2, h2;
       
    79     RectRectCombination(
       
    80         const QString &tag,
       
    81         const qreal x1, const qreal y1, const qreal w1, const qreal h1,
       
    82         const qreal x2, const qreal y2, const qreal w2, const qreal h2)
       
    83         : tag(tag), x1(x1), y1(y1), w1(w1), h1(h1), x2(x2), y2(y2), w2(w2), h2(h2) {}
       
    84 };
       
    85 
       
    86 static QList<RectRectCombination> createRectRectCombinations()
       
    87 {
       
    88     QList<RectRectCombination> result;
       
    89     result << RectRectCombination("null", 0, 0, 0, 0, 0, 0, 0, 0);
       
    90     result << RectRectCombination("null1", 0, 0, 0, 0, 0, 0, 10, 10);
       
    91     result << RectRectCombination("null2", 0, 0, 10, 10, 0, 0, 0, 0);
       
    92 
       
    93     result << RectRectCombination("miss", 0, 0, 10, 10, 11, 11, 10, 10);
       
    94     result << RectRectCombination("intersect", 0, 0, 10, 10, 5, 5, 10, 10);
       
    95     result << RectRectCombination("contain1", 0, 0, 10, 10, 1, 1, 8, 8);
       
    96     result << RectRectCombination("contain2", 1, 1, 8, 8, 0, 0, 10, 10);
       
    97 
       
    98     result << RectRectCombination("miss_flip1", 9, 9, -10, -10, 11, 11, 10, 10);
       
    99     result << RectRectCombination("intersect_flip1", 9, 9, -10, -10, 5, 5, 10, 10);
       
   100     result << RectRectCombination("contain1_flip1", 9, 9, -10, -10, 1, 1, 8, 8);
       
   101     result << RectRectCombination("contain2_flip1", 8, 8, -8, -8, 0, 0, 10, 10);
       
   102 
       
   103     result << RectRectCombination("miss_flip2", 0, 0, 10, 10, 20, 20, -10, -10);
       
   104     result << RectRectCombination("intersect_flip2", 0, 0, 10, 10, 14, 14, -10, -10);
       
   105     result << RectRectCombination("contain1_flip2", 0, 0, 10, 10, 8, 8, -8, -8);
       
   106     result << RectRectCombination("contain2_flip2", 1, 1, 8, 8, 9, 9, -10, -10);
       
   107 
       
   108     return result;
       
   109 }
       
   110 
       
   111 static void addRectRectData(bool includeProperArg = false)
       
   112 {
       
   113     QTest::addColumn<QRectF>("rf1");
       
   114     QTest::addColumn<QRectF>("rf2");
       
   115     if (includeProperArg)
       
   116         QTest::addColumn<bool>("proper");
       
   117     for (int i = 0; i < (includeProperArg ? 2 : 1); ++i) {
       
   118         QList<RectRectCombination> combinations = createRectRectCombinations();
       
   119         foreach (RectRectCombination c, combinations) {
       
   120             QTestData &testData = QTest::newRow(c.tag.toLatin1().data());
       
   121             QRectF r1(c.x1, c.y1, c.w1, c.h1);
       
   122             QRectF r2(c.x2, c.y2, c.w2, c.h2);
       
   123             testData << r1 << r2;
       
   124             if (includeProperArg)
       
   125                 testData << (i == 0);
       
   126         }
       
   127     }
       
   128 }
       
   129 
       
   130 struct RectPointCombination
       
   131 {
       
   132     QString tag;
       
   133     qreal x, y, w, h, px, py;
       
   134     RectPointCombination(
       
   135         const QString &tag,
       
   136         const qreal x, const qreal y, const qreal w, const qreal h, const qreal px, const qreal py)
       
   137         : tag(tag), x(x), y(y), w(w), h(h), px(px), py(py) {}
       
   138 };
       
   139 
       
   140 static QList<RectPointCombination> createRectPointCombinations()
       
   141 {
       
   142     QList<RectPointCombination> result;
       
   143     result << RectPointCombination("null", 0, 0, 0, 0, 0, 0);
       
   144 
       
   145     result << RectPointCombination("miss", 0, 0, 10, 10, -1, -1);
       
   146     result << RectPointCombination("contain", 0, 0, 10, 10, 0, 0);
       
   147     result << RectPointCombination("contain_proper", 0, 0, 10, 10, 1, 1);
       
   148 
       
   149     result << RectPointCombination("miss_flip", 9, 9, -10, -10, -1, -1);
       
   150     result << RectPointCombination("contain_flip", 9, 9, -10, -10, 0, 0);
       
   151     result << RectPointCombination("contain_flip_proper", 9, 9, -10, -10, 1, 1);
       
   152 
       
   153     return result;
       
   154 }
       
   155 
       
   156 static void addRectPointData(bool includeProperArg = false)
       
   157 {
       
   158     QTest::addColumn<QRectF>("rf");
       
   159     QTest::addColumn<QPointF>("pf");
       
   160     if (includeProperArg)
       
   161         QTest::addColumn<bool>("proper");
       
   162     for (int i = 0; i < (includeProperArg ? 2 : 1); ++i) {
       
   163         QList<RectPointCombination> combinations = createRectPointCombinations();
       
   164         foreach (RectPointCombination c, combinations) {
       
   165             QTestData &testData = QTest::newRow(c.tag.toLatin1().data());
       
   166             QRectF r(c.x, c.y, c.w, c.h);
       
   167             QPointF p(c.px, c.py);
       
   168             testData << r << p;
       
   169             if (includeProperArg)
       
   170                 testData << (i == 0);
       
   171         }
       
   172     }
       
   173 }
       
   174 
       
   175 void tst_qrect::contains_point_data()
       
   176 {
       
   177     addRectPointData(true);
       
   178 }
       
   179 
       
   180 void tst_qrect::contains_point()
       
   181 {
       
   182     QFETCH(QRectF, rf);
       
   183     QFETCH(QPointF, pf);
       
   184     QFETCH(bool, proper);
       
   185     QRect r(rf.toRect());
       
   186     QPoint p(pf.toPoint());
       
   187     QBENCHMARK {
       
   188         r.contains(p, proper);
       
   189     }
       
   190 }
       
   191 
       
   192 void tst_qrect::contains_rect_data()
       
   193 {
       
   194     addRectRectData(true);
       
   195 }
       
   196 
       
   197 void tst_qrect::contains_rect()
       
   198 {
       
   199     QFETCH(QRectF, rf1);
       
   200     QFETCH(QRectF, rf2);
       
   201     QFETCH(bool, proper);
       
   202     QRect r1(rf1.toRect());
       
   203     QRect r2(rf2.toRect());
       
   204     QBENCHMARK {
       
   205         r1.contains(r2, proper);
       
   206     }
       
   207 }
       
   208 
       
   209 void tst_qrect::intersects_data()
       
   210 {
       
   211     addRectRectData();
       
   212 }
       
   213 
       
   214 void tst_qrect::intersects()
       
   215 {
       
   216     QFETCH(QRectF, rf1);
       
   217     QFETCH(QRectF, rf2);
       
   218     QRect r1(rf1.toRect());
       
   219     QRect r2(rf2.toRect());
       
   220     QBENCHMARK {
       
   221         r1.intersects(r2);
       
   222     }
       
   223 }
       
   224 
       
   225 void tst_qrect::intersected_data()
       
   226 {
       
   227     addRectRectData();
       
   228 }
       
   229 
       
   230 void tst_qrect::intersected()
       
   231 {
       
   232     QFETCH(QRectF, rf1);
       
   233     QFETCH(QRectF, rf2);
       
   234     QRect r1(rf1.toRect());
       
   235     QRect r2(rf2.toRect());
       
   236     QBENCHMARK {
       
   237         r1.intersected(r2);
       
   238     }
       
   239 }
       
   240 
       
   241 void tst_qrect::united_data()
       
   242 {
       
   243     addRectRectData();
       
   244 }
       
   245 
       
   246 void tst_qrect::united()
       
   247 {
       
   248     QFETCH(QRectF, rf1);
       
   249     QFETCH(QRectF, rf2);
       
   250     QRect r1(rf1.toRect());
       
   251     QRect r2(rf2.toRect());
       
   252     QBENCHMARK {
       
   253         r1.united(r2);
       
   254     }
       
   255 }
       
   256 
       
   257 void tst_qrect::contains_point_f_data()
       
   258 {
       
   259     addRectPointData();
       
   260 }
       
   261 
       
   262 void tst_qrect::contains_point_f()
       
   263 {
       
   264     QFETCH(QRectF, rf);
       
   265     QFETCH(QPointF, pf);
       
   266     QBENCHMARK {
       
   267         rf.contains(pf);
       
   268     }
       
   269 }
       
   270 
       
   271 void tst_qrect::contains_rect_f_data()
       
   272 {
       
   273     addRectRectData();
       
   274 }
       
   275 
       
   276 void tst_qrect::contains_rect_f()
       
   277 {
       
   278     QFETCH(QRectF, rf1);
       
   279     QFETCH(QRectF, rf2);
       
   280     QBENCHMARK {
       
   281         rf1.contains(rf2);
       
   282     }
       
   283 }
       
   284 
       
   285 void tst_qrect::intersects_f_data()
       
   286 {
       
   287     addRectRectData();
       
   288 }
       
   289 
       
   290 void tst_qrect::intersects_f()
       
   291 {
       
   292     QFETCH(QRectF, rf1);
       
   293     QFETCH(QRectF, rf2);
       
   294     QBENCHMARK {
       
   295         rf1.intersects(rf2);
       
   296     }
       
   297 }
       
   298 
       
   299 void tst_qrect::intersected_f_data()
       
   300 {
       
   301     addRectRectData();
       
   302 }
       
   303 
       
   304 void tst_qrect::intersected_f()
       
   305 {
       
   306     QFETCH(QRectF, rf1);
       
   307     QFETCH(QRectF, rf2);
       
   308     QBENCHMARK {
       
   309         rf1.intersected(rf2);
       
   310     }
       
   311 }
       
   312 
       
   313 void tst_qrect::united_f_data()
       
   314 {
       
   315     addRectRectData();
       
   316 }
       
   317 
       
   318 void tst_qrect::united_f()
       
   319 {
       
   320     QFETCH(QRectF, rf1);
       
   321     QFETCH(QRectF, rf2);
       
   322     QBENCHMARK {
       
   323         rf1.united(rf2);
       
   324     }
       
   325 }
       
   326 
       
   327 QTEST_MAIN(tst_qrect)
       
   328 
       
   329 #include "main.moc"