File indexing completed on 2024-06-16 05:01:59

0001 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include <QTest>
0024 #include <functional>
0025 #include "test_algorithms.h"
0026 
0027 #include "Common/FindWithUnknown.h"
0028 
0029 Q_DECLARE_METATYPE(QList<int>)
0030 
0031 bool isZero(const int num)
0032 {
0033     return num == 0;
0034 }
0035 
0036 void TestCommonAlgorithms::testLowerBoundWithUnknown()
0037 {
0038     QFETCH(QList<int>, list);
0039     QFETCH(int, needle);
0040     QFETCH(int, offset);
0041 
0042     QList<int>::const_iterator it = Common::linearLowerBoundWithUnknownElements(list.constBegin(), list.constEnd(), needle, isZero, std::less<int>());
0043     QCOMPARE(it - list.constBegin(), offset);
0044     it = Common::lowerBoundWithUnknownElements(list.constBegin(), list.constEnd(), needle, isZero, std::less<int>());
0045     QCOMPARE(it - list.constBegin(), offset);
0046 }
0047 
0048 void TestCommonAlgorithms::testLowerBoundWithUnknown_data()
0049 {
0050     QTest::addColumn<QList<int> >("list");
0051     QTest::addColumn<int>("needle");
0052     QTest::addColumn<int>("offset");
0053 
0054     // The basic variant where there are no dummy items
0055     QTest::newRow("empty-list") << QList<int>() << 10 << 0;
0056     QTest::newRow("one-item-which-is-bigger") << (QList<int>() << 5) << 1 << 0;
0057     QTest::newRow("one-item-which-is-lower") << (QList<int>() << 5) << 10 << 1;
0058     QTest::newRow("one-item-exact") << (QList<int>() << 5) << 5 << 0;
0059     QTest::newRow("three-items-before-first") << (QList<int>() << 5 << 10 << 15) << 4 << 0;
0060     QTest::newRow("three-items-first") << (QList<int>() << 5 << 10 << 15) << 5 << 0;
0061     QTest::newRow("three-items-after-first") << (QList<int>() << 5 << 10 << 15) << 6 << 1;
0062     QTest::newRow("three-items-before-last") << (QList<int>() << 5 << 10 << 15) << 14 << 2;
0063     QTest::newRow("three-items-last") << (QList<int>() << 5 << 10 << 15) << 15 << 2;
0064     QTest::newRow("three-items-after-last") << (QList<int>() << 5 << 10 << 15) << 16 << 3;
0065 
0066     // Add some fake items to the mix
0067     QTest::newRow("all-fakes") << (QList<int>() << 0 << 0 << 0) << 1 << 3;
0068     QTest::newRow("one-fake") << (QList<int>() << 0) << 1 << 1;
0069     QTest::newRow("fake-match-fake") << (QList<int>() << 0 << 1 << 0) << 1 << 1;
0070     QTest::newRow("fake-fake-match-fake") << (QList<int>() << 0 << 0 << 1 << 0) << 1 << 2;
0071     QTest::newRow("fake-lower-fake-fake") << (QList<int>() << 0 << 1 << 0 << 0) << 2 << 4;
0072 
0073     QList<int> list;
0074     list << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 0 << 13;
0075     QTest::newRow("many-items-just-one-fake-2") << list << 2 << 1;
0076     QTest::newRow("many-items-just-one-fake-11") << list << 11 << 10;
0077     QTest::newRow("many-items-just-one-fake-12") << list << 12 << 12;
0078     QTest::newRow("many-items-just-one-fake-13") << list << 13 << 12;
0079 }
0080 
0081 QTEST_GUILESS_MAIN(TestCommonAlgorithms)