File indexing completed on 2024-12-29 05:08:32
0001 /* 0002 * This file is part of the KDE wacomtablet project. For copyright 0003 * information and license terms see the AUTHORS and COPYING files 0004 * in the top-level directory of this distribution. 0005 * 0006 * This program is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU General Public License as 0008 * published by the Free Software Foundation; either version 2 of 0009 * the License, or (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "common/enum.h" 0021 0022 #include <QtTest> 0023 0024 using namespace Wacom; 0025 0026 /** 0027 * @file testenum.cpp 0028 * 0029 * @test UnitTest for the device properties 0030 */ 0031 class TestEnum : public QObject 0032 { 0033 Q_OBJECT 0034 0035 private slots: 0036 void testCompare(); 0037 void testConstructor(); 0038 void testFind(); 0039 void testIterator(); 0040 void testKey(); 0041 void testKeys(); 0042 void testList(); 0043 void testOperator(); 0044 void testSize(); 0045 }; 0046 0047 QTEST_MAIN(TestEnum) 0048 0049 /* 0050 * Forward Declarations, Typedefs & Helper classes. 0051 */ 0052 class EnumTest; 0053 struct EnumTestTemplateSpecializationLessFunctor; 0054 struct EnumTestTemplateSpecializationEqualFunctor; 0055 0056 typedef Enum<EnumTest, QString, EnumTestTemplateSpecializationLessFunctor, EnumTestTemplateSpecializationEqualFunctor> EnumTestTemplateSpecialization; 0057 0058 struct EnumTestTemplateSpecializationLessFunctor { 0059 bool operator()(const EnumTestTemplateSpecialization *v1, const EnumTestTemplateSpecialization *v2) 0060 { 0061 return (v1->key() < v2->key()); 0062 } 0063 }; 0064 0065 struct EnumTestTemplateSpecializationEqualFunctor { 0066 bool operator()(const QString &v1, const QString &v2) 0067 { 0068 return (v1.compare(v2, Qt::CaseInsensitive) == 0); 0069 } 0070 }; 0071 0072 // template specialization of helper class 0073 template<> 0074 EnumTestTemplateSpecialization::Container EnumTestTemplateSpecialization::instances = EnumTestTemplateSpecialization::Container(); 0075 0076 /* 0077 * A helper class required for this unit test. 0078 */ 0079 class EnumTest : public EnumTestTemplateSpecialization 0080 { 0081 private: 0082 explicit EnumTest(const QString &value) 0083 : EnumTestTemplateSpecialization(this, value) 0084 { 0085 } 0086 0087 public: 0088 static const EnumTest VAL01_PRIO10; 0089 static const EnumTest VAL02_PRIO10; 0090 static const EnumTest VAL03_PRIO10; 0091 static const EnumTest VAL01_PRIO50; 0092 static const EnumTest VAL01_PRIO99; 0093 static const EnumTest VAL02_PRIO99; 0094 }; 0095 0096 // Do NOT reorder these instances! This has to be done by the enum itself. 0097 const EnumTest EnumTest::VAL01_PRIO10(QLatin1String("VAL01_PRIO10")); 0098 const EnumTest EnumTest::VAL03_PRIO10(QLatin1String("VAL03_PRIO10")); 0099 const EnumTest EnumTest::VAL02_PRIO10(QLatin1String("VAL02_PRIO10")); 0100 const EnumTest EnumTest::VAL02_PRIO99(QLatin1String("VAL02_PRIO99")); 0101 const EnumTest EnumTest::VAL01_PRIO99(QLatin1String("VAL01_PRIO99")); 0102 const EnumTest EnumTest::VAL01_PRIO50(QLatin1String("VAL01_PRIO50")); 0103 0104 void TestEnum::testCompare() 0105 { 0106 // make sure sort order is correct 0107 EnumTest::const_iterator iter = EnumTest::begin(); 0108 QCOMPARE(iter++->key(), QLatin1String("VAL01_PRIO10")); 0109 QCOMPARE(iter++->key(), QLatin1String("VAL01_PRIO50")); 0110 QCOMPARE(iter++->key(), QLatin1String("VAL01_PRIO99")); 0111 QCOMPARE(iter++->key(), QLatin1String("VAL02_PRIO10")); 0112 QCOMPARE(iter++->key(), QLatin1String("VAL02_PRIO99")); 0113 QCOMPARE(iter++->key(), QLatin1String("VAL03_PRIO10")); 0114 } 0115 0116 void TestEnum::testConstructor() 0117 { 0118 // copy constructor 0119 EnumTest test(EnumTest::VAL01_PRIO10); 0120 QVERIFY(test == EnumTest::VAL01_PRIO10); 0121 } 0122 0123 void TestEnum::testFind() 0124 { 0125 QCOMPARE(EnumTest::find(QLatin1String("NON_EXISTANT")), nullptr); 0126 0127 const EnumTest *find = EnumTest::find(QLatin1String("VAL01_PRIO50")); 0128 QVERIFY(find != nullptr); 0129 QCOMPARE(*find, EnumTest::VAL01_PRIO50); 0130 } 0131 0132 void TestEnum::testIterator() 0133 { 0134 EnumTest::const_iterator begin = EnumTest::begin(); 0135 EnumTest::const_iterator end = EnumTest::end(); 0136 0137 EnumTest::const_iterator iter, second; 0138 0139 // begin(), operator*, operator-> 0140 QCOMPARE(begin->key(), QLatin1String("VAL01_PRIO10")); 0141 QCOMPARE((*begin).key(), EnumTest::VAL01_PRIO10.key()); 0142 QVERIFY(*begin == EnumTest::VAL01_PRIO10); 0143 QVERIFY(&(*begin) == &EnumTest::VAL01_PRIO10); 0144 0145 // operator== 0146 QVERIFY(begin == EnumTest::begin()); 0147 QVERIFY(end == EnumTest::end()); 0148 0149 // operator!= 0150 QVERIFY(begin != EnumTest::end()); 0151 QVERIFY(end != EnumTest::begin()); 0152 0153 // operator= 0154 iter = begin; 0155 QVERIFY(iter == iter); 0156 QVERIFY(iter == begin); 0157 QVERIFY(iter == EnumTest::begin()); 0158 0159 // operator++ 0160 second = begin; 0161 ++second; 0162 QCOMPARE(second->key(), QLatin1String("VAL01_PRIO50")); 0163 0164 iter = begin; 0165 QVERIFY(iter++ == begin); 0166 QVERIFY(iter == second); 0167 0168 // operator++(int) 0169 iter = begin; 0170 QVERIFY(++iter == second); 0171 0172 // operator-- 0173 iter = second; 0174 QVERIFY(iter-- == second); 0175 QVERIFY(iter == begin); 0176 0177 // operator--(int) 0178 iter = second; 0179 QVERIFY(--iter == begin); 0180 0181 // end() 0182 iter = end; 0183 --iter; 0184 QCOMPARE(iter->key(), QLatin1String("VAL03_PRIO10")); 0185 } 0186 0187 void TestEnum::testKey() 0188 { 0189 QCOMPARE(EnumTest::VAL01_PRIO10.key(), QLatin1String("VAL01_PRIO10")); 0190 QCOMPARE(EnumTest::VAL01_PRIO50.key(), QLatin1String("VAL01_PRIO50")); 0191 QCOMPARE(EnumTest::VAL01_PRIO99.key(), QLatin1String("VAL01_PRIO99")); 0192 QCOMPARE(EnumTest::VAL02_PRIO10.key(), QLatin1String("VAL02_PRIO10")); 0193 QCOMPARE(EnumTest::VAL02_PRIO99.key(), QLatin1String("VAL02_PRIO99")); 0194 QCOMPARE(EnumTest::VAL03_PRIO10.key(), QLatin1String("VAL03_PRIO10")); 0195 } 0196 0197 void TestEnum::testKeys() 0198 { 0199 QStringList values = EnumTest::keys(); 0200 0201 QStringList::ConstIterator iter = values.constBegin(); 0202 QCOMPARE(*(iter++), QLatin1String("VAL01_PRIO10")); 0203 QCOMPARE(*(iter++), QLatin1String("VAL01_PRIO50")); 0204 QCOMPARE(*(iter++), QLatin1String("VAL01_PRIO99")); 0205 QCOMPARE(*(iter++), QLatin1String("VAL02_PRIO10")); 0206 QCOMPARE(*(iter++), QLatin1String("VAL02_PRIO99")); 0207 QCOMPARE(*(iter++), QLatin1String("VAL03_PRIO10")); 0208 } 0209 0210 void TestEnum::testList() 0211 { 0212 QList<EnumTest> list = EnumTest::list(); 0213 QList<EnumTest>::ConstIterator iter = list.constBegin(); 0214 0215 QVERIFY(*(iter++) == EnumTest::VAL01_PRIO10); 0216 QVERIFY(*(iter++) == EnumTest::VAL01_PRIO50); 0217 QVERIFY(*(iter++) == EnumTest::VAL01_PRIO99); 0218 QVERIFY(*(iter++) == EnumTest::VAL02_PRIO10); 0219 QVERIFY(*(iter++) == EnumTest::VAL02_PRIO99); 0220 QVERIFY(*(iter++) == EnumTest::VAL03_PRIO10); 0221 } 0222 0223 void TestEnum::testOperator() 0224 { 0225 // operator== 0226 QVERIFY(EnumTest::VAL01_PRIO10 == EnumTest::VAL01_PRIO10); 0227 QVERIFY(EnumTest::VAL02_PRIO10 == EnumTest::VAL02_PRIO10); 0228 0229 // operator!= 0230 QVERIFY(EnumTest::VAL01_PRIO10 != EnumTest::VAL02_PRIO10); 0231 0232 // operator= 0233 EnumTest test = EnumTest::VAL01_PRIO10; 0234 QVERIFY(test == EnumTest::VAL01_PRIO10); 0235 } 0236 0237 void TestEnum::testSize() 0238 { 0239 int count = 6; 0240 QCOMPARE((int)EnumTest::size(), count); 0241 QCOMPARE(EnumTest::keys().size(), count); 0242 QCOMPARE(EnumTest::list().size(), count); 0243 } 0244 0245 #include "testenum.moc"