From cd3d86140e1a5e8cf1efe0c1400207bd14d6f5b1 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Wed, 17 Apr 2024 18:57:08 +0300 Subject: [PATCH] QEasingCurve optimization Signed-off-by: Ivailo Monev --- src/core/tools/qeasingcurve.cpp | 19 +-- tests/auto/qeasingcurve/tst_qeasingcurve.cpp | 171 ++++++++----------- 2 files changed, 81 insertions(+), 109 deletions(-) diff --git a/src/core/tools/qeasingcurve.cpp b/src/core/tools/qeasingcurve.cpp index 4f074740f..8770eb02c 100644 --- a/src/core/tools/qeasingcurve.cpp +++ b/src/core/tools/qeasingcurve.cpp @@ -246,7 +246,8 @@ public: QEasingCurvePrivate() : type(QEasingCurve::Linear), per(s_defaultperiod), amp(s_defaultamplitude), over(s_defaultovershoot) - { } + { + } QEasingCurve::Type type; qreal per; @@ -291,12 +292,10 @@ QEasingCurve::~QEasingCurve() */ QEasingCurve &QEasingCurve::operator=(const QEasingCurve &other) { - if (*this != other) { - d_ptr->type = other.d_ptr->type; - d_ptr->per = other.d_ptr->per; - d_ptr->amp = other.d_ptr->amp; - d_ptr->over = other.d_ptr->over; - } + d_ptr->type = other.d_ptr->type; + d_ptr->per = other.d_ptr->per; + d_ptr->amp = other.d_ptr->amp; + d_ptr->over = other.d_ptr->over; return *this; } @@ -308,9 +307,9 @@ bool QEasingCurve::operator==(const QEasingCurve &other) const { bool res = (d_ptr->type == other.d_ptr->type); if (res) { - res = qFuzzyCompare(period(), other.period()) && - qFuzzyCompare(amplitude(), other.amplitude()) && - qFuzzyCompare(overshoot(), other.overshoot()); + res = qFuzzyCompare(d_ptr->per, other.d_ptr->per) && + qFuzzyCompare(d_ptr->amp, other.d_ptr->amp) && + qFuzzyCompare(d_ptr->over, other.d_ptr->over); } return res; } diff --git a/tests/auto/qeasingcurve/tst_qeasingcurve.cpp b/tests/auto/qeasingcurve/tst_qeasingcurve.cpp index 8abb7cb01..cc8b2acbf 100644 --- a/tests/auto/qeasingcurve/tst_qeasingcurve.cpp +++ b/tests/auto/qeasingcurve/tst_qeasingcurve.cpp @@ -23,20 +23,14 @@ #include #include +#include -//TESTED_CLASS= -//TESTED_FILES= +// TESTED_CLASS= +// TESTED_FILES= -class tst_QEasingCurve : public QObject { - Q_OBJECT - -public: - tst_QEasingCurve(); - virtual ~tst_QEasingCurve(); - -public Q_SLOTS: - void init(); - void cleanup(); +class tst_QEasingCurve : public QObject +{ + Q_OBJECT private slots: void type(); @@ -46,108 +40,89 @@ private slots: void operators(); void properties(); void metaTypes(); - -protected: }; -tst_QEasingCurve::tst_QEasingCurve() -{ -} - -tst_QEasingCurve::~tst_QEasingCurve() -{ -} - -void tst_QEasingCurve::init() -{ -} - -void tst_QEasingCurve::cleanup() -{ -} -#include - void tst_QEasingCurve::type() { { - QEasingCurve curve(QEasingCurve::Linear); - QCOMPARE(curve.period(), 0.3); - QCOMPARE(curve.amplitude(), 1.0); + QEasingCurve curve(QEasingCurve::Linear); + QCOMPARE(curve.period(), 0.3); + QCOMPARE(curve.amplitude(), 1.0); - curve.setPeriod(5); - curve.setAmplitude(3); - QCOMPARE(curve.period(), 5.0); - QCOMPARE(curve.amplitude(), 3.0); + curve.setPeriod(5); + curve.setAmplitude(3); + QCOMPARE(curve.period(), 5.0); + QCOMPARE(curve.amplitude(), 3.0); - curve.setType(QEasingCurve::InElastic); - QCOMPARE(curve.period(), 5.0); - QCOMPARE(curve.amplitude(), 3.0); + curve.setType(QEasingCurve::InElastic); + QCOMPARE(curve.period(), 5.0); + QCOMPARE(curve.amplitude(), 3.0); } { - QEasingCurve curve(QEasingCurve::InElastic); - QCOMPARE(curve.period(), 0.3); - QCOMPARE(curve.amplitude(), 1.0); - curve.setAmplitude(2); - QCOMPARE(curve.type(), QEasingCurve::InElastic); - curve.setType(QEasingCurve::Linear); + QEasingCurve curve(QEasingCurve::InElastic); + QCOMPARE(curve.period(), 0.3); + QCOMPARE(curve.amplitude(), 1.0); + curve.setAmplitude(2); + QCOMPARE(curve.type(), QEasingCurve::InElastic); + curve.setType(QEasingCurve::Linear); } { - // check bounaries - QEasingCurve curve(QEasingCurve::InCubic); - QTest::ignoreMessage(QtWarningMsg, "QEasingCurve: Invalid curve type 9999"); - curve.setType((QEasingCurve::Type)9999); - QCOMPARE(curve.type(), QEasingCurve::InCubic); - QTest::ignoreMessage(QtWarningMsg, "QEasingCurve: Invalid curve type -9999"); - curve.setType((QEasingCurve::Type)-9999); - QCOMPARE(curve.type(), QEasingCurve::InCubic); - QTest::ignoreMessage(QtWarningMsg, QString::fromAscii("QEasingCurve: Invalid curve type %1") - .arg(QEasingCurve::NCurveTypes).toLatin1().constData()); - curve.setType(QEasingCurve::NCurveTypes); - QCOMPARE(curve.type(), QEasingCurve::InCubic); - QTest::ignoreMessage(QtWarningMsg, QString::fromAscii("QEasingCurve: Invalid curve type %1") - .arg(-1).toLatin1().constData()); - curve.setType((QEasingCurve::Type)-1); - QCOMPARE(curve.type(), QEasingCurve::InCubic); - curve.setType(QEasingCurve::Linear); - QCOMPARE(curve.type(), QEasingCurve::Linear); - curve.setType(QEasingCurve::CosineCurve); - QCOMPARE(curve.type(), QEasingCurve::CosineCurve); + // check bounaries + QEasingCurve curve(QEasingCurve::InCubic); + QTest::ignoreMessage(QtWarningMsg, "QEasingCurve: Invalid curve type 9999"); + curve.setType((QEasingCurve::Type)9999); + QCOMPARE(curve.type(), QEasingCurve::InCubic); + QTest::ignoreMessage(QtWarningMsg, "QEasingCurve: Invalid curve type -9999"); + curve.setType((QEasingCurve::Type)-9999); + QCOMPARE(curve.type(), QEasingCurve::InCubic); + QTest::ignoreMessage(QtWarningMsg, QString::fromAscii("QEasingCurve: Invalid curve type %1") + .arg(QEasingCurve::NCurveTypes).toLatin1().constData()); + curve.setType(QEasingCurve::NCurveTypes); + QCOMPARE(curve.type(), QEasingCurve::InCubic); + QTest::ignoreMessage(QtWarningMsg, QString::fromAscii("QEasingCurve: Invalid curve type %1") + .arg(-1).toLatin1().constData()); + curve.setType((QEasingCurve::Type)-1); + QCOMPARE(curve.type(), QEasingCurve::InCubic); + curve.setType(QEasingCurve::Linear); + QCOMPARE(curve.type(), QEasingCurve::Linear); + curve.setType(QEasingCurve::CosineCurve); + QCOMPARE(curve.type(), QEasingCurve::CosineCurve); } } void tst_QEasingCurve::propertyDefaults() { { - // checks if the defaults are correct, but also demonstrates a weakness with the API. - QEasingCurve curve(QEasingCurve::InElastic); - QCOMPARE(curve.period(), 0.3); - QCOMPARE(curve.amplitude(), 1.0); - QCOMPARE(curve.overshoot(), qreal(1.70158)); - curve.setType(QEasingCurve::InBounce); - QCOMPARE(curve.period(), 0.3); - QCOMPARE(curve.amplitude(), 1.0); - QCOMPARE(curve.overshoot(), qreal(1.70158)); - curve.setType(QEasingCurve::Linear); - QCOMPARE(curve.period(), 0.3); - QCOMPARE(curve.amplitude(), 1.0); - QCOMPARE(curve.overshoot(), qreal(1.70158)); - curve.setType(QEasingCurve::InElastic); - QCOMPARE(curve.period(), 0.3); - QCOMPARE(curve.amplitude(), 1.0); - QCOMPARE(curve.overshoot(), qreal(1.70158)); - curve.setPeriod(0.4); - curve.setAmplitude(0.6); - curve.setOvershoot(1.0); - curve.setType(QEasingCurve::Linear); - QCOMPARE(curve.period(), 0.4); - QCOMPARE(curve.amplitude(), 0.6); - QCOMPARE(curve.overshoot(), 1.0); - curve.setType(QEasingCurve::InElastic); - QCOMPARE(curve.period(), 0.4); - QCOMPARE(curve.amplitude(), 0.6); - QCOMPARE(curve.overshoot(), 1.0); + // checks if the defaults are correct, but also demonstrates a weakness with the API. + QEasingCurve curve(QEasingCurve::InElastic); + QCOMPARE(curve.period(), 0.3); + QCOMPARE(curve.amplitude(), 1.0); + QCOMPARE(curve.overshoot(), qreal(1.70158)); + curve.setType(QEasingCurve::InBounce); + QCOMPARE(curve.period(), 0.3); + QCOMPARE(curve.amplitude(), 1.0); + QCOMPARE(curve.overshoot(), qreal(1.70158)); + curve.setType(QEasingCurve::Linear); + QCOMPARE(curve.period(), 0.3); + QCOMPARE(curve.amplitude(), 1.0); + QCOMPARE(curve.overshoot(), qreal(1.70158)); + curve.setType(QEasingCurve::InElastic); + QCOMPARE(curve.period(), 0.3); + QCOMPARE(curve.amplitude(), 1.0); + QCOMPARE(curve.overshoot(), qreal(1.70158)); + curve.setPeriod(0.4); + curve.setAmplitude(0.6); + curve.setOvershoot(1.0); + curve.setType(QEasingCurve::Linear); + QCOMPARE(curve.period(), 0.4); + QCOMPARE(curve.amplitude(), 0.6); + QCOMPARE(curve.overshoot(), 1.0); + curve.setType(QEasingCurve::InElastic); + QCOMPARE(curve.period(), 0.4); + QCOMPARE(curve.amplitude(), 0.6); + QCOMPARE(curve.overshoot(), 1.0); } } @@ -486,8 +461,7 @@ void tst_QEasingCurve::properties() amplitude = linear.amplitude(); period = linear.period(); - obj.setProperty("easing", - qVariantFromValue(QEasingCurve(QEasingCurve::Linear))); + obj.setProperty("easing", qVariantFromValue(QEasingCurve(QEasingCurve::Linear))); easing = qvariant_cast(obj.property("easing")); QCOMPARE(easing.type(), QEasingCurve::Linear); @@ -500,8 +474,7 @@ void tst_QEasingCurve::metaTypes() { QVERIFY(QMetaType::type("QEasingCurve") == QMetaType::QEasingCurve); - QCOMPARE(QByteArray(QMetaType::typeName(QMetaType::QEasingCurve)), - QByteArray("QEasingCurve")); + QCOMPARE(QByteArray(QMetaType::typeName(QMetaType::QEasingCurve)), QByteArray("QEasingCurve")); QVERIFY(QMetaType::isRegistered(QMetaType::QEasingCurve));