app.cpp 31.1 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
con's avatar
con committed
29

30
31
32
33
34
//#include <complex>


//template <typename T> class B;  B foo() {}

con's avatar
con committed
35
#include <QtCore/QDebug>
36
#include <QtCore/QDateTime>
con's avatar
con committed
37
38
39
#include <QtCore/QDir>
#include <QtCore/QHash>
#include <QtCore/QLibrary>
40
41
#include <QtCore/QLinkedList>
#include <QtCore/QList>
con's avatar
con committed
42
43
44
#include <QtCore/QMap>
#include <QtCore/QPointer>
#include <QtCore/QString>
45
#include <QtCore/QStringList>
hjk's avatar
hjk committed
46
#include <QtCore/QStack>
con's avatar
con committed
47
48
49
#include <QtCore/QThread>
#include <QtCore/QVariant>
#include <QtCore/QVector>
50
#if QT_VERSION >= 0x040500
51
#include <QtCore/QSharedPointer>
52
#endif
con's avatar
con committed
53
54
55
56
57
58
59
60

#include <QtGui/QApplication>
#include <QtGui/QAction>
#include <QtGui/QColor>
#include <QtGui/QFont>
#include <QtGui/QLabel>
#include <QtGui/QPainter>
#include <QtGui/QPainterPath>
61
#include <QtGui/QStandardItemModel>
con's avatar
con committed
62
63
64

#include <QtNetwork/QHostAddress>

hjk's avatar
hjk committed
65
#include <deque>
con's avatar
con committed
66
#include <iostream>
67
#include <map>
hjk's avatar
hjk committed
68
#include <list>
69
#include <set>
hjk's avatar
hjk committed
70
#include <stack>
con's avatar
con committed
71
72
73
#include <string>
#include <vector>

74
75
76
77
78
#define USE_GCC_EXT 1
#if USE_GCC_EXT
#include <hash_set>
#endif

con's avatar
con committed
79
80
#ifdef Q_OS_WIN
#include <windows.h>
81
82
#undef min
#undef max
con's avatar
con committed
83
84
#endif

85
86
87
88
89
90
91
92
Q_DECLARE_METATYPE(QHostAddress)
Q_DECLARE_METATYPE(QList<int>)
Q_DECLARE_METATYPE(QStringList)

typedef QMap<uint, QStringList> MyType;
#define COMMA ,
Q_DECLARE_METATYPE(QMap<uint COMMA QStringList>)

93
94
95
96
97
98
99
100
101
102
103
104
template <typename T> class Vector
{
public:
    explicit Vector(int size) : m_size(size), m_data(new T[size]) {}
    ~Vector() { delete [] m_data; }
    //...
private:
    int m_size;
    T *m_data;
};


105
106
107
108
109
110
111
112
113
114
115
116
namespace nsX {
namespace nsY {
int z;
}
}

namespace nsXY = nsX::nsY;

int qwert()
{
    return nsXY::z;
}
con's avatar
con committed
117
118
119
120
121
122
123
124
125
126
127

uint qHash(const QMap<int, int> &)
{
    return 0;
}

uint qHash(const double & f)
{
    return int(f);
}

hjk's avatar
hjk committed
128

hjk's avatar
hjk committed
129
class Foo
con's avatar
con committed
130
131
{
public:
hjk's avatar
hjk committed
132
    Foo(int i = 0)
con's avatar
con committed
133
134
135
136
137
138
        : a(i), b(2)
    {
        int s = 1;
        int t = 2;
        b = 2 + s + t;
        a += 1;
139
140
141
142
143
144
145
146
147
148
149
150
151
152
        a += 1;
        a += 1;
        a += 1;
        a += 1;
        a += 1;
        a += 1;
        a += 1;
        a += 1;
        a += 1;
        a += 1;
        a += 1;
        a += 1;
        a += 1;
        a += 1;
con's avatar
con committed
153
    }
154
155
156
157
158
159

    ~Foo()
    {
        a = 5;
    }

con's avatar
con committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    void doit()
    {
        static QObject ob;
        m["1"] = "2";
        h[&ob] = m.begin();

        a += 1;
        --b;
        //s += 'x';
    }


    struct Bar {
        Bar() : ob(0) {}
        QObject *ob;
    };

public:
    int a, b;
    char x[6];

private:
    //QString s;
    typedef QMap<QString, QString> Map;
    Map m;
    QHash<QObject *, Map::iterator> h;
};

hjk's avatar
hjk committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
class X : virtual public Foo
{
public:
    X() {
    }
};

class Y : virtual public Foo
{
public:
    Y() {
    }
};

con's avatar
con committed
202
203
void testArray()
{
hjk's avatar
hjk committed
204
    X xxx;
205
206
207
208
209
    double d[3][3];
    for (int i = 0; i != 3; ++i)
        for (int j = 0; j != 3; ++j)
            d[i][j] = i + j;

hjk's avatar
hjk committed
210
211
212
213
214
215
    char c[20];
    c[0] = 'a';
    c[1] = 'b';
    c[2] = 'c';
    c[3] = 'd';

hjk's avatar
hjk committed
216
    QString x[20];
con's avatar
con committed
217
218
219
220
221
222
223
224
225
226
227
228
229
    x[0] = "a";
    x[1] = "b";
    x[2] = "c";
    x[3] = "d";

    Foo foo[10];
    //for (int i = 0; i != sizeof(foo)/sizeof(foo[0]); ++i) {
    for (int i = 0; i < 5; ++i) {
        foo[i].a = i;
        foo[i].doit();
    }
}

