void tst_QCache::insert() { QCache<QString, Foo> cache; Foo *f1 = new Foo; cache.insert("one", f1, 1); QVERIFY(cache.contains("one")); Foo *f2 = new Foo; cache.insert("two", f2, 2); QVERIFY(cache.contains("two")); QCOMPARE(cache.size(), 2); Foo *f3 = new Foo; cache.insert("two", f3, 2); QVERIFY(cache.contains("two")); QCOMPARE(cache.size(), 2); QVERIFY(cache["two"] == f3); QCOMPARE(Foo::count, 2); /* If the new item is too big, any item with the same name in the cache must still be removed, otherwise the application might get bad results. */ Foo *f4 = new Foo; cache.insert("two", f4, 10000); QVERIFY(!cache.contains("two")); QCOMPARE(cache.size(), 1); QCOMPARE(Foo::count, 1); }
void tst_QCache::totalCost() { QCache<QString, int> cache; QCOMPARE(cache.totalCost(), 0); cache.insert("one", new int(1), 0); QCOMPARE(cache.totalCost(), 0); cache.insert("two", new int(2), 1); QCOMPARE(cache.totalCost(), 1); cache.insert("three", new int(3), 2); QCOMPARE(cache.totalCost(), 3); cache.insert("four", new int(4), 10000); QCOMPARE(cache.totalCost(), 3); QVERIFY(!cache.contains("four")); cache.insert("five", new int(5), -5); QCOMPARE(cache.totalCost(), -2); cache.insert("six", new int(6), 101); QCOMPARE(cache.totalCost(), -2); cache.insert("seven", new int(7), 100); QCOMPARE(cache.totalCost(), 98); QCOMPARE(cache.size(), 5); cache.insert("eight", new int(8), 2); QCOMPARE(cache.totalCost(), 100); QCOMPARE(cache.size(), 6); }
QIcon get_file_icon(const QString& content_type) { static QCache<QString, QIcon> cache; cache.setMaxCost(100); if (cache.contains(content_type)) return QIcon(*(cache.object(content_type))); QByteArray ba = content_type.toLocal8Bit(); GIcon* gicon = g_content_type_get_icon(ba.constData()); QStringList tried_names; if (G_IS_THEMED_ICON(gicon)) { const gchar * const * names = g_themed_icon_get_names(reinterpret_cast<GThemedIcon*>(gicon)); if (names != 0) { int i = 0; while(names[i] != 0) { QString name = QString::fromLocal8Bit(names[i]); QIcon r = QIcon::fromTheme(name); if (r.isNull()) { tried_names << name; } else { cache.insert(content_type, new QIcon(r)); g_object_unref(gicon); return r; } i++; } } else { qDebug() << "get_file_icon: empty or invalid result of g_themed_icon_get_names"; } } else { qDebug() << "get_file_icon: gicon is not themed icon"; } qDebug() << "get_file_icon(" << content_type << "): no valid names" << tried_names << "; returning null icon"; cache.insert(content_type, new QIcon()); g_object_unref(gicon); return QIcon(); }
void tst_QCache::axioms_on_key_type() { QCache<KeyType, ValueType> foo; foo.setMaxCost(1); foo.clear(); foo.insert(KeyType(123), new ValueType(123)); foo.object(KeyType(123)); foo.contains(KeyType(456)); foo[KeyType(456)]; foo.remove(KeyType(456)); foo.remove(KeyType(123)); foo.take(KeyType(789)); // If this fails, contact the maintaner QVERIFY(sizeof(QHash<int, int>) == sizeof(void *)); }
QString DateStringBuilder::getShortDate(const KDateTime &dateTime) { if (!dateTime.isValid() || dateTime.isNull()) { return QString(); } QDate currentDate = QDateTime().currentDateTime().date(); if (currentDate.weekNumber() == dateTime.date().weekNumber() || currentDate.addDays(1) == dateTime.date()) { //this week or tomorrow (i.e. on sunday) return getDayName(dateTime); } if (currentDate.year() == dateTime.date().year()) { //this year //Micro optimization because this function showed up as hotspot static QCache<uint, QString> cache; uint hash = dateTime.date().month() ^ dateTime.date().day(); if (!cache.contains(hash)) { cache.insert(hash, new QString(dateTime.toString("%d.%m"))); } return *cache[hash]; } return dateTime.toString("%d.%m.%Y"); }
void tst_QCache::contains() { QCache<int, int> cache; QVERIFY(!cache.contains(0)); QVERIFY(!cache.contains(1)); cache.insert(1, new int(1), 1); QVERIFY(!cache.contains(0)); QVERIFY(cache.contains(1)); cache.remove(0); cache.remove(1); QVERIFY(!cache.contains(0)); QVERIFY(!cache.contains(1)); cache.insert(1, new int(1), 1); QVERIFY(!cache.contains(0)); QVERIFY(cache.contains(1)); cache.clear(); QVERIFY(!cache.contains(0)); QVERIFY(!cache.contains(1)); }
bool QGLBlurTextureCache::hasBlurTextureInfo(quint64 cacheKey) const { return cache.contains(cacheKey); }