File indexing completed on 2025-05-04 05:17:16

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "difftest.h"
0008 
0009 #include <QtTest>
0010 
0011 #include <diff.h>
0012 #include <solution.h>
0013 
0014 void DiffTest::solutionTest()
0015 {
0016     /*
0017 
0018     */
0019     Diff::Solution solution;
0020     solution << qMakePair(1, 1);
0021     solution << qMakePair(3, 4);
0022     solution << qMakePair(5, 6);
0023     Diff::SolutionIterator it{solution, 5, 6};
0024     it.begin();
0025 
0026     auto r = it.pick();
0027     QCOMPARE(r.success, true);
0028     QCOMPARE(r.oldStart, 0);
0029     QCOMPARE(r.newStart, 0);
0030 }
0031 
0032 void DiffTest::basicList()
0033 {
0034     QStringList oldList{"a", "b", "c", "e"};
0035     QStringList newList{"a", "b", "d", "e"};
0036     auto diffResult = Diff::diff(oldList, newList);
0037     QCOMPARE(diffResult.size(), 3);
0038 
0039     QStringList f1{"a", "b"};
0040 
0041     auto r = diffResult.at(0);
0042     QCOMPARE(r->type, Diff::SegmentType::SameOnBoth);
0043     QCOMPARE(r->oldText, f1);
0044     QCOMPARE(r->newText, f1);
0045 
0046     r = diffResult.at(1);
0047     QCOMPARE(r->type, Diff::SegmentType::DifferentOnBoth);
0048     QCOMPARE(r->oldText, oldList.mid(2, 1));
0049     QCOMPARE(r->newText, newList.mid(2, 1));
0050 
0051     r = diffResult.at(2);
0052     QCOMPARE(r->type, Diff::SegmentType::SameOnBoth);
0053     QCOMPARE(r->oldText, oldList.mid(3, 1));
0054     QCOMPARE(r->newText, newList.mid(3, 1));
0055 }
0056 
0057 void DiffTest::randomMissedNumber()
0058 {
0059     constexpr int total{100};
0060 
0061     QStringList oldList;
0062     QStringList newList;
0063     auto n = 50; // QRandomGenerator::global()->bounded(2, 98);
0064     for (auto i = 0; i < total; i++) {
0065         oldList << QString::number(i);
0066         newList << QString::number(i);
0067     }
0068     newList.removeAt(n);
0069 
0070     auto diffResult = Diff::diff(oldList, newList);
0071 
0072     int oldCount{0};
0073     int newCount{0};
0074     for (const auto &r : diffResult) {
0075         oldCount += r->oldText.size();
0076         newCount += r->newText.size();
0077     }
0078 
0079     QCOMPARE(oldCount, 100);
0080     QCOMPARE(newCount, 99);
0081 
0082     QCOMPARE(diffResult.size(), 3);
0083     auto r = diffResult.at(0);
0084     QCOMPARE(r->type, Diff::SegmentType::SameOnBoth);
0085     QCOMPARE(r->oldText.size(), n);
0086     QCOMPARE(r->newText.size(), n);
0087 
0088     r = diffResult.at(1);
0089     QCOMPARE(r->type, Diff::SegmentType::OnlyOnLeft);
0090     QCOMPARE(r->oldText.size(), 1);
0091     QCOMPARE(r->newText.size(), 0);
0092 
0093     r = diffResult.at(2);
0094     QCOMPARE(r->type, Diff::SegmentType::SameOnBoth);
0095 
0096     QCOMPARE(r->oldText.first().toInt(), n + 1);
0097     QCOMPARE(r->newText.first().toInt(), n + 1);
0098 
0099     QCOMPARE(r->oldText.size(), total - n - 1);
0100     QCOMPARE(r->newText.size(), total - n - 1);
0101 }
0102 
0103 void DiffTest::removeFromLast()
0104 {
0105     QStringList oldList{"a", "b", "c"};
0106     QStringList newList{"a", "b"};
0107     auto diffResult = Diff::diff(oldList, newList);
0108 
0109     QStringList ab{"a", "b"};
0110     QStringList c{"c"};
0111     QCOMPARE(diffResult.size(), 2);
0112 
0113     auto r = diffResult.at(0);
0114     QCOMPARE(r->type, Diff::SegmentType::SameOnBoth);
0115     QCOMPARE(r->oldText, ab);
0116     QCOMPARE(r->newText, ab);
0117 
0118     r = diffResult.at(1);
0119     QCOMPARE(r->type, Diff::SegmentType::OnlyOnLeft);
0120     QCOMPARE(r->oldText, c);
0121     QCOMPARE(r->newText, QStringList());
0122 }
0123 
0124 void DiffTest::allPlacesRemove()
0125 {
0126     QStringList baseList{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
0127     auto total = baseList.size();
0128 
0129     for (auto i = 0; i < total; i++) {
0130         auto inTheMiddle{i && i < total - 1};
0131 
0132         for (auto t = 0; t < 2; t++) {
0133             auto oldList = baseList;
0134             auto newList = baseList;
0135 
0136             if (t)
0137                 newList.removeAt(i);
0138             else
0139                 oldList.removeAt(i);
0140 
0141             auto diffResult = Diff::diff(oldList, newList);
0142 
0143             int oldCount{0};
0144             int newCount{0};
0145             for (const auto &r : diffResult) {
0146                 oldCount += r->oldText.size();
0147                 newCount += r->newText.size();
0148             }
0149 
0150             QCOMPARE(oldCount, total - (t ? 0 : 1));
0151             QCOMPARE(newCount, total - (t ? 1 : 0));
0152             QCOMPARE(diffResult.size(), inTheMiddle ? 3 : 2);
0153         }
0154     }
0155 }
0156 
0157 void DiffTest::removedTest()
0158 {
0159     QStringList oldList{"a", "b", "c"};
0160     QStringList newList{"a", "c"};
0161     auto diffResult = Diff::diff(oldList, newList);
0162 
0163     QStringList a{"a"};
0164     QStringList b{"b"};
0165     QStringList c{"c"};
0166     QCOMPARE(diffResult.size(), 3);
0167 
0168     auto r = diffResult.at(0);
0169     QCOMPARE(r->type, Diff::SegmentType::SameOnBoth);
0170     QCOMPARE(r->oldText, a);
0171     QCOMPARE(r->newText, a);
0172 
0173     r = diffResult.at(1);
0174     QCOMPARE(r->type, Diff::SegmentType::OnlyOnLeft);
0175     QCOMPARE(r->oldText, b);
0176     QCOMPARE(r->newText, QStringList());
0177 
0178     r = diffResult.at(2);
0179     QCOMPARE(r->type, Diff::SegmentType::SameOnBoth);
0180     QCOMPARE(r->oldText, c);
0181     QCOMPARE(r->newText, c);
0182 }
0183 
0184 QTEST_MAIN(DiffTest)
0185 
0186 #include "moc_difftest.cpp"