/************************************************************************** ** ** This file is part of Qt Creator ** ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). ** ** Contact: Qt Software Information (qt-info@nokia.com) ** ** Commercial Usage ** ** 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. ** ** GNU Lesser General Public License Usage ** ** 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. ** ** If you are unsure which license is appropriate for your use, please ** contact the sales department at qt-sales@nokia.com. ** **************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef Q_OS_WIN #include #endif uint qHash(const QMap &) { return 0; } uint qHash(const double & f) { return int(f); } class Foo { public: Foo(int i=0) : a(i), b(2) { int s = 1; int t = 2; b = 2 + s + t; a += 1; } ~Foo() { a = 5; } 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 Map; Map m; QHash h; }; void testArray() { char c[20]; c[0] = 'a'; c[1] = 'b'; c[2] = 'c'; c[3] = 'd'; QString x[20]; 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(); } } void testQByteArray() { QByteArray ba = "Hello"; ba += '"'; ba += "World"; const char *str1 = "\356"; const char *str2 = "\xee"; const char *str3 = "\\ee"; QByteArray buf1( str1 ); QByteArray buf2( str2 ); QByteArray buf3( str3 ); ba += char(0); ba += 1; ba += 2; } void testQHash() { #if 1 QHash hgg0; hgg0[11] = 11.0; hgg0[22] = 22.0; hgg0[22] = 22.0; hgg0[22] = 22.0; hgg0[22] = 22.0; hgg0[22] = 22.0; hgg0[22] = 22.0; #endif #if 1 QHash hgg1; hgg1["22.0"] = 22.0; 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 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; #endif #if 1 QHash hgg2; hgg2[22] = "22.0"; QHash hgg3; hgg3["22.0"] = Foo(22); hgg3["33.0"] = Foo(33); QObject ob; QHash > hash; hash.insert("Hallo", QPointer(&ob)); hash.insert("Welt", QPointer(&ob)); hash.insert(".", QPointer(&ob)); #endif } void testQImage() { 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(); } void testIO() { qDebug() << "qDebug() 1"; qDebug() << "qDebug() 2"; qDebug() << "qDebug() 3"; qDebug() << "qDebug "; std::cout << "std::cout @@ 1" << std::endl; std::cout << "std::cout @@ 2\n"; std::cout << "std::cout @@ 3" << std::endl; std::cout << "std::cout \n"; std::cerr << "std::cerr 1\n"; std::cerr << "std::cerr 2\n"; std::cerr << "std::cerr 3\n"; std::cerr << "std::cerr \n"; } void testQLinkedList() { #if 1 QLinkedList li; QLinkedList lu; for (int i = 0; i != 3; ++i) li.append(i); li.append(102); lu.append(102); lu.append(102); lu.append(102); QLinkedList lpi; lpi.append(new Foo(1)); lpi.append(0); lpi.append(new Foo(3)); QLinkedList l; l.append(42); l.append(43); l.append(44); l.append(45); QLinkedList f; f.append(Foo(1)); f.append(Foo(2)); #endif QLinkedList v; v.push_back("aa"); v.push_back("bb"); v.push_back("cc"); v.push_back("dd"); } void testQList() { #if 1 QList li; QList lu; for (int i = 0; i != 30; ++i) { 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); QList lpi; lpi.append(new int(1)); lpi.append(new int(2)); lpi.append(new int(3)); for (int i = 0; i != 3; ++i) { lu.append(i); } lu.append(101); lu.append(102); lu.append(102); lu.append(102); lu.append(102); lu.append(102); lu.append(102); lu.append(102); lu.append(102); QList i; i.append(42); i.append(43); i.append(44); i.append(45); QList l; l.append(42); l.append(43); l.append(44); l.append(45); QList f; f.append(Foo(1)); f.append(Foo(2)); #endif QList v; v.push_back("aa"); v.push_back("bb"); v.push_back("cc"); v.push_back("dd"); } void testQMap() { QMap ggl; ggl[11] = QStringList() << "11"; ggl[22] = QStringList() << "22"; typedef QMap T; T ggt; ggt[11] = QStringList() << "11"; ggt[22] = QStringList() << "22"; #if 1 QMap gg0; gg0[11] = 11.0; gg0[22] = 22.0; QMap gg1; gg1["22.0"] = 22.0; QMap gg2; gg2[22] = "22.0"; QMap gg3; gg3["22.0"] = Foo(22); gg3["33.0"] = Foo(33); QObject ob; QMap > map; map.insert("Hallo", QPointer(&ob)); map.insert("Welt", QPointer(&ob)); map.insert(".", QPointer(&ob)); #endif } void testQMultiMap() { QMultiMap gg0; gg0.insert(11, 11.0); gg0.insert(22, 22.0); gg0.insert(22, 33.0); gg0.insert(22, 34.0); gg0.insert(22, 35.0); gg0.insert(22, 36.0); #if 1 QMultiMap gg1; gg1.insert("22.0", 22.0); QMultiMap gg2; gg2.insert(22, "22.0"); QMultiMap gg3; gg3.insert("22.0", Foo(22)); gg3.insert("33.0", Foo(33)); gg3.insert("22.0", Foo(22)); QObject ob; QMultiMap > map; map.insert("Hallo", QPointer(&ob)); map.insert("Welt", QPointer(&ob)); map.insert(".", QPointer(&ob)); map.insert(".", QPointer(&ob)); #endif } void testQObject(int &argc, char *argv[]) { QApplication app(argc, argv); QAction act("xxx", &app); QString t = act.text(); t += "y"; t += "y"; t += "y"; t += "y"; t += "y"; #if 1 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())); QList obs; obs.append(&ob); obs.append(&ob1); obs.append(0); obs.append(&app); ob1.setObjectName("A Subobject"); #endif QString str = QString::fromUtf8("XXXXXXXXXXXXXXyyXXX รถ"); QLabel l(str); l.show(); app.exec(); } void testQPixmap() { 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"); #endif int (*foo)() = (int(*)()) lib.resolve("pluginTest"); qDebug() << "library resolve: " << foo; if (foo) { int res = foo(); res += 1; } else { qDebug() << lib.errorString(); } } void testQSet() { QSet hgg0; hgg0.insert(11); hgg0.insert(22); QSet hgg1; hgg1.insert("22.0"); QObject ob; QSet > hash; QPointer ptr(&ob); //hash.insert(ptr); //hash.insert(ptr); //hash.insert(ptr); } 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 d; }; void testQSharedPointer() { //Employee e1(1, "Herbert"); //Employee e2 = e1; QSharedPointer iptr(new int(43)); QSharedPointer iptr2 = iptr; QSharedPointer iptr3 = iptr; QSharedPointer ptr(new QString("hallo")); QSharedPointer ptr2 = ptr; QSharedPointer ptr3 = ptr; QWeakPointer wiptr(iptr); QWeakPointer wiptr2 = wiptr; QWeakPointer wiptr3 = wiptr; QWeakPointer wptr(ptr); QWeakPointer wptr2 = wptr; QWeakPointer wptr3 = wptr; } void stringRefTest(const QString &refstring) { Q_UNUSED(refstring); } void testStdDeque() { std::deque 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 flist2; flist2.push_back(1); flist2.push_back(2); std::deque plist; plist.push_back(new Foo(1)); plist.push_back(new Foo(2)); std::deque flist; flist.push_back(1); flist.push_front(2); } void testStdList() { std::list plist1; plist1.push_back(new int(1)); plist1.push_back(0); plist1.push_back(new int(2)); std::list 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::list plist; plist.push_back(new Foo(1)); plist.push_back(0); plist.push_back(new Foo(2)); std::list flist; flist.push_back(1); flist.push_back(2); flist.push_back(3); flist.push_back(4); foreach (Foo f, flist) {} std::list vec; vec.push_back(true); vec.push_back(false); } void testStdMap() { std::map gg3; gg3["22.0"] = Foo(22); gg3["33.0"] = Foo(33); gg3["44.0"] = Foo(44); std::map m1; m1["22.0"] = Foo(22); m1["33.0"] = Foo(33); m1["44.0"] = Foo(44); #if 1 std::map gg; gg[11] = 1; gg[22] = 2; gg[33] = 3; gg[44] = 4; gg[55] = 5; std::map ggl; ggl[11] = QStringList() << "11"; ggl[22] = QStringList() << "22"; ggl[33] = QStringList() << "33"; ggl[44] = QStringList() << "44"; ggl[55] = QStringList() << "55"; typedef std::map T; T ggt; ggt[11] = QStringList() << "11"; ggt[22] = QStringList() << "22"; std::map gg0; gg0[11] = 11.0; gg0[22] = 22.0; std::map gg1; gg1["22.0"] = 22.0; std::map gg2; gg2[22] = "22.0"; QObject ob; std::map > map; map["Hallo"] = QPointer(&ob); map["Welt"] = QPointer(&ob); map["."] = QPointer(&ob); #endif } void testStdSet() { std::set hgg0; hgg0.insert(11); hgg0.insert(22); hgg0.insert(33); #if 1 std::set hgg1; hgg1.insert("22.0"); QObject ob; std::set > hash; QPointer ptr(&ob); #endif } void testStdStack() { std::stack plist1; plist1.push(new int(1)); plist1.push(0); plist1.push(new int(2)); plist1.pop(); plist1.pop(); plist1.pop(); std::stack flist2; flist2.push(1); flist2.push(2); std::stack plist; plist.push(new Foo(1)); plist.push(new Foo(2)); std::stack flist; flist.push(1); flist.push(2); } void testStdString() { QString foo; std::string str; std::wstring wstr; std::vector 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 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); } void testStdVector() { std::vector plist1; plist1.push_back(new int(1)); plist1.push_back(0); plist1.push_back(new int(2)); std::vector 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 plist; plist.push_back(new Foo(1)); plist.push_back(0); plist.push_back(new Foo(2)); std::vector flist; flist.push_back(1); flist.push_back(2); flist.push_back(3); flist.push_back(4); //flist.takeFirst(); //flist.takeFirst(); std::vector vec; vec.push_back(true); vec.push_back(false); } void testQString() { QString str = "Hello "; str += " big, "; str += " fat "; str += " World "; str += " World "; str += " World "; str += " World "; str += " World "; } void testQString3() { 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; } void testQStringList() { 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; }; void testQThread() { Thread thread1(1); Thread thread2(2); thread1.start(); thread2.start(); thread1.wait(); thread2.wait(); } void testQVariant1() { QVariant v; v = 1; v = 1.0; v = "string"; v = 1; } void testQVariant2() { int i = 1; QVariant var; var.setValue(1); var.setValue(2); var.setValue(3); var.setValue(QString("Hello")); var.setValue(QString("World")); var.setValue(QString("Hello")); var.setValue(QStringList() << "World"); var.setValue(QStringList() << "World" << "Hello"); var.setValue(QStringList() << "Hello" << "Hello"); var.setValue(QStringList() << "World" << "Hello" << "Hello"); #if 0 QVariant var3; QHostAddress ha("127.0.0.1"); qVariantSetValue(var, ha); var3 = var; var3 = var; var3 = var; var3 = var; QHostAddress ha1 = var.value(); typedef QMap MyType; MyType my; my[1] = (QStringList() << "Hello"); my[3] = (QStringList() << "World"); var.setValue(my); QString type = var.typeName(); var.setValue(my); var.setValue(my); var.setValue(my); var.setValue(my); #endif } void testQVariant3() { QList list; list << 1 << 2 << 3; QVariant variant = qVariantFromValue(list); list.clear(); list = qVariantValue >(variant); } void testQVector() { QVector big(10000); QVector plist; plist.append(new Foo(1)); plist.append(0); plist.append(new Foo(2)); QVector flist; flist.append(1); flist.append(2); flist.append(3); flist.append(4); //flist.takeFirst(); //flist.takeFirst(); QVector vec; vec.append(true); vec.append(false); } void testQVectorOfQList() { QVector > v; QVector > *pv = &v; v.append(QList() << 1); v.append(QList() << 2 << 3); Q_UNUSED(pv); } void foo() {} void foo(int) {} void foo(QList) {} void foo(QList >) {} void foo(QList *>) {} void foo(QList *>) {} template void foo(QList *>) {} namespace { namespace A { int barz() { return 42;} } namespace B { int barz() { return 43;} } } 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); } void testHidden() { 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; } int main(int argc, char *argv[]) { QString hallo = "hallo"; QStringList list; list << "aaa" << "bbb" << "cc"; testIO(); testHidden(); testArray(); testQByteArray(); testStdDeque(); testStdList(); testStdMap(); testStdSet(); testStdStack(); testStdString(); testStdVector(); testPlugin(); testQList(); testQLinkedList(); testNamespace(); //return 0; testQHash(); testQImage(); testQMap(); testQMultiMap(); testQString(); testQSet(); testQSharedPointer(); testQStringList(); testStruct(); //testThreads(); testQVariant1(); testQVariant2(); testQVariant3(); testQVector(); testQVectorOfQList(); //*(int *)0 = 0; testQObject(argc, argv); //QColor color(255,128,10); //QFont font; while(true) ; return 0; } //Q_DECLARE_METATYPE(QHostAddress) Q_DECLARE_METATYPE(QList) //#define COMMA , //Q_DECLARE_METATYPE(QMap) QT_BEGIN_NAMESPACE template <> struct QMetaTypeId { enum { Defined = 1 }; static int qt_metatype_id() { static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); if (!metatype_id) metatype_id = qRegisterMetaType ("myns::QHostAddress"); return metatype_id; \ } \ }; template <> struct QMetaTypeId< QMap > { enum { Defined = 1 }; static int qt_metatype_id() { static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); if (!metatype_id) metatype_id = qRegisterMetaType< QMap > ("myns::QMap"); return metatype_id; \ } \ }; QT_END_NAMESPACE