tst_dumpers.cpp 86.8 KB
Newer Older
1
2
#include "gdb/gdbmi.h"
#include "gdbmacros.h"
3
#include "gdbmacros_p.h"
4

5
6
7
8
9
10
11
12
#include <QtCore/private/qobject_p.h>

#include <QtGui/QStandardItemModel>
#include <QtGui/QStringListModel>

#include <QtTest/QtTest>
//#include <QtTest/qtest_gui.h>

13
14
15
16
17
18
19
20
21
#undef NS
#ifdef QT_NAMESPACE
#   define STRINGIFY0(s) #s
#   define STRINGIFY1(s) STRINGIFY0(s)
#   define NS STRINGIFY1(QT_NAMESPACE) "::"
#else
#   define NS ""
#endif

22
23
24
using namespace Debugger;
using namespace Debugger::Internal;

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
static QByteArray operator<<(QByteArray ba, const QByteArray &replacement)
{
    int pos = ba.indexOf('%');
    Q_ASSERT(pos != -1);
    return ba.replace(pos, 1, replacement);
}

static QByteArray &operator<<=(QByteArray &ba, const QByteArray &replacement)
{
    int pos = ba.indexOf('%');
    Q_ASSERT(pos != -1);
    return ba.replace(pos, 1, replacement);
}


template <typename T> 
inline QByteArray N(T t) { return QByteArray::number(t); }

43
static const char gdbmi1[] =
hjk's avatar
hjk committed
44
    "[frame={level=\"0\",addr=\"0x00000000004061ca\","
45
46
47
    "func=\"main\",file=\"test1.cpp\","
    "fullname=\"/home/apoenitz/work/test1/test1.cpp\",line=\"209\"}]";

48
static const char gdbmi2[] =
hjk's avatar
hjk committed
49
    "[frame={level=\"0\",addr=\"0x00002ac058675840\","
50
51
52
    "func=\"QApplication\",file=\"/home/apoenitz/dev/qt/src/gui/kernel/qapplication.cpp\","
    "fullname=\"/home/apoenitz/dev/qt/src/gui/kernel/qapplication.cpp\",line=\"592\"},"
    "frame={level=\"1\",addr=\"0x00000000004061e0\",func=\"main\",file=\"test1.cpp\","
hjk's avatar
hjk committed
53
    "fullname=\"/home/apoenitz/work/test1/test1.cpp\",line=\"209\"}]";
54

55
static const char gdbmi3[] =
hjk's avatar
hjk committed
56
    "[stack={frame={level=\"0\",addr=\"0x00000000004061ca\","
57
    "func=\"main\",file=\"test1.cpp\","
hjk's avatar
hjk committed
58
    "fullname=\"/home/apoenitz/work/test1/test1.cpp\",line=\"209\"}}]";
59

60
static const char gdbmi4[] =
hjk's avatar
hjk committed
61
    "&\"source /home/apoenitz/dev/ide/main/bin/gdb/qt4macros\\n\""
62
63
    "4^done\n";

64
static const char gdbmi5[] =
hjk's avatar
hjk committed
65
    "[reason=\"breakpoint-hit\",bkptno=\"1\",thread-id=\"1\","
66
67
68
    "frame={addr=\"0x0000000000405738\",func=\"main\","
    "args=[{name=\"argc\",value=\"1\"},{name=\"argv\",value=\"0x7fff1ac78f28\"}],"
    "file=\"test1.cpp\",fullname=\"/home/apoenitz/work/test1/test1.cpp\","
hjk's avatar
hjk committed
69
    "line=\"209\"}]";
70

71
static const char gdbmi8[] =
hjk's avatar
hjk committed
72
    "[data={locals={{name=\"a\"},{name=\"w\"}}}]";
73

74
static const char gdbmi9[] =
hjk's avatar
hjk committed
75
    "[data={locals=[name=\"baz\",name=\"urgs\",name=\"purgs\"]}]";
76

77
static const char gdbmi10[] =
hjk's avatar
hjk committed
78
    "[name=\"urgs\",numchild=\"1\",type=\"Urgs\"]";
79

80
static const char gdbmi11[] =
81
82
83
    "[{name=\"size\",value=\"1\",type=\"size_t\",readonly=\"true\"},"
     "{name=\"0\",value=\"one\",type=\"QByteArray\"}]";

84
static const char gdbmi12[] =
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
85
    "[{iname=\"local.hallo\",value=\"\\\"\\\\\\00382\\t\\377\",type=\"QByteArray\","
hjk's avatar
hjk committed
86
     "numchild=\"0\"}]";
87

88

hjk's avatar
hjk committed
89
90
91
92
93
94
95
96
97
98
struct Int3 {
    Int3() { i1 = 42; i2 = 43; i3 = 44; }
    int i1, i2, i3;
};

struct QString3 {
    QString3() { s1 = "a"; s2 = "b"; s3 = "c"; }
    QString s1, s2, s3;
};

99
100
101
102
103
104
105
106
107
108
109
110
111
112
class tst_Debugger : public QObject
{
    Q_OBJECT

public:
    tst_Debugger() {}

    void testMi(const char* input)
    {
        QCOMPARE('\n' + QString::fromLatin1(GdbMi(input).toString(false)),
            '\n' + QString(input));
    }

private slots:
113
114
115
116
117
118
119
120
121
122
123
    void mi1()  { testMi(gdbmi1);  }
    void mi2()  { testMi(gdbmi2);  }
    void mi3()  { testMi(gdbmi3);  }
    //void mi4()  { testMi(gdbmi4);  }
    void mi5()  { testMi(gdbmi5);  }
    void mi8()  { testMi(gdbmi8);  }
    void mi9()  { testMi(gdbmi9);  }
    void mi10() { testMi(gdbmi10); }
    void mi11() { testMi(gdbmi11); }
    //void mi12() { testMi(gdbmi12); }

124
    void infoBreak();
125
126
    void niceType();
    void niceType_data();
127

128
    void dumperCompatibility();
129
    void dumpQAbstractItemAndModelIndex();
ck's avatar
ck committed
130
131
    void dumpQAbstractItemModel();
    void dumpQByteArray();
ck's avatar
ck committed
132
    void dumpQChar();
ck's avatar
ck committed
133
    void dumpQDateTime();
ck's avatar
ck committed
134
    void dumpQDir();
ck's avatar
ck committed
135
    void dumpQFile();
ck's avatar
ck committed
136
    void dumpQFileInfo();
137
    void dumpQHash();
ck's avatar
ck committed
138
    void dumpQHashNode();
ck's avatar
ck committed
139
140
    void dumpQImage();
    void dumpQImageData();
141
    void dumpQLinkedList();
142
143
144
145
146
    void dumpQList_int();
    void dumpQList_char();
    void dumpQList_QString();
    void dumpQList_QString3();
    void dumpQList_Int3();
ck's avatar
ck committed
147
    void dumpQLocale();
ck's avatar
ck committed
148
    void dumpQMap();
149
    void dumpQMapNode();
150
    void dumpQObject();
151
    void dumpQObjectChildList();
ck's avatar
ck committed
152
153
    void dumpQObjectMethodList();
    void dumpQObjectPropertyList();
ck's avatar
ck committed
154
    void dumpQObjectSignal();
155
    void dumpQObjectSignalList();
ck's avatar
ck committed
156
157
    void dumpQObjectSlot();
    void dumpQObjectSlotList();
ck's avatar
ck committed
158
    void dumpQPixmap();
ck's avatar
ck committed
159
    void dumpQSharedPointer();
160
    void dumpQString();
ck's avatar
ck committed
161
    void dumpQTextCodec();
hjk's avatar
hjk committed
162
163
164
    void dumpQVariant_invalid();
    void dumpQVariant_QString();
    void dumpQVariant_QStringList();
165
    void dumpStdVector();
166
    void dumpQWeakPointer();
167
    void initTestCase();
168

hjk's avatar
hjk committed
169
private:
ck's avatar
ck committed
170
    void dumpQAbstractItemHelper(QModelIndex &index);
ck's avatar
ck committed
171
    void dumpQAbstractItemModelHelper(QAbstractItemModel &m);
172
    void dumpQDateTimeHelper(const QDateTime &d);
ck's avatar
ck committed
173
    void dumpQFileHelper(const QString &name, bool exists);
ck's avatar
ck committed
174
    template <typename K, typename V> void dumpQHashNodeHelper(QHash<K, V> &hash);
175
    void dumpQImageHelper(const QImage &img);
ck's avatar
ck committed
176
    void dumpQImageDataHelper(QImage &img);
177
    template <typename T> void dumpQLinkedListHelper(QLinkedList<T> &l);
ck's avatar
ck committed
178
    void dumpQLocaleHelper(QLocale &loc);
ck's avatar
ck committed
179
    template <typename K, typename V> void dumpQMapHelper(QMap<K, V> &m);
180
181
    template <typename K, typename V> void dumpQMapNodeHelper(QMap<K, V> &m);
    void dumpQObjectChildListHelper(QObject &o);
ck's avatar
ck committed
182
    void dumpQObjectSignalHelper(QObject &o, int sigNum);
ck's avatar
ck committed
183
184
#if QT_VERSION >= 0x040500
    template <typename T>
185
    void dumpQSharedPointerHelper(QSharedPointer<T> &ptr);
186
187
    template <typename T>
    void dumpQWeakPointerHelper(QWeakPointer<T> &ptr);
ck's avatar
ck committed
188
#endif
ck's avatar
ck committed
189
    void dumpQTextCodecHelper(QTextCodec *codec);
190
191
};

