1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <qtest.h>
43 #include <QPixmapCache>
44 //TESTED_FILES=
45 
46 class tst_QPixmapCache : public QObject
47 {
48     Q_OBJECT
49 
50 public:
51     tst_QPixmapCache();
52     virtual ~tst_QPixmapCache();
53 
54 public slots:
55     void init();
56     void cleanup();
57 
58 private slots:
59     void insert_data();
60     void insert();
61     void find_data();
62     void find();
63     void styleUseCaseComplexKey();
64     void styleUseCaseComplexKey_data();
65 };
66 
tst_QPixmapCache()67 tst_QPixmapCache::tst_QPixmapCache()
68 {
69 }
70 
~tst_QPixmapCache()71 tst_QPixmapCache::~tst_QPixmapCache()
72 {
73 }
74 
init()75 void tst_QPixmapCache::init()
76 {
77 }
78 
cleanup()79 void tst_QPixmapCache::cleanup()
80 {
81 }
82 
insert_data()83 void tst_QPixmapCache::insert_data()
84 {
85     QTest::addColumn<bool>("cacheType");
86     QTest::newRow("QPixmapCache") << true;
87     QTest::newRow("QPixmapCache (int API)") << false;
88 }
89 
90 QList<QPixmapCache::Key> keys;
91 
insert()92 void tst_QPixmapCache::insert()
93 {
94     QFETCH(bool, cacheType);
95     QPixmap p;
96     if (cacheType) {
97         QBENCHMARK {
98             for (int i = 0 ; i <= 10000 ; i++)
99             {
100                 QString tmp;
101                 tmp.sprintf("my-key-%d", i);
102                 QPixmapCache::insert(tmp, p);
103             }
104         }
105     } else {
106         QBENCHMARK {
107             for (int i = 0 ; i <= 10000 ; i++)
108                 keys.append(QPixmapCache::insert(p));
109         }
110     }
111 }
112 
find_data()113 void tst_QPixmapCache::find_data()
114 {
115     QTest::addColumn<bool>("cacheType");
116     QTest::newRow("QPixmapCache") << true;
117     QTest::newRow("QPixmapCache (int API)") << false;
118 }
119 
find()120 void tst_QPixmapCache::find()
121 {
122     QFETCH(bool, cacheType);
123     QPixmap p;
124     if (cacheType) {
125         QBENCHMARK {
126             QString tmp;
127             for (int i = 0 ; i <= 10000 ; i++)
128             {
129                 tmp.sprintf("my-key-%d", i);
130                 QPixmapCache::find(tmp, p);
131             }
132         }
133     } else {
134         QBENCHMARK {
135             for (int i = 0 ; i <= 10000 ; i++)
136                 QPixmapCache::find(keys.at(i), &p);
137         }
138     }
139 
140 }
141 
styleUseCaseComplexKey_data()142 void tst_QPixmapCache::styleUseCaseComplexKey_data()
143 {
144     QTest::addColumn<bool>("cacheType");
145     QTest::newRow("QPixmapCache") << true;
146     QTest::newRow("QPixmapCache (int API)") << false;
147 }
148 
149 struct styleStruct {
150     QString key;
151     uint state;
152     uint direction;
153     uint complex;
154     uint palette;
155     int width;
156     int height;
operator ==styleStruct157     bool operator==(const styleStruct &str) const
158     {
159         return  str.state == state && str.direction == direction
160                 && str.complex == complex && str.palette == palette && str.width == width
161                 && str.height == height && str.key == key;
162     }
163 };
164 
qHash(const styleStruct & myStruct)165 uint qHash(const styleStruct &myStruct)
166 {
167     return qHash(myStruct.state);
168 }
169 
styleUseCaseComplexKey()170 void tst_QPixmapCache::styleUseCaseComplexKey()
171 {
172     QFETCH(bool, cacheType);
173     QPixmap p;
174     if (cacheType) {
175         QBENCHMARK {
176             for (int i = 0 ; i <= 10000 ; i++)
177             {
178                 QString tmp;
179                 tmp.sprintf("%s-%d-%d-%d-%d-%d-%d", QString("my-progressbar-%1").arg(i).toLatin1().constData(), 5, 3, 0, 358, 100, 200);
180                 QPixmapCache::insert(tmp, p);
181             }
182 
183             for (int i = 0 ; i <= 10000 ; i++)
184             {
185                 QString tmp;
186                 tmp.sprintf("%s-%d-%d-%d-%d-%d-%d", QString("my-progressbar-%1").arg(i).toLatin1().constData(), 5, 3, 0, 358, 100, 200);
187                 QPixmapCache::find(tmp, p);
188             }
189         }
190     } else {
191         QHash<styleStruct, QPixmapCache::Key> hash;
192         QBENCHMARK {
193             for (int i = 0 ; i <= 10000 ; i++)
194             {
195                 styleStruct myStruct;
196                 myStruct.key = QString("my-progressbar-%1").arg(i);
197                 myStruct.key = 5;
198                 myStruct.key = 4;
199                 myStruct.key = 3;
200                 myStruct.palette = 358;
201                 myStruct.width = 100;
202                 myStruct.key = 200;
203                 QPixmapCache::Key key = QPixmapCache::insert(p);
204                 hash.insert(myStruct, key);
205             }
206             for (int i = 0 ; i <= 10000 ; i++)
207             {
208                 styleStruct myStruct;
209                 myStruct.key = QString("my-progressbar-%1").arg(i);
210                 myStruct.key = 5;
211                 myStruct.key = 4;
212                 myStruct.key = 3;
213                 myStruct.palette = 358;
214                 myStruct.width = 100;
215                 myStruct.key = 200;
216                 QPixmapCache::Key key = hash.value(myStruct);
217                 QPixmapCache::find(key, &p);
218             }
219         }
220     }
221 
222 }
223 
224 
225 QTEST_MAIN(tst_QPixmapCache)
226 #include "tst_qpixmapcache.moc"
227