File indexing completed on 2022-11-29 18:25:14

0001 /* This file is part of the KDE project
0002 
0003    Copyright 1999-2008 David Faure <faure@kde.org>
0004    Copyright 2000-2005 Stephan Kulow <coolo@kde.org>
0005    Copyright 2007      Thiago Macieira <thiago@kde.org>
0006 
0007    This library is free software; you can redistribute it and/or modify
0008    it under the terms of the GNU Library General Public License as published
0009    by the Free Software Foundation; either version 2 of the License or
0010    ( at your option ) version 3 or, at the discretion of KDE e.V.
0011    ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version.
0012 
0013    This library is distributed in the hope that it will be useful,
0014    but WITHOUT ANY WARRANTY; without even the implied warranty of
0015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016    Library General Public License for more details.
0017 
0018    You should have received a copy of the GNU Library General Public License
0019    along with this library; see the file COPYING.LIB.  If not, write to
0020    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0021    Boston, MA 02110-1301, USA.
0022 */
0023 
0024 // to test debug messages, kdebug should be enabled regardless of build mode
0025 
0026 #include <QCoreApplication>
0027 #include <QStringList>
0028 #include <QDebug>
0029 #undef QT_NO_DEBUG
0030 
0031 #include "kdebug.h"
0032 #include <kcomponentdata.h>
0033 #include <iostream>
0034 #include <QPoint>
0035 #include <QRect>
0036 #include <QVariant>
0037 #include <map>
0038 #include <list>
0039 
0040 class TestClass1
0041 {
0042 public:
0043     enum Something { foo };
0044 
0045     void func_void()
0046     {
0047         KWARNING_NOTIMPLEMENTED
0048     }
0049     int func_int()
0050     {
0051         kDebug();
0052         return 0;
0053     }
0054     unsigned func_unsigned()
0055     {
0056         kDebug();
0057         return 0;
0058     }
0059     long func_long()
0060     {
0061         kDebug();
0062         return 0;
0063     }
0064     long long func_ll()
0065     {
0066         kDebug();
0067         return 0;
0068     }
0069     unsigned long long func_ull()
0070     {
0071         kDebug();
0072         return 0;
0073     }
0074     char func_char()
0075     {
0076         kDebug();
0077         return 0;
0078     }
0079     signed char func_schar()
0080     {
0081         kDebug();
0082         return 0;
0083     }
0084     unsigned char func_uchar()
0085     {
0086         kDebug();
0087         return 0;
0088     }
0089     char *func_Pchar()
0090     {
0091         kDebug();
0092         return nullptr;
0093     }
0094     const char *func_KPchar()
0095     {
0096         kDebug();
0097         return nullptr;
0098     }
0099     const volatile char *func_VKPchar()
0100     {
0101         kDebug();
0102         return nullptr;
0103     }
0104     const volatile unsigned long long *const volatile func_KVPKVull()
0105     {
0106         kDebug();
0107         return nullptr;
0108     }
0109     const void *const volatile *func_KPKVvoid()
0110     {
0111         kDebug();
0112         return nullptr;
0113     }
0114 
0115     QList<int> func_ai()
0116     {
0117         kDebug();
0118         return QList<int>();
0119     }
0120     QList<unsigned long long const volatile *> func_aptr()
0121     {
0122         kDebug();
0123         return QList<unsigned long long const volatile *>();
0124     }
0125 
0126     QList<Something> func_aenum()
0127     {
0128         kDebug();
0129         return QList<Something>();
0130     }
0131     QList<QList<const void *> > func_aaptr()
0132     {
0133         kDebug();
0134         return QList<QList<const void *> >();
0135     }
0136 
0137     QMap<int, Something> func_ienummap()
0138     {
0139         kDebug();
0140         return QMap<int, Something>();
0141     }
0142 
0143     template<typename T>
0144     T *func_template1()
0145     {
0146         kDebug();
0147         return nullptr;
0148     }
0149     template<Something val>
0150     long func_template2()
0151     {
0152         kDebug();
0153         return long(val);
0154     }
0155 
0156     typedef unsigned long long *(*fptr)();
0157     typedef unsigned long long *(TestClass1::* pmf)();
0158     typedef fptr(TestClass1::* uglypmf)();
0159     fptr func_fptr()
0160     {
0161         kDebug();
0162         return nullptr;
0163     }
0164     pmf func_pmf()
0165     {
0166         kDebug();
0167         return nullptr;
0168     }
0169     uglypmf func_uglypmf(uglypmf = nullptr)
0170     {
0171         kDebug();
0172         return nullptr;
0173     }
0174     QMap<QString, uglypmf> func_uglypmf2()
0175     {
0176         kDebug();
0177         return QMap<QString, uglypmf>();
0178     }
0179 
0180     void testBlock()
0181     {
0182         KDEBUG_BLOCK
0183         func_int();
0184         testNestedBlock();
0185     }
0186 
0187     void testNestedBlock()
0188     {
0189         KDEBUG_BLOCK
0190         func_void();
0191         deprecatedMethod();
0192     }
0193 
0194     void testCharStarBlock()
0195     {
0196         KDebug::Block myBlock(qPrintable(QString::fromLatin1("Block"))); // the char* goes out of scope
0197         func_void();
0198         deprecatedMethod();
0199     }
0200 
0201     void deprecatedMethod()
0202     {
0203         KWARNING_DEPRECATED
0204     }
0205 
0206 public:
0207     TestClass1()
0208     {
0209         // instantiate
0210         func_void();
0211         func_int();
0212         func_unsigned();
0213         func_long();
0214         func_ll();
0215         func_ull();
0216         func_char();
0217         func_schar();
0218         func_uchar();
0219         func_Pchar();
0220         func_KPchar();
0221         func_VKPchar();
0222         func_KVPKVull();
0223         func_KPKVvoid();
0224         func_ai();
0225         func_aptr();
0226         func_aenum();
0227         func_aaptr();
0228         func_ienummap();
0229         func_template1<TestClass1>();
0230         func_template2<foo>();
0231         func_fptr();
0232         func_pmf();
0233         func_uglypmf();
0234         func_uglypmf2();
0235     }
0236 };
0237 
0238 template<typename T> class TestClass2
0239 {
0240     long func_long()
0241     {
0242         kDebug();
0243         return 0;
0244     }
0245     template<typename S>
0246     T *func_template1()
0247     {
0248         kDebug();
0249         return nullptr;
0250     }
0251     template<TestClass1::Something val>
0252     long func_template2()
0253     {
0254         kDebug();
0255         return long(val);
0256     }
0257 public:
0258     TestClass2()
0259     {
0260         func_long();
0261         func_template1<TestClass2>();
0262         func_template2<TestClass1::foo>();
0263     }
0264 };
0265 
0266 template<typename T, TestClass1::Something v> class TestClass3
0267 {
0268     long func_long()
0269     {
0270         kDebug();
0271         return 0;
0272     }
0273     template<typename S>
0274     S *func_template1()
0275     {
0276         kDebug();
0277         return nullptr;
0278     }
0279     template<TestClass1::Something val>
0280     long func_template2()
0281     {
0282         kDebug();
0283         return long(val);
0284     }
0285 public:
0286     struct Foo {
0287         TestClass3 foo;
0288     };
0289     TestClass3()
0290     {
0291         func_long();
0292         func_template1<TestClass2<T> >();
0293         func_template2<TestClass1::foo>();
0294     }
0295 };
0296 
0297 class TestClass4
0298 {
0299     TestClass1 c1;
0300 
0301     TestClass2<std::map<long, const void *> > func2()
0302     {
0303         kDebug();
0304         return TestClass2<std::map<long, const void *> >();
0305     }
0306     TestClass3<std::map<std::list<int>, const void *>, TestClass1::foo>::Foo func3()
0307     {
0308         kDebug();
0309         return TestClass3<std::map<std::list<int>, const void *>, TestClass1::foo>::Foo();
0310     }
0311 public:
0312     TestClass4()
0313     {
0314         func2();
0315         func3();
0316         kDebug();
0317     }
0318     ~TestClass4()
0319     {
0320         kDebug();
0321     }
0322 };
0323 
0324 namespace
0325 {
0326 class TestClass5
0327 {
0328 public:
0329     TestClass5()
0330     {
0331         kDebug();
0332     }
0333 };
0334 TestClass5 func5()
0335 {
0336     kDebug();
0337     return TestClass5();
0338 }
0339 } // namespace
0340 
0341 namespace N
0342 {
0343 namespace
0344 {
0345 class TestClass6
0346 {
0347 public:
0348     TestClass6()
0349     {
0350         kDebug();
0351     }
0352 };
0353 TestClass6 func6()
0354 {
0355     kDebug();
0356     return TestClass6();
0357 }
0358 } // namespace
0359 } // namespace N
0360 
0361 void testKDebug()
0362 {
0363     QString test = "%20C this is a string";
0364     kDebug(180) << test;
0365     QByteArray cstr = test.toLatin1();
0366     kDebug(180) << test;
0367     QChar ch = 'a';
0368     kDebug() << "QChar a: " << ch;
0369     ch = '\r';
0370     kDebug() << "QChar \\r: " << ch;
0371     kDebug() << k_lineinfo << "error on this line";
0372     kDebug(2 == 2) << "this is right " << perror;
0373     kDebug() << "Before instance creation";
0374     kDebug(1202) << "Before instance creation";
0375     KComponentData i("kdebugtest");
0376     kDebug(1) << "kDebugInfo with inexisting area number";
0377     kDebug(1202) << "This number has a value of " << 5;
0378     // kDebug() << "This number should come out as appname " << 5 << " " << "test";
0379     kWarning() << "1+1 = " << 1 + 1 + 1;
0380     kError(1 + 1 != 2) << "there is something really odd!";
0381     QString s = "mystring";
0382     kDebug() << s;
0383     kError(1202) << "Error !!!";
0384     kError() << "Error with no area";
0385 
0386     kDebug() << "Printing a null QObject pointer: " << (QObject *)nullptr;
0387 
0388     kDebug() << "char " << '^' << " " << char(26);
0389     QPoint p(0, 9);
0390     kDebug() << p;
0391 
0392     QRect r(9, 12, 58, 234);
0393     kDebug() << r;
0394 
0395     QStringList sl;
0396     sl << "hi" << "this" << "list" << "is" << "short";
0397     kDebug() << sl;
0398 
0399     QList<int> il;
0400     kDebug() << "Empty QList<int>: " << il;
0401     il << 1 << 2 << 3 << 4 << 5;
0402     kDebug() << "QList<int> filled: " << il;
0403 
0404     qint64 big = 65536LL * 65536 * 500;
0405     kDebug() << big;
0406 
0407     QVariant v(0.12345);
0408     kDebug() << "Variant: " << v;
0409 
0410     QByteArray data;
0411     data.resize(6);
0412     data[0] = 42;
0413     data[1] = 'H';
0414     data[2] = 'e';
0415     data[3] = 'l';
0416     data[4] = 'l';
0417     data[5] = 'o';
0418     kDebug() << data;
0419     data.resize(80);
0420     data.fill(42);
0421     kDebug() << data;
0422 
0423 #ifdef Q_CC_GNU
0424     bool dotest = true;
0425 #else
0426     bool dotest = false;
0427 #endif
0428     if (dotest) {
0429         kDebug() << "Testing the function names. The following should display only the base name of the functions";
0430         TestClass4 c4;
0431         func5();
0432         using namespace N;
0433         func6();
0434     }
0435 
0436     TestClass1 c1;
0437     c1.testBlock();
0438     c1.testCharStarBlock();
0439 }
0440 
0441 // Concurrency testing, based on code from bug 133026
0442 // Copyright 2006 Marcel Wiesweg <marcel.wiesweg@gmx.de>
0443 
0444 //#define THREAD_TEST 1
0445 
0446 #ifdef THREAD_TEST
0447 #include <QThread>
0448 class DebugKDebug : public QThread
0449 {
0450 public:
0451     DebugKDebug(int num) : m_num(num) {}
0452 protected:
0453     virtual void run()
0454     {
0455         int count = 1000;
0456         while (--count) {
0457             kDebug() << "Test" << m_num;
0458             //usleep(1);
0459         }
0460     }
0461 private:
0462     int m_num;
0463 };
0464 
0465 static void startThreads()
0466 {
0467     QVector<DebugKDebug *> threads;
0468     threads.resize(100);
0469     for (int i = 0; i < threads.size(); i++) {
0470         threads[i] = new DebugKDebug(i);
0471         threads[i]->start();
0472     }
0473     for (int i = 0; i < threads.size(); i++) {
0474         threads[i]->wait();
0475     }
0476 }
0477 #endif
0478 
0479 int main(int argc, char **argv)
0480 {
0481     QCoreApplication app(argc, argv);
0482     KComponentData mainData("kdebugtest");
0483     testKDebug();
0484 #ifdef THREAD_TEST
0485     startThreads();
0486 #else
0487     Q_UNUSED(argc);
0488     Q_UNUSED(argv);
0489 #endif
0490     return 0;
0491 }
0492