qtmobility/tests/auto/qcontactmemusage/tst_qcontactmemusage.cpp
changeset 4 90517678cc4f
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <malloc.h>
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 #include "qtcontacts.h"
       
    46 #include "qcontactmanagerdataholder.h" //QContactManagerDataHolder
       
    47 
       
    48 //TESTED_CLASS=
       
    49 //TESTED_FILES=
       
    50 
       
    51 
       
    52 // Define global op new so we can track some amount of allocations
       
    53 static qulonglong cAllocated = 0;
       
    54 static qulonglong nAllocations = 0;
       
    55 
       
    56 /*
       
    57 void * operator new(size_t sz)
       
    58 {
       
    59     nAllocations++;
       
    60     cAllocated += sz;
       
    61     return malloc(sz);
       
    62 }
       
    63 void * operator new[](size_t sz)
       
    64 {
       
    65     nAllocations++;
       
    66     cAllocated += sz;
       
    67     return malloc(sz);
       
    68 }
       
    69 void operator delete(void * block)
       
    70 {
       
    71     if (block)
       
    72         free(block);
       
    73 }
       
    74 
       
    75 void operator delete[](void *block)
       
    76 {
       
    77     if (block)
       
    78         free(block);
       
    79 }
       
    80 */
       
    81 
       
    82 
       
    83 // Also hook malloc (with glibc)
       
    84 void *(*old_hook)(size_t, const void*);
       
    85 void *qmallochook(size_t size, const void* )
       
    86 {
       
    87     void * ret;
       
    88     nAllocations++;
       
    89     cAllocated += size;
       
    90     __malloc_hook = old_hook;
       
    91     ret = malloc(size);
       
    92     __malloc_hook = qmallochook;
       
    93     return ret;
       
    94 }
       
    95 
       
    96 // I can't seem to get the __malloc_init_hook method working (C++ mangling?)
       
    97 static int qmallocinithook()
       
    98 {
       
    99     old_hook = __malloc_hook;
       
   100     __malloc_hook = qmallochook;
       
   101     return 1;
       
   102 }
       
   103 
       
   104 Q_CONSTRUCTOR_FUNCTION(qmallocinithook);
       
   105 
       
   106 class QMemUsed
       
   107 {
       
   108 public:
       
   109     QMemUsed() : mLabel(0), mAllocated(cAllocated), mAllocations(nAllocations), mItems(0)
       
   110     {
       
   111     }
       
   112     QMemUsed(const char* name) : mLabel(name), mAllocated(cAllocated), mAllocations(nAllocations), mItems(0)
       
   113     {
       
   114     }
       
   115     QMemUsed(const char* name, int nItems) : mLabel(name), mAllocated(cAllocated), mAllocations(nAllocations), mItems(nItems)
       
   116     {
       
   117     }
       
   118     ~QMemUsed()
       
   119     {
       
   120         mAllocated = cAllocated - mAllocated;
       
   121         mAllocations = nAllocations - mAllocations;
       
   122         QString allocations = mAllocations > 1 ? QString("allocations") : QString("allocation");
       
   123         if (mItems > 1) {
       
   124             QString msg("%1 bytes in %2 %8 (for %3 x %6 -> %4 bytes, %5 allocations per %7)");
       
   125             qDebug() << msg.arg(mAllocated).arg(mAllocations).arg(mItems).arg(((double)mAllocated) / mItems).arg(((double) mAllocations) / mItems).arg(mLabel).arg(mLabel).arg(allocations).toLatin1().constData();
       
   126         } else if (mLabel.latin1()){
       
   127             QString msg("%1 bytes in %2 %4 for %3");
       
   128             qDebug() << msg.arg(mAllocated).arg(mAllocations).arg(mLabel).arg(allocations).toLatin1().constData();
       
   129         } else {
       
   130             QString msg("%1 bytes in %2 %3");
       
   131             qDebug() << msg.arg(mAllocated).arg(mAllocations).arg(allocations).toLatin1().constData();
       
   132         }
       
   133     }
       
   134     QLatin1String mLabel;
       
   135     qulonglong mAllocated;
       
   136     qulonglong mAllocations;
       
   137     int mItems;
       
   138 };
       
   139 
       
   140 QTM_USE_NAMESPACE
       
   141 class tst_QContactMemUsage : public QObject
       
   142 {
       
   143 Q_OBJECT
       
   144 
       
   145 public:
       
   146     tst_QContactMemUsage();
       
   147     virtual ~tst_QContactMemUsage();
       
   148 
       
   149 private:
       
   150     QContactManagerDataHolder managerDataHolder;
       
   151 
       
   152 public slots:
       
   153     void init();
       
   154     void cleanup();
       
   155 
       
   156 private slots:
       
   157     void single();
       
   158     void multiple();
       
   159 
       
   160 private:
       
   161     QMemUsed mem;
       
   162 };
       
   163 
       
   164 tst_QContactMemUsage::tst_QContactMemUsage()
       
   165 {
       
   166 }
       
   167 
       
   168 tst_QContactMemUsage::~tst_QContactMemUsage()
       
   169 {
       
   170 }
       
   171 
       
   172 void tst_QContactMemUsage::init()
       
   173 {
       
   174 }
       
   175 
       
   176 void tst_QContactMemUsage::cleanup()
       
   177 {
       
   178 }
       
   179 
       
   180 void tst_QContactMemUsage::single()
       
   181 {
       
   182     // Try single allocations
       
   183     {
       
   184         QMemUsed m("PhoneNumber");
       
   185         QContactPhoneNumber p1;
       
   186     }
       
   187 
       
   188     {
       
   189         QMemUsed m("Address");
       
   190         QContactAddress a;
       
   191     }
       
   192 
       
   193     {
       
   194         QMemUsed m("Name");
       
   195         QContactName p1;
       
   196     }
       
   197 }
       
   198 
       
   199 void tst_QContactMemUsage::multiple()
       
   200 {
       
   201     // First 10
       
   202     {
       
   203         QMemUsed m("PhoneNumber", 10);
       
   204         QContactPhoneNumber p1;
       
   205         QContactPhoneNumber p2;
       
   206         QContactPhoneNumber p3;
       
   207         QContactPhoneNumber p4;
       
   208         QContactPhoneNumber p5;
       
   209         QContactPhoneNumber p6;
       
   210         QContactPhoneNumber p7;
       
   211         QContactPhoneNumber p8;
       
   212         QContactPhoneNumber p9;
       
   213         QContactPhoneNumber p10;
       
   214     }
       
   215 
       
   216     {
       
   217         QMemUsed m("Address", 10);
       
   218         QContactAddress p1;
       
   219         QContactAddress p2;
       
   220         QContactAddress p3;
       
   221         QContactAddress p4;
       
   222         QContactAddress p5;
       
   223         QContactAddress p6;
       
   224         QContactAddress p7;
       
   225         QContactAddress p8;
       
   226         QContactAddress p9;
       
   227         QContactAddress p10;
       
   228     }
       
   229 
       
   230     {
       
   231         QMemUsed m("Address", 10);
       
   232         QContactAddress p1;
       
   233         QContactAddress p2;
       
   234         QContactAddress p3;
       
   235         QContactAddress p4;
       
   236         QContactAddress p5;
       
   237         QContactAddress p6;
       
   238         QContactAddress p7;
       
   239         QContactAddress p8;
       
   240         QContactAddress p9;
       
   241         QContactAddress p10;
       
   242     }
       
   243 
       
   244     {
       
   245         QMemUsed m("Name", 10);
       
   246         QContactName p1;
       
   247         QContactName p2;
       
   248         QContactName p3;
       
   249         QContactName p4;
       
   250         QContactName p5;
       
   251         QContactName p6;
       
   252         QContactName p7;
       
   253         QContactName p8;
       
   254         QContactName p9;
       
   255         QContactName p10;
       
   256     }
       
   257 
       
   258     {
       
   259         QList<QContactDetail> details;
       
   260         QMemUsed m("PhoneNumber", 100);
       
   261         for (int i = 0; i < 100; i++) {
       
   262             QContactPhoneNumber a;
       
   263             details.append(a);
       
   264         }
       
   265     }
       
   266 
       
   267 
       
   268     {
       
   269         QList<QContactDetail> details;
       
   270         QMemUsed m("Address", 100);
       
   271         for (int i = 0; i < 100; i++) {
       
   272             QContactAddress a;
       
   273             details.append(a);
       
   274         }
       
   275     }
       
   276 
       
   277 
       
   278     {
       
   279         QList<QContactDetail> details;
       
   280         QMemUsed m("Name", 100);
       
   281         for (int i = 0; i < 100; i++) {
       
   282             QContactName a;
       
   283             details.append(a);
       
   284         }
       
   285     }
       
   286 
       
   287 
       
   288     // Now set some values
       
   289     {
       
   290         QString v("1234");
       
   291         QMemUsed m("PhoneNumber with number");
       
   292         QContactPhoneNumber p;
       
   293         p.setNumber(v);
       
   294     }
       
   295 
       
   296     {
       
   297         QVariant v("1234");
       
   298         QMemUsed m("PhoneNumber with number as variant");
       
   299         QContactPhoneNumber p;
       
   300         p.setValue(QContactPhoneNumber::FieldNumber, v);
       
   301     }
       
   302 
       
   303 
       
   304     {
       
   305         QMemUsed m("PhoneNumber with number and subtype");
       
   306         QContactPhoneNumber p;
       
   307         p.setNumber("1234");
       
   308         p.setSubTypes(QContactPhoneNumber::SubTypeMobile);
       
   309     }
       
   310 
       
   311     {
       
   312         QMemUsed m("PhoneNumber with custom fields");
       
   313         QContactPhoneNumber p;
       
   314         QVariant v = QString("1234");
       
   315         p.setValue(QContactAddress::FieldCountry, v);
       
   316         p.setValue(QContactUrl::FieldUrl, v);
       
   317         p.setValue(QContactEmailAddress::FieldEmailAddress, v);
       
   318         p.setValue(QContactName::FieldFirstName, v);
       
   319         p.setValue(QContactName::FieldLastName, v);
       
   320         p.setValue(QContactName::FieldMiddleName, v);
       
   321         p.setValue(QContactName::FieldPrefix, v);
       
   322         p.setValue(QContactName::FieldSuffix, v);
       
   323         p.setValue(QContactAddress::FieldLocality, v);
       
   324         p.setValue(QContactAddress::FieldRegion, v);
       
   325     }
       
   326 
       
   327 
       
   328 }
       
   329 
       
   330 QTEST_MAIN(tst_QContactMemUsage)
       
   331 #include "tst_qcontactmemusage.moc"