192
193
void tst_Debugger::infoBreak()
{
ck's avatar
ck committed
194
    // This tests the regular expression used in GdbEngine::extractDataFromInfoBreak
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
    // to discover breakpoints in constructors.

    // Copied from gdbengine.cpp:

    QRegExp re("MULTIPLE.*(0x[0-9a-f]+) in (.*)\\s+at (.*):([\\d]+)([^\\d]|$)");
    re.setMinimal(true);

    QCOMPARE(re.indexIn(
        "2       breakpoint     keep y   <MULTIPLE> 0x0040168e\n"
        "2.1                         y     0x0040168e "
            "in MainWindow::MainWindow(QWidget*) at mainwindow.cpp:7\n"
        "2.2                         y     0x00401792 "
            "in MainWindow::MainWindow(QWidget*) at mainwindow.cpp:7\n"), 33);
    QCOMPARE(re.cap(1), QString("0x0040168e"));
    QCOMPARE(re.cap(2).trimmed(), QString("MainWindow::MainWindow(QWidget*)"));
    QCOMPARE(re.cap(3), QString("mainwindow.cpp"));
    QCOMPARE(re.cap(4), QString("7"));


    QCOMPARE(re.indexIn(
        "Num     Type           Disp Enb Address            What"
        "4       breakpoint     keep y   <MULTIPLE>         0x00000000004066ad"
        "4.1                         y     0x00000000004066ad in CTorTester"
        " at /main/tests/manual/gdbdebugger/simple/app.cpp:124"), 88);

    QCOMPARE(re.cap(1), QString("0x00000000004066ad"));
    QCOMPARE(re.cap(2).trimmed(), QString("CTorTester"));
    QCOMPARE(re.cap(3), QString("/main/tests/manual/gdbdebugger/simple/app.cpp"));
    QCOMPARE(re.cap(4), QString("124"));
}

226
227
228
229
//
// type simplification
//

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
static QString chopConst(QString type)
{
   while (1) {
        if (type.startsWith("const"))
            type = type.mid(5);
        else if (type.startsWith(' '))
            type = type.mid(1);
        else if (type.endsWith("const"))
            type.chop(5);
        else if (type.endsWith(' '))
            type.chop(1);
        else
            break;
    }
    return type;
}

QString niceType(QString type)
{
    type.replace('*', '@');

    for (int i = 0; i < 10; ++i) {
        int start = type.indexOf("std::allocator<");
        if (start == -1)
ck's avatar
ck committed
254
            break;
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
        // search for matching '>'
        int pos;
        int level = 0;
        for (pos = start + 12; pos < type.size(); ++pos) {
            int c = type.at(pos).unicode();
            if (c == '<') {
                ++level;
            } else if (c == '>') {
                --level;
                if (level == 0)
                    break;
            }
        }
        QString alloc = type.mid(start, pos + 1 - start).trimmed();
        QString inner = alloc.mid(15, alloc.size() - 16).trimmed();
        //qDebug() << "MATCH: " << pos << alloc << inner;

        if (inner == QLatin1String("char"))
            // std::string
            type.replace(QLatin1String("basic_string<char, std::char_traits<char>, "
                "std::allocator<char> >"), QLatin1String("string"));
        else if (inner == QLatin1String("wchar_t"))
            // std::wstring
            type.replace(QLatin1String("basic_string<wchar_t, std::char_traits<wchar_t>, "
                "std::allocator<wchar_t> >"), QLatin1String("wstring"));

        // std::vector, std::deque, std::list
        QRegExp re1(QString("(vector|list|deque)<%1, %2\\s*>").arg(inner, alloc));
        if (re1.indexIn(type) != -1)
            type.replace(re1.cap(0), QString("%1<%2>").arg(re1.cap(1), inner));


        // std::stack
        QRegExp re6(QString("stack<%1, std::deque<%2> >").arg(inner, inner));
        re6.setMinimal(true);
        if (re6.indexIn(type) != -1)
            type.replace(re6.cap(0), QString("stack<%1>").arg(inner));

        // std::set
        QRegExp re4(QString("set<%1, std::less<%2>, %3\\s*>").arg(inner, inner, alloc));
        re4.setMinimal(true);
        if (re4.indexIn(type) != -1)
            type.replace(re4.cap(0), QString("set<%1>").arg(inner));


        // std::map
        if (inner.startsWith("std::pair<")) {
            // search for outermost ','
            int pos;
            int level = 0;
            for (pos = 10; pos < inner.size(); ++pos) {
                int c = inner.at(pos).unicode();
                if (c == '<')
                    ++level;
                else if (c == '>')
                    --level;
                else if (c == ',' && level == 0)
                    break;
            }
            QString ckey = inner.mid(10, pos - 10);
            QString key = chopConst(ckey);
            QString value = inner.mid(pos + 2, inner.size() - 3 - pos);

            QRegExp re5(QString("map<%1, %2, std::less<%3>, %4\\s*>")
                .arg(key, value, key, alloc));
            re5.setMinimal(true);
            if (re5.indexIn(type) != -1)
                type.replace(re5.cap(0), QString("map<%1, %2>").arg(key, value));
            else {
                QRegExp re7(QString("map<const %1, %2, std::less<const %3>, %4\\s*>")
                    .arg(key, value, key, alloc));
                re7.setMinimal(true);
                if (re7.indexIn(type) != -1)
                    type.replace(re7.cap(0), QString("map<const %1, %2>").arg(key, value));
            }
        }
    }
    type.replace('@', '*');
    type.replace(QLatin1String(" >"), QString(QLatin1Char('>')));
    return type;
}

void tst_Debugger::niceType()
{
    // cf. watchutils.cpp
    QFETCH(QString, input);
    QFETCH(QString, simplified);
    QCOMPARE(::niceType(input), simplified);
}

