| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the test suite of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 |  | 
| 29 | #include <QtTest/QtTest> | 
| 30 | #include <qsize.h> | 
| 31 |  | 
| 32 | Q_DECLARE_METATYPE(QMarginsF) | 
| 33 |  | 
| 34 | class tst_QSizeF : public QObject | 
| 35 | { | 
| 36 |     Q_OBJECT | 
| 37 | private slots: | 
| 38 |     void isNull_data(); | 
| 39 |     void isNull(); | 
| 40 |  | 
| 41 |     void scale(); | 
| 42 |  | 
| 43 |     void expandedTo(); | 
| 44 |     void expandedTo_data(); | 
| 45 |  | 
| 46 |     void boundedTo_data(); | 
| 47 |     void boundedTo(); | 
| 48 |  | 
| 49 |     void grownOrShrunkBy_data(); | 
| 50 |     void grownOrShrunkBy(); | 
| 51 |  | 
| 52 |     void transpose_data(); | 
| 53 |     void transpose(); | 
| 54 | }; | 
| 55 |  | 
| 56 | void tst_QSizeF::isNull_data() | 
| 57 | { | 
| 58 |     QTest::addColumn<qreal>(name: "width" ); | 
| 59 |     QTest::addColumn<qreal>(name: "height" ); | 
| 60 |     QTest::addColumn<bool>(name: "isNull" ); | 
| 61 |  | 
| 62 |     QTest::newRow(dataTag: "0, 0" ) << qreal(0.0) << qreal(0.0) << true; | 
| 63 |     QTest::newRow(dataTag: "-0, -0" ) << qreal(-0.0) << qreal(-0.0) << true; | 
| 64 |     QTest::newRow(dataTag: "0, -0" ) << qreal(0) << qreal(-0.0) << true; | 
| 65 |     QTest::newRow(dataTag: "-0, 0" ) << qreal(-0.0) << qreal(0) << true; | 
| 66 |     QTest::newRow(dataTag: "-0.1, 0" ) << qreal(-0.1) << qreal(0) << false; | 
| 67 |     QTest::newRow(dataTag: "0, -0.1" ) << qreal(0) << qreal(-0.1) << false; | 
| 68 |     QTest::newRow(dataTag: "0.1, 0" ) << qreal(0.1) << qreal(0) << false; | 
| 69 |     QTest::newRow(dataTag: "0, 0.1" ) << qreal(0) << qreal(0.1) << false; | 
| 70 | } | 
| 71 |  | 
| 72 | void tst_QSizeF::isNull() | 
| 73 | { | 
| 74 |     QFETCH(qreal, width); | 
| 75 |     QFETCH(qreal, height); | 
| 76 |     QFETCH(bool, isNull); | 
| 77 |  | 
| 78 |     QSizeF size(width, height); | 
| 79 |     QCOMPARE(size.width(), width); | 
| 80 |     QCOMPARE(size.height(), height); | 
| 81 |     QCOMPARE(size.isNull(), isNull); | 
| 82 | } | 
| 83 |  | 
| 84 | void tst_QSizeF::scale() { | 
| 85 |     QSizeF t1(10.4, 12.8); | 
| 86 |     t1.scale(w: 60.6, h: 60.6, mode: Qt::IgnoreAspectRatio); | 
| 87 |     QCOMPARE(t1, QSizeF(60.6, 60.6)); | 
| 88 |  | 
| 89 |     QSizeF t2(10.4, 12.8); | 
| 90 |     t2.scale(w: 43.52, h: 43.52, mode: Qt::KeepAspectRatio); | 
| 91 |     QCOMPARE(t2, QSizeF(35.36, 43.52)); | 
| 92 |  | 
| 93 |     QSizeF t3(9.6, 12.48); | 
| 94 |     t3.scale(w: 31.68, h: 31.68, mode: Qt::KeepAspectRatioByExpanding); | 
| 95 |     QCOMPARE(t3, QSizeF(31.68, 41.184)); | 
| 96 |  | 
| 97 |     QSizeF t4(12.8, 10.4); | 
| 98 |     t4.scale(w: 43.52, h: 43.52, mode: Qt::KeepAspectRatio); | 
| 99 |     QCOMPARE(t4, QSizeF(43.52, 35.36)); | 
| 100 |  | 
| 101 |     QSizeF t5(12.48, 9.6); | 
| 102 |     t5.scale(w: 31.68, h: 31.68, mode: Qt::KeepAspectRatioByExpanding); | 
| 103 |     QCOMPARE(t5, QSizeF(41.184, 31.68)); | 
| 104 |  | 
| 105 |     QSizeF t6(0.0, 0.0); | 
| 106 |     t6.scale(w: 200, h: 240, mode: Qt::IgnoreAspectRatio); | 
| 107 |     QCOMPARE(t6, QSizeF(200, 240)); | 
| 108 |  | 
| 109 |     QSizeF t7(0.0, 0.0); | 
| 110 |     t7.scale(w: 200, h: 240, mode: Qt::KeepAspectRatio); | 
| 111 |     QCOMPARE(t7, QSizeF(200, 240)); | 
| 112 |  | 
| 113 |     QSizeF t8(0.0, 0.0); | 
| 114 |     t8.scale(w: 200, h: 240, mode: Qt::KeepAspectRatioByExpanding); | 
| 115 |     QCOMPARE(t8, QSizeF(200, 240)); | 
| 116 | } | 
| 117 |  | 
| 118 |  | 
| 119 | void tst_QSizeF::expandedTo_data() { | 
| 120 |     QTest::addColumn<QSizeF>(name: "input1" ); | 
| 121 |     QTest::addColumn<QSizeF>(name: "input2" ); | 
| 122 |     QTest::addColumn<QSizeF>(name: "expected" ); | 
| 123 |  | 
| 124 |     QTest::newRow(dataTag: "data0" ) << QSizeF(10.4, 12.8) << QSizeF(6.6, 4.4) << QSizeF(10.4, 12.8); | 
| 125 |     QTest::newRow(dataTag: "data1" ) << QSizeF(0.0, 0.0) << QSizeF(6.6, 4.4) << QSizeF(6.6, 4.4); | 
| 126 |     // This should pick the highest of w,h components independently of each other, | 
| 127 |     // thus the result don't have to be equal to neither input1 nor input2. | 
| 128 |     QTest::newRow(dataTag: "data3" ) << QSizeF(6.6, 4.4) << QSizeF(4.4, 6.6) << QSizeF(6.6, 6.6); | 
| 129 | } | 
| 130 |  | 
| 131 | void tst_QSizeF::expandedTo() { | 
| 132 |     QFETCH( QSizeF, input1); | 
| 133 |     QFETCH( QSizeF, input2); | 
| 134 |     QFETCH( QSizeF, expected); | 
| 135 |  | 
| 136 |     QCOMPARE( input1.expandedTo(input2), expected); | 
| 137 | } | 
| 138 |  | 
| 139 | void tst_QSizeF::boundedTo_data() { | 
| 140 |     QTest::addColumn<QSizeF>(name: "input1" ); | 
| 141 |     QTest::addColumn<QSizeF>(name: "input2" ); | 
| 142 |     QTest::addColumn<QSizeF>(name: "expected" ); | 
| 143 |  | 
| 144 |     QTest::newRow(dataTag: "data0" ) << QSizeF(10.4, 12.8) << QSizeF(6.6, 4.4) << QSizeF(6.6, 4.4); | 
| 145 |     QTest::newRow(dataTag: "data1" ) << QSizeF(0.0, 0.0) << QSizeF(6.6, 4.4) << QSizeF(0.0, 0.0); | 
| 146 |     // This should pick the lowest of w,h components independently of each other, | 
| 147 |     // thus the result don't have to be equal to neither input1 nor input2. | 
| 148 |     QTest::newRow(dataTag: "data3" ) << QSizeF(6.6, 4.4) << QSizeF(4.4, 6.6) << QSizeF(4.4, 4.4); | 
| 149 | } | 
| 150 |  | 
| 151 | void tst_QSizeF::boundedTo() { | 
| 152 |     QFETCH( QSizeF, input1); | 
| 153 |     QFETCH( QSizeF, input2); | 
| 154 |     QFETCH( QSizeF, expected); | 
| 155 |  | 
| 156 |     QCOMPARE( input1.boundedTo(input2), expected); | 
| 157 | } | 
| 158 |  | 
| 159 | void tst_QSizeF::grownOrShrunkBy_data() | 
| 160 | { | 
| 161 |     QTest::addColumn<QSizeF>(name: "input" ); | 
| 162 |     QTest::addColumn<QMarginsF>(name: "margins" ); | 
| 163 |     QTest::addColumn<QSizeF>(name: "grown" ); | 
| 164 |     QTest::addColumn<QSizeF>(name: "shrunk" ); | 
| 165 |  | 
| 166 |     auto row = [](QSizeF i, QMarginsF m, QSizeF g, QSizeF s) { | 
| 167 |         QTest::addRow(format: "{%g,%g}/{%g,%g,%g,%g}" , i.width(), i.height(), | 
| 168 |                       m.left(), m.top(), m.right(), m.bottom()) | 
| 169 |                 << i << m << g << s; | 
| 170 |     }; | 
| 171 |  | 
| 172 |     const QSizeF zero = {0, 0}; | 
| 173 |     const QSizeF some = {100, 200}; | 
| 174 |     const QMarginsF zeroMargins = {}; | 
| 175 |     const QMarginsF negative = {-1, -2, -3, -4}; | 
| 176 |     const QMarginsF positive = { 1,  2,  3,  4}; | 
| 177 |  | 
| 178 |     row(zero, zeroMargins, zero, zero); | 
| 179 |     row(zero, negative, {-4, -6}, { 4,  6}); | 
| 180 |     row(zero, positive, { 4,  6}, {-4, -6}); | 
| 181 |     row(some, zeroMargins, some, some); | 
| 182 |     row(some, negative, { 96, 194}, {104, 206}); | 
| 183 |     row(some, positive, {104, 206}, { 96, 194}); | 
| 184 | } | 
| 185 |  | 
| 186 | void tst_QSizeF::grownOrShrunkBy() | 
| 187 | { | 
| 188 |     QFETCH(const QSizeF, input); | 
| 189 |     QFETCH(const QMarginsF, margins); | 
| 190 |     QFETCH(const QSizeF, grown); | 
| 191 |     QFETCH(const QSizeF, shrunk); | 
| 192 |  | 
| 193 |     QCOMPARE(input.grownBy(margins), grown); | 
| 194 |     QCOMPARE(input.shrunkBy(margins), shrunk); | 
| 195 |     QCOMPARE(grown.shrunkBy(margins), input); | 
| 196 |     QCOMPARE(shrunk.grownBy(margins), input); | 
| 197 | } | 
| 198 |  | 
| 199 | void tst_QSizeF::transpose_data() { | 
| 200 |     QTest::addColumn<QSizeF>(name: "input1" ); | 
| 201 |     QTest::addColumn<QSizeF>(name: "expected" ); | 
| 202 |  | 
| 203 |     QTest::newRow(dataTag: "data0" ) << QSizeF(10.4, 12.8) << QSizeF(12.8, 10.4); | 
| 204 |     QTest::newRow(dataTag: "data1" ) << QSizeF(0.0, 0.0) << QSizeF(0.0, 0.0); | 
| 205 |     QTest::newRow(dataTag: "data3" ) << QSizeF(6.6, 4.4) << QSizeF(4.4, 6.6); | 
| 206 | } | 
| 207 |  | 
| 208 | void tst_QSizeF::transpose() { | 
| 209 |     QFETCH( QSizeF, input1); | 
| 210 |     QFETCH( QSizeF, expected); | 
| 211 |  | 
| 212 |     // transpose() works only inplace and does not return anything, so we must do the operation itself before the compare. | 
| 213 |     input1.transpose(); | 
| 214 |     QCOMPARE(input1 , expected); | 
| 215 | } | 
| 216 |  | 
| 217 | QTEST_APPLESS_MAIN(tst_QSizeF) | 
| 218 | #include "tst_qsizef.moc" | 
| 219 |  |