230
#ifndef Q_CC_RVCT
231
232
233
234
235
236
237
238
struct TestAnonymous
{
    union {
        struct { int i; int b; };
        struct { float f; };
        double d;
    };
};
239
#endif
hjk's avatar
hjk committed
240
241
242
243
244

void testPeekAndPoke3()
{
    // Anonymous structs
    {
245
#ifndef Q_CC_RVCT
hjk's avatar
hjk committed
246
247
248
249
250
251
252
253
        union {
            struct { int i; int b; };
            struct { float f; };
            double d;
        } a = { 42, 43 };
        a.i = 1; // Break here. Expand a. Step.
        a.i = 2; // Change a.i in Locals view to 0. This changes f, d but expectedly not b. Step.
        a.i = 3; // Continue.
254
#endif
hjk's avatar
hjk committed
255
256
257
258
259
260
261
262
263
264
265
266
    }

    // Complex watchers
    {
        struct S { int a; double b; } s[10];
        for (int i = 0; i != 10; ++i) {
            s[i].a = i;  // Break here. Expand s and s[0]. Step.
            // Watcher Context: "Add New Watcher".
            // Type    ['s[%d].a' % i for i in range(5)]
            // Expand it, continue stepping.
        }
    }
hjk's avatar
hjk committed
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283

    // QImage display
    {
        QImage im(QSize(200, 200), QImage::Format_RGB32);
        im.fill(QColor(200, 10, 30).rgba());
        QPainter pain;
        pain.begin(&im);
        pain.setPen(QPen(Qt::black, 5.0, Qt::SolidLine, Qt::RoundCap));
        pain.drawEllipse(20, 20, 160, 160); // Break here. Step.
        // Toggle between "Normal" and "Displayed" in L&W Context Menu, entry "Display of Type QImage". Step.
        pain.drawArc(70, 115, 60, 30, 200 * 16, 140 * 16);
        pain.setBrush(Qt::black);
        pain.drawEllipse(65, 70, 15, 15); // Step.
        pain.drawEllipse(120, 70, 15, 15); // Step.
        pain.end();
    }

hjk's avatar
hjk committed
284
}
285

286
287

#ifndef Q_CC_RVCT
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
namespace { // anon

struct Something
{
    Something() { a = b = 1; }

    void foo()
    {
        a = 42;
        b = 43;
    }

    int a, b;
};

} // anon
304
305
#endif

306

307
308
void testAnonymous()
{
309
#ifndef Q_CC_RVCT
310
311
312
313
    TestAnonymous a;
    a.i = 1;
    a.i = 2;
    a.i = 3;
314
315
316

    Something s;
    s.foo();
317
#endif
318
319
}

320
321
322
323
324
325
void testFunctionPointer()
{
    typedef void (*func_t)();
    func_t f1 = testAnonymous;
    func_t f2 = testFunctionPointer;
    func_t f3 = testFunctionPointer;
326
327
328
    Q_UNUSED(f1);
    Q_UNUSED(f2);
    Q_UNUSED(f3);
329
330
}

331
void testQByteArray()
con's avatar
con committed
332
333
334
335
{
    QByteArray ba = "Hello";
    ba += '"';
    ba += "World";
hjk's avatar
hjk committed
336
337
338
339
340
341
342
343

    const char *str1 = "\356";
    const char *str2 = "\xee";
    const char *str3 = "\\ee";
    QByteArray buf1( str1 );
    QByteArray buf2( str2 );
    QByteArray buf3( str3 );

con's avatar
con committed
344
345
346
347
348
    ba += char(0);
    ba += 1;
    ba += 2;
}

349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
static void throwit1()
{
    throw 14;
}

static void throwit()
{
    throwit1();
}

int testCatchThrow()
{
    int gotit = 0;
    try {
        throwit();
    } catch (int what) {
        gotit = what;
    }
    return gotit;
}

370
371
372
373
374
375
376
377
378
379
void testQDateTime()
{
    QDateTime date;
    date = QDateTime::currentDateTime();
    date = date.addSecs(5);
    date = date.addSecs(5);
    date = date.addSecs(5);
    date = date.addSecs(5);
}

380
381
382
383
void testQFileInfo()
{
    QFileInfo fi("/tmp/t");
    QString s = fi.absoluteFilePath();
384
385
386
    s = fi.bundleName();
    s = fi.bundleName();
    s = fi.bundleName();
387
388
}

389
void testQHash()
con's avatar
con committed
390
{
hjk's avatar
hjk committed
391
#if 1
con's avatar
con committed
392
393
394
    QHash<int, float> hgg0;
    hgg0[11] = 11.0;
    hgg0[22] = 22.0;
395
396
397
398
399
    hgg0[22] = 22.0;
    hgg0[22] = 22.0;
    hgg0[22] = 22.0;
    hgg0[22] = 22.0;
    hgg0[22] = 22.0;
con's avatar
con committed
400

401
402
403
#endif

#if 1
con's avatar
con committed
404

405
    QHash<QString, int> hgg1;
con's avatar
con committed
406
    hgg1["22.0"] = 22.0;
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
    hgg1["123.0"] = 22.0;
    hgg1["111111ss111128.0"] = 28.0;
    hgg1["11124.0"] = 22.0;
    hgg1["1111125.0"] = 22.0;
    hgg1["11111126.0"] = 22.0;
    hgg1["111111127.0"] = 27.0;
    hgg1["111111111128.0"] = 28.0;
    hgg1["111111111111111111129.0"] = 29.0;

    QHash<QByteArray, float> hgx1;
    hgx1["22.0"] = 22.0;
    hgx1["123.0"] = 22.0;
    hgx1["111111ss111128.0"] = 28.0;
    hgx1["11124.0"] = 22.0;
    hgx1["1111125.0"] = 22.0;
    hgx1["11111126.0"] = 22.0;
    hgx1["111111127.0"] = 27.0;
    hgx1["111111111128.0"] = 28.0;
    hgx1["111111111111111111129.0"] = 29.0;
426
#endif
427
#if 1
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
428
429
    QHash<int, QString> hgg2;
    hgg2[22] = "22.0";
con's avatar
con committed
430
431
432
433
434
435
436
437
438
439

    QHash<QString, Foo> hgg3;
    hgg3["22.0"] = Foo(22);
    hgg3["33.0"] = Foo(33);

    QObject ob;
    QHash<QString, QPointer<QObject> > hash;
    hash.insert("Hallo", QPointer<QObject>(&ob));
    hash.insert("Welt", QPointer<QObject>(&ob));
    hash.insert(".", QPointer<QObject>(&ob));
440
#endif
con's avatar
con committed
441
442
}