void tst_Debugger::niceType_data()
{
    QTest::addColumn<QString>("input");
    QTest::addColumn<QString>("simplified");

    QTest::newRow("list")
        << "std::list<int, std::allocator<int> >"
        << "std::list<int>";

    QTest::newRow("combined")
        << "std::vector<std::list<int, std::allocator<int> >*, "
           "std::allocator<std::list<int, std::allocator<int> >*> >"
        << "std::vector<std::list<int>*>";

    QTest::newRow("stack")
        << "std::stack<int, std::deque<int, std::allocator<int> > >"
        << "std::stack<int>";
ck's avatar
ck committed
362

363
364
365
366
367
368
369
370
371
372
373
    QTest::newRow("map")
        << "std::map<myns::QString, Foo, std::less<myns::QString>, "
           "std::allocator<std::pair<const myns::QString, Foo> > >"
        << "std::map<myns::QString, Foo>";

    QTest::newRow("map2")
        << "std::map<const char*, Foo, std::less<const char*>, "
           "std::allocator<std::pair<const char* const, Foo> > >"
        << "std::map<const char*, Foo>";
}

374
375
376
377
//
// Dumpers
//

378
static void testDumper(QByteArray expected0, const void *data, QByteArray outertype,
379
380
    bool dumpChildren, QByteArray innertype = "", QByteArray exp = "",
    int extraInt0 = 0, int extraInt1 = 0, int extraInt2 = 0, int extraInt3 = 0)
ck's avatar
ck committed
381
382
{
    sprintf(xDumpInBuffer, "%s%c%s%c%s%c%s%c%s%c",
383
384
        outertype.data(), 0, "iname", 0, exp.data(), 0,
        innertype.data(), 0, "iname", 0);
385
    //qDebug() << "FIXME qDumpObjectData440 signature to use const void *";
386
    void *res = qDumpObjectData440(2, 42, data, dumpChildren,
387
388
389
390
        extraInt0, extraInt1, extraInt2, extraInt3);
    QString expected(expected0);
    char buf[100];
    sprintf(buf, "%p", data);
391
392
    if ((!expected.startsWith('t') && !expected.startsWith('f'))
            || expected.startsWith("type"))
393
394
395
396
397
398
399
400
401
402
403
404
405
        expected = "tiname='$I',addr='$A'," + expected;
    expected.replace("$I", "iname");
    expected.replace("$T", QByteArray(outertype));
    expected.replace("$A", QByteArray(buf));
    expected.replace('\'', '"');
    QString actual____ = QString::fromLatin1(xDumpOutBuffer);
    actual____.replace('\'', '"');
    QCOMPARE(res, xDumpOutBuffer);
    if (actual____ != expected) {
        QStringList l1 = actual____.split(",");
        QStringList l2 = expected.split(",");
        for (int i = 0; i < l1.size() && i < l2.size(); ++i) {
            if (l1.at(i) == l2.at(i))
406
407
408
409
                qWarning() << "== " << l1.at(i);
            else 
                //qWarning() << "!= " << l1.at(i).right(30) << l2.at(i).right(30);
                qWarning() << "!= " << l1.at(i) << l2.at(i);
410
411
        }
        if (l1.size() != l2.size())
412
            qWarning() << "!= size: " << l1.size() << l2.size();
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
    }
    QCOMPARE(actual____, expected);
}

QByteArray str(const void *p)
{
    char buf[100];
    sprintf(buf, "%p", p);
    return buf;
}

static const void *deref(const void *p)
{
    return *reinterpret_cast<const char* const*>(p);
}

void tst_Debugger::dumperCompatibility()
{
431
432
433
    // Ensure that no arbitrary padding is introduced by QVectorTypedData.
    const size_t qVectorDataSize = 16;
    QCOMPARE(sizeof(QVectorData), qVectorDataSize);
hjk's avatar
hjk committed
434
435
    QVectorTypedData<int> *v = 0;
    QCOMPARE(size_t(&v->array), qVectorDataSize);
436
437
}

ck's avatar
ck committed
438
439
440
441
442
443
444
445
446
447
static const QByteArray utfToBase64(const QString &string)
{
    return QByteArray(reinterpret_cast<const char *>(string.utf16()), 2 * string.size()).toBase64();
}

static const char *boolToVal(bool b)
{
    return b ? "'true'" : "'false'";
}

ck's avatar
ck committed
448
static const QByteArray ptrToBa(const void *p, bool symbolicNull = true)
ck's avatar
ck committed
449
{
ck's avatar
ck committed
450
    return QByteArray().append(p == 0 && symbolicNull ?
ck's avatar
ck committed
451
        "<null>" :
452
        QByteArray("0x") + QByteArray::number((quintptr) p, 16));
ck's avatar
ck committed
453
454
}

455
static const QByteArray generateQStringSpec(const QString &str)
ck's avatar
ck committed
456
{
457
458
    return QByteArray("value='%',type='"NS"QString',numchild='0',valueencoded='2'")
        << utfToBase64(str);
ck's avatar
ck committed
459
460
}

ck's avatar
ck committed
461
462
static const QByteArray generateQCharSpec(const QChar& ch)
{
463
464
465
    return QByteArray("value='%',valueencoded='2',type='"NS"QChar',numchild='0'")
        << utfToBase64(QString(QLatin1String("'%1' (%2, 0x%3)")).
                   arg(ch).arg(ch.unicode()).arg(ch.unicode(), 0, 16));
ck's avatar
ck committed
466
467
}

ck's avatar
ck committed
468
469
static const QByteArray generateBoolSpec(bool b)
{
470
471
    return QByteArray("value=%,type='bool',numchild='0'")
        << boolToVal(b);
ck's avatar
ck committed
472
473
}

ck's avatar
ck committed
474
475
static const QByteArray generateLongSpec(long n)
{
476
477
    return QByteArray("value='%',type='long',numchild='0'")
        << N(qlonglong(n));
ck's avatar
ck committed
478
479
480
481
}

static const QByteArray generateIntSpec(int n)
{
482
483
    return QByteArray("value='%',type='int',numchild='0'")
        << N(n);
ck's avatar
ck committed
484
485
}

486
487
const QByteArray createExp(const void *ptr,
    const QByteArray &type, const QByteArray &method)
ck's avatar
ck committed
488
{
489
490
    return QByteArray("exp='(("NSX"%"NSY"*)%)->%'")
        << type << ptrToBa(ptr) << method;
ck's avatar
ck committed
491
492
}

ck's avatar
ck committed
493
494
495
496
497
498
499
500
// Helper functions.

#ifdef Q_CC_MSVC
#  define MAP_NODE_TYPE_END ">"
#else
#  define MAP_NODE_TYPE_END " >"
#endif

501
template <typename T> static const char *typeToString()
ck's avatar
ck committed
502
503
504
505
506
507
508
509
510
{
    return "<unknown type>";
}
template <typename T> const QByteArray valToString(const T &)
{
    return "<unknown value>";
}
template <> const QByteArray valToString(const int &n)
{
511
    return QByteArray().append(N(n));
ck's avatar
ck committed
512
513
514
515
516
}
template <> const QByteArray valToString(const QString &s)
{
    return QByteArray(utfToBase64(s)).append("',valueencoded='2");
}
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
template <> const QByteArray valToString(int * const &p)
{
    return ptrToBa(p);
}
template <typename T> const QByteArray derefValToString(const T &v)
{
    return valToString(v);
}
template <> const QByteArray derefValToString(int * const &ptr)
{
    return valToString(*ptr);
}
const QString stripPtrType(const QString &ptrTypeStr)
{
    return ptrTypeStr.mid(0, ptrTypeStr.size() - 2);
}
template <> const char *typeToString<int>()
ck's avatar
ck committed
534
535
536
{
    return "int";
}
537
template <> const char *typeToString<QString>()
ck's avatar
ck committed
538
539
540
{
    return NS"QString";
}
541
542
543
544
545
template <> const char *typeToString<int *>()
{
    return "int *";
}
template <typename T> bool isSimpleType()
ck's avatar
ck committed
546
547
548
{
    return false;
}
549
template <> bool isSimpleType<int>()
ck's avatar
ck committed
550
551
552
{
    return true;
}
553
554
555
556
557
558
559
560
561
562
template <typename T> bool isPointer()
{
    return false;
}
template <> bool isPointer<int *>()
{
    return true;
}

