| 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 | #ifndef QLOCATIONTESTUTILS_P_H |
| 30 | #define QLOCATIONTESTUTILS_P_H |
| 31 | |
| 32 | #include <QDebug> |
| 33 | #include <QString> |
| 34 | #include <QTest> |
| 35 | |
| 36 | namespace QLocationTestUtils |
| 37 | { |
| 38 | bool hasDefaultSource(); |
| 39 | bool hasDefaultMonitor(); |
| 40 | |
| 41 | QString addNmeaChecksumAndBreaks(const QString &sentence); |
| 42 | |
| 43 | QString createRmcSentence(const QDateTime &dt); |
| 44 | QString createGgaSentence(const QTime &time); |
| 45 | QString createGgaSentence(int lat, int lng, const QTime &time); |
| 46 | QString createZdaSentence(const QDateTime &dt); |
| 47 | QString createGsaSentence(); |
| 48 | |
| 49 | //The purpose of compareEquality() is to test equality |
| 50 | //operators where it is expected that A == B. |
| 51 | template<typename A, typename B> |
| 52 | bool compareEquality(const A &first, const B &second) { |
| 53 | if (first != second) { |
| 54 | qWarning() << "compareEquality() failed: first != second" ; |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | if (second != first) { |
| 59 | qWarning() << "compareEquality() failed: second != first" ; |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | if (!(first == second)) { |
| 64 | qWarning() << "compareEquality() failed: !(first == second)" ; |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | if (!(second == first)) { |
| 69 | qWarning() << "compareEquality() failed: !(second == first)" ; |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | //The purpose of compareInequality() is to test equality |
| 77 | //operators where it is expected that A != B. |
| 78 | //Using !compareEquality(...) is not sufficient because |
| 79 | //only the first operator checked would end up being tested. |
| 80 | template<typename A, typename B> |
| 81 | bool compareInequality(const A &first, const B &second) { |
| 82 | if (!(first != second)){ |
| 83 | qWarning() << "compareInequality() failed: !(first != second)" ; |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | if (!(second != first)) { |
| 88 | qWarning() << "compareInequality() failed: !(second != first)" ; |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | if (first == second) { |
| 93 | qWarning() << "compareInequality() failed: first == second)" ; |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | if (second == first) { |
| 98 | qWarning() << "compareInequality() failed: second == first" ; |
| 99 | return false; |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | // Tests conversions between sub and base classes |
| 105 | // TC (test case) must implement: |
| 106 | // SubClass initialSubObject(); |
| 107 | // bool checkType(const BaseClass &) |
| 108 | // void detach(BaseClass *) - calls a mutator method, but doesn't actually modify the |
| 109 | // property to something different. |
| 110 | // void setSubClassProperty(SubClass *) - sets a property in the subclass instance |
| 111 | template<typename TC, typename BaseClass, typename SubClass> |
| 112 | void testConversion(TC *tc) { |
| 113 | SubClass sub = tc->initialSubObject(); |
| 114 | //check conversion from SubClass -> BaseClass |
| 115 | //using assignment operator |
| 116 | BaseClass base; |
| 117 | base = sub; |
| 118 | QVERIFY(QLocationTestUtils::compareEquality(base, sub)); |
| 119 | QVERIFY(tc->checkType(base)); |
| 120 | |
| 121 | //check comparing base classes |
| 122 | BaseClass base2; |
| 123 | base2 = sub; |
| 124 | QVERIFY(QLocationTestUtils::compareEquality(base, base2)); |
| 125 | |
| 126 | //check conversion from BaseClass -> SubClass |
| 127 | //using assignment operator |
| 128 | SubClass sub2; |
| 129 | sub2 = base; |
| 130 | QVERIFY(QLocationTestUtils::compareEquality(sub, sub2)); |
| 131 | QVERIFY(tc->checkType(sub2)); |
| 132 | |
| 133 | //check that equality still holds with detachment of underlying data pointer |
| 134 | tc->detach(&base); |
| 135 | sub2 = base; |
| 136 | QVERIFY(QLocationTestUtils::compareEquality(sub, sub2)); |
| 137 | QVERIFY(QLocationTestUtils::compareEquality(sub, base)); |
| 138 | QVERIFY(QLocationTestUtils::compareEquality(base, base2)); |
| 139 | |
| 140 | //check that comparing objects are not the same |
| 141 | //when an underlying subclass field has been modified |
| 142 | tc->setSubClassProperty(&sub2); |
| 143 | base2 = sub2; |
| 144 | QVERIFY(QLocationTestUtils::compareInequality(sub, sub2)); |
| 145 | QVERIFY(QLocationTestUtils::compareInequality(sub, base2)); |
| 146 | QVERIFY(QLocationTestUtils::compareInequality(base, base2)); |
| 147 | |
| 148 | //check conversion from SubClass -> BaseClass |
| 149 | //using copy constructor |
| 150 | BaseClass base3(sub); |
| 151 | QVERIFY(QLocationTestUtils::compareEquality(sub, base3)); |
| 152 | QVERIFY(QLocationTestUtils::compareEquality(base, base3)); |
| 153 | |
| 154 | //check conversion from BaseClass -> SubClass |
| 155 | //using copy constructor |
| 156 | SubClass sub3(base3); |
| 157 | QVERIFY(QLocationTestUtils::compareEquality(sub, sub3)); |
| 158 | |
| 159 | //check conversion to subclass using a default base class instance |
| 160 | BaseClass baseDefault; |
| 161 | SubClass subDefault; |
| 162 | SubClass sub4(baseDefault); |
| 163 | QVERIFY(QLocationTestUtils::compareEquality(sub4, subDefault)); |
| 164 | |
| 165 | SubClass sub5 = baseDefault; |
| 166 | QVERIFY(QLocationTestUtils::compareEquality(sub5, subDefault)); |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | #endif |
| 171 | |