File indexing completed on 2025-01-05 03:58:04

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-06-09
0007  * Description : a test for the freerotation tool
0008  *
0009  * SPDX-FileCopyrightText: 2009 by Andi Clemens <andi dot clemens at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "dimgfreerotation_utest.h"
0016 
0017 // C++ includes
0018 
0019 #include <cmath>
0020 
0021 // Qt includes
0022 
0023 #include <QTest>
0024 
0025 // Local includes
0026 
0027 #include "freerotationfilter.h"
0028 
0029 using namespace Digikam;
0030 
0031 QTEST_GUILESS_MAIN(DImgFreeRotationTest)
0032 
0033 DImgFreeRotationTest::DImgFreeRotationTest(QObject* const parent)
0034     : QObject(parent)
0035 {
0036 }
0037 
0038 void DImgFreeRotationTest::testCalculateAngle_data()
0039 {
0040     QTest::addColumn<QPoint>("p1");
0041     QTest::addColumn<QPoint>("p2");
0042     QTest::addColumn<double>("result");
0043 
0044     QTest::newRow("empty")
0045             << QPoint()
0046             << QPoint()
0047             << 0.0;
0048 
0049     QTest::newRow("invalid")
0050             << QPoint(-1, -3)
0051             << QPoint(2, 2)
0052             << 0.0;
0053 
0054     QTest::newRow("p1=p2")
0055             << QPoint(10, 10)
0056             << QPoint(10, 10)
0057             << 0.0;
0058 
0059     QTest::newRow("p1.x=p2.x")
0060             << QPoint(10, 10)
0061             << QPoint(200, 10)
0062             << 0.0;
0063 
0064     QTest::newRow("p1.y=p2.y")
0065             << QPoint(10, 10)
0066             << QPoint(10, 200)
0067             << 90.0;
0068 
0069     QTest::newRow("-45 degrees")
0070             << QPoint(10, 10)
0071             << QPoint(20, 20)
0072             << -45.0;
0073 
0074     QTest::newRow("45 degrees")
0075             << QPoint(10, 20)
0076             << QPoint(20, 10)
0077             << 45.0;
0078 
0079     QTest::newRow("28.82 degrees")
0080             << QPoint(0, 241)
0081             << QPoint(438, 0)
0082             << 28.82;
0083 
0084     QTest::newRow("-28.82 degrees")
0085             << QPoint(0, 0)
0086             << QPoint(438, 241)
0087             << -28.82;
0088 
0089     // point layout shouldn't matter
0090 
0091     QPoint p1(10, 20);
0092     QPoint p2(20, 10);
0093 
0094     QTest::newRow("layout1")
0095             << p1
0096             << p2
0097             << 45.0;
0098 
0099     QTest::newRow("layout2")
0100             << p2
0101             << p1
0102             << 45.0;
0103 }
0104 
0105 void DImgFreeRotationTest::testCalculateAngle()
0106 {
0107     QFETCH(QPoint, p1);
0108     QFETCH(QPoint, p2);
0109     QFETCH(double, result);
0110 
0111     double angle = FreeRotationFilter::calculateAngle(p1, p2);
0112     QCOMPARE(myRound(angle, 2), result);
0113 }
0114 
0115 double DImgFreeRotationTest::myRound(double val, int accuracy)
0116 {
0117     double tmp = pow(10.0, accuracy);
0118     double x   = val;
0119 
0120     x *= tmp;
0121     x += 0.5;
0122     x  = floor(x);
0123     x /= tmp;
0124 
0125     return x;
0126 }
0127 
0128 #include "moc_dimgfreerotation_utest.cpp"