tests/auto/qcache/tst_qcache.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 test suite of the Qt Toolkit.
       
     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 <QtTest/QtTest>
       
    43 
       
    44 #include <qcache.h>
       
    45 
       
    46 //TESTED_CLASS=
       
    47 //TESTED_FILES=
       
    48 
       
    49 class tst_QCache : public QObject
       
    50 {
       
    51     Q_OBJECT
       
    52 
       
    53 public:
       
    54     tst_QCache();
       
    55     virtual ~tst_QCache();
       
    56 
       
    57 public slots:
       
    58     void initTestCase();
       
    59     void cleanupTestCase();
       
    60 private slots:
       
    61     void maxCost();
       
    62     void setMaxCost();
       
    63     void totalCost();
       
    64     void clear();
       
    65     void insert();
       
    66     void contains();
       
    67     void operator_bracket_bracket();
       
    68     void remove();
       
    69     void take();
       
    70     void axioms_on_key_type();
       
    71 };
       
    72 
       
    73 
       
    74 struct Foo {
       
    75     static int count;
       
    76     Foo():c(count) { ++count; }
       
    77     Foo(const Foo& o):c(o.c) { ++count; }
       
    78     ~Foo() { --count; }
       
    79     int c;
       
    80     int data[8];
       
    81 };
       
    82 
       
    83 int Foo::count = 0;
       
    84 
       
    85 tst_QCache::tst_QCache()
       
    86 {
       
    87 }
       
    88 
       
    89 tst_QCache::~tst_QCache()
       
    90 {
       
    91 }
       
    92 
       
    93 void tst_QCache::initTestCase()
       
    94 {
       
    95     Foo::count = 0;
       
    96 }
       
    97 
       
    98 void tst_QCache::cleanupTestCase()
       
    99 {
       
   100     // always check for memory leaks
       
   101     QCOMPARE(Foo::count, 0);
       
   102 }
       
   103 
       
   104 void tst_QCache::maxCost()
       
   105 {
       
   106     QCache<QString, int> cache1, cache2(100), cache3(200), cache4(-50);
       
   107     QCOMPARE(cache1.maxCost(), 100);
       
   108     QCOMPARE(cache2.maxCost(), 100);
       
   109     QCOMPARE(cache3.maxCost(), 200);
       
   110     QCOMPARE(cache4.maxCost(), -50); // 0 would also make sense
       
   111 
       
   112     cache1.setMaxCost(101);
       
   113     QCOMPARE(cache1.maxCost(), 101);
       
   114 
       
   115     cache1.insert("one", new int(1), 1);
       
   116     cache1.insert("two", new int(2), 1);
       
   117     QCOMPARE(cache1.totalCost(), 2);
       
   118     QCOMPARE(cache1.size(), 2);
       
   119     QCOMPARE(cache1.maxCost(), 101);
       
   120 
       
   121     cache1.insert("three", new int(3), 98);
       
   122     QCOMPARE(cache1.totalCost(), 100);
       
   123     QCOMPARE(cache1.size(), 3);
       
   124     QCOMPARE(cache1.maxCost(), 101);
       
   125 
       
   126     cache1.insert("four", new int(4), 1);
       
   127     QCOMPARE(cache1.totalCost(), 101);
       
   128     QCOMPARE(cache1.size(), 4);
       
   129     QCOMPARE(cache1.maxCost(), 101);
       
   130 
       
   131     cache1.insert("five", new int(4), 1);
       
   132     QVERIFY(cache1.totalCost() <= 101);
       
   133     QVERIFY(cache1.size() == 4);
       
   134     QCOMPARE(cache1.maxCost(), 101);
       
   135 
       
   136     cache1.setMaxCost(-1);
       
   137     QCOMPARE(cache1.totalCost(), 0);
       
   138     QCOMPARE(cache1.maxCost(), -1);
       
   139 
       
   140     cache2.setMaxCost(202);
       
   141     QCOMPARE(cache2.maxCost(), 202);
       
   142 
       
   143     cache3.setMaxCost(-50);
       
   144     QCOMPARE(cache3.maxCost(), -50);
       
   145 }
       
   146 
       
   147 void tst_QCache::setMaxCost()
       
   148 {
       
   149     QCache<int, Foo> cache;
       
   150     cache.setMaxCost(2);
       
   151     cache.insert(1, new Foo);
       
   152     cache.insert(2, new Foo);
       
   153     QCOMPARE(cache.totalCost(), 2);
       
   154     QCOMPARE(Foo::count, 2);
       
   155 
       
   156     cache.insert(3, new Foo);
       
   157     QCOMPARE(cache.totalCost(), 2);
       
   158     QCOMPARE(Foo::count, 2);
       
   159 
       
   160     cache.setMaxCost(3);
       
   161     QCOMPARE(cache.totalCost(), 2);
       
   162     QCOMPARE(Foo::count, 2);
       
   163 
       
   164     cache.setMaxCost(2);
       
   165     QCOMPARE(cache.totalCost(), 2);
       
   166     QCOMPARE(Foo::count, 2);
       
   167 
       
   168     cache.setMaxCost(1);
       
   169     QCOMPARE(cache.totalCost(), 1);
       
   170     QCOMPARE(Foo::count, 1);
       
   171 
       
   172     cache.setMaxCost(0);
       
   173     QCOMPARE(cache.totalCost(), 0);
       
   174     QCOMPARE(Foo::count, 0);
       
   175 
       
   176     cache.setMaxCost(-1);
       
   177     QCOMPARE(cache.totalCost(), 0);
       
   178     QCOMPARE(Foo::count, 0);
       
   179 }
       
   180 
       
   181 void tst_QCache::totalCost()
       
   182 {
       
   183     QCache<QString, int> cache;
       
   184     QCOMPARE(cache.totalCost(), 0);
       
   185 
       
   186     cache.insert("one", new int(1), 0);
       
   187     QCOMPARE(cache.totalCost(), 0);
       
   188 
       
   189     cache.insert("two", new int(2), 1);
       
   190     QCOMPARE(cache.totalCost(), 1);
       
   191 
       
   192     cache.insert("three", new int(3), 2);
       
   193     QCOMPARE(cache.totalCost(), 3);
       
   194 
       
   195     cache.insert("four", new int(4), 10000);
       
   196     QCOMPARE(cache.totalCost(), 3);
       
   197     QVERIFY(!cache.contains("four"));
       
   198 
       
   199     cache.insert("five", new int(5), -5);
       
   200     QCOMPARE(cache.totalCost(), -2);
       
   201 
       
   202     cache.insert("six", new int(6), 101);
       
   203     QCOMPARE(cache.totalCost(), -2);
       
   204 
       
   205     cache.insert("seven", new int(7), 100);
       
   206     QCOMPARE(cache.totalCost(), 98);
       
   207     QCOMPARE(cache.size(), 5);
       
   208 
       
   209     cache.insert("eight", new int(8), 2);
       
   210     QCOMPARE(cache.totalCost(), 100);
       
   211     QCOMPARE(cache.size(), 6);
       
   212 }
       
   213 
       
   214 void tst_QCache::clear()
       
   215 {
       
   216     {
       
   217         QCache<QString, Foo> cache(200);
       
   218         QCOMPARE(cache.totalCost(), 0);
       
   219 
       
   220         for (int i = -3; i < 9; ++i)
       
   221             cache.insert(QString::number(i), new Foo, i);
       
   222         QCOMPARE(cache.totalCost(), 30);
       
   223 
       
   224         QCOMPARE(cache.size(), 12);
       
   225         QVERIFY(!cache.isEmpty());
       
   226         cache.setMaxCost(300);
       
   227 
       
   228         for (int j = 0; j < 3; ++j) {
       
   229             cache.clear();
       
   230             QCOMPARE(cache.totalCost(), 0);
       
   231             QCOMPARE(cache.size(), 0);
       
   232             QVERIFY(cache.isEmpty());
       
   233             QCOMPARE(Foo::count, 0);
       
   234             QCOMPARE(cache.maxCost(), 300);
       
   235         }
       
   236         cache.insert("10", new Foo, 10);
       
   237         QCOMPARE(cache.size(), 1);
       
   238         cache.setMaxCost(9);
       
   239         QCOMPARE(cache.size(), 0);
       
   240 
       
   241         cache.insert("11", new Foo, 9);
       
   242         QCOMPARE(cache.size(), 1);
       
   243         QCOMPARE(Foo::count, 1);
       
   244     }
       
   245     QCOMPARE(Foo::count, 0);
       
   246 }
       
   247 
       
   248 void tst_QCache::insert()
       
   249 {
       
   250     QCache<QString, Foo> cache;
       
   251 
       
   252     Foo *f1 = new Foo;
       
   253     cache.insert("one", f1, 1);
       
   254     QVERIFY(cache.contains("one"));
       
   255 
       
   256     Foo *f2 = new Foo;
       
   257     cache.insert("two", f2, 2);
       
   258     QVERIFY(cache.contains("two"));
       
   259     QCOMPARE(cache.size(), 2);
       
   260 
       
   261     Foo *f3 = new Foo;
       
   262     cache.insert("two", f3, 2);
       
   263     QVERIFY(cache.contains("two"));
       
   264     QCOMPARE(cache.size(), 2);
       
   265 
       
   266     QVERIFY(cache["two"] == f3);
       
   267     QCOMPARE(Foo::count, 2);
       
   268 
       
   269     /*
       
   270         If the new item is too big, any item with the same name in
       
   271         the cache must still be removed, otherwise the application
       
   272         might get bad results.
       
   273     */
       
   274     Foo *f4 = new Foo;
       
   275     cache.insert("two", f4, 10000);
       
   276     QVERIFY(!cache.contains("two"));
       
   277     QCOMPARE(cache.size(), 1);
       
   278     QCOMPARE(Foo::count, 1);
       
   279 }
       
   280 
       
   281 void tst_QCache::contains()
       
   282 {
       
   283     QCache<int, int> cache;
       
   284     QVERIFY(!cache.contains(0));
       
   285     QVERIFY(!cache.contains(1));
       
   286 
       
   287     cache.insert(1, new int(1), 1);
       
   288     QVERIFY(!cache.contains(0));
       
   289     QVERIFY(cache.contains(1));
       
   290 
       
   291     cache.remove(0);
       
   292     cache.remove(1);
       
   293     QVERIFY(!cache.contains(0));
       
   294     QVERIFY(!cache.contains(1));
       
   295 
       
   296     cache.insert(1, new int(1), 1);
       
   297     QVERIFY(!cache.contains(0));
       
   298     QVERIFY(cache.contains(1));
       
   299 
       
   300     cache.clear();
       
   301     QVERIFY(!cache.contains(0));
       
   302     QVERIFY(!cache.contains(1));
       
   303 }
       
   304 
       
   305 void tst_QCache::operator_bracket_bracket()
       
   306 {
       
   307     QCache<int, int> cache;
       
   308     cache.insert(1, new int(2));
       
   309     QVERIFY(cache[0] == 0);
       
   310     QVERIFY(cache[1] != 0);
       
   311     QCOMPARE(*cache[1], 2);
       
   312 
       
   313     cache.insert(1, new int(4));
       
   314     QVERIFY(cache[1] != 0);
       
   315     QCOMPARE(*cache[1], 4);
       
   316 
       
   317     // check that operator[] doesn't remove the item
       
   318     QVERIFY(cache[1] != 0);
       
   319     QCOMPARE(*cache[1], 4);
       
   320 
       
   321     cache.remove(1);
       
   322     QVERIFY(cache[1] == 0);
       
   323 }
       
   324 
       
   325 void tst_QCache::remove()
       
   326 {
       
   327     QCache<QString, Foo> cache;
       
   328     cache.remove(QString());
       
   329     cache.remove("alpha");
       
   330     QVERIFY(cache.isEmpty());
       
   331 
       
   332     cache.insert("alpha", new Foo, 10);
       
   333     QCOMPARE(cache.size(), 1);
       
   334 
       
   335     cache.insert("beta", new Foo, 20);
       
   336     QCOMPARE(cache.size(), 2);
       
   337 
       
   338     for (int i = 0; i < 10; ++i) {
       
   339         cache.remove("alpha");
       
   340         QCOMPARE(cache.size(), 1);
       
   341         QCOMPARE(cache.totalCost(), 20);
       
   342     }
       
   343 
       
   344     cache.setMaxCost(1);
       
   345     QCOMPARE(cache.size(), 0);
       
   346     cache.remove("beta");
       
   347     QCOMPARE(cache.size(), 0);
       
   348 }
       
   349 
       
   350 void tst_QCache::take()
       
   351 {
       
   352     QCache<QString, Foo> cache;
       
   353     QCOMPARE(cache.take(QString()), (Foo*)0);
       
   354     QCOMPARE(cache.take("alpha"), (Foo*)0);
       
   355     QVERIFY(cache.isEmpty());
       
   356 
       
   357     Foo *f1 = new Foo;
       
   358     cache.insert("alpha", f1, 10);
       
   359     QCOMPARE(cache.size(), 1);
       
   360     QVERIFY(cache["alpha"] == f1);
       
   361 
       
   362     cache.insert("beta", new Foo, 20);
       
   363     QCOMPARE(cache.size(), 2);
       
   364 
       
   365     QCOMPARE(cache.take("alpha"), f1);
       
   366     QCOMPARE(cache.size(), 1);
       
   367     QCOMPARE(cache.totalCost(), 20);
       
   368     QCOMPARE(Foo::count, 2);
       
   369     delete f1;
       
   370     QCOMPARE(Foo::count, 1);
       
   371 
       
   372     QCOMPARE(cache.take("alpha"), (Foo*)0);
       
   373     QCOMPARE(Foo::count, 1);
       
   374     QCOMPARE(cache.size(), 1);
       
   375     QCOMPARE(cache.totalCost(), 20);
       
   376 
       
   377     cache.setMaxCost(1);
       
   378     QCOMPARE(cache.size(), 0);
       
   379     QCOMPARE(cache.take("beta"), (Foo*)0);
       
   380     QCOMPARE(cache.size(), 0);
       
   381 }
       
   382 
       
   383 struct KeyType
       
   384 {
       
   385     int foo;
       
   386 
       
   387     KeyType(int x) : foo(x) {}
       
   388 
       
   389 private:
       
   390     KeyType &operator=(const KeyType &);
       
   391 };
       
   392 
       
   393 struct ValueType
       
   394 {
       
   395     int foo;
       
   396 
       
   397     ValueType(int x) : foo(x) {}
       
   398 
       
   399 private:
       
   400     ValueType(const ValueType &);
       
   401     ValueType &operator=(const ValueType &);
       
   402 };
       
   403 
       
   404 bool operator==(const KeyType &key1, const KeyType &key2)
       
   405 {
       
   406     return key1.foo == key2.foo;
       
   407 }
       
   408 
       
   409 uint qHash(const KeyType &key)
       
   410 {
       
   411     return qHash(key.foo);
       
   412 }
       
   413 
       
   414 void tst_QCache::axioms_on_key_type()
       
   415 {
       
   416     QCache<KeyType, ValueType> foo;
       
   417     foo.setMaxCost(1);
       
   418     foo.clear();
       
   419     foo.insert(KeyType(123), new ValueType(123));
       
   420     foo.object(KeyType(123));
       
   421     foo.contains(KeyType(456));
       
   422     foo[KeyType(456)];
       
   423     foo.remove(KeyType(456));
       
   424     foo.remove(KeyType(123));
       
   425     foo.take(KeyType(789));
       
   426 // If this fails, contact the maintaner
       
   427     QVERIFY(sizeof(QHash<int, int>) == sizeof(void *));
       
   428 }
       
   429 
       
   430 QTEST_APPLESS_MAIN(tst_QCache)
       
   431 #include "tst_qcache.moc"