File indexing completed on 2024-12-01 07:26:59

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 "genericcolor.h"
0007 
0008 #include "helpermath.h"
0009 #include <lcms2.h>
0010 #include <qglobal.h>
0011 #include <qobject.h>
0012 #include <qtest.h>
0013 #include <qtestcase.h>
0014 
0015 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0016 #include <qtmetamacros.h>
0017 #else
0018 #include <qobjectdefs.h>
0019 #include <qstring.h>
0020 #endif
0021 
0022 namespace PerceptualColor
0023 {
0024 class TestGenericColor : public QObject
0025 {
0026     Q_OBJECT
0027 
0028 public:
0029     explicit TestGenericColor(QObject *parent = nullptr)
0030         : QObject(parent)
0031     {
0032     }
0033 
0034 private Q_SLOTS:
0035     void initTestCase()
0036     {
0037         // Called before the first test function is executed
0038     }
0039 
0040     void cleanupTestCase()
0041     {
0042         // Called after the last test function was executed
0043     }
0044 
0045     void init()
0046     {
0047         // Called before each test function is executed
0048     }
0049 
0050     void cleanup()
0051     {
0052         // Called after every test function
0053     }
0054 
0055     void testConstructorWithoutArguments()
0056     {
0057         constexpr GenericColor color{};
0058         QCOMPARE(color.first, 0.0);
0059         QCOMPARE(color.second, 0.0);
0060         QCOMPARE(color.third, 0.0);
0061         QCOMPARE(color.fourth, 0.0);
0062     }
0063 
0064     void testConstructorWithTrio()
0065     {
0066         const Trio testValue = createMatrix<1, 3, double>(10., 20., 30.);
0067         // NOTE Not using constexpr here because Trio is derived from
0068         // QGenericMatrix which cannot be constructed as constexpr.
0069         GenericColor color(testValue);
0070         QCOMPARE(color.first, 10.0);
0071         QCOMPARE(color.second, 20.0);
0072         QCOMPARE(color.third, 30.0);
0073         QCOMPARE(color.fourth, 0.0);
0074     }
0075 
0076     void testConstructorWithCmsCIELab()
0077     {
0078         constexpr cmsCIELab testValue = {50, 20, 30};
0079         constexpr GenericColor color(testValue);
0080         QCOMPARE(color.first, 50.0);
0081         QCOMPARE(color.second, 20.0);
0082         QCOMPARE(color.third, 30.0);
0083         QCOMPARE(color.fourth, 0.0);
0084     }
0085 
0086     void testConstructorWithCmsCIEXYZ()
0087     {
0088         constexpr cmsCIEXYZ testValue = {0.1, 0.2, 0.3};
0089         constexpr GenericColor color(testValue);
0090         QCOMPARE(color.first, 0.1);
0091         QCOMPARE(color.second, 0.2);
0092         QCOMPARE(color.third, 0.3);
0093         QCOMPARE(color.fourth, 0.0);
0094     }
0095 
0096     void testConstructorWith3Args()
0097     {
0098         constexpr GenericColor color(10, 20, 30);
0099         QCOMPARE(color.first, 10.0);
0100         QCOMPARE(color.second, 20.0);
0101         QCOMPARE(color.third, 30.0);
0102         QCOMPARE(color.fourth, 0);
0103     }
0104 
0105     void testConstructorWith4Args()
0106     {
0107         constexpr GenericColor color(10, 20, 30, 40);
0108         QCOMPARE(color.first, 10.0);
0109         QCOMPARE(color.second, 20.0);
0110         QCOMPARE(color.third, 30.0);
0111         QCOMPARE(color.fourth, 40.0);
0112     }
0113 
0114     void testToTrio()
0115     {
0116         GenericColor color(10, 20, 30);
0117         Trio trio = color.toTrio();
0118         QCOMPARE(trio(0, 0), 10.0);
0119         QCOMPARE(trio(1, 0), 20.0);
0120         QCOMPARE(trio(2, 0), 30.0);
0121     }
0122 
0123     void testToCmsCIEXYZ()
0124     {
0125         GenericColor color(0.1, 0.2, 0.3);
0126         cmsCIEXYZ ciexyz = color.reinterpretAsXyzToCmsciexyz();
0127         QCOMPARE(ciexyz.X, 0.1);
0128         QCOMPARE(ciexyz.Y, 0.2);
0129         QCOMPARE(ciexyz.Z, 0.3);
0130     }
0131 
0132     void testToCmsCIELab()
0133     {
0134         GenericColor color(50, 20, 30);
0135         cmsCIELab cielab = color.reinterpretAsLabToCmscielab();
0136         QCOMPARE(cielab.L, 50.0);
0137         QCOMPARE(cielab.a, 20.0);
0138         QCOMPARE(cielab.b, 30.0);
0139     }
0140 
0141     void testEqualityOperatorsEqual1()
0142     {
0143         // Create two GenericColor objects with the same values
0144         GenericColor color1(1.0, 2.0, 3.0);
0145         GenericColor color2(1.0, 2.0, 3.0);
0146 
0147         // Test equality
0148         QVERIFY(color1 == color2);
0149         QVERIFY(!(color1 != color2));
0150     }
0151 
0152     void testEqualityOperatorsEqual2()
0153     {
0154         // Create two GenericColor objects with the same values
0155         GenericColor color1(0.0, 0.0, 0.0);
0156         GenericColor color2;
0157 
0158         // Test equality
0159         QVERIFY(color1 == color2);
0160         QVERIFY(!(color1 != color2));
0161     }
0162 
0163     void testEqualityOperatorsUnequal1()
0164     {
0165         // Create two GenericColor objects with different values
0166         GenericColor color1(1.0, 2.0, 3.0);
0167         GenericColor color2(4.0, 5.0, 6.0);
0168 
0169         // Test inequality
0170         QVERIFY(!(color1 == color2));
0171         QVERIFY(color1 != color2);
0172     }
0173 
0174     void testEqualityOperatorsUnequal2()
0175     {
0176         // Create two GenericColor objects with different values
0177         GenericColor color1(1.0, 2.0, 3.0);
0178         GenericColor color2;
0179 
0180         // Test inequality
0181         QVERIFY(!(color1 == color2));
0182         QVERIFY(color1 != color2);
0183     }
0184 
0185     void testEqualityOperatorsUnequal3()
0186     {
0187         // Create two GenericColor objects with different values
0188         GenericColor color1(0.0, 0.0, 0.1);
0189         GenericColor color2;
0190 
0191         // Test inequality
0192         QVERIFY(!(color1 == color2));
0193         QVERIFY(color1 != color2);
0194     }
0195 };
0196 
0197 } // namespace PerceptualColor
0198 
0199 QTEST_MAIN(PerceptualColor::TestGenericColor)
0200 
0201 // The following “include” is necessary because we do not use a header file:
0202 #include "testgenericcolor.moc"