443
void testQImage()
con's avatar
con committed
444
445
446
447
448
449
450
451
452
453
454
{
    QImage im(QSize(200, 200), QImage::Format_RGB32);
    im.fill(QColor(200, 100, 130).rgba());
    QPainter pain;
    pain.begin(&im);
    pain.drawLine(2, 2, 130, 130);
    pain.drawLine(4, 2, 130, 140);
    pain.drawRect(30, 30, 80, 80);
    pain.end();
}

455
456
457
458
459
460
461
462
463
464
465
466
struct Function
{
    Function(QByteArray var, QByteArray f, double min, double max)
      : var(var), f(f), min(min), max(max) {}
    QByteArray var;
    QByteArray f;
    double min;
    double max;
};

void testFunction()
{
hjk's avatar
hjk committed
467
    // In order to use this, switch on the 'qDump__Function' in gdbmacros.py
468
469
470
471
472
473
474
475
476
477
    Function func("x", "sin(x)", 0, 1);
    func.max = 10;
    func.f = "cos(x)";
    func.max = 4;
    func.max = 5;
    func.max = 6;
    func.max = 7;
    func.max = 8;
}

con's avatar
con committed
478
479
480
481
482
void testIO()
{
    qDebug() << "qDebug() 1";
    qDebug() << "qDebug() 2";
    qDebug() << "qDebug() 3";
483
    qDebug() << "qDebug <foo & bar>";
con's avatar
con committed
484
485
486
487

    std::cout << "std::cout @@ 1" << std::endl;
    std::cout << "std::cout @@ 2\n";
    std::cout << "std::cout @@ 3" << std::endl;
488
    std::cout << "std::cout <foo & bar>\n";
con's avatar
con committed
489
490
491
492

    std::cerr << "std::cerr 1\n";
    std::cerr << "std::cerr 2\n";
    std::cerr << "std::cerr 3\n";
493
    std::cerr << "std::cerr <foo & bar>\n";
con's avatar
con committed
494
495
}

496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
void testQLinkedList()
{
#if 1
    QLinkedList<int> li;
    QLinkedList<uint> lu;

    for (int i = 0; i != 3; ++i)
        li.append(i);
    li.append(102);


    lu.append(102);
    lu.append(102);
    lu.append(102);

hjk's avatar
hjk committed
511
512
513
514
515
    QLinkedList<Foo *> lpi;
    lpi.append(new Foo(1));
    lpi.append(0);
    lpi.append(new Foo(3));

516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
    QLinkedList<qulonglong> l;
    l.append(42);
    l.append(43);
    l.append(44);
    l.append(45);

    QLinkedList<Foo> f;
    f.append(Foo(1));
    f.append(Foo(2));
#endif
    QLinkedList<std::string> v;
    v.push_back("aa");
    v.push_back("bb");
    v.push_back("cc");
    v.push_back("dd");
 }
con's avatar
con committed
532

533
void testQList()
con's avatar
con committed
534
{
535
536
537
538
539
540
541
542
543
544
    QList<int> big;
    for (int i = 0; i < 10000; ++i)
        big.push_back(i);

    QList<Foo> flist;
    for (int i = 0; i < 100; ++i)
        flist.push_back(i + 15);
    flist.push_back(1000);
    flist.push_back(1001);
    flist.push_back(1002);
545
#if 1
con's avatar
con committed
546
547
548
    QList<int> li;
    QList<uint> lu;

549
    for (int i = 0; i != 30; ++i) {
con's avatar
con committed
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
        li.append(i);
    }
    li.append(101);
    li.append(102);
    li.append(102);
    li.append(102);
    li.append(102);
    li.append(102);
    li.append(102);
    li.append(102);
    li.append(102);
    li.append(102);
    li.append(102);
    li.append(102);
    li.append(102);
    li.append(102);

567
568
569
570
571
    QList<int *> lpi;
    lpi.append(new int(1));
    lpi.append(new int(2));
    lpi.append(new int(3));

con's avatar
con committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586

    for (int i = 0; i != 3; ++i) {
        lu.append(i);
    }
    lu.append(101);
    lu.append(102);
    lu.append(102);
    lu.append(102);

    QList<uint> i;
    i.append(42);
    i.append(43);
    i.append(44);
    i.append(45);

587
588
589
590
591
592
593
594
595
596
597
598
    QList<ushort> ls;
    ls.append(42);
    ls.append(43);
    ls.append(44);
    ls.append(45);

    QList<QChar> lc;
    lc.append(QChar('a'));
    lc.append(QChar('b'));
    lc.append(QChar('c'));
    lc.append(QChar('d'));

con's avatar
con committed
599
600
601
602
603
604
605
606
607
608
    QList<qulonglong> l;
    l.append(42);
    l.append(43);
    l.append(44);
    l.append(45);

    QList<Foo> f;
    f.append(Foo(1));
    f.append(Foo(2));

609
    QList<std::string> v;
con's avatar
con committed
610
611
612
613
    v.push_back("aa");
    v.push_back("bb");
    v.push_back("cc");
    v.push_back("dd");
614
#endif
con's avatar
con committed
615
616
 }