template <typename T> static const char *typeToNumchild()
ck's avatar
ck committed
563
564
565
{
    return "1";
}
566
template <> const char *typeToNumchild<int>()
ck's avatar
ck committed
567
568
569
{
    return "0";
}
570
template <> const char *typeToNumchild<QString>()
ck's avatar
ck committed
571
572
573
{
    return "0";
}
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
template <typename K, typename V>
QByteArray getMapType()
{
    return QByteArray(typeToString<K>()) + "@" + QByteArray(typeToString<V>());
}
template <typename K, typename V>
void getMapNodeParams(size_t &nodeSize, size_t &valOffset)
{
#if QT_VERSION >= 0x040500
    typedef QMapNode<K, V> NodeType;
    NodeType *node = 0;
    nodeSize = sizeof(NodeType);
    valOffset = size_t(&node->value);
#else
    nodeSize = sizeof(K) + sizeof(V) + 2*sizeof(void *);
    valOffset = sizeof(K);
#endif
}
ck's avatar
ck committed
592
593
594
595

void tst_Debugger::dumpQAbstractItemHelper(QModelIndex &index)
{
    const QAbstractItemModel *model = index.model();
596
597
    const QString &rowStr = N(index.row());
    const QString &colStr = N(index.column());
ck's avatar
ck committed
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
    const QByteArray &internalPtrStrSymbolic = ptrToBa(index.internalPointer());
    const QByteArray &internalPtrStrValue = ptrToBa(index.internalPointer(), false);
    const QByteArray &modelPtrStr = ptrToBa(model);
    QByteArray indexSpecSymbolic = QByteArray().append(rowStr + "," + colStr + ",").
        append(internalPtrStrSymbolic + "," + modelPtrStr);
    QByteArray indexSpecValue = QByteArray().append(rowStr + "," + colStr + ",").
        append(internalPtrStrValue + "," + modelPtrStr);
    QByteArray expected = QByteArray("tiname='iname',addr='").append(ptrToBa(&index)).
        append("',type='"NS"QAbstractItem',addr='$").append(indexSpecSymbolic).
        append("',value='").append(valToString(model->data(index).toString())).
        append("',numchild='1',children=[");
    int rowCount = model->rowCount(index);
    int columnCount = model->columnCount(index);
    for (int row = 0; row < rowCount; ++row) {
        for (int col = 0; col < columnCount; ++col) {
            const QModelIndex &childIndex = model->index(row, col, index);
            expected.append("{name='[").append(valToString(row)).append(",").
615
616
617
                append(N(col)).append("]',numchild='1',addr='$").
                append(N(childIndex.row())).append(",").
                append(N(childIndex.column())).append(",").
ck's avatar
ck committed
618
619
620
621
622
623
624
625
626
627
628
                append(ptrToBa(childIndex.internalPointer())).append(",").
                append(modelPtrStr).append("',type='"NS"QAbstractItem',value='").
                append(valToString(model->data(childIndex).toString())).append("'}");
            if (col < columnCount - 1 || row < rowCount - 1)
                expected.append(",");
        }
    }
    expected.append("]");
    testDumper(expected, &index, NS"QAbstractItem", true, indexSpecValue);
}

629
void tst_Debugger::dumpQAbstractItemAndModelIndex()
ck's avatar
ck committed
630
631
632
633
634
635
636
637
{
    class PseudoTreeItemModel : public QAbstractItemModel
    {
    public:
        PseudoTreeItemModel() : QAbstractItemModel(), parent1(0),
            parent1Child(1), parent2(10), parent2Child1(11), parent2Child2(12)
        {}

hjk's avatar
hjk committed
638
        int columnCount(const QModelIndex &parent = QModelIndex()) const
ck's avatar
ck committed
639
        {
hjk's avatar
hjk committed
640
            Q_UNUSED(parent);
ck's avatar
ck committed
641
642
643
644
645
646
647
648
649
            return 1;
        }

        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
        {
            return !index.isValid() || role != Qt::DisplayRole ?
                    QVariant() : *static_cast<int *>(index.internalPointer());
        }

hjk's avatar
hjk committed
650
        QModelIndex index(int row, int column,
ck's avatar
ck committed
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
                          const QModelIndex & parent = QModelIndex()) const
        {
            QModelIndex index;
            if (column == 0) {
                if (!parent.isValid()) {
                    if (row == 0)
                        index = createIndex(row, column, &parent1);
                    else if (row == 1)
                        index = createIndex(row, column, &parent2);
                } else if (parent.internalPointer() == &parent1 && row == 0) {
                    index = createIndex(row, column, &parent1Child);
                } else if (parent.internalPointer() == &parent2) {
                    index = createIndex(row, column,
                                row == 0 ? &parent2Child1 : &parent2Child2);
                }
            }
            return index;
        }

hjk's avatar
hjk committed
670
        QModelIndex parent(const QModelIndex & index) const
ck's avatar
ck committed
671
672
673
674
675
676
677
678
679
680
681
682
        {
            QModelIndex parent;
            if (index.isValid()) {
                if (index.internalPointer() == &parent1Child)
                    parent = createIndex(0, 0, &parent1);
                else if (index.internalPointer() == &parent2Child1 ||
                         index.internalPointer() == &parent2Child2)
                    parent = createIndex(1, 0, &parent2);
            }
            return parent;
        }

hjk's avatar
hjk committed
683
        int rowCount(const QModelIndex &parent = QModelIndex()) const
ck's avatar
ck committed
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
        {
            int rowCount;
            if (!parent.isValid() || parent.internalPointer() == &parent2)
                rowCount = 2;
            else if (parent.internalPointer() == &parent1)
                rowCount = 1;
            else
                rowCount = 0;
            return rowCount;
        }

    private:
        mutable int parent1;
        mutable int parent1Child;
        mutable int parent2;
        mutable int parent2Child1;
        mutable int parent2Child2;
hjk's avatar
hjk committed
701
702
703
    };

    PseudoTreeItemModel m2;
ck's avatar
ck committed
704

705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
    // Case 1: ModelIndex with no children.
    QStringListModel m(QStringList() << "item1" << "item2" << "item3");
    QModelIndex index = m.index(2, 0);

    testDumper(QByteArray("type='$T',value='(2, 0)',numchild='5',children=["
        "{name='row',value='2',type='int',numchild='0'},"
        "{name='column',value='0',type='int',numchild='0'},"
        "{name='parent',value='<invalid>',exp='(('$T'*)$A)->parent()',"
            "type='$T',numchild='1'},"
        "{name='internalId',%},"
        "{name='model',value='%',type='"NS"QAbstractItemModel*',"
            "numchild='1'}]")
         << generateQStringSpec(N(index.internalId()))
         << ptrToBa(&m),
        &index, NS"QModelIndex", true);

ck's avatar
ck committed
721
722
723
    // Case 2: ModelIndex with one child.
    QModelIndex index2 = m2.index(0, 0);
    dumpQAbstractItemHelper(index2);
724
725
726
727
728
729
730
731
732
733
734
735
736
737

    qDebug() << "FIXME: invalid indices should not have children";
    testDumper(QByteArray("type='$T',value='(0, 0)',numchild='5',children=["
        "{name='row',value='0',type='int',numchild='0'},"
        "{name='column',value='0',type='int',numchild='0'},"
        "{name='parent',value='<invalid>',exp='(('$T'*)$A)->parent()',"
            "type='$T',numchild='1'},"
        "{name='internalId',%},"
        "{name='model',value='%',type='"NS"QAbstractItemModel*',"
            "numchild='1'}]")
         << generateQStringSpec(N(index2.internalId()))
         << ptrToBa(&m2),
        &index2, NS"QModelIndex", true);

ck's avatar
ck committed
738
739
740
741

    // Case 3: ModelIndex with two children.
    QModelIndex index3 = m2.index(1, 0);
    dumpQAbstractItemHelper(index3);
742
743
744
745
746
747
748
749
750
751
752
753
754

    testDumper(QByteArray("type='$T',value='(1, 0)',numchild='5',children=["
        "{name='row',value='1',type='int',numchild='0'},"
        "{name='column',value='0',type='int',numchild='0'},"
        "{name='parent',value='<invalid>',exp='(('$T'*)$A)->parent()',"
            "type='$T',numchild='1'},"
        "{name='internalId',%},"
        "{name='model',value='%',type='"NS"QAbstractItemModel*',"
            "numchild='1'}]")
         << generateQStringSpec(N(index3.internalId()))
         << ptrToBa(&m2),
        &index3, NS"QModelIndex", true);

755
756
757

    // Case 4: ModelIndex with a parent.
    index = m2.index(0, 0, index3);
758
759
760
761
762
763
764
765
766
767
768
769
    testDumper(QByteArray("type='$T',value='(0, 0)',numchild='5',children=["
        "{name='row',value='0',type='int',numchild='0'},"
        "{name='column',value='0',type='int',numchild='0'},"
        "{name='parent',value='(1, 0)',exp='(('$T'*)$A)->parent()',"
            "type='$T',numchild='1'},"
        "{name='internalId',%},"
        "{name='model',value='%',type='"NS"QAbstractItemModel*',"
            "numchild='1'}]")
         << generateQStringSpec(N(index.internalId()))
         << ptrToBa(&m2),
        &index, NS"QModelIndex", true);

770
771
772

    // Case 5: Empty ModelIndex
    QModelIndex index4;
773
774
    testDumper("type='$T',value='<invalid>',numchild='0'",
        &index4, NS"QModelIndex", true);
ck's avatar
ck committed
775
776
}

