File indexing completed on 2024-07-07 04:30:16

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 "helperconversion.h"
0007 
0008 #include "lchdouble.h"
0009 #include <cmath>
0010 #include <lcms2.h>
0011 #include <qglobal.h>
0012 #include <qmetatype.h>
0013 #include <qobject.h>
0014 #include <qtest.h>
0015 #include <qtestcase.h>
0016 
0017 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0018 #include <qtmetamacros.h>
0019 #else
0020 #include <qobjectdefs.h>
0021 #include <qstring.h>
0022 #endif
0023 
0024 Q_DECLARE_METATYPE(cmsCIELab)
0025 
0026 namespace PerceptualColor
0027 {
0028 
0029 class TestHelperConversion : public QObject
0030 {
0031     Q_OBJECT
0032 
0033 public:
0034     explicit TestHelperConversion(QObject *parent = nullptr)
0035         : QObject(parent)
0036     {
0037     }
0038 
0039 private Q_SLOTS:
0040 
0041     void initTestCase()
0042     {
0043         // Called before the first test function is executed
0044     }
0045 
0046     void cleanupTestCase()
0047     {
0048         // Called after the last test function was executed
0049     }
0050 
0051     void init()
0052     {
0053         // Called before each test function is executed
0054     }
0055     void cleanup()
0056     {
0057         // Called after every test function
0058     }
0059 
0060     void testLchConvertion()
0061     {
0062         // Check if round-trips work fine.
0063         // One sense
0064         cmsCIELCh startValue;
0065         startValue.L = 50.1;
0066         startValue.C = 20.1;
0067         startValue.h = 80.1;
0068         QCOMPARE(toCmsLch(toLchDouble(startValue)).L, startValue.L);
0069         QCOMPARE(toCmsLch(toLchDouble(startValue)).C, startValue.C);
0070         QCOMPARE(toCmsLch(toLchDouble(startValue)).h, startValue.h);
0071         // The other sense
0072         LchDouble startValue2;
0073         startValue2.l = 50.1;
0074         startValue2.c = 20.1;
0075         startValue2.h = 80.1;
0076         QCOMPARE(toLchDouble(toCmsLch(startValue2)).l, startValue2.l);
0077         QCOMPARE(toLchDouble(toCmsLch(startValue2)).c, startValue2.c);
0078         QCOMPARE(toLchDouble(toCmsLch(startValue2)).h, startValue2.h);
0079     }
0080 
0081     void testFromXyzToOklabCubeRoot()
0082     {
0083         // The function fromXyzToOklab relies on the assumption that
0084         // std::cbrt() returns negative results for negative radicands,
0085         // and not simply “nan”. As std::cbrt() isn’t constexpr, we cannot
0086         // use a static assert within the function. Therefore, we have this
0087         // unit test:
0088         const auto actual = std::cbrt(-27);
0089         const decltype(actual) expected = -3;
0090         QCOMPARE(actual, expected);
0091     }
0092 };
0093 
0094 } // namespace PerceptualColor
0095 
0096 QTEST_MAIN(PerceptualColor::TestHelperConversion)
0097 // The following “include” is necessary because we do not use a header file:
0098 #include "testhelperconversion.moc"