File indexing completed on 2024-05-12 16:35:58

0001 #include "TestRTree.h"
0002 
0003 #include "RTree.h"
0004 
0005 #include <QTest>
0006 #include <QSharedData>
0007 
0008 
0009 using namespace Calligra::Sheets;
0010 
0011 class TestClass : public QSharedData
0012 {
0013 public:
0014     TestClass() : member() {}
0015     TestClass(const QString& m) : member(m) {}
0016     virtual ~TestClass() {}
0017     virtual int type() const {
0018         return 0;
0019     }
0020     bool operator<(const TestClass& other) const {
0021         return member < other.member;
0022     }
0023     bool operator==(const TestClass& other) const {
0024         return member == other.member;
0025     }
0026     QString member;
0027 };
0028 
0029 class SharedTestClass
0030 {
0031 public:
0032     SharedTestClass() : d(new TestClass()) {}
0033     SharedTestClass(TestClass* subStyle) : d(subStyle) {}
0034     inline const TestClass *operator->() const {
0035         return d.data();
0036     }
0037     bool operator<(const SharedTestClass& o) const {
0038         return d->operator<(*o.d.data());
0039     }
0040     bool operator==(const SharedTestClass& o) const {
0041         return d->operator==(*o.d.data());
0042     }
0043 
0044 private:
0045     QSharedDataPointer<TestClass> d;
0046 };
0047 
0048 class DerivedClass : public TestClass
0049 {
0050 public:
0051     DerivedClass() : TestClass() {}
0052     DerivedClass(const QString& s) : TestClass(s) {}
0053     int type() const override {
0054         return 1;
0055     }
0056 };
0057 
0058 void TestRTree::testIntersectingPairs()
0059 {
0060     RTree<SharedTestClass> tree;
0061     tree.insert(QRect(1, 1, 1, 1), new DerivedClass(QString("foo")));
0062     QList< QPair<QRectF, SharedTestClass> > pairs = tree.intersectingPairs(QRect(1, 1, 10, 10)).values();
0063     QVERIFY(pairs.count() == 1);
0064     QCOMPARE(pairs[0].first, QRectF(1, 1, 1, 1));
0065     QCOMPARE(pairs[0].second->member, QString("foo"));
0066     QCOMPARE(pairs[0].second->type(), 1);
0067 }
0068 
0069 void TestRTree::testInsertShiftRight()
0070 {
0071     RTree<SharedTestClass> tree;
0072     tree.insert(QRect(2, 2, 2, 1), new DerivedClass(QString("foo")));
0073     tree.insertShiftRight(QRect(2, 1, 3, 4));
0074     QList< QPair<QRectF, SharedTestClass> > pairs = tree.intersectingPairs(QRect(1, 1, 10, 10)).values();
0075     QCOMPARE(pairs.count(), 3);
0076     QCOMPARE(pairs[0].first, QRectF(2, 2, 2, 1));
0077     QCOMPARE(pairs[0].second->member, QString("foo"));
0078     QCOMPARE(pairs[1].first, QRectF(2, 1, KS_colMax - 1, 4));
0079     QCOMPARE(pairs[1].second->member, QString(""));
0080     QCOMPARE(pairs[2].first, QRectF(5, 2, 2, 1));
0081     QCOMPARE(pairs[2].second->member, QString("foo"));
0082 }
0083 
0084 void TestRTree::testInsertShiftDown()
0085 {
0086     RTree<SharedTestClass> tree;
0087     tree.insert(QRect(2, 2, 1, 2), new DerivedClass(QString("foo")));
0088     tree.insertShiftDown(QRect(2, 1, 4, 3));
0089     QList< QPair<QRectF, SharedTestClass> > pairs = tree.intersectingPairs(QRect(1, 1, 10, 10)).values();
0090     QCOMPARE(pairs.count(), 3);
0091     QCOMPARE(pairs[0].first, QRectF(2, 2, 1, 2));
0092     QCOMPARE(pairs[0].second->member, QString("foo"));
0093     QCOMPARE(pairs[1].first, QRectF(2, 1, 4, KS_rowMax));
0094     QCOMPARE(pairs[1].second->member, QString(""));
0095     QCOMPARE(pairs[2].first, QRectF(2, 5, 1, 2));
0096     QCOMPARE(pairs[2].second->member, QString("foo"));
0097 }
0098 
0099 void TestRTree::testRemoveShiftLeft()
0100 {
0101     RTree<SharedTestClass> tree;
0102     tree.insert(QRect(5, 2, 2, 1), new DerivedClass(QString("foo")));
0103     tree.removeShiftLeft(QRect(2, 1, 3, 4));
0104     QList< QPair<QRectF, SharedTestClass> > pairs = tree.intersectingPairs(QRect(1, 1, 10, 10)).values();
0105     QCOMPARE(pairs.count(), 3);
0106     QCOMPARE(pairs[0].first, QRectF(5, 2, 2, 1));
0107     QCOMPARE(pairs[0].second->member, QString("foo"));
0108     QCOMPARE(pairs[1].first, QRectF(2, 1, KS_colMax - 1, 4));
0109     QCOMPARE(pairs[1].second->member, QString(""));
0110     QCOMPARE(pairs[2].first, QRectF(2, 2, 2, 1));
0111     QCOMPARE(pairs[2].second->member, QString("foo"));
0112 }
0113 
0114 void TestRTree::testRemoveShiftUp()
0115 {
0116     RTree<SharedTestClass> tree;
0117     tree.insert(QRect(2, 5, 1, 2), new DerivedClass(QString("foo")));
0118     tree.removeShiftUp(QRect(2, 1, 4, 3));
0119     QList< QPair<QRectF, SharedTestClass> > pairs = tree.intersectingPairs(QRect(1, 1, 10, 10)).values();
0120     QCOMPARE(pairs.count(), 3);
0121     QCOMPARE(pairs[0].first, QRectF(2, 5, 1, 2));
0122     QCOMPARE(pairs[0].second->member, QString("foo"));
0123     QCOMPARE(pairs[1].first, QRectF(2, 1, 4, KS_rowMax));
0124     QCOMPARE(pairs[1].second->member, QString(""));
0125     QCOMPARE(pairs[2].first, QRectF(2, 2, 1, 2));
0126     QCOMPARE(pairs[2].second->member, QString("foo"));
0127 }
0128 
0129 void TestRTree::testInsertColumns()
0130 {
0131     // RTree::InsertMode = RTree::CopyPrevious
0132     RTree<QString> tree;
0133     tree.insert(QRect(1, 1, 2, 1), QString("1"));
0134     tree.insert(QRect(1, 2, 3, 1), QString("2"));
0135     tree.insert(QRect(2, 3, 4, 1), QString("3"));
0136     tree.insert(QRect(2, 4, 5, 1), QString("4"));
0137     tree.insert(QRect(3, 5, 3, 1), QString("5"));
0138     tree.insert(QRect(3, 6, 4, 1), QString("6"));
0139     tree.insert(QRect(4, 7, 2, 1), QString("7"));
0140     tree.insert(QRect(4, 8, 3, 1), QString("8"));
0141     tree.insert(QRect(6, 9, 3, 1), QString("9"));
0142     QList< QPair<QRectF, QString> > undo = tree.insertColumns(3, 3);
0143     QList< QPair<QRectF, QString> > pairs = tree.intersectingPairs(QRect(1, 1, 10, 10)).values();
0144     QCOMPARE(pairs.count(), 9);
0145     QCOMPARE(pairs[0].first, QRectF(1, 1, 5, 1));
0146     QCOMPARE(pairs[0].second, QString("1"));
0147     QCOMPARE(pairs[1].first, QRectF(1, 2, 6, 1));
0148     QCOMPARE(pairs[1].second, QString("2"));
0149     QCOMPARE(pairs[2].first, QRectF(2, 3, 7, 1));
0150     QCOMPARE(pairs[2].second, QString("3"));
0151     QCOMPARE(pairs[3].first, QRectF(2, 4, 8, 1));
0152     QCOMPARE(pairs[3].second, QString("4"));
0153     QCOMPARE(pairs[4].first, QRectF(6, 5, 3, 1));
0154     QCOMPARE(pairs[4].second, QString("5"));
0155     QCOMPARE(pairs[5].first, QRectF(6, 6, 4, 1));
0156     QCOMPARE(pairs[5].second, QString("6"));
0157     QCOMPARE(pairs[6].first, QRectF(7, 7, 2, 1));
0158     QCOMPARE(pairs[6].second, QString("7"));
0159     QCOMPARE(pairs[7].first, QRectF(7, 8, 3, 1));
0160     QCOMPARE(pairs[7].second, QString("8"));
0161     QCOMPARE(pairs[8].first, QRectF(9, 9, 3, 1));
0162     QCOMPARE(pairs[8].second, QString("9"));
0163     QCOMPARE(undo.count(), 0);
0164 #if 0
0165     // RTree::InsertMode = RTree::CopyCurrent
0166     tree.clear();
0167     tree.insert(QRect(1, 1, 2, 1), QString("1"));
0168     tree.insert(QRect(1, 2, 3, 1), QString("2"));
0169     undo = tree.insertColumns(3, 3, RTree<QString>::CopyCurrent);
0170     pairs = tree.intersectingPairs(QRect(1, 1, 10, 10));
0171     QCOMPARE(pairs.count(), 2);
0172     QCOMPARE(pairs[0].first, QRectF(1, 1, 2, 1));
0173     QCOMPARE(pairs[1].first, QRectF(1, 2, 6, 1));
0174     QCOMPARE(undo.count(), 0);
0175 
0176     // RTree::InsertMode = RTree::CopyNone
0177     tree.clear();
0178     tree.insert(QRect(1, 1, 2, 1), QString("1"));
0179     tree.insert(QRect(1, 2, 3, 1), QString("2"));
0180     undo = tree.insertColumns(3, 3, RTree<QString>::CopyNone);
0181     pairs = tree.intersectingPairs(QRect(1, 1, 10, 10));
0182     QCOMPARE(pairs.count(), 3);
0183     QCOMPARE(pairs[0].first, QRectF(1, 1, 2, 1));
0184     QCOMPARE(pairs[1].first, QRectF(1, 2, 2, 1));
0185     QCOMPARE(pairs[2].first, QRectF(6, 2, 1, 1));
0186     QCOMPARE(undo.count(), 0);
0187 #endif
0188 }
0189 
0190 void TestRTree::testInsertRows()
0191 {
0192     // RTree::InsertMode = RTree::CopyPrevious
0193     RTree<QString> tree;
0194     tree.insert(QRect(1, 1, 1, 2), QString("1"));
0195     tree.insert(QRect(2, 1, 1, 3), QString("2"));
0196     tree.insert(QRect(3, 2, 1, 4), QString("3"));
0197     tree.insert(QRect(4, 2, 1, 5), QString("4"));
0198     tree.insert(QRect(5, 3, 1, 3), QString("5"));
0199     tree.insert(QRect(6, 3, 1, 4), QString("6"));
0200     tree.insert(QRect(7, 4, 1, 2), QString("7"));
0201     tree.insert(QRect(8, 4, 1, 3), QString("8"));
0202     tree.insert(QRect(9, 6, 1, 3), QString("9"));
0203     QList< QPair<QRectF, QString> > undo = tree.insertRows(3, 3);
0204     QList< QPair<QRectF, QString> > pairs = tree.intersectingPairs(QRect(1, 1, 10, 10)).values();
0205     QCOMPARE(pairs.count(), 9);
0206     QCOMPARE(pairs[0].first, QRectF(1, 1, 1, 2));
0207     QCOMPARE(pairs[0].second, QString("1"));
0208     QCOMPARE(pairs[1].first, QRectF(2, 1, 1, 6));
0209     QCOMPARE(pairs[1].second, QString("2"));
0210     QCOMPARE(pairs[2].first, QRectF(3, 2, 1, 7));
0211     QCOMPARE(pairs[2].second, QString("3"));
0212     QCOMPARE(pairs[3].first, QRectF(4, 2, 1, 8));
0213     QCOMPARE(pairs[3].second, QString("4"));
0214     QCOMPARE(pairs[4].first, QRectF(5, 6, 1, 3));
0215     QCOMPARE(pairs[4].second, QString("5"));
0216     QCOMPARE(pairs[5].first, QRectF(6, 6, 1, 4));
0217     QCOMPARE(pairs[5].second, QString("6"));
0218     QCOMPARE(pairs[6].first, QRectF(7, 7, 1, 2));
0219     QCOMPARE(pairs[6].second, QString("7"));
0220     QCOMPARE(pairs[7].first, QRectF(8, 7, 1, 3));
0221     QCOMPARE(pairs[7].second, QString("8"));
0222     QCOMPARE(pairs[8].first, QRectF(9, 9, 1, 3));
0223     QCOMPARE(pairs[8].second, QString("9"));
0224     QCOMPARE(undo.count(), 0);
0225 #if 0
0226     // RTree::InsertMode = RTree::CopyCurrent
0227     tree.clear();
0228     tree.insert(QRect(1, 1, 1, 2), QString("1"));
0229     tree.insert(QRect(2, 1, 1, 3), QString("2"));
0230     undo = tree.insertColumns(3, 3, RTree<QString>::CopyCurrent);
0231     pairs = tree.intersectingPairs(QRect(1, 1, 10, 10));
0232     QCOMPARE(pairs.count(), 2);
0233     QCOMPARE(pairs[0].first, QRectF(1, 1, 1, 2));
0234     QCOMPARE(pairs[1].first, QRectF(2, 1, 1, 6));
0235     QCOMPARE(undo.count(), 0);
0236 #endif
0237 
0238     RTree<bool> boolTree;
0239     boolTree.insert(QRect(1, 2, 2, 1), true);
0240     boolTree.insert(QRect(1, 3, 1, 2), true);
0241     boolTree.insertRows(6, 1);
0242     QList< QPair<QRectF, bool> > boolPairs = boolTree.intersectingPairs(QRect(1, 2, 1, 1)).values();
0243     QCOMPARE(boolPairs.count(), 1);
0244     QCOMPARE(boolPairs[0].first, QRectF(1, 2, 2, 1));
0245     QCOMPARE(boolPairs[0].second, true);
0246     boolPairs = boolTree.intersectingPairs(QRect(1, 3, 1, 1)).values();
0247     QCOMPARE(boolPairs.count(), 1);
0248     QCOMPARE(boolPairs[0].first, QRectF(1, 3, 1, 2));
0249     QCOMPARE(boolPairs[0].second, true);
0250 }
0251 
0252 void TestRTree::testRemoveColumns()
0253 {
0254     RTree<QString> tree;
0255     tree.insert(QRect(1, 1, 2, 1), QString("1"));
0256     tree.insert(QRect(1, 2, 3, 1), QString("2"));
0257     tree.insert(QRect(2, 3, 4, 1), QString("3"));
0258     tree.insert(QRect(2, 4, 5, 1), QString("4"));
0259     tree.insert(QRect(3, 5, 3, 1), QString("5"));
0260     tree.insert(QRect(3, 6, 4, 1), QString("6"));
0261     tree.insert(QRect(4, 7, 2, 1), QString("7"));
0262     tree.insert(QRect(4, 8, 3, 1), QString("8"));
0263     tree.insert(QRect(6, 9, 3, 1), QString("9"));
0264     QList< QPair<QRectF, QString> > undo = tree.removeColumns(3, 3);
0265     QList< QPair<QRectF, QString> > pairs = tree.intersectingPairs(QRect(1, 1, 10, 10)).values();
0266     QCOMPARE(pairs.count(), 7);
0267     QCOMPARE(pairs[0].first, QRectF(1, 1, 2, 1));
0268     QCOMPARE(pairs[0].second, QString("1"));
0269     QCOMPARE(pairs[1].first, QRectF(1, 2, 2, 1));
0270     QCOMPARE(pairs[1].second, QString("2"));
0271     QCOMPARE(pairs[2].first, QRectF(2, 3, 1, 1));
0272     QCOMPARE(pairs[2].second, QString("3"));
0273     QCOMPARE(pairs[3].first, QRectF(2, 4, 2, 1));
0274     QCOMPARE(pairs[3].second, QString("4"));
0275     QCOMPARE(pairs[4].first, QRectF(3, 6, 1, 1));
0276     QCOMPARE(pairs[4].second, QString("6"));
0277     QCOMPARE(pairs[5].first, QRectF(3, 8, 1, 1));
0278     QCOMPARE(pairs[5].second, QString("8"));
0279     QCOMPARE(pairs[6].first, QRectF(3, 9, 3, 1));
0280     QCOMPARE(pairs[6].second, QString("9"));
0281     QCOMPARE(undo.count(), 2);
0282     QCOMPARE(undo[0].first.toRect(), QRect(3, 5, 3, 1));
0283     QCOMPARE(undo[0].second, QString("5"));
0284     QCOMPARE(undo[1].first.toRect(), QRect(4, 7, 2, 1));
0285     QCOMPARE(undo[1].second, QString("7"));
0286 }
0287 
0288 void TestRTree::testRemoveRows()
0289 {
0290     RTree<QString> tree;
0291     tree.insert(QRect(1, 1, 1, 2), QString("1"));
0292     tree.insert(QRect(2, 1, 1, 3), QString("2"));
0293     tree.insert(QRect(3, 2, 1, 4), QString("3"));
0294     tree.insert(QRect(4, 2, 1, 5), QString("4"));
0295     tree.insert(QRect(5, 3, 1, 3), QString("5"));
0296     tree.insert(QRect(6, 3, 1, 4), QString("6"));
0297     tree.insert(QRect(7, 4, 1, 2), QString("7"));
0298     tree.insert(QRect(8, 4, 1, 3), QString("8"));
0299     tree.insert(QRect(9, 6, 1, 3), QString("9"));
0300     QList< QPair<QRectF, QString> > undo = tree.removeRows(3, 3);
0301     QList< QPair<QRectF, QString> > pairs = tree.intersectingPairs(QRect(1, 1, 10, 10)).values();
0302     QCOMPARE(pairs.count(), 7);
0303     QCOMPARE(pairs[0].first, QRectF(1, 1, 1, 2));
0304     QCOMPARE(pairs[0].second, QString("1"));
0305     QCOMPARE(pairs[1].first, QRectF(2, 1, 1, 2));
0306     QCOMPARE(pairs[1].second, QString("2"));
0307     QCOMPARE(pairs[2].first, QRectF(3, 2, 1, 1));
0308     QCOMPARE(pairs[2].second, QString("3"));
0309     QCOMPARE(pairs[3].first, QRectF(4, 2, 1, 2));
0310     QCOMPARE(pairs[3].second, QString("4"));
0311     QCOMPARE(pairs[4].first, QRectF(6, 3, 1, 1));
0312     QCOMPARE(pairs[4].second, QString("6"));
0313     QCOMPARE(pairs[5].first, QRectF(8, 3, 1, 1));
0314     QCOMPARE(pairs[5].second, QString("8"));
0315     QCOMPARE(pairs[6].first, QRectF(9, 3, 1, 3));
0316     QCOMPARE(pairs[6].second, QString("9"));
0317     QCOMPARE(undo.count(), 2);
0318     QCOMPARE(undo[0].first.toRect(), QRect(5, 3, 1, 3));
0319     QCOMPARE(undo[0].second, QString("5"));
0320     QCOMPARE(undo[1].first.toRect(), QRect(7, 4, 1, 2));
0321     QCOMPARE(undo[1].second, QString("7"));
0322 }
0323 
0324 void TestRTree::testPrimitive()
0325 {
0326     RTree<bool> tree;
0327     tree.insert(QRect(2, 5, 1, 2), true);
0328     QCOMPARE(tree.contains(QPoint(2, 2)).isEmpty(), true);
0329     QCOMPARE(tree.contains(QPoint(2, 5)).first(), true);
0330     QCOMPARE(tree.contains(QPoint(3, 5)).isEmpty(), true);
0331     QCOMPARE(tree.contains(QPoint(2, 6)).first(), true);
0332     const QList< QPair<QRectF, bool> > pairs = tree.intersectingPairs(QRect(2, 5, 1, 2)).values();
0333     QCOMPARE(pairs.count(), 1);
0334     QCOMPARE(pairs.first().first.toRect(), QRect(2, 5, 1, 2));
0335     QCOMPARE(pairs.first().second, true);
0336 }
0337 
0338 QTEST_MAIN(TestRTree)