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 QtBluetooth module 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
31#include <QDebug>
32
33#include <qbluetoothaddress.h>
34
35QT_USE_NAMESPACE
36
37class tst_QBluetoothAddress : public QObject
38{
39 Q_OBJECT
40
41public:
42 tst_QBluetoothAddress();
43 ~tst_QBluetoothAddress();
44
45private slots:
46 void tst_construction_data();
47 void tst_construction();
48
49 void tst_assignment();
50
51 void tst_comparison_data();
52 void tst_comparison();
53
54 void tst_lessThan_data();
55 void tst_lessThan();
56
57 void tst_clear_data();
58 void tst_clear();
59};
60
61tst_QBluetoothAddress::tst_QBluetoothAddress()
62{
63}
64
65tst_QBluetoothAddress::~tst_QBluetoothAddress()
66{
67}
68
69void tst_QBluetoothAddress::tst_construction_data()
70{
71 QTest::addColumn<quint64>(name: "addressUInt");
72 QTest::addColumn<QString>(name: "addressS12");
73 QTest::addColumn<QString>(name: "addressS17");
74
75 QTest::newRow(dataTag: "11:22:33:44:55:66") << Q_UINT64_C(0x112233445566) << QString("112233445566") << QString("11:22:33:44:55:66");
76 QTest::newRow(dataTag: "AA:BB:CC:DD:EE:FF") << Q_UINT64_C(0xAABBCCDDEEFF) << QString("AABBCCDDEEFF") << QString("AA:BB:CC:DD:EE:FF");
77 QTest::newRow(dataTag: "aa:bb:cc:dd:ee:ff") << Q_UINT64_C(0xAABBCCDDEEFF) << QString("aabbccddeeff") << QString("AA:BB:CC:DD:EE:FF");
78 QTest::newRow(dataTag: "FF:FF:FF:FF:FF:FF") << Q_UINT64_C(0xFFFFFFFFFFFF) << QString("FFFFFFFFFFFF") << QString("FF:FF:FF:FF:FF:FF");
79}
80
81void tst_QBluetoothAddress::tst_construction()
82{
83 QFETCH(quint64, addressUInt);
84 QFETCH(QString, addressS12);
85 QFETCH(QString, addressS17);
86
87 {
88 QBluetoothAddress address;
89
90 QVERIFY(address.isNull());
91 }
92
93 {
94 /* construct from quint64 */
95 QBluetoothAddress address(addressUInt);
96
97 QVERIFY(!address.isNull());
98
99 QVERIFY(address.toUInt64() == addressUInt);
100
101 QCOMPARE(address.toString(), addressS17);
102 }
103
104 {
105 /* construct from string without colons */
106 QBluetoothAddress address(addressS12);
107
108 QVERIFY(!address.isNull());
109
110 QVERIFY(address.toUInt64() == addressUInt);
111
112 QCOMPARE(address.toString(), addressS17);
113 }
114
115 {
116 /* construct from string with colons */
117 QBluetoothAddress address(addressS17);
118
119 QVERIFY(!address.isNull());
120
121 QVERIFY(address.toUInt64() == addressUInt);
122
123 QCOMPARE(address.toString(), addressS17);
124 }
125
126 {
127 QString empty;
128 QBluetoothAddress address(empty);
129
130 QVERIFY(address.isNull());
131 }
132
133 {
134 QBluetoothAddress address(addressUInt);
135
136 QBluetoothAddress copy(address);
137
138 QVERIFY(address.toUInt64() == copy.toUInt64());
139 }
140}
141
142void tst_QBluetoothAddress::tst_assignment()
143{
144 QBluetoothAddress address(Q_UINT64_C(0x112233445566));
145
146 {
147 QBluetoothAddress copy = address;
148
149 QCOMPARE(address.toUInt64(), copy.toUInt64());
150 }
151
152 {
153 QBluetoothAddress copy1;
154 QBluetoothAddress copy2;
155
156 QVERIFY(copy1.isNull());
157 QVERIFY(copy2.isNull());
158
159 copy1 = copy2 = address;
160
161 QVERIFY(!copy1.isNull());
162 QVERIFY(!copy2.isNull());
163
164 QVERIFY(address.toUInt64() == copy1.toUInt64());
165 QVERIFY(address.toUInt64() == copy2.toUInt64());
166
167 copy1.clear();
168 QVERIFY(copy1.isNull());
169 QVERIFY2(copy1 != address, "Verify that copy1 is a copy of address, the d_ptr are being copied");
170 }
171}
172
173void tst_QBluetoothAddress::tst_comparison_data()
174{
175 QTest::addColumn<QBluetoothAddress>(name: "address1");
176 QTest::addColumn<QBluetoothAddress>(name: "address2");
177 QTest::addColumn<bool>(name: "result");
178
179 QTest::newRow(dataTag: "invalid == invalid") << QBluetoothAddress() << QBluetoothAddress() << true;
180 QTest::newRow(dataTag: "valid != invalid") << QBluetoothAddress(Q_UINT64_C(0x112233445566)) << QBluetoothAddress() << false;
181 QTest::newRow(dataTag: "valid == valid") << QBluetoothAddress(Q_UINT64_C(0x112233445566)) << QBluetoothAddress(Q_UINT64_C(0x112233445566)) << true;
182}
183
184void tst_QBluetoothAddress::tst_comparison()
185{
186 QFETCH(QBluetoothAddress, address1);
187 QFETCH(QBluetoothAddress, address2);
188 QFETCH(bool, result);
189
190 QCOMPARE(address1 == address2, result);
191 QCOMPARE(address2 == address1, result);
192 QCOMPARE(address1 != address2, !result);
193 QCOMPARE(address2 != address1, !result);
194}
195
196void tst_QBluetoothAddress::tst_lessThan_data()
197{
198 QTest::addColumn<QBluetoothAddress>(name: "address1");
199 QTest::addColumn<QBluetoothAddress>(name: "address2");
200 QTest::addColumn<bool>(name: "result");
201
202 QTest::newRow(dataTag: "invalid < invalid") << QBluetoothAddress() << QBluetoothAddress() << false;
203 QTest::newRow(dataTag: "invalid < valid") << QBluetoothAddress() << QBluetoothAddress(Q_UINT64_C(0x112233445566)) << true;
204 QTest::newRow(dataTag: "valid < invalid") << QBluetoothAddress(Q_UINT64_C(0x112233445566)) << QBluetoothAddress() << false;
205 QTest::newRow(dataTag: "valid < valid") << QBluetoothAddress(Q_UINT64_C(0x112233445566)) << QBluetoothAddress(Q_UINT64_C(0xAABBCCDDEEFF)) << true;
206 QTest::newRow(dataTag: "valid < valid") << QBluetoothAddress(Q_UINT64_C(0xAABBCCDDEEFF)) << QBluetoothAddress(Q_UINT64_C(0x112233445566)) << false;
207}
208
209void tst_QBluetoothAddress::tst_lessThan()
210{
211 QFETCH(QBluetoothAddress, address1);
212 QFETCH(QBluetoothAddress, address2);
213 QFETCH(bool, result);
214
215 QCOMPARE(address1 < address2, result);
216}
217
218void tst_QBluetoothAddress::tst_clear_data()
219{
220 QTest::addColumn<QString>(name: "addressS17");
221
222 QTest::newRow(dataTag: "FF:00:F3:25:00:00") << QString("FF:00:F3:25:00:00");
223}
224
225void tst_QBluetoothAddress::tst_clear()
226{
227 QFETCH(QString, addressS17);
228
229 QBluetoothAddress address(addressS17);
230 QVERIFY(!address.isNull());
231 address.clear();
232 QVERIFY(address.toString() == QString("00:00:00:00:00:00"));
233}
234
235QTEST_MAIN(tst_QBluetoothAddress)
236
237#include "tst_qbluetoothaddress.moc"
238
239

source code of qtconnectivity/tests/auto/qbluetoothaddress/tst_qbluetoothaddress.cpp