ck's avatar
ck committed
777
void tst_Debugger::dumpQAbstractItemModelHelper(QAbstractItemModel &m)
ck's avatar
ck committed
778
{
779
780
781
782
783
784
785
786
787
788
789
790
    QByteArray address = ptrToBa(&m);
    QByteArray expected = QByteArray("tiname='iname',addr='%',"
        "type='"NS"QAbstractItemModel',value='(%,%)',numchild='1',children=["
        "{numchild='1',name='"NS"QObject',addr='%',value='%',"
        "valueencoded='2',type='"NS"QObject',displayedtype='%'}")
            << address
            << N(m.rowCount())
            << N(m.columnCount())
            << address
            << utfToBase64(m.objectName())
            << m.metaObject()->className();

ck's avatar
ck committed
791
792
793
    for (int row = 0; row < m.rowCount(); ++row) {
        for (int column = 0; column < m.columnCount(); ++column) {
            QModelIndex mi = m.index(row, column);
794
795
796
797
798
799
800
801
802
803
            expected.append(QByteArray(",{name='[%,%]',value='%',"
                "valueencoded='2',numchild='1',addr='$%,%,%,%',"
                "type='"NS"QAbstractItem'}")
                << N(row)
                << N(column)
                << utfToBase64(m.data(mi).toString())
                << N(mi.row())
                << N(mi.column())
                << ptrToBa(mi.internalPointer())
                << ptrToBa(mi.model()));
ck's avatar
ck committed
804
805
806
807
808
809
        }
    }
    expected.append("]");
    testDumper(expected, &m, NS"QAbstractItemModel", true);
}

ck's avatar
ck committed
810
void tst_Debugger::dumpQAbstractItemModel()
ck's avatar
ck committed
811
812
813
814
{
    // Case 1: No rows, one column.
    QStringList strList;
    QStringListModel model(strList);
ck's avatar
ck committed
815
    dumpQAbstractItemModelHelper(model);
ck's avatar
ck committed
816
817
818
819

    // Case 2: One row, one column.
    strList << "String 1";
    model.setStringList(strList);
ck's avatar
ck committed
820
    dumpQAbstractItemModelHelper(model);
ck's avatar
ck committed
821
822
823
824

    // Case 3: Two rows, one column.
    strList << "String 2";
    model.setStringList(strList);
ck's avatar
ck committed
825
    dumpQAbstractItemModelHelper(model);
ck's avatar
ck committed
826
827
828

    // Case 4: No rows, two columns.
    QStandardItemModel model2(0, 2);
ck's avatar
ck committed
829
    dumpQAbstractItemModelHelper(model2);
ck's avatar
ck committed
830
831
832
833
834

    // Case 5: One row, two columns.
    QStandardItem item1("Item (0,0)");
    QStandardItem item2("(Item (0,1)");
    model2.appendRow(QList<QStandardItem *>() << &item1 << &item2);
ck's avatar
ck committed
835
    dumpQAbstractItemModelHelper(model2);
ck's avatar
ck committed
836
837
838
839
840

    // Case 6: Two rows, two columns
    QStandardItem item3("Item (1,0");
    QStandardItem item4("Item (1,1)");
    model2.appendRow(QList<QStandardItem *>() << &item3 << &item4);
ck's avatar
ck committed
841
842
843
844
845
846
847
    dumpQAbstractItemModelHelper(model);
}

void tst_Debugger::dumpQByteArray()
{
    // Case 1: Empty object.
    QByteArray ba;
848
849
850
    testDumper("value='',valueencoded='1',type='"NS"QByteArray',numchild='0',"
            "childtype='char',childnumchild='0',children=[]",
        &ba, NS"QByteArray", true);
ck's avatar
ck committed
851
852
853

    // Case 2: One element.
    ba.append('a');
854
855
856
    testDumper("value='YQ==',valueencoded='1',type='"NS"QByteArray',numchild='1',"
            "childtype='char',childnumchild='0',children=[{value='61  (97 'a')'}]",
        &ba, NS"QByteArray", true);
ck's avatar
ck committed
857
858
859

    // Case 3: Two elements.
    ba.append('b');
860
861
862
863
    testDumper("value='YWI=',valueencoded='1',type='"NS"QByteArray',numchild='2',"
            "childtype='char',childnumchild='0',children=["
            "{value='61  (97 'a')'},{value='62  (98 'b')'}]",
        &ba, NS"QByteArray", true);
ck's avatar
ck committed
864
865
866

    // Case 4: > 100 elements.
    ba = QByteArray(101, 'a');
867
868
869
870
871
872
873
874
875
876
    QByteArray children;
    for (int i = 0; i < 101; i++)
        children.append("{value='61  (97 'a')'},");
    children.chop(1);
    testDumper(QByteArray("value='YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh"
            "YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh"
            "YWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ== <size: 101, cut...>',"
            "valueencoded='1',type='"NS"QByteArray',numchild='101',"
            "childtype='char',childnumchild='0',children=[%]") << children,
        &ba, NS"QByteArray", true);
ck's avatar
ck committed
877
878
879

    // Case 5: Regular and special characters and the replacement character.
    ba = QByteArray("abc\a\n\r\e\'\"?");
880
881
882
883
884
885
886
887
    testDumper("value='YWJjBwoNGyciPw==',valueencoded='1',type='"NS"QByteArray',"
            "numchild='10',childtype='char',childnumchild='0',children=["
            "{value='61  (97 'a')'},{value='62  (98 'b')'},"
            "{value='63  (99 'c')'},{value='07  (7 '?')'},"
            "{value='0a  (10 '?')'},{value='0d  (13 '?')'},"
            "{value='1b  (27 '?')'},{value='27  (39 '?')'},"
            "{value='22  (34 '?')'},{value='3f  (63 '?')'}]",
        &ba, NS"QByteArray", true);
ck's avatar
ck committed
888
889
890
891
892
893
}

