| 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 <qmargins.h> | 
| 31 |  | 
| 32 | Q_DECLARE_METATYPE(QMargins) | 
| 33 |  | 
| 34 | class tst_QMargins : public QObject | 
| 35 | { | 
| 36 |     Q_OBJECT | 
| 37 | private slots: | 
| 38 |     void getSetCheck(); | 
| 39 |     void dataStreamCheck(); | 
| 40 |     void operators(); | 
| 41 |  | 
| 42 |     void getSetCheckF(); | 
| 43 |     void dataStreamCheckF(); | 
| 44 |     void operatorsF(); | 
| 45 | }; | 
| 46 |  | 
| 47 | // Testing get/set functions | 
| 48 | void tst_QMargins::getSetCheck() | 
| 49 | { | 
| 50 |     QMargins margins; | 
| 51 |     // int QMargins::width() | 
| 52 |     // void QMargins::setWidth(int) | 
| 53 |     margins.setLeft(0); | 
| 54 |     QCOMPARE(0, margins.left()); | 
| 55 |     margins.setTop(INT_MIN); | 
| 56 |     QCOMPARE(INT_MIN, margins.top()); | 
| 57 |     margins.setBottom(INT_MAX); | 
| 58 |     QCOMPARE(INT_MAX, margins.bottom()); | 
| 59 |     margins.setRight(INT_MAX); | 
| 60 |     QCOMPARE(INT_MAX, margins.right()); | 
| 61 |  | 
| 62 |     margins = QMargins(); | 
| 63 |     QVERIFY(margins.isNull()); | 
| 64 |     margins.setLeft(5); | 
| 65 |     margins.setRight(5); | 
| 66 |     QVERIFY(!margins.isNull()); | 
| 67 |     QCOMPARE(margins, QMargins(5, 0, 5, 0)); | 
| 68 | } | 
| 69 |  | 
| 70 | void tst_QMargins::operators() | 
| 71 | { | 
| 72 |     const QMargins m1(12, 14, 16, 18); | 
| 73 |     const QMargins m2(2, 3, 4, 5); | 
| 74 |  | 
| 75 |     const QMargins added = m1 + m2; | 
| 76 |     QCOMPARE(added, QMargins(14, 17, 20, 23)); | 
| 77 |     QMargins a = m1; | 
| 78 |     a += m2; | 
| 79 |     QCOMPARE(a, added); | 
| 80 |  | 
| 81 |     const QMargins subtracted = m1 - m2; | 
| 82 |     QCOMPARE(subtracted, QMargins(10, 11, 12, 13)); | 
| 83 |     a = m1; | 
| 84 |     a -= m2; | 
| 85 |     QCOMPARE(a, subtracted); | 
| 86 |  | 
| 87 |     QMargins h = m1; | 
| 88 |     h += 2; | 
| 89 |     QCOMPARE(h, QMargins(14, 16, 18, 20)); | 
| 90 |     h -= 2; | 
| 91 |     QCOMPARE(h, m1); | 
| 92 |  | 
| 93 |     const QMargins doubled = m1 * 2; | 
| 94 |     QCOMPARE(doubled, QMargins(24, 28, 32, 36)); | 
| 95 |     QCOMPARE(2 * m1, doubled); | 
| 96 |     QCOMPARE(qreal(2) * m1, doubled); | 
| 97 |     QCOMPARE(m1 * qreal(2), doubled); | 
| 98 |  | 
| 99 |     a = m1; | 
| 100 |     a *= 2; | 
| 101 |     QCOMPARE(a, doubled); | 
| 102 |     a = m1; | 
| 103 |     a *= qreal(2); | 
| 104 |     QCOMPARE(a, doubled); | 
| 105 |  | 
| 106 |     const QMargins halved = m1 / 2; | 
| 107 |     QCOMPARE(halved, QMargins(6, 7, 8, 9)); | 
| 108 |  | 
| 109 |     a = m1; | 
| 110 |     a /= 2; | 
| 111 |     QCOMPARE(a, halved); | 
| 112 |     a = m1; | 
| 113 |     a /= qreal(2); | 
| 114 |     QCOMPARE(a, halved); | 
| 115 |  | 
| 116 |     QCOMPARE(m1 + (-m1), QMargins()); | 
| 117 |  | 
| 118 |     QMargins m3 = QMargins(10, 11, 12, 13); | 
| 119 |     QCOMPARE(m3 + 1, QMargins(11, 12, 13, 14)); | 
| 120 |     QCOMPARE(1 + m3, QMargins(11, 12, 13, 14)); | 
| 121 |     QCOMPARE(m3 - 1, QMargins(9, 10, 11, 12)); | 
| 122 |     QCOMPARE(+m3, QMargins(10, 11, 12, 13)); | 
| 123 |     QCOMPARE(-m3, QMargins(-10, -11, -12, -13)); | 
| 124 | } | 
| 125 |  | 
| 126 | // Testing QDataStream operators | 
| 127 | void tst_QMargins::dataStreamCheck() | 
| 128 | { | 
| 129 |     QByteArray buffer; | 
| 130 |  | 
| 131 |     // stream out | 
| 132 |     { | 
| 133 |         QMargins marginsOut(0,INT_MIN,INT_MAX,6852); | 
| 134 |         QDataStream streamOut(&buffer, QIODevice::WriteOnly); | 
| 135 |         streamOut << marginsOut; | 
| 136 |     } | 
| 137 |  | 
| 138 |     // stream in & compare | 
| 139 |     { | 
| 140 |         QMargins marginsIn; | 
| 141 |         QDataStream streamIn(&buffer, QIODevice::ReadOnly); | 
| 142 |         streamIn >> marginsIn; | 
| 143 |  | 
| 144 |         QCOMPARE(marginsIn.left(), 0); | 
| 145 |         QCOMPARE(marginsIn.top(), INT_MIN); | 
| 146 |         QCOMPARE(marginsIn.right(), INT_MAX); | 
| 147 |         QCOMPARE(marginsIn.bottom(), 6852); | 
| 148 |     } | 
| 149 | } | 
| 150 |  | 
| 151 | // Testing get/set functions | 
| 152 | void tst_QMargins::getSetCheckF() | 
| 153 | { | 
| 154 |     QMarginsF margins; | 
| 155 |     // int QMarginsF::width() | 
| 156 |     // void QMarginsF::setWidth(int) | 
| 157 |     margins.setLeft(1.1); | 
| 158 |     QCOMPARE(1.1, margins.left()); | 
| 159 |     margins.setTop(2.2); | 
| 160 |     QCOMPARE(2.2, margins.top()); | 
| 161 |     margins.setBottom(3.3); | 
| 162 |     QCOMPARE(3.3, margins.bottom()); | 
| 163 |     margins.setRight(4.4); | 
| 164 |     QCOMPARE(4.4, margins.right()); | 
| 165 |  | 
| 166 |     margins = QMarginsF(); | 
| 167 |     QVERIFY(margins.isNull()); | 
| 168 |     margins.setLeft(5.5); | 
| 169 |     margins.setRight(5.5); | 
| 170 |     QVERIFY(!margins.isNull()); | 
| 171 |     QCOMPARE(margins, QMarginsF(5.5, 0.0, 5.5, 0.0)); | 
| 172 | } | 
| 173 |  | 
| 174 | void tst_QMargins::operatorsF() | 
| 175 | { | 
| 176 |     const QMarginsF m1(12.1, 14.1, 16.1, 18.1); | 
| 177 |     const QMarginsF m2(2.1, 3.1, 4.1, 5.1); | 
| 178 |  | 
| 179 |     const QMarginsF added = m1 + m2; | 
| 180 |     QCOMPARE(added, QMarginsF(14.2, 17.2, 20.2, 23.2)); | 
| 181 |     QMarginsF a = m1; | 
| 182 |     a += m2; | 
| 183 |     QCOMPARE(a, added); | 
| 184 |  | 
| 185 |     const QMarginsF subtracted = m1 - m2; | 
| 186 |     QCOMPARE(subtracted, QMarginsF(10.0, 11.0, 12.0, 13.0)); | 
| 187 |     a = m1; | 
| 188 |     a -= m2; | 
| 189 |     QCOMPARE(a, subtracted); | 
| 190 |  | 
| 191 |     QMarginsF h = m1; | 
| 192 |     h += 2.1; | 
| 193 |     QCOMPARE(h, QMarginsF(14.2, 16.2, 18.2, 20.2)); | 
| 194 |     h -= 2.1; | 
| 195 |     QCOMPARE(h, m1); | 
| 196 |  | 
| 197 |     const QMarginsF doubled = m1 * 2.0; | 
| 198 |     QCOMPARE(doubled, QMarginsF(24.2, 28.2, 32.2, 36.2)); | 
| 199 |     QCOMPARE(2.0 * m1, doubled); | 
| 200 |     QCOMPARE(m1 * 2.0, doubled); | 
| 201 |  | 
| 202 |     a = m1; | 
| 203 |     a *= 2.0; | 
| 204 |     QCOMPARE(a, doubled); | 
| 205 |  | 
| 206 |     const QMarginsF halved = m1 / 2.0; | 
| 207 |     QCOMPARE(halved, QMarginsF(6.05, 7.05, 8.05, 9.05)); | 
| 208 |  | 
| 209 |     a = m1; | 
| 210 |     a /= 2.0; | 
| 211 |     QCOMPARE(a, halved); | 
| 212 |  | 
| 213 |     QCOMPARE(m1 + (-m1), QMarginsF()); | 
| 214 |  | 
| 215 |     QMarginsF m3 = QMarginsF(10.3, 11.4, 12.5, 13.6); | 
| 216 |     QCOMPARE(m3 + 1.1, QMarginsF(11.4, 12.5, 13.6, 14.7)); | 
| 217 |     QCOMPARE(1.1 + m3, QMarginsF(11.4, 12.5, 13.6, 14.7)); | 
| 218 |     QCOMPARE(m3 - 1.1, QMarginsF(9.2, 10.3, 11.4, 12.5)); | 
| 219 |     QCOMPARE(+m3, QMarginsF(10.3, 11.4, 12.5, 13.6)); | 
| 220 |     QCOMPARE(-m3, QMarginsF(-10.3, -11.4, -12.5, -13.6)); | 
| 221 | } | 
| 222 |  | 
| 223 | // Testing QDataStream operators | 
| 224 | void tst_QMargins::dataStreamCheckF() | 
| 225 | { | 
| 226 |     QByteArray buffer; | 
| 227 |  | 
| 228 |     // stream out | 
| 229 |     { | 
| 230 |         QMarginsF marginsOut(1.1, 2.2, 3.3, 4.4); | 
| 231 |         QDataStream streamOut(&buffer, QIODevice::WriteOnly); | 
| 232 |         streamOut << marginsOut; | 
| 233 |     } | 
| 234 |  | 
| 235 |     // stream in & compare | 
| 236 |     { | 
| 237 |         QMarginsF marginsIn; | 
| 238 |         QDataStream streamIn(&buffer, QIODevice::ReadOnly); | 
| 239 |         streamIn >> marginsIn; | 
| 240 |  | 
| 241 |         QCOMPARE(marginsIn.left(), 1.1); | 
| 242 |         QCOMPARE(marginsIn.top(), 2.2); | 
| 243 |         QCOMPARE(marginsIn.right(), 3.3); | 
| 244 |         QCOMPARE(marginsIn.bottom(), 4.4); | 
| 245 |     } | 
| 246 | } | 
| 247 |  | 
| 248 | QTEST_APPLESS_MAIN(tst_QMargins) | 
| 249 | #include "tst_qmargins.moc" | 
| 250 |  |