617
void testQMap()
con's avatar
con committed
618
619
620
621
622
623
624
625
626
627
{
    QMap<uint, QStringList> ggl;
    ggl[11] = QStringList() << "11";
    ggl[22] = QStringList() << "22";

    typedef QMap<uint, QStringList> T;
    T ggt;
    ggt[11] = QStringList() << "11";
    ggt[22] = QStringList() << "22";

hjk's avatar
hjk committed
628
#if 1
con's avatar
con committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
    QMap<uint, float> gg0;
    gg0[11] = 11.0;
    gg0[22] = 22.0;


    QMap<QString, float> gg1;
    gg1["22.0"] = 22.0;

    QMap<int, QString> gg2;
    gg2[22] = "22.0";

    QMap<QString, Foo> gg3;
    gg3["22.0"] = Foo(22);
    gg3["33.0"] = Foo(33);

    QObject ob;
    QMap<QString, QPointer<QObject> > map;
    map.insert("Hallo", QPointer<QObject>(&ob));
    map.insert("Welt", QPointer<QObject>(&ob));
    map.insert(".", QPointer<QObject>(&ob));
#endif
}

hjk's avatar
hjk committed
652
653
654
655
656
657
void testQMultiMap()
{
    QMultiMap<uint, float> gg0;
    gg0.insert(11, 11.0);
    gg0.insert(22, 22.0);
    gg0.insert(22, 33.0);
hjk's avatar
hjk committed
658
659
660
661
    gg0.insert(22, 34.0);
    gg0.insert(22, 35.0);
    gg0.insert(22, 36.0);
#if 1
hjk's avatar
hjk committed
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
    QMultiMap<QString, float> gg1;
    gg1.insert("22.0", 22.0);

    QMultiMap<int, QString> gg2;
    gg2.insert(22, "22.0");

    QMultiMap<QString, Foo> gg3;
    gg3.insert("22.0", Foo(22));
    gg3.insert("33.0", Foo(33));
    gg3.insert("22.0", Foo(22));

    QObject ob;
    QMultiMap<QString, QPointer<QObject> > map;
    map.insert("Hallo", QPointer<QObject>(&ob));
    map.insert("Welt", QPointer<QObject>(&ob));
    map.insert(".", QPointer<QObject>(&ob));
    map.insert(".", QPointer<QObject>(&ob));
hjk's avatar
hjk committed
679
#endif
hjk's avatar
hjk committed
680
681
}

682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
namespace Names {
namespace Bar {

struct Ui {
    Ui() { w = 0; }
    QWidget *w;
};

class TestObject : public QObject
{
    Q_OBJECT

public:
    TestObject(QObject *parent = 0)
      : QObject(parent)
    {
        m_ui = new Ui;
        m_ui->w = 0;
    }

    Ui *m_ui;
};

} // namespace Bar
} // namespace Names

708
void testQObject(int &argc, char *argv[])
con's avatar
con committed
709
710
{
    QApplication app(argc, argv);
711
#if 1
712
713
    Names::Bar::TestObject test;

con's avatar
con committed
714
715
716
    QAction act("xxx", &app);
    QString t = act.text();
    t += "y";
hjk's avatar
hjk committed
717
718
719
720
    t += "y";
    t += "y";
    t += "y";
    t += "y";
721
#endif
con's avatar
con committed
722

723
#if 1
con's avatar
con committed
724
725
726
727
728
729
730
    QObject ob(&app);
    ob.setObjectName("An Object");
    QObject ob1;
    ob1.setObjectName("Another Object");

    QObject::connect(&ob, SIGNAL(destroyed()), &ob1, SLOT(deleteLater()));
    QObject::connect(&app, SIGNAL(lastWindowClosed()), &ob, SLOT(deleteLater()));
731
#endif
con's avatar
con committed
732

733
#if 1
con's avatar
con committed
734
735
736
737
738
739
    QList<QObject *> obs;
    obs.append(&ob);
    obs.append(&ob1);
    obs.append(0);
    obs.append(&app);
    ob1.setObjectName("A Subobject");
740
#endif
741
742

#if 1
hjk's avatar
hjk committed
743
744
    QString str = QString::fromUtf8("XXXXXXXXXXXXXXyyXXX ö");
    QLabel l(str);
745
    l.setObjectName("Some Label");
con's avatar
con committed
746
    l.show();
747
#endif
con's avatar
con committed
748
749
750
    app.exec();
}

751
void testQPixmap()
con's avatar
con committed
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
{
    QImage im(QSize(200, 200), QImage::Format_RGB32);
    im.fill(QColor(200, 100, 130).rgba());
    QPainter pain;
    pain.begin(&im);
    pain.drawLine(2, 2, 130, 130);
    pain.end();
    QPixmap pm = QPixmap::fromImage(im);
    int i = 1;
    Q_UNUSED(i);
}