void tst_Debugger::dumpQChar()
{
    // Case 1: Printable ASCII character.
    QChar c('X');
894
895
    testDumper("value=''X', ucs=88',numchild='0'",
        &c, NS"QChar", false);
ck's avatar
ck committed
896
897
898

    // Case 2: Printable non-ASCII character.
    c = QChar(0x600);
899
900
    testDumper("value=''?', ucs=1536',numchild='0'",
        &c, NS"QChar", false);
ck's avatar
ck committed
901
902
903

    // Case 3: Non-printable ASCII character.
    c = QChar::fromAscii('\a');
904
905
    testDumper("value=''?', ucs=7',numchild='0'",
        &c, NS"QChar", false);
ck's avatar
ck committed
906
907
908

    // Case 4: Non-printable non-ASCII character.
    c = QChar(0x9f);
909
910
    testDumper("value=''?', ucs=159',numchild='0'",
        &c, NS"QChar", false);
ck's avatar
ck committed
911
912
913

    // Case 5: Printable ASCII Character that looks like the replacement character.
    c = QChar::fromAscii('?');
914
915
    testDumper("value=''?', ucs=63',numchild='0'",
        &c, NS"QChar", false);
ck's avatar
ck committed
916
917
}

918
void tst_Debugger::dumpQDateTimeHelper(const QDateTime &d)
ck's avatar
ck committed
919
{
920
    QByteArray value;
ck's avatar
ck committed
921
    if (d.isNull())
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
        value = "value='(null)'";
    else 
        value = QByteArray("value='%',valueencoded='2'")
            << utfToBase64(d.toString());

    QByteArray expected = QByteArray("%,type='$T',numchild='3',children=["
        "{name='isNull',%},"
        "{name='toTime_t',%},"
        "{name='toString',%},"
        "{name='toString_(ISO)',%},"
        "{name='toString_(SystemLocale)',%},"
        "{name='toString_(Locale)',%}]")
            << value
            << generateBoolSpec(d.isNull())
            << generateLongSpec((d.toTime_t()))
            << generateQStringSpec(d.toString())
            << generateQStringSpec(d.toString(Qt::ISODate))
            << generateQStringSpec(d.toString(Qt::SystemLocaleDate))
            << generateQStringSpec(d.toString(Qt::LocaleDate));
ck's avatar
ck committed
941
942
943
944
945
946
947
948
949
950
951
952
953
954
    testDumper(expected, &d, NS"QDateTime", true);
}

void tst_Debugger::dumpQDateTime()
{
    // Case 1: Null object.
    QDateTime d;
    dumpQDateTimeHelper(d);

    // Case 2: Non-null object.
    d = QDateTime::currentDateTime();
    dumpQDateTimeHelper(d);
}

ck's avatar
ck committed
955
956
957
958
void tst_Debugger::dumpQDir()
{
    // Case 1: Current working directory.
    QDir dir = QDir::current();
959
960
961
962
963
964
    testDumper(QByteArray("value='%',valueencoded='2',type='"NS"QDir',numchild='3',"
        "children=[{name='absolutePath',%},{name='canonicalPath',%}]")
            << utfToBase64(dir.absolutePath())
            << generateQStringSpec(dir.absolutePath())
            << generateQStringSpec(dir.canonicalPath()),
        &dir, NS"QDir", true);
ck's avatar
ck committed
965
966
967

    // Case 2: Root directory.
    dir = QDir::root();
968
969
970
971
972
973
    testDumper(QByteArray("value='%',valueencoded='2',type='"NS"QDir',numchild='3',"
        "children=[{name='absolutePath',%},{name='canonicalPath',%}]")
            << utfToBase64(dir.absolutePath())
            << generateQStringSpec(dir.absolutePath())
            << generateQStringSpec(dir.canonicalPath()),
        &dir, NS"QDir", true);
ck's avatar
ck committed
974
975
}

ck's avatar
ck committed
976
977
978
void tst_Debugger::dumpQFileHelper(const QString &name, bool exists)
{
    QFile file(name);
ck's avatar
ck committed
979
    QByteArray filenameAsBase64 = utfToBase64(name);
980
981
982
983
984
985
    testDumper(QByteArray("value='%',valueencoded='2',type='$T',numchild='2',"
        "children=[{name='fileName',value='%',type='"NS"QString',"
        "numchild='0',valueencoded='2'},"
        "{name='exists',value=%,type='bool',numchild='0'}]")
            << filenameAsBase64 << filenameAsBase64 << boolToVal(exists),
        &file, NS"QFile", true);
ck's avatar
ck committed
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
}

void tst_Debugger::dumpQFile()
{
    // Case 1: Empty file name => Does not exist.
    dumpQFileHelper("", false);

    // Case 2: File that is known to exist.
    QTemporaryFile file;
    file.open();
    dumpQFileHelper(file.fileName(), true);

    // Case 3: File with a name that most likely does not exist.
    dumpQFileHelper("jfjfdskjdflsdfjfdls", false);
}

