File indexing completed on 2024-04-28 03:51:39

0001 /*
0002     This file is part of the KDE Baloo project.
0003     SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "mtimedb.h"
0009 #include "postingiterator.h"
0010 #include "singledbtest.h"
0011 
0012 using namespace Baloo;
0013 
0014 class MTimeDBTest : public SingleDBTest
0015 {
0016     Q_OBJECT
0017 private Q_SLOTS:
0018     void test() {
0019         MTimeDB db(MTimeDB::create(m_txn), m_txn);
0020 
0021         db.put(5, 1);
0022         QCOMPARE(db.get(5), QVector<quint64>() << 1);
0023         db.del(5, 1);
0024         QCOMPARE(db.get(5), QVector<quint64>());
0025     }
0026 
0027     void testMultiple() {
0028         MTimeDB db(MTimeDB::create(m_txn), m_txn);
0029 
0030         db.put(5, 1);
0031         db.put(5, 2);
0032         db.put(5, 3);
0033 
0034         QCOMPARE(db.get(5), QVector<quint64>() << 1 << 2 << 3);
0035         db.del(5, 2);
0036         QCOMPARE(db.get(5), QVector<quint64>() << 1 << 3);
0037 
0038         QCOMPARE(db.get(4), QVector<quint64>());
0039         QCOMPARE(db.get(6), QVector<quint64>());
0040     }
0041 
0042     void testIter() {
0043         MTimeDB db(MTimeDB::create(m_txn), m_txn);
0044 
0045         db.put(5, 1);
0046         db.put(6, 2);
0047         db.put(6, 3);
0048         db.put(7, 4);
0049         db.put(8, 5);
0050         db.put(9, 6);
0051 
0052         std::unique_ptr<PostingIterator> it{db.iterRange(6, std::numeric_limits<quint32>::max())};
0053         QVERIFY(it);
0054 
0055         QVector<quint64> result = {2, 3, 4, 5, 6};
0056         for (quint64 val : result) {
0057             QCOMPARE(it->next(), static_cast<quint64>(val));
0058             QCOMPARE(it->docId(), static_cast<quint64>(val));
0059         }
0060 
0061         it.reset(db.iterRange(0, 10));
0062         QVERIFY(it);
0063 
0064         result = {1, 2, 3, 4, 5, 6};
0065         for (quint64 val : std::as_const(result)) {
0066             QCOMPARE(it->next(), static_cast<quint64>(val));
0067             QCOMPARE(it->docId(), static_cast<quint64>(val));
0068         }
0069 
0070         it.reset(db.iterRange(0, 7));
0071         QVERIFY(it);
0072 
0073         result = {1, 2, 3, 4};
0074         for (quint64 val : std::as_const(result)) {
0075             QCOMPARE(it->next(), static_cast<quint64>(val));
0076             QCOMPARE(it->docId(), static_cast<quint64>(val));
0077         }
0078 
0079         it.reset(db.iterRange(0, 6));
0080         QVERIFY(it);
0081 
0082         result = {1, 2, 3};
0083         for (quint64 val : std::as_const(result)) {
0084             QCOMPARE(it->next(), static_cast<quint64>(val));
0085             QCOMPARE(it->docId(), static_cast<quint64>(val));
0086         }
0087     }
0088 
0089     void testRangeIter() {
0090         MTimeDB db(MTimeDB::create(m_txn), m_txn);
0091 
0092         db.put(5, 1);
0093         db.put(6, 2);
0094         db.put(6, 3);
0095         db.put(7, 4);
0096         db.put(8, 5);
0097         db.put(9, 6);
0098 
0099         std::unique_ptr<PostingIterator> it{db.iterRange(6, 8)};
0100         QVERIFY(it);
0101 
0102         QVector<quint64> result = {2, 3, 4, 5};
0103         for (quint64 val : result) {
0104             QCOMPARE(it->next(), static_cast<quint64>(val));
0105             QCOMPARE(it->docId(), static_cast<quint64>(val));
0106         }
0107 
0108         // Empty range
0109         it.reset(db.iterRange(4, 4));
0110         QVERIFY(!it);
0111 
0112         it.reset(db.iterRange(10, 20));
0113         QVERIFY(!it);
0114     }
0115 
0116     void testSortedAndUnique()
0117     {
0118         MTimeDB db(MTimeDB::create(m_txn), m_txn);
0119 
0120         db.put(5, 1);
0121         db.put(6, 4);
0122         db.put(6, 2);
0123         db.put(6, 3);
0124         db.put(7, 3);
0125 
0126         QCOMPARE(db.get(6), QVector<quint64>() << 2 << 3 << 4);
0127 
0128         std::unique_ptr<PostingIterator> it{db.iterRange(5, 7)};
0129         QVERIFY(it);
0130 
0131         {
0132             QVector<quint64> result = {1, 2, 3, 4};
0133             for (quint64 val : result) {
0134                 QCOMPARE(it->next(), static_cast<quint64>(val));
0135                 QCOMPARE(it->docId(), static_cast<quint64>(val));
0136             }
0137         }
0138 
0139         {
0140             it.reset(db.iterRange(6, std::numeric_limits<quint32>::max()));
0141             QVERIFY(it);
0142 
0143             QVector<quint64> result = {2, 3, 4};
0144             for (quint64 val : result) {
0145                 QCOMPARE(it->next(), static_cast<quint64>(val));
0146                 QCOMPARE(it->docId(), static_cast<quint64>(val));
0147             }
0148         }
0149     }
0150 
0151     void testBeginOfEpoch() {
0152         MTimeDB db(MTimeDB::create(m_txn), m_txn);
0153 
0154         db.put(0, 1);
0155         db.put(0, 2);
0156         db.put(0, 3);
0157         db.put(1, 4);
0158 
0159         QCOMPARE(db.get(0), QVector<quint64>({1, 2, 3}));
0160         db.del(99, 2);
0161         QCOMPARE(db.get(0), QVector<quint64>({1, 2, 3}));
0162         QCOMPARE(db.get(1), QVector<quint64>({4}));
0163         db.del(0, 2);
0164         QCOMPARE(db.get(0), QVector<quint64>({1, 3}));
0165 
0166         std::unique_ptr<PostingIterator> it{db.iterRange(0, 0)};
0167         QVector<quint64> result;
0168         while (it->next()) {
0169             result.append(it->docId());
0170         }
0171         QCOMPARE(result, QVector<quint64>({1, 3}));
0172 
0173         it.reset(db.iterRange(1, std::numeric_limits<quint32>::max()));
0174         QVERIFY(it->next());
0175         QCOMPARE(it->docId(), 4);
0176     }
0177 };
0178 
0179 QTEST_MAIN(MTimeDBTest)
0180 
0181 #include "mtimedbtest.moc"