void testPlugin()
{
    QString dir = QDir::currentPath();
#ifdef Q_OS_LINUX
    QLibrary lib(dir + "/libplugin.so");
#endif
#ifdef Q_OS_MAC
    dir = QFileInfo(dir + "/../..").canonicalPath();
    QLibrary lib(dir + "/libplugin.dylib");
#endif
#ifdef Q_OS_WIN
    QLibrary lib(dir + "/plugin.dll");
776
777
778
#endif
#ifdef Q_OS_SYMBIAN
    QLibrary lib(dir + "/plugin.dll");
con's avatar
con committed
779
780
781
782
783
784
785
786
787
788
789
#endif
    int (*foo)() = (int(*)()) lib.resolve("pluginTest");
    qDebug() << "library resolve: " << foo;
    if (foo) {
        int res = foo();
        res += 1;
    } else {
        qDebug() << lib.errorString();
    }
}

790
void testQSet()
hjk's avatar
hjk committed
791
792
793
794
795
796
797
798
799
800
801
{
    QSet<int> hgg0;
    hgg0.insert(11);
    hgg0.insert(22);

    QSet<QString> hgg1;
    hgg1.insert("22.0");

    QObject ob;
    QSet<QPointer<QObject> > hash;
    QPointer<QObject> ptr(&ob);
802
    Q_UNUSED(ptr);
hjk's avatar
hjk committed
803
804
805
806
807
    //hash.insert(ptr);
    //hash.insert(ptr);
    //hash.insert(ptr);
}

808
#if QT_VERSION >= 0x040500
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
class EmployeeData : public QSharedData
{
public:
    EmployeeData() : id(-1) { name.clear(); }
    EmployeeData(const EmployeeData &other)
        : QSharedData(other), id(other.id), name(other.name) { }
    ~EmployeeData() { }

    int id;
    QString name;
};

class Employee
{
public:
    Employee() { d = new EmployeeData; }
    Employee(int id, QString name) {
        d = new EmployeeData;
        setId(id);
        setName(name);
    }
    Employee(const Employee &other)
          : d (other.d)
    {
    }
    void setId(int id) { d->id = id; }
    void setName(QString name) { d->name = name; }

    int id() const { return d->id; }
    QString name() const { return d->name; }

   private:
     QSharedDataPointer<EmployeeData> d;
842
};
843

844
845
846

void testQSharedPointer()
{
847
848
849
850
851
852
853
854
855
856
857
    //Employee e1(1, "Herbert");
    //Employee e2 = e1;

    QSharedPointer<int> iptr(new int(43));
    QSharedPointer<int> iptr2 = iptr;
    QSharedPointer<int> iptr3 = iptr;

    QSharedPointer<QString> ptr(new QString("hallo"));
    QSharedPointer<QString> ptr2 = ptr;
    QSharedPointer<QString> ptr3 = ptr;

hjk's avatar
hjk committed
858
859
860
861
862
863
864
    QWeakPointer<int> wiptr(iptr);
    QWeakPointer<int> wiptr2 = wiptr;
    QWeakPointer<int> wiptr3 = wiptr;

    QWeakPointer<QString> wptr(ptr);
    QWeakPointer<QString> wptr2 = wptr;
    QWeakPointer<QString> wptr3 = wptr;
865
}
866
#endif
867

con's avatar
con committed
868
869
870
871
872
void stringRefTest(const QString &refstring)
{
    Q_UNUSED(refstring);
}

hjk's avatar
hjk committed
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
void testStdDeque()
{
    std::deque<int *> plist1;
    plist1.push_back(new int(1));
    plist1.push_back(0);
    plist1.push_back(new int(2));
    plist1.pop_back();
    plist1.pop_front();
    plist1.pop_front();

    std::deque<int> flist2;
    flist2.push_back(1);
    flist2.push_back(2);

    std::deque<Foo *> plist;
    plist.push_back(new Foo(1));
    plist.push_back(new Foo(2));

    std::deque<Foo> flist;
    flist.push_back(1);
    flist.push_front(2);
}

896
897
898
899
900
901
902
903
904
905
906
907
908
909
void testStdHashSet()
{
#if USE_GCC_EXT
    using namespace __gnu_cxx;
    hash_set<int> h;
    h.insert(1);
    h.insert(194);
    h.insert(2);
    h.insert(3);
    h.insert(4);
    h.insert(5);
#endif
}

hjk's avatar
hjk committed
910
void testStdList()
con's avatar
con committed
911
{
912
913
914
915
    std::list<int> big;
    for (int i = 0; i < 10000; ++i)
        big.push_back(i);

916
917
918
919
    std::list<Foo> flist;
    for (int i = 0; i < 100; ++i)
        flist.push_back(i + 15);

920
#if 1
hjk's avatar
hjk committed
921
    std::list<int *> plist1;
con's avatar
con committed
922
923
924
925
    plist1.push_back(new int(1));
    plist1.push_back(0);
    plist1.push_back(new int(2));

hjk's avatar
hjk committed
926
    std::list<int> flist2;
con's avatar
con committed
927
928
929
930
931
932
933
934
935
936
    flist2.push_back(1);
    flist2.push_back(2);
    flist2.push_back(3);
    flist2.push_back(4);

    flist2.push_back(1);
    flist2.push_back(2);
    flist2.push_back(3);
    flist2.push_back(4);

hjk's avatar
hjk committed
937
    std::list<Foo *> plist;
con's avatar
con committed
938
939
940
941
942
    plist.push_back(new Foo(1));
    plist.push_back(0);
    plist.push_back(new Foo(2));


943
944
945
    foreach (Foo f, flist)
    {}

hjk's avatar
hjk committed
946
    std::list<bool> vec;
con's avatar
con committed
947
948
    vec.push_back(true);
    vec.push_back(false);
949
#endif
con's avatar
con committed
950
951
}