ck's avatar
ck committed
1002
1003
1004
void tst_Debugger::dumpQFileInfo()
{
    QFileInfo fi(".");
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
    QByteArray expected("value='%',valueencoded='2',type='$T',numchild='3',"
        "children=["
        "{name='absolutePath',%},"
        "{name='absoluteFilePath',%},"
        "{name='canonicalPath',%},"
        "{name='canonicalFilePath',%},"
        "{name='completeBaseName',%},"
        "{name='completeSuffix',%},"
        "{name='baseName',%},"
#ifdef QX
        "{name='isBundle',%},"
        "{name='bundleName',%},"
#endif
        "{name='fileName',%},"
        "{name='filePath',%},"
        "{name='group',%},"
        "{name='owner',%},"
        "{name='path',%},"
        "{name='groupid',%},"
        "{name='ownerid',%},"
ck's avatar
ck committed
1025
1026
1027
1028
1029
1030
        "{name='permissions',value=' ',type='%',numchild='10',"
        "children=[{name='ReadOwner',%},{name='WriteOwner',%},"
        "{name='ExeOwner',%},{name='ReadUser',%},{name='WriteUser',%},"
        "{name='ExeUser',%},{name='ReadGroup',%},{name='WriteGroup',%},"
        "{name='ExeGroup',%},{name='ReadOther',%},{name='WriteOther',%},"
        "{name='ExeOther',%}]},"
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
        "{name='caching',%},"
        "{name='exists',%},"
        "{name='isAbsolute',%},"
        "{name='isDir',%},"
        "{name='isExecutable',%},"
        "{name='isFile',%},"
        "{name='isHidden',%},"
        "{name='isReadable',%},"
        "{name='isRelative',%},"
        "{name='isRoot',%},"
        "{name='isSymLink',%},"
        "{name='isWritable',%},"
        "{name='created',value='%',valueencoded='2',%,"
            "type='"NS"QDateTime',numchild='1'},"
        "{name='lastModified',value='%',valueencoded='2',%,"
            "type='"NS"QDateTime',numchild='1'},"
        "{name='lastRead',value='%',valueencoded='2',%,"
            "type='"NS"QDateTime',numchild='1'}]");
    expected <<= utfToBase64(fi.filePath());
    expected <<= generateQStringSpec(fi.absolutePath());
    expected <<= generateQStringSpec(fi.absoluteFilePath());
    expected <<= generateQStringSpec(fi.canonicalPath());
    expected <<= generateQStringSpec(fi.canonicalFilePath());
    expected <<= generateQStringSpec(fi.completeBaseName());
    expected <<= generateQStringSpec(fi.completeSuffix());
    expected <<= generateQStringSpec(fi.baseName());
ck's avatar
ck committed
1057
#ifdef Q_OS_MACX
1058
1059
    expected <<= generateBoolSpec(fi.isBundle());
    expected <<= generateQStringSpec(fi.bundleName());
ck's avatar
ck committed
1060
#endif
1061
1062
1063
1064
1065
1066
1067
    expected <<= generateQStringSpec(fi.fileName());
    expected <<= generateQStringSpec(fi.filePath());
    expected <<= generateQStringSpec(fi.group());
    expected <<= generateQStringSpec(fi.owner());
    expected <<= generateQStringSpec(fi.path());
    expected <<= generateLongSpec(fi.groupId());
    expected <<= generateLongSpec(fi.ownerId());
ck's avatar
ck committed
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
    QFile::Permissions perms = fi.permissions();
    expected <<= QByteArray(NS"QFile::Permissions");
    expected <<= generateBoolSpec((perms & QFile::ReadOwner) != 0);
    expected <<= generateBoolSpec((perms & QFile::WriteOwner) != 0);
    expected <<= generateBoolSpec((perms & QFile::ExeOwner) != 0);
    expected <<= generateBoolSpec((perms & QFile::ReadUser) != 0);
    expected <<= generateBoolSpec((perms & QFile::WriteUser) != 0);
    expected <<= generateBoolSpec((perms & QFile::ExeUser) != 0);
    expected <<= generateBoolSpec((perms & QFile::ReadGroup) != 0);
    expected <<= generateBoolSpec((perms & QFile::WriteGroup) != 0);
    expected <<= generateBoolSpec((perms & QFile::ExeGroup) != 0);
    expected <<= generateBoolSpec((perms & QFile::ReadOther) != 0);
    expected <<= generateBoolSpec((perms & QFile::WriteOther) != 0);
    expected <<= generateBoolSpec((perms & QFile::ExeOther) != 0);
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
    expected <<= generateBoolSpec(fi.caching());
    expected <<= generateBoolSpec(fi.exists());
    expected <<= generateBoolSpec(fi.isAbsolute());
    expected <<= generateBoolSpec(fi.isDir());
    expected <<= generateBoolSpec(fi.isExecutable());
    expected <<= generateBoolSpec(fi.isFile());
    expected <<= generateBoolSpec(fi.isHidden());
    expected <<= generateBoolSpec(fi.isReadable());
    expected <<= generateBoolSpec(fi.isRelative());
    expected <<= generateBoolSpec(fi.isRoot());
    expected <<= generateBoolSpec(fi.isSymLink());
    expected <<= generateBoolSpec(fi.isWritable());
    expected <<= utfToBase64(fi.created().toString());
    expected <<= createExp(&fi, "QFileInfo", "created()");
    expected <<= utfToBase64(fi.lastModified().toString());
    expected <<= createExp(&fi, "QFileInfo", "lastModified()");
    expected <<= utfToBase64(fi.lastRead().toString());
    expected <<= createExp(&fi, "QFileInfo", "lastRead()");

ck's avatar
ck committed
1101
    testDumper(expected, &fi, NS"QFileInfo", true);
ck's avatar
ck committed
1102
1103
}

1104
1105
1106
1107
1108
1109
1110
1111
1112
void tst_Debugger::dumpQHash()
{
    QHash<QString, QList<int> > hash;
    hash.insert("Hallo", QList<int>());
    hash.insert("Welt", QList<int>() << 1);
    hash.insert("!", QList<int>() << 1 << 2);
    hash.insert("!", QList<int>() << 1 << 2);
}

ck's avatar
ck committed
1113
template <typename K, typename V>
hjk's avatar
hjk committed
1114
void tst_Debugger::dumpQHashNodeHelper(QHash<K, V> &hash)
ck's avatar
ck committed
1115
1116
1117
{
    typename QHash<K, V>::iterator it = hash.begin();
    typedef QHashNode<K, V> HashNode;
hjk's avatar
hjk committed
1118
    HashNode *dummy = 0;
ck's avatar
ck committed
1119
1120
    HashNode *node =
        reinterpret_cast<HashNode *>(reinterpret_cast<char *>(const_cast<K *>(&it.key())) -
hjk's avatar
hjk committed
1121
            size_t(&dummy->key));
ck's avatar
ck committed
1122
1123
1124
    const K &key = it.key();
    const V &val = it.value();
    QByteArray expected("value='");
1125
    if (isSimpleType<V>())
ck's avatar
ck committed
1126
1127
        expected.append(valToString(val));
    expected.append("',numchild='2',children=[{name='key',type='").
1128
1129
        append(typeToString<K>()).append("',addr='").append(ptrToBa(&key)).
        append("'},{name='value',type='").append(typeToString<V>()).
ck's avatar
ck committed
1130
1131
        append("',addr='").append(ptrToBa(&val)).append("'}]");
    testDumper(expected, node, NS"QHashNode", true,
1132
        getMapType<K, V>(), "", sizeof(it.key()), sizeof(it.value()));
ck's avatar
ck committed
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
}

void tst_Debugger::dumpQHashNode()
{
    // Case 1: simple type -> simple type.
    QHash<int, int> hash1;
    hash1[2] = 3;
    dumpQHashNodeHelper(hash1);

    // Case 2: simple type -> composite type.
    QHash<int, QString> hash2;
    hash2[5] = "String 7";
    dumpQHashNodeHelper(hash2);

    // Case 3: composite type -> simple type
    QHash<QString, int> hash3;
    hash3["String 11"] = 13;
    dumpQHashNodeHelper(hash3);

    // Case 4: composite type -> composite type
    QHash<QString, QString> hash4;
    hash4["String 17"] = "String 19";
    dumpQHashNodeHelper(hash4);
}

1158
void tst_Debugger::dumpQImageHelper(const QImage &img)
ck's avatar
ck committed
1159
{
1160
1161
1162
1163
1164
    QByteArray expected = "value='(%x%)',type='"NS"QImage',numchild='1',"
        "children=[{name='data',type='"NS"QImageData',addr='%'}]"
            << N(img.width())
            << N(img.height())
            << ptrToBa(&img);
ck's avatar
ck committed
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
    testDumper(expected, &img, NS"QImage", true);
}

void tst_Debugger::dumpQImage()
{
    // Case 1: Null image.
    QImage img;
    dumpQImageHelper(img);

    // Case 2: Normal image.
    img = QImage(3, 700, QImage::Format_RGB555);
    dumpQImageHelper(img);

    // Case 3: Invalid image.
    img = QImage(100, 0, QImage::Format_Invalid);
    dumpQImageHelper(img);
}

void tst_Debugger::dumpQImageDataHelper(QImage &img)
{
    const QByteArray ba(QByteArray::fromRawData((const char*) img.bits(), img.numBytes()));
    QByteArray expected = QByteArray("tiname='$I',addr='$A',type='"NS"QImageData',").
        append("numchild='0',value='<hover here>',valuetooltipencoded='1',").
1188
        append("valuetooltipsize='").append(N(ba.size())).append("',").
ck's avatar
ck committed
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
        append("valuetooltip='").append(ba.toBase64()).append("'");
    testDumper(expected, &img, NS"QImageData", false);
}

void tst_Debugger::dumpQImageData()
{
    // Case 1: Null image.
    QImage img;
    dumpQImageDataHelper(img);

    // Case 2: Normal image.
    img = QImage(3, 700, QImage::Format_RGB555);
    dumpQImageDataHelper(img);

    // Case 3: Invalid image.
    img = QImage(100, 0, QImage::Format_Invalid);
    dumpQImageDataHelper(img);
}

