File indexing completed on 2024-04-28 17:02:21

0001 /*
0002    This file is part of Massif Visualizer
0003 
0004    Copyright 2010 Milian Wolff <mail@milianw.de>
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 "datamodeltest.h"
0024 
0025 #include "modeltest.h"
0026 
0027 #include "massifdata/parser.h"
0028 #include "massifdata/filedata.h"
0029 #include "massifdata/snapshotitem.h"
0030 #include "massifdata/treeleafitem.h"
0031 #include "massifdata/util.h"
0032 
0033 #include "visualizer/totalcostmodel.h"
0034 #include "visualizer/detailedcostmodel.h"
0035 #include "visualizer/datatreemodel.h"
0036 
0037 #include <QtCore/QFile>
0038 #include <QtTest/QTest>
0039 #include <QtCore/QDebug>
0040 
0041 #include <KConfigGroup>
0042 #include <KSharedConfig>
0043 
0044 QTEST_MAIN(DataModelTest)
0045 
0046 using namespace Massif;
0047 
0048 void DataModelTest::parseFile()
0049 {
0050     const QString path = QFINDTESTDATA("/data/massif.out.kate");
0051     QFile file(path);
0052     QVERIFY(file.open(QIODevice::ReadOnly));
0053 
0054     Parser parser;
0055     QScopedPointer<FileData> scopedData(parser.parse(&file));
0056     FileData* data = scopedData.data();
0057     QVERIFY(data);
0058 
0059     {
0060         TotalCostModel* model = new TotalCostModel(this);
0061         new ModelTest(model, this);
0062         model->setSource(data);
0063         QVERIFY(model->rowCount() == data->snapshots().size());
0064         for ( int r = 0; r < model->rowCount(); ++r ) {
0065             for ( int c = 0; c < model->columnCount(); ++c ) {
0066                 qDebug() << r << c << model->data(model->index(r, c));
0067             }
0068         }
0069         // remove data
0070         model->setSource(0);
0071     }
0072 
0073     {
0074         DetailedCostModel* model = new DetailedCostModel(this);
0075         new ModelTest(model, this);
0076         model->setSource(data);
0077         for ( int r = 0; r < model->rowCount(); ++r ) {
0078             for ( int c = 0; c < model->columnCount(); ++c ) {
0079                 qDebug() << r << c << model->data(model->index(r, c));
0080             }
0081             if ( r ) {
0082                 // we want that the snapshots are ordered properly
0083                 QVERIFY(model->data(model->index(r, 0)).toDouble() > model->data(model->index(r - 1, 0)).toDouble());
0084             }
0085         }
0086         // remove data
0087         model->setSource(0);
0088     }
0089 
0090     {
0091         DataTreeModel* model = new DataTreeModel(this);
0092         new ModelTest(model, this);
0093         model->setSource(data);
0094         QVERIFY(model->rowCount() == data->snapshots().size());
0095         // remove data
0096         model->setSource(0);
0097     }
0098 
0099 }
0100 
0101 void DataModelTest::testUtils()
0102 {
0103     {
0104     QByteArray l("0x6F675AB: KDevelop::IndexedIdentifier::IndexedIdentifier(KDevelop::Identifier const&) (identifier.cpp:1050)");
0105     QCOMPARE(prettyLabel(l), QByteArray("KDevelop::IndexedIdentifier::IndexedIdentifier(KDevelop::Identifier const&) (identifier.cpp:1050)"));
0106     QCOMPARE(functionInLabel(l), QByteArray("KDevelop::IndexedIdentifier::IndexedIdentifier(KDevelop::Identifier const&)"));
0107     QCOMPARE(addressInLabel(l), QByteArray("0x6F675AB"));
0108     QCOMPARE(locationInLabel(l), QByteArray("identifier.cpp:1050"));
0109     }
0110     {
0111     QByteArray l("0x6F675AB: moz_xmalloc (mozalloc.cpp:98)");
0112     QCOMPARE(prettyLabel(l), QByteArray("moz_xmalloc (mozalloc.cpp:98)"));
0113     QCOMPARE(functionInLabel(l), QByteArray("moz_xmalloc"));
0114     QCOMPARE(addressInLabel(l), QByteArray("0x6F675AB"));
0115     QCOMPARE(locationInLabel(l), QByteArray("mozalloc.cpp:98"));
0116     }
0117     {
0118     QByteArray l("0xC94BD60: KDevelop::Bucket<KDevelop::(anonymous namespace)::IndexedStringData, KDevelop::(anonymous namespace)::IndexedStringRepositoryItemRequest, false, 0u>::prepareChange() (itemrepository.h:1007)");
0119     QCOMPARE(prettyLabel(l), QByteArray("KDevelop::Bucket<KDevelop::(anonymous namespace)::IndexedStringData, KDevelop::(anonymous namespace)::IndexedStringRepositoryItemRequest, false, 0u>::prepareChange() (itemrepository.h:1007)"));
0120     QCOMPARE(functionInLabel(l), QByteArray("KDevelop::Bucket<KDevelop::(anonymous namespace)::IndexedStringData, KDevelop::(anonymous namespace)::IndexedStringRepositoryItemRequest, false, 0u>::prepareChange()"));
0121     QCOMPARE(addressInLabel(l), QByteArray("0xC94BD60"));
0122     QCOMPARE(locationInLabel(l), QByteArray("itemrepository.h:1007"));
0123     }
0124     {
0125     QByteArray l("0x8E2B358: QString::QString(QChar const*, int) (in /usr/lib/libQtCore.so.4.8.4)");
0126     QCOMPARE(prettyLabel(l), QByteArray("QString::QString(QChar const*, int) (in /usr/lib/libQtCore.so.4.8.4)"));
0127     QCOMPARE(functionInLabel(l), QByteArray("QString::QString(QChar const*, int)"));
0128     QCOMPARE(addressInLabel(l), QByteArray("0x8E2B358"));
0129     QCOMPARE(locationInLabel(l), QByteArray("in /usr/lib/libQtCore.so.4.8.4"));
0130     }
0131 }
0132 
0133 void DataModelTest::shortenTemplates_data()
0134 {
0135     QTest::addColumn<QByteArray>("id");
0136     QTest::addColumn<QByteArray>("idShortened");
0137 
0138     QTest::newRow("no-tpl") << QByteArray("A::B(C::D const&) (file.cpp:1)")
0139                             << QByteArray("A::B(C::D const&) (file.cpp:1)");
0140     QTest::newRow("tpl-func") << QByteArray("A::B<T1, T2>(C::D const&) (file.cpp:1)")
0141                               << QByteArray("A::B<>(C::D const&) (file.cpp:1)");
0142     QTest::newRow("tpl-arg") << QByteArray("A::B(C::D<T1, T2> const&) (file.cpp:1)")
0143                              << QByteArray("A::B(C::D<> const&) (file.cpp:1)");
0144     QTest::newRow("tpl-multi") << QByteArray("A::B<T1, T2>(C<T3>::D<T4> const&) (file.cpp:1)")
0145                                << QByteArray("A::B<>(C<>::D<> const&) (file.cpp:1)");
0146     QTest::newRow("tpl-nested") << QByteArray("A::B<T1<T2>, T2>(C<T3>::D<T4> const&) (file.cpp:1)")
0147                                 << QByteArray("A::B<>(C<>::D<> const&) (file.cpp:1)");
0148 }
0149 
0150 void DataModelTest::shortenTemplates()
0151 {
0152     QFETCH(QByteArray, id);
0153     QFETCH(QByteArray, idShortened);
0154 
0155     KConfigGroup conf = KSharedConfig::openConfig()->group(QLatin1String("Settings"));
0156 
0157     conf.writeEntry(QLatin1String("shortenTemplates"), true);
0158     QCOMPARE(prettyLabel(id), idShortened);
0159     conf.writeEntry(QLatin1String("shortenTemplates"), false);
0160     QCOMPARE(prettyLabel(id), id);
0161 }
0162 
0163 void DataModelTest::bigMem()
0164 {
0165     // see also: https://bugs.kde.org/show_bug.cgi?id=294108
0166 
0167     const QString path = QFINDTESTDATA("/data/massif.out.huge");
0168     QFile file(path);
0169     QVERIFY(file.open(QIODevice::ReadOnly));
0170 
0171     Parser parser;
0172     QScopedPointer<FileData> scopedData(parser.parse(&file));
0173     FileData* data = scopedData.data();
0174     QVERIFY(data);
0175 
0176     QCOMPARE(data->snapshots().count(), 1);
0177     QCOMPARE(data->peak()->memHeap(), quint64(5021305210));
0178     QCOMPARE(data->peak()->memHeapExtra(), quint64(5021305211));
0179     QCOMPARE(data->peak()->memStacks(), quint64(5021305212));
0180 }