952
953
void testStdMap()
{
954
#if 0
hjk's avatar
hjk committed
955
956
957
958
959
    std::map<QString, Foo> gg3;
    gg3["22.0"] = Foo(22);
    gg3["33.0"] = Foo(33);
    gg3["44.0"] = Foo(44);

960
961
962
963
    std::map<const char *, Foo> m1;
    m1["22.0"] = Foo(22);
    m1["33.0"] = Foo(33);
    m1["44.0"] = Foo(44);
964

hjk's avatar
hjk committed
965
966
967
968
969
970
971
    std::map<uint, uint> gg;
    gg[11] = 1;
    gg[22] = 2;
    gg[33] = 3;
    gg[44] = 4;
    gg[55] = 5;

972
973
974
975
    std::pair<uint, QStringList> p = std::make_pair(3, QStringList() << "11");
    std::vector< std::pair<uint, QStringList> > v;
    v.push_back(p);
    v.push_back(p);
976

977
978
979
    std::map<uint, QStringList> ggl;
    ggl[11] = QStringList() << "11";
    ggl[22] = QStringList() << "22";
hjk's avatar
hjk committed
980
981
982
    ggl[33] = QStringList() << "33";
    ggl[44] = QStringList() << "44";
    ggl[55] = QStringList() << "55";
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998

    typedef std::map<uint, QStringList> T;
    T ggt;
    ggt[11] = QStringList() << "11";
    ggt[22] = QStringList() << "22";

    std::map<uint, float> gg0;
    gg0[11] = 11.0;
    gg0[22] = 22.0;


    std::map<QString, float> gg1;
    gg1["22.0"] = 22.0;

    std::map<int, QString> gg2;
    gg2[22] = "22.0";
999
#endif
1000

1001
#if 1
1002
1003
    QObject ob;
    std::map<QString, QPointer<QObject> > map;
hjk's avatar
hjk committed
1004
1005
1006
    map["Hallo"] = QPointer<QObject>(&ob);
    map["Welt"] = QPointer<QObject>(&ob);
    map["."] = QPointer<QObject>(&ob);
1007
1008
1009
#endif
}

1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
void testStdSet()
{
    std::set<int> hgg0;
    hgg0.insert(11);
    hgg0.insert(22);
    hgg0.insert(33);
#if 1
    std::set<QString> hgg1;
    hgg1.insert("22.0");

    QObject ob;
    std::set<QPointer<QObject> > hash;
    QPointer<QObject> ptr(&ob);
#endif
}

hjk's avatar
hjk committed
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
void testStdStack()
{
    std::stack<int *> plist1;
    plist1.push(new int(1));
    plist1.push(0);
    plist1.push(new int(2));
    plist1.pop();
    plist1.pop();
    plist1.pop();

    std::stack<int> flist2;
    flist2.push(1);
    flist2.push(2);

    std::stack<Foo *> plist;
    plist.push(new Foo(1));
    plist.push(new Foo(2));

    std::stack<Foo> flist;
    flist.push(1);
    flist.push(2);
}

con's avatar
con committed
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
void testStdString()
{
    QString foo;
    std::string str;
    std::wstring wstr;

    std::vector<std::string> v;

    foo += "a";
    str += "b";
    wstr += wchar_t('e');
    foo += "c";
    str += "d";
    foo += "e";
    wstr += wchar_t('e');
    str += "e";
    foo += "a";
    str += "b";
    foo += "c";
    str += "d";
    str += "e";
    wstr += wchar_t('e');
    wstr += wchar_t('e');
    str += "e";

    QList<std::string> l;

    l.push_back(str);
    l.push_back(str);
    l.push_back(str);
    l.push_back(str);

    v.push_back(str);
    v.push_back(str);
    v.push_back(str);
    v.push_back(str);
}

hjk's avatar
hjk committed
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
void testStdVector()
{
    std::vector<int *> plist1;
    plist1.push_back(new int(1));
    plist1.push_back(0);
    plist1.push_back(new int(2));

    std::vector<int> flist2;
    flist2.push_back(1);
    flist2.push_back(2);
    flist2.push_back(3);
    flist2.push_back(4);

    flist2.push_back(1);
    flist2.push_back(2);
    flist2.push_back(3);
    flist2.push_back(4);

    std::vector<Foo *> plist;
    plist.push_back(new Foo(1));
    plist.push_back(0);
    plist.push_back(new Foo(2));

    std::vector<Foo> flist;
    flist.push_back(1);
    flist.push_back(2);
    flist.push_back(3);
    flist.push_back(4);
    //flist.takeFirst();
    //flist.takeFirst();

    std::vector<bool> vec;
    vec.push_back(true);
    vec.push_back(false);
}

1123
1124
void testQStandardItemModel()
{
1125
1126
    // This is known not to work with the Python dumpers.
    // Tracked as http://bugreports.qt.nokia.com/browse/QTCREATORBUG-1009
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
    QStandardItemModel m;
    QStandardItem *i1, *i2, *i11;
    m.appendRow(QList<QStandardItem *>()
         << (i1 = new QStandardItem("1")) << (new QStandardItem("a")));
    m.appendRow(QList<QStandardItem *>()
         << (i2 = new QStandardItem("2")) << (new QStandardItem("b")));
    i1->appendRow(QList<QStandardItem *>()
         << (i11 = new QStandardItem("11")) << (new QStandardItem("aa")));
    int i = 1;
    ++i;
1137
    ++i;
1138
1139
}