1208
1209
1210
1211
template <typename T>
        void tst_Debugger::dumpQLinkedListHelper(QLinkedList<T> &l)
{
    const int size = qMin(l.size(), 1000);
1212
    const QString &sizeStr = N(size);
1213
1214
    const QByteArray elemTypeStr = typeToString<T>();
    QByteArray expected = QByteArray("value='<").append(sizeStr).
1215
        append(" items>',valueeditable='false',numchild='").append(sizeStr).
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
        append("',childtype='").append(elemTypeStr).append("',childnumchild='").
        append(typeToNumchild<T>()).append("',children=[");
    typename QLinkedList<T>::const_iterator iter = l.constBegin();
    for (int i = 0; i < size; ++i, ++iter) {
        expected.append("{");
        const T &curElem = *iter;
        if (isPointer<T>()) {
            const QString typeStr = stripPtrType(typeToString<T>());
            const QByteArray addrStr = valToString(curElem);
            if (curElem != 0) {
                expected.append("addr='").append(addrStr).append("',saddr='").
                        append(addrStr).append("',type='").append(typeStr).
                        append("',value='").
                        append(derefValToString(curElem)).append("'");
            } else {
                expected.append("addr='").append(ptrToBa(&curElem)).append("',type='").
                    append(typeStr).append("',value='<null>',numchild='0'");
            }
        } else {
            expected.append("addr='").append(ptrToBa(&curElem)).
                append("',value='").append(valToString(curElem)).append("'");
        }
        expected.append("}");
        if (i < size - 1)
            expected.append(",");
    }
    if (size < l.size())
        expected.append(",...");
    expected.append("]");
    testDumper(expected, &l, NS"QLinkedList", true, elemTypeStr);
}

void tst_Debugger::dumpQLinkedList()
{
    // Case 1: Simple element type.
    QLinkedList<int> l;

    // Case 1.1: Empty list.
    dumpQLinkedListHelper(l);

    // Case 1.2: One element.
    l.append(2);
    dumpQLinkedListHelper(l);

    // Case 1.3: Two elements
    l.append(3);
    dumpQLinkedListHelper(l);

    // Case 2: Composite element type.
    QLinkedList<QString> l2;

    // Case 2.1: Empty list.
    dumpQLinkedListHelper(l2);

    // Case 2.2: One element.
    l2.append("Teststring 1");
    dumpQLinkedListHelper(l2);

    // Case 2.3: Two elements.
    l2.append("Teststring 2");
    dumpQLinkedListHelper(l2);

    // Case 2.4: > 1000 elements.
    for (int i = 3; i <= 1002; ++i)
1280
        l2.append("Test " + N(i));
1281
1282
1283
1284
1285
1286
1287
1288
1289

    // Case 3: Pointer type.
    QLinkedList<int *> l3;
    l3.append(new int(5));
    l3.append(new int(7));
    l3.append(0);
    dumpQLinkedListHelper(l3);
}

1290
1291
1292
1293
1294
1295
1296
    #if 0
    void tst_Debugger::dumpQLinkedList()
    {
        // Case 1: Simple element type.
        QLinkedList<int> l;

        // Case 1.1: Empty list.
1297
        testDumper("value='<0 items>',valueeditable='false',numchild='0',"
1298
1299
1300
1301
1302
            "childtype='int',childnumchild='0',children=[]",
            &l, NS"QLinkedList", true, "int");

        // Case 1.2: One element.
        l.append(2);
1303
        testDumper("value='<1 items>',valueeditable='false',numchild='1',"
1304
1305
1306
1307
1308
1309
1310
1311
            "childtype='int',childnumchild='0',children=[{addr='%',value='2'}]"
                << ptrToBa(l.constBegin().operator->()),
            &l, NS"QLinkedList", true, "int");

        // Case 1.3: Two elements
        l.append(3);
        QByteArray it0 = ptrToBa(l.constBegin().operator->());
        QByteArray it1 = ptrToBa(l.constBegin().operator++().operator->());
1312
        testDumper("value='<2 items>',valueeditable='false',numchild='2',"
1313
1314
1315
1316
1317
1318
1319
1320
1321
            "childtype='int',childnumchild='0',children=[{addr='%',value='2'},"
            "{addr='%',value='3'}]" << it0 << it1,
            &l, NS"QLinkedList", true, "int");

        // Case 2: Composite element type.
        QLinkedList<QString> l2;
        QLinkedList<QString>::const_iterator iter;

        // Case 2.1: Empty list.
1322
        testDumper("value='<0 items>',valueeditable='false',numchild='0',"
1323
1324
1325
1326
1327
1328
1329
            "childtype='"NS"QString',childnumchild='0',children=[]",
            &l2, NS"QLinkedList", true, NS"QString");

        // Case 2.2: One element.
        l2.append("Teststring 1");
        iter = l2.constBegin();
        qDebug() << *iter;
1330
        testDumper("value='<1 items>',valueeditable='false',numchild='1',"
1331
1332
1333
1334
1335
            "childtype='"NS"QString',childnumchild='0',children=[{addr='%',value='%',}]"
                << ptrToBa(iter.operator->()) << utfToBase64(*iter),
            &l2, NS"QLinkedList", true, NS"QString");

        // Case 2.3: Two elements.
1336
        QByteArray expected = "value='<2 items>',valueeditable='false',numchild='2',"
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
            "childtype='int',childnumchild='0',children=[";
        iter = l2.constBegin();
        expected.append("{addr='%',%},"
            << ptrToBa(iter.operator->()) << utfToBase64(*iter));
        ++iter;
        expected.append("{addr='%',%}]"
            << ptrToBa(iter.operator->()) << utfToBase64(*iter));
        testDumper(expected,
            &l, NS"QLinkedList", true, NS"QString");

        // Case 2.4: > 1000 elements.
        for (int i = 3; i <= 1002; ++i)
            l2.append("Test " + N(i));

1351
        expected = "value='<1002 items>',valueeditable='false',"
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
            "numchild='1002',childtype='"NS"QString',childnumchild='0',children=['";
        iter = l2.constBegin();
        for (int i = 0; i < 1002; ++i, ++iter)
            expected.append("{addr='%',value='%'},"
                << ptrToBa(iter.operator->()) << utfToBase64(*iter));
        expected.append(",...]");
        testDumper(expected, &l, NS"QLinkedList", true, NS"QString");


        // Case 3: Pointer type.
        QLinkedList<int *> l3;
        l3.append(new int(5));
        l3.append(new int(7));
        l3.append(0);
        //dumpQLinkedListHelper(l3);
        testDumper("", &l, NS"QLinkedList", true, NS"QString");
    }
    #endif

1371
void tst_Debugger::dumpQList_int()
hjk's avatar
hjk committed
1372
1373
{
    QList<int> ilist;
1374
    testDumper("value='<0 items>',valueeditable='false',numchild='0',"
1375
        "internal='1',children=[]",
hjk's avatar
hjk committed
1376
1377
1378
        &ilist, NS"QList", true, "int");
    ilist.append(1);
    ilist.append(2);
1379
    testDumper("value='<2 items>',valueeditable='false',numchild='2',"
1380
        "internal='1',childtype='int',childnumchild='0',children=["
1381
1382
        "{addr='" + str(&ilist.at(0)) + "',value='1'},"
        "{addr='" + str(&ilist.at(1)) + "',value='2'}]",
hjk's avatar
hjk committed
1383
        &ilist, NS"QList", true, "int");
1384
}
hjk's avatar
hjk committed
1385

1386
1387
void tst_Debugger::dumpQList_char()
{
hjk's avatar
hjk committed
1388
    QList<char> clist;
1389
    testDumper("value='<0 items>',valueeditable='false',numchild='0',"