File indexing completed on 2025-04-27 07:35:02
0001 // SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com> 0002 // SPDX-License-Identifier: BSD-2-Clause OR MIT 0003 0004 // First included header is the public header of the class we are testing; 0005 // this forces the header to be self-contained. 0006 #include "lchdouble.h" 0007 0008 #include <qdebug.h> 0009 #include <qglobal.h> 0010 #include <qobject.h> 0011 #include <qstring.h> 0012 #include <qtest.h> 0013 #include <qtestcase.h> 0014 #include <qvariant.h> 0015 0016 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) 0017 #include <qtmetamacros.h> 0018 #else 0019 #include <qobjectdefs.h> 0020 #endif 0021 0022 static void snippet01() 0023 { 0024 //! [Use LchDouble] 0025 PerceptualColor::LchDouble myValue; 0026 myValue.l = 50; // Lightness: 50% 0027 myValue.c = 25; // Chroma: 25 0028 myValue.h = 5; // Hue: 5° 0029 //! [Use LchDouble] 0030 Q_UNUSED(myValue) 0031 } 0032 0033 namespace PerceptualColor 0034 { 0035 class TestLchDouble : public QObject 0036 { 0037 Q_OBJECT 0038 0039 public: 0040 explicit TestLchDouble(QObject *parent = nullptr) 0041 : QObject(parent) 0042 { 0043 } 0044 0045 private: 0046 static void voidMessageHandler(QtMsgType, const QMessageLogContext &, const QString &) 0047 { 0048 // dummy message handler that does not print messages 0049 } 0050 0051 private Q_SLOTS: 0052 void initTestCase() 0053 { 0054 // Called before the first test function is executed 0055 } 0056 0057 void cleanupTestCase() 0058 { 0059 // Called after the last test function was executed 0060 } 0061 0062 void init() 0063 { 0064 // Called before each test function is executed 0065 } 0066 0067 void cleanup() 0068 { 0069 // Called after every test function 0070 } 0071 0072 void testConstructorDestructor() 0073 { 0074 // This should not crash. 0075 LchDouble test; 0076 test.l = 50; 0077 Q_UNUSED(test) 0078 } 0079 0080 void testCopyConstructor() 0081 { 0082 // This should not crash. 0083 LchDouble test; 0084 test.l = 50; 0085 test.c = 25; 0086 test.h = 5; 0087 LchDouble copy(test); 0088 QCOMPARE(copy.l, 50); 0089 QCOMPARE(copy.c, 25); 0090 QCOMPARE(copy.h, 5); 0091 } 0092 0093 void testHasSameCoordinates() 0094 { 0095 LchDouble a; 0096 a.l = 50; 0097 a.c = 20; 0098 a.h = 5; 0099 LchDouble b = a; 0100 QVERIFY(a.hasSameCoordinates(b)); 0101 QVERIFY(b.hasSameCoordinates(a)); 0102 QVERIFY(a.hasSameCoordinates(a)); 0103 QVERIFY(b.hasSameCoordinates(b)); 0104 b.h = 365; 0105 QVERIFY(!a.hasSameCoordinates(b)); 0106 QVERIFY(!b.hasSameCoordinates(a)); 0107 QVERIFY(a.hasSameCoordinates(a)); 0108 QVERIFY(b.hasSameCoordinates(b)); 0109 // When chroma is 0, hue becomes meaningless. Nevertheless, different 0110 // hues should be detected. 0111 a.c = 0; 0112 b.c = 0; 0113 QVERIFY(!a.hasSameCoordinates(b)); 0114 QVERIFY(!b.hasSameCoordinates(a)); 0115 QVERIFY(a.hasSameCoordinates(a)); 0116 QVERIFY(b.hasSameCoordinates(b)); 0117 // And when returning to the same hue, everything should be considered 0118 // as with same coordinates. 0119 b.h = 5; 0120 QVERIFY(a.hasSameCoordinates(b)); 0121 QVERIFY(b.hasSameCoordinates(a)); 0122 QVERIFY(a.hasSameCoordinates(a)); 0123 QVERIFY(b.hasSameCoordinates(b)); 0124 } 0125 0126 void testQDebugSupport() 0127 { 0128 PerceptualColor::LchDouble test; 0129 // suppress warning for generating invalid QColor 0130 qInstallMessageHandler(voidMessageHandler); 0131 qDebug() << test; 0132 // do not suppress warning for generating invalid QColor anymore 0133 qInstallMessageHandler(nullptr); 0134 } 0135 0136 void testMetaTypeDeclaration() 0137 { 0138 QVariant test; 0139 // The next line should produce a compiler error if the 0140 // type is not declared to Qt’s Meta Object System. 0141 test.setValue(LchDouble()); 0142 } 0143 0144 void testConstExpr() 0145 { 0146 // Test if a constexpr can be instantiated: 0147 constexpr LchDouble temp = {1, 2, 3}; 0148 0149 // The aggregate initialization order should be l, c, h. 0150 // Test is this has actually been the case: 0151 static_assert(temp.l == 1); 0152 static_assert(temp.c == 2); 0153 static_assert(temp.h == 3); 0154 } 0155 0156 void testSnippet01() 0157 { 0158 snippet01(); 0159 } 0160 }; 0161 0162 } // namespace PerceptualColor 0163 0164 QTEST_MAIN(PerceptualColor::TestLchDouble) 0165 0166 // The following “include” is necessary because we do not use a header file: 0167 #include "testlchdouble.moc"