summaryrefslogtreecommitdiffstats
path: root/tests/auto/addresscache/tst_addresscache.cpp
blob: c17df8145c974733a81154ebe9fd79ba0d86a01d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/****************************************************************************
**
** Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Enterprise Perf Profiler Add-on.
**
** GNU General Public License Usage
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in
** the file LICENSE.GPLv3 included in the packaging of this file. Please
** review the following information to ensure the GNU General Public License
** requirements will be met: https://www.gnu.org/licenses/gpl.html.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io/contact-us
**
****************************************************************************/

#include <QObject>
#include <QTest>
#include <QDebug>
#include <QTemporaryFile>

#include "perfaddresscache.h"

class TestAddressCache : public QObject
{
    Q_OBJECT
private slots:
    void testRelative()
    {
        PerfElfMap::ElfInfo info_a{{}, 0x100, 100, 0,
                                   QByteArrayLiteral("libfoo.so"),
                                   QByteArrayLiteral("/usr/lib/libfoo.so")};
        PerfElfMap::ElfInfo info_b = info_a;
        info_b.addr = 0x200;

        PerfAddressCache cache;
        PerfAddressCache::OffsetAddressCache invalidAddressCache;
        PerfAddressCache::AddressCacheEntry entry{42, true};
        cache.cache(info_a, 0x110, entry, &invalidAddressCache);
        QCOMPARE(cache.find(info_a, 0x110, &invalidAddressCache).locationId, entry.locationId);
        QCOMPARE(cache.find(info_b, 0x210, &invalidAddressCache).locationId, entry.locationId);
    }

    void testInvalid()
    {
        PerfAddressCache cache;
        PerfAddressCache::OffsetAddressCache invalidAddressCache_a;
        PerfAddressCache::OffsetAddressCache invalidAddressCache_b;
        PerfAddressCache::AddressCacheEntry entry{42, true};
        cache.cache(PerfElfMap::ElfInfo{}, 0x110, entry, &invalidAddressCache_a);
        QCOMPARE(cache.find(PerfElfMap::ElfInfo{}, 0x110, &invalidAddressCache_a).locationId, entry.locationId);
        QCOMPARE(cache.find(PerfElfMap::ElfInfo{}, 0x110, &invalidAddressCache_b).locationId, -1);
    }

    void testEmpty()
    {
        PerfAddressCache cache;
        PerfAddressCache::OffsetAddressCache invalidAddressCache;
        QCOMPARE(cache.find(PerfElfMap::ElfInfo{}, 0x123, &invalidAddressCache).locationId, -1);
    }

    void testSymbolCache()
    {
        PerfElfMap::ElfInfo info_a{{}, 0x100, 100, 0,
                                   QByteArrayLiteral("libfoo_a.so"),
                                   QByteArrayLiteral("/usr/lib/libfoo_a.so")};
        PerfElfMap::ElfInfo info_b{{}, 0x200, 100, 0,
                                   QByteArrayLiteral("libfoo_b.so"),
                                   QByteArrayLiteral("/usr/lib/libfoo_b.so")};

        PerfAddressCache cache;

        QVERIFY(!cache.findSymbol(info_a, 0x100).isValid());
        QVERIFY(!cache.findSymbol(info_b, 0x100).isValid());

        cache.cacheSymbol(info_a, 0x100, 10, "Foo");
        for (auto addr : {0x100, 0x100 + 9}) {
            const auto cached = cache.findSymbol(info_a, addr);
            QVERIFY(cached.isValid());
            QCOMPARE(cached.offset, 0);
            QCOMPARE(cached.size, 10);
            QCOMPARE(cached.symname, "Foo");
        }
        QVERIFY(!cache.findSymbol(info_a, 0x100 + 10).isValid());
        QVERIFY(!cache.findSymbol(info_b, 0x100).isValid());
        QVERIFY(!cache.findSymbol(info_b, 0x100 + 9).isValid());
    }
};

QTEST_GUILESS_MAIN(TestAddressCache)

#include "tst_addresscache.moc"