diff options
author | Alex Blasche <alexander.blasche@digia.com> | 2014-06-30 14:17:47 +0200 |
---|---|---|
committer | Alex Blasche <alexander.blasche@digia.com> | 2014-07-02 16:17:20 +0200 |
commit | e1fdef843fd8a6d8495bf96810e0ffc32d9ca0f6 (patch) | |
tree | bed0dc1d419dc11f294f19d2d8b77d3ddb64e5d6 | |
parent | a354431ba0d8c057bd360c0b23565b650814ffc3 (diff) |
Add comparison operators for QLECharacteristic & QLEDescriptor
Change-Id: I5e23af887aa423c6c80335399d0342bec964f6d2
Reviewed-by: Fabian Bumberger <fbumberger@rim.com>
-rw-r--r-- | src/bluetooth/qlowenergycharacteristic.cpp | 35 | ||||
-rw-r--r-- | src/bluetooth/qlowenergycharacteristic.h | 2 | ||||
-rw-r--r-- | src/bluetooth/qlowenergydescriptor.cpp | 36 | ||||
-rw-r--r-- | src/bluetooth/qlowenergydescriptor.h | 2 | ||||
-rw-r--r-- | tests/auto/qlowenergycharacteristic/tst_qlowenergycharacteristic.cpp | 44 | ||||
-rw-r--r-- | tests/auto/qlowenergydescriptor/tst_qlowenergydescriptor.cpp | 45 |
6 files changed, 164 insertions, 0 deletions
diff --git a/src/bluetooth/qlowenergycharacteristic.cpp b/src/bluetooth/qlowenergycharacteristic.cpp index f74aa808..aa1795c3 100644 --- a/src/bluetooth/qlowenergycharacteristic.cpp +++ b/src/bluetooth/qlowenergycharacteristic.cpp @@ -219,6 +219,41 @@ QLowEnergyCharacteristic &QLowEnergyCharacteristic::operator=(const QLowEnergyCh } /*! + Returns \c true if \a other is equal to this QLowEnergyCharacteristic; otherwise \c false. + + Two QLowEnergyCharcteristic instances are considered to be equal if they refer to + the same charcteristic on the same remote Bluetooth Low Energy device. + */ +bool QLowEnergyCharacteristic::operator==(const QLowEnergyCharacteristic &other) const +{ + if (d_ptr != other.d_ptr) + return false; + + if ((data && !other.data) || (!data && other.data)) + return false; + + if (!data) + return true; + + if (data->handle != other.data->handle) + return false; + + return true; +} + +/*! + Returns \c true if \a other is not equal to this QLowEnergyCharacteristic; otherwise \c false. + + Two QLowEnergyCharcteristic instances are considered to be equal if they refer to + the same charcteristic on the same remote Bluetooth Low Energy device. + */ + +bool QLowEnergyCharacteristic::operator!=(const QLowEnergyCharacteristic &other) const +{ + return !(*this == other); +} + +/*! Returns \c true if the QLowEnergyCharacteristic object is valid, otherwise returns \c false. An invalid characteristic object is not associated to any service diff --git a/src/bluetooth/qlowenergycharacteristic.h b/src/bluetooth/qlowenergycharacteristic.h index 4478b910..9a14d57c 100644 --- a/src/bluetooth/qlowenergycharacteristic.h +++ b/src/bluetooth/qlowenergycharacteristic.h @@ -75,6 +75,8 @@ public: ~QLowEnergyCharacteristic(); QLowEnergyCharacteristic &operator=(const QLowEnergyCharacteristic &other); + bool operator==(const QLowEnergyCharacteristic &other) const; + bool operator!=(const QLowEnergyCharacteristic &other) const; QString name() const; diff --git a/src/bluetooth/qlowenergydescriptor.cpp b/src/bluetooth/qlowenergydescriptor.cpp index c0dfdbb8..d28e1a6e 100644 --- a/src/bluetooth/qlowenergydescriptor.cpp +++ b/src/bluetooth/qlowenergydescriptor.cpp @@ -137,6 +137,42 @@ QLowEnergyDescriptor &QLowEnergyDescriptor::operator=(const QLowEnergyDescriptor } /*! + Returns \c true if \a other is equal to this QLowEnergyCharacteristic; otherwise \c false. + + Two QLowEnergyDescriptor instances are considered to be equal if they refer to + the same descriptor on the same remote Bluetooth Low Energy device. + */ +bool QLowEnergyDescriptor::operator==(const QLowEnergyDescriptor &other) const +{ + if (d_ptr != other.d_ptr) + return false; + + if ((data && !other.data) || (!data && other.data)) + return false; + + if (!data) + return true; + + if (data->charHandle != other.data->charHandle + || data->descHandle != other.data->descHandle) { + return false; + } + + return true; +} + +/*! + Returns \c true if \a other is not equal to this QLowEnergyCharacteristic; otherwise \c false. + + Two QLowEnergyDescriptor instances are considered to be equal if they refer to + the same descriptor on the same remote Bluetooth Low Energy device. + */ +bool QLowEnergyDescriptor::operator!=(const QLowEnergyDescriptor &other) const +{ + return !(*this == other); +} + +/*! Returns \c true if the QLowEnergyDescriptor object is valid, otherwise returns \c false. An invalid descriptor instance is not associated to any service diff --git a/src/bluetooth/qlowenergydescriptor.h b/src/bluetooth/qlowenergydescriptor.h index d721b8fc..24aaf534 100644 --- a/src/bluetooth/qlowenergydescriptor.h +++ b/src/bluetooth/qlowenergydescriptor.h @@ -62,6 +62,8 @@ public: ~QLowEnergyDescriptor(); QLowEnergyDescriptor &operator=(const QLowEnergyDescriptor &other); + bool operator==(const QLowEnergyDescriptor &other) const; + bool operator!=(const QLowEnergyDescriptor &other) const; bool isValid() const; diff --git a/tests/auto/qlowenergycharacteristic/tst_qlowenergycharacteristic.cpp b/tests/auto/qlowenergycharacteristic/tst_qlowenergycharacteristic.cpp index 0c0cdb31..27c32db9 100644 --- a/tests/auto/qlowenergycharacteristic/tst_qlowenergycharacteristic.cpp +++ b/tests/auto/qlowenergycharacteristic/tst_qlowenergycharacteristic.cpp @@ -199,7 +199,18 @@ void tst_QLowEnergyCharacteristic::tst_constructionDefault() QCOMPARE(copyConstructed.descriptors().count(), 0); QCOMPARE(copyConstructed.properties(), QLowEnergyCharacteristic::Unknown); + QVERIFY(copyConstructed == characteristic); + QVERIFY(characteristic == copyConstructed); + QVERIFY(!(copyConstructed != characteristic)); + QVERIFY(!(characteristic != copyConstructed)); + QLowEnergyCharacteristic assigned; + + QVERIFY(assigned == characteristic); + QVERIFY(characteristic == assigned); + QVERIFY(!(assigned != characteristic)); + QVERIFY(!(characteristic != assigned)); + assigned = characteristic; QVERIFY(!assigned.isValid()); QCOMPARE(assigned.value(), QByteArray()); @@ -208,6 +219,11 @@ void tst_QLowEnergyCharacteristic::tst_constructionDefault() QCOMPARE(assigned.name(), QString()); QCOMPARE(assigned.descriptors().count(), 0); QCOMPARE(assigned.properties(), QLowEnergyCharacteristic::Unknown); + + QVERIFY(assigned == characteristic); + QVERIFY(characteristic == assigned); + QVERIFY(!(assigned != characteristic)); + QVERIFY(!(characteristic != assigned)); } void tst_QLowEnergyCharacteristic::tst_assignCompare() @@ -253,6 +269,11 @@ void tst_QLowEnergyCharacteristic::tst_assignCompare() if (!noDescriptors) QVERIFY(target.descriptors().count() > 0); + QVERIFY(target == chars[indexWithDescriptor]); + QVERIFY(chars[indexWithDescriptor] == target); + QVERIFY(!(target != chars[indexWithDescriptor])); + QVERIFY(!(chars[indexWithDescriptor] != target)); + QCOMPARE(target.isValid(), chars[indexWithDescriptor].isValid()); QCOMPARE(target.name(), chars[indexWithDescriptor].name()); QCOMPARE(target.handle(), chars[indexWithDescriptor].handle()); @@ -282,6 +303,11 @@ void tst_QLowEnergyCharacteristic::tst_assignCompare() QCOMPARE(copyConstructed.descriptors().count(), chars[indexWithDescriptor].descriptors().count()); + QVERIFY(copyConstructed == target); + QVERIFY(target == copyConstructed); + QVERIFY(!(copyConstructed != target)); + QVERIFY(!(target != copyConstructed)); + // test invalidation QLowEnergyCharacteristic invalid; target = invalid; @@ -292,6 +318,24 @@ void tst_QLowEnergyCharacteristic::tst_assignCompare() QCOMPARE(target.name(), QString()); QCOMPARE(target.descriptors().count(), 0); QCOMPARE(target.properties(), QLowEnergyCharacteristic::Unknown); + + QVERIFY(invalid == target); + QVERIFY(target == invalid); + QVERIFY(!(invalid != target)); + QVERIFY(!(target != invalid)); + + QVERIFY(!(chars[indexWithDescriptor] == target)); + QVERIFY(!(target == chars[indexWithDescriptor])); + QVERIFY(chars[indexWithDescriptor] != target); + QVERIFY(target != chars[indexWithDescriptor]); + + if (chars.count() >= 2) { + // at least two characteristics + QVERIFY(!(chars[0] == chars[1])); + QVERIFY(!(chars[1] == chars[0])); + QVERIFY(chars[0] != chars[1]); + QVERIFY(chars[1] != chars[0]); + } } QTEST_MAIN(tst_QLowEnergyCharacteristic) diff --git a/tests/auto/qlowenergydescriptor/tst_qlowenergydescriptor.cpp b/tests/auto/qlowenergydescriptor/tst_qlowenergydescriptor.cpp index 5ab59a60..326e4dd0 100644 --- a/tests/auto/qlowenergydescriptor/tst_qlowenergydescriptor.cpp +++ b/tests/auto/qlowenergydescriptor/tst_qlowenergydescriptor.cpp @@ -198,7 +198,18 @@ void tst_QLowEnergyDescriptor::tst_constructionDefault() QCOMPARE(copyConstructed.name(), QString()); QCOMPARE(copyConstructed.type(), QBluetoothUuid::UnknownDescriptorType); + QVERIFY(copyConstructed == descriptor); + QVERIFY(descriptor == copyConstructed); + QVERIFY(!(copyConstructed != descriptor)); + QVERIFY(!(descriptor != copyConstructed)); + QLowEnergyDescriptor assigned; + + QVERIFY(assigned == descriptor); + QVERIFY(descriptor == assigned); + QVERIFY(!(assigned != descriptor)); + QVERIFY(!(descriptor != assigned)); + assigned = descriptor; QVERIFY(!assigned.isValid()); QCOMPARE(assigned.value(), QByteArray()); @@ -206,6 +217,11 @@ void tst_QLowEnergyDescriptor::tst_constructionDefault() QVERIFY(assigned.handle() == 0); QCOMPARE(assigned.name(), QString()); QCOMPARE(assigned.type(), QBluetoothUuid::UnknownDescriptorType); + + QVERIFY(assigned == descriptor); + QVERIFY(descriptor == assigned); + QVERIFY(!(assigned != descriptor)); + QVERIFY(!(descriptor != assigned)); } @@ -244,6 +260,11 @@ void tst_QLowEnergyDescriptor::tst_assignCompare() QVERIFY(!target.uuid().isNull()); QVERIFY(!target.value().isEmpty()); + QVERIFY(target == targets.first()); + QVERIFY(targets.first() == target); + QVERIFY(!(target != targets.first())); + QVERIFY(!(targets.first() != target)); + QCOMPARE(target.isValid(), targets.first().isValid()); QCOMPARE(target.type(), targets.first().type()); QCOMPARE(target.name(), targets.first().name()); @@ -260,6 +281,11 @@ void tst_QLowEnergyDescriptor::tst_assignCompare() QCOMPARE(copyConstructed.uuid(), targets.first().uuid()); QCOMPARE(copyConstructed.value(), targets.first().value()); + QVERIFY(copyConstructed == target); + QVERIFY(target == copyConstructed); + QVERIFY(!(copyConstructed != target)); + QVERIFY(!(target != copyConstructed)); + // test invalidation QLowEnergyDescriptor invalid; target = invalid; @@ -269,6 +295,25 @@ void tst_QLowEnergyDescriptor::tst_assignCompare() QVERIFY(target.handle() == 0); QCOMPARE(target.name(), QString()); QCOMPARE(target.type(), QBluetoothUuid::UnknownDescriptorType); + + QVERIFY(invalid == target); + QVERIFY(target == invalid); + QVERIFY(!(invalid != target)); + QVERIFY(!(target != invalid)); + + QVERIFY(!(targets.first() == target)); + QVERIFY(!(target == targets.first())); + QVERIFY(targets.first() != target); + QVERIFY(target != targets.first()); + + if (targets.count() >= 2) { + QLowEnergyDescriptor second = targets[1]; + // at least two descriptors + QVERIFY(!(targets.first() == second)); + QVERIFY(!(second == targets.first())); + QVERIFY(targets.first() != second); + QVERIFY(second != targets.first()); + } } QTEST_MAIN(tst_QLowEnergyDescriptor) |