hjk's avatar
hjk committed
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
void testQStack()
{
    QVector<int> bigv;
    for (int i = 0; i < 10; ++i)
        bigv.append(i);
    QStack<int> big;
    for (int i = 0; i < 10; ++i)
        big.append(i);
    QStack<Foo *> plist;
    plist.append(new Foo(1));
    plist.append(0);
    plist.append(new Foo(2));
    QStack<Foo> flist;
    flist.append(1);
    flist.append(2);
    flist.append(3);
    flist.append(4);
    //flist.takeFirst();
    //flist.takeFirst();
    QStack<bool> vec;
    vec.append(true);
    vec.append(false);
}

1164
void testQString()
con's avatar
con committed
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
{
    QString str = "Hello ";
    str += " big, ";
    str += " fat ";
    str += " World ";
    str += " World ";
    str += " World ";
    str += " World ";
    str += " World ";
}

1176
void testQString3()
con's avatar
con committed
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
{
    QString str = "Hello ";
    str += " big, ";
    str += " fat ";
    str += " World ";

    QString string("String Test");
    QString *pstring = new QString("Pointer String Test");
    stringRefTest(QString("Ref String Test"));
    string = "Hi";
    string += "Du";
    qDebug() << string;
    delete pstring;
}

1192
void testQStringList()
con's avatar
con committed
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
{
    QStringList l;
    l << "Hello ";
    l << " big, ";
    l << " fat ";
    l << " World ";
}

void testStruct()
{
    Foo f(2);
    f.doit();
    f.doit();
    f.doit();
}

class Thread : public QThread
{
public:
    Thread(int id) : m_id(id) {}

    void run()
    {
        for (int i = 0; i != 100000; ++i) {
            //sleep(1);
            std::cerr << m_id;
        }
    }

private:
    int m_id;
};

1226
void testQThread()
con's avatar
con committed
1227
1228
1229
1230
1231
1232
1233
1234
1235
{
    Thread thread1(1);
    Thread thread2(2);
    thread1.start();
    thread2.start();
    thread1.wait();
    thread2.wait();
}

1236
void testQVariant1()
con's avatar
con committed
1237
1238
1239
1240
1241
{
    QVariant v;
    v = 1;
    v = 1.0;
    v = "string";
hjk's avatar
hjk committed
1242
    v = QRect(100, 200, 300, 400);
con's avatar
con committed
1243
1244
1245
    v = 1;
}

1246
void testQVariant2()
con's avatar
con committed
1247
{
1248
1249
1250
1251
1252
    QVariant value;
    QVariant::Type t = QVariant::String;
    value = QVariant(t, (void*)0);
    *(QString*)value.data() = QString("XXX");

1253
    int i = 1;
1254
    Q_UNUSED(i);
hjk's avatar
hjk committed
1255
#if 1
con's avatar
con committed
1256
    QVariant var;
1257
1258
1259
1260
1261
1262
    var.setValue(1);
    var.setValue(2);
    var.setValue(3);
    var.setValue(QString("Hello"));
    var.setValue(QString("World"));
    var.setValue(QString("Hello"));
1263
    var.setValue(QStringList() << "World");
1264
1265
1266
    var.setValue(QStringList() << "World" << "Hello");
    var.setValue(QStringList() << "Hello" << "Hello");
    var.setValue(QStringList() << "World" << "Hello" << "Hello");
1267
#endif
hjk's avatar
hjk committed
1268
#if 1
con's avatar
con committed
1269
1270
    QVariant var3;
    QHostAddress ha("127.0.0.1");
hjk's avatar
hjk committed
1271
    var.setValue(ha);
con's avatar
con committed
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
    var3 = var;
    var3 = var;
    var3 = var;
    var3 = var;
    QHostAddress ha1 = var.value<QHostAddress>();
    typedef QMap<uint, QStringList> MyType;
    MyType my;
    my[1] = (QStringList() << "Hello");
    my[3] = (QStringList() << "World");
    var.setValue(my);
1282
    // FIXME: Known to break
con's avatar
con committed
1283
1284
1285
1286
1287
    QString type = var.typeName();
    var.setValue(my);
    var.setValue(my);
    var.setValue(my);
    var.setValue(my);
1288
#endif
con's avatar
con committed
1289
1290
}

1291
void testQVariant3()
con's avatar
con committed
1292
1293
1294
1295
1296
1297
1298
1299
{
    QList<int> list;
    list << 1 << 2 << 3;
    QVariant variant = qVariantFromValue(list);
    list.clear();
    list = qVariantValue<QList<int> >(variant);
}

1300
void testQVector()
con's avatar
con committed
1301
{
hjk's avatar
hjk committed
1302
1303
    QVector<int> big(10000);

con's avatar
con committed
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
    QVector<Foo *> plist;
    plist.append(new Foo(1));
    plist.append(0);
    plist.append(new Foo(2));

    QVector<Foo> flist;
    flist.append(1);
    flist.append(2);
    flist.append(3);
    flist.append(4);
    //flist.takeFirst();
    //flist.takeFirst();

    QVector<bool> vec;
    vec.append(true);
    vec.append(false);
}

1322
void testQVectorOfQList()
con's avatar
con committed
1323
1324
1325
1326
1327
1328
1329
1330
{
    QVector<QList<int> > v;
    QVector<QList<int> > *pv = &v;
    v.append(QList<int>() << 1);
    v.append(QList<int>() << 2 << 3);
    Q_UNUSED(pv);
}

1331

hjk's avatar
hjk committed
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
class Goo
{
public:
   Goo(const QString& str, const int n) :
           str_(str), n_(n)
   {
   }
private:
   QString str_;
   int n_;
};

typedef QList<Goo> GooList;

1346
1347
void testNoArgumentName(int i, int, int k)
{
hjk's avatar
hjk committed
1348
1349
1350
1351
1352
1353
1354
1355
    GooList list;
    list.append(Goo("Hello", 1));
    list.append(Goo("World", 2));

    QList<Goo> list2;
    list2.append(Goo("Hello", 1));
    list2.append(Goo("World", 2));

hjk's avatar
hjk committed
1356
1357
1358
1359
    i = 1000;
    k = 2000;
    ++k;
    ++k;
1360
1361
}

con's avatar
con committed
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
void foo() {}
void foo(int) {}
void foo(QList<int>) {}
void foo(QList<QVector<int> >) {}
void foo(QList<QVector<int> *>) {}
void foo(QList<QVector<int *> *>) {}

template <class T>
void foo(QList<QVector<T> *>) {}


namespace somespace {

class MyBase : public QObject
{
public:
    MyBase() {}
    virtual void doit(int i)
    {
       n = i;
    }
protected:
    int n;
};

namespace nested {

class MyFoo : public MyBase
{
public:
    MyFoo() {}
    virtual void doit(int i)
    {
       n = i;
    }
protected:
    int n;
};

class MyBar : public MyFoo
{
public:
    virtual void doit(int i)
    {
       n = i + 1;
    }
};

namespace {

class MyAnon : public MyBar
{
public:
    virtual void doit(int i)
    {
       n = i + 3;
    }
};

namespace baz {

class MyBaz : public MyAnon
{
public:
    virtual void doit(int i)
    {
       n = i + 5;
    }
};

} // namespace baz

} // namespace anon


} // namespace nested
} // namespace somespace

void testNamespace()
{
    using namespace somespace;
    using namespace nested;
    MyFoo foo;
    MyBar bar;
    MyAnon anon;
    baz::MyBaz baz;
    baz.doit(1);
    anon.doit(1);
    foo.doit(1);
    bar.doit(1);
    bar.doit(1);
    bar.doit(1);
    bar.doit(1);
    bar.doit(1);
    bar.doit(1);
    bar.doit(1);
}

hjk's avatar
hjk committed
1460
1461
void testHidden()
{
con's avatar
con committed
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
    int  n = 1;
    n = 2;
    n = 3;
    n = 3;
    n = 3;
    n = 3;
    n = 3;
    n = 3;
    n = 3;
    {
        QString n = "2";
        n = "3";
        n = "4";
        {
            double n = 3.5;
            ++n;
            ++n;
        }
        n = "3";
        n = "4";
    }
    ++n;
    ++n;
hjk's avatar
hjk committed
1485
}
con's avatar
con committed
1486

1487
1488
1489
1490
1491
1492
void testObject1()
{
    QObject parent;
    parent.setObjectName("A Parent");
    QObject child(&parent);
    child.setObjectName("A Child");
1493
1494
    QObject::connect(&child, SIGNAL(destroyed()), qApp, SLOT(quit()));
    QObject::disconnect(&child, SIGNAL(destroyed()), qApp, SLOT(quit()));
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
    child.setObjectName("A renamed Child");
}

void testVector1()
{
    std::vector<std::list<int> *> vector;
    std::list<int> list;
    vector.push_back(new std::list<int>(list));
    vector.push_back(0);
    list.push_back(45);
    vector.push_back(new std::list<int>(list));
    vector.push_back(0);
}

void testQHash1()
{
    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);
}

1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
void testPointer()
{
    Foo *f = new Foo();
    int i = 0;
    ++i;
    ++i;
    ++i;
}

class Z : public QObject
{
public:
    Z() {
        f = new Foo();
        i = 0;
        i = 1;
        i = 2;
        i = 3;
    }
    int i;
    Foo *f;
};

hjk's avatar
hjk committed
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
void testUninitialized()
{
    QString s;
    QStringList sl;
    QMap<int, int> mii;
    QMap<QString, QString> mss;
    QHash<int, int> hii;
    QHash<QString, QString> hss;
    QList<int> li;
    QVector<int> vi;
    QStack<int> si;

    std::string ss;
    std::map<int, int> smii;
    std::map<std::string, std::string> smss;
    std::list<int> sli;
    std::list<std::string> ssl;
    std::vector<int> svi;
    std::stack<int> ssi;
}
1561

1562
1563
1564
1565
1566
void testEndlessRecursion()
{
    testEndlessRecursion();
}

1567
1568
1569
1570
1571
1572
1573
1574
int testEndlessLoop()
{
    qlonglong a = 1;
    while (a > 0)
        ++a;
    return a;
}

1575
1576
1577
1578
1579
1580
1581
1582
1583
QString fooxx()
{
    return "bababa";
}

int testReference()
{
    QString a = "hello";
    const QString &b = fooxx();
1584
    const QString c = "world";
1585
1586
1587
    return a.size() + b.size() + c.size();
}

hjk's avatar
hjk committed
1588
1589
int main(int argc, char *argv[])
{
hjk's avatar
hjk committed
1590
    testPeekAndPoke3();
1591
    testFunctionPointer();
1592
    testAnonymous();
1593
    testReference();
1594
    //testEndlessLoop();
1595
    //testEndlessRecursion();
hjk's avatar
hjk committed
1596
1597
    testQStack();
    testUninitialized();
1598
    testPointer();