Пример #1
0
void tst_QCache::take()
{
    QCache<QString, Foo> cache;
    QCOMPARE(cache.take(QString()), (Foo*)0);
    QCOMPARE(cache.take("alpha"), (Foo*)0);
    QVERIFY(cache.isEmpty());

    Foo *f1 = new Foo;
    cache.insert("alpha", f1, 10);
    QCOMPARE(cache.size(), 1);
    QVERIFY(cache["alpha"] == f1);

    cache.insert("beta", new Foo, 20);
    QCOMPARE(cache.size(), 2);

    QCOMPARE(cache.take("alpha"), f1);
    QCOMPARE(cache.size(), 1);
    QCOMPARE(cache.totalCost(), 20);
    QCOMPARE(Foo::count, 2);
    delete f1;
    QCOMPARE(Foo::count, 1);

    QCOMPARE(cache.take("alpha"), (Foo*)0);
    QCOMPARE(Foo::count, 1);
    QCOMPARE(cache.size(), 1);
    QCOMPARE(cache.totalCost(), 20);

    cache.setMaxCost(1);
    QCOMPARE(cache.size(), 0);
    QCOMPARE(cache.take("beta"), (Foo*)0);
    QCOMPARE(cache.size(), 0);
}
Пример #2
0
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();
}
Пример #3
0
void tst_QCache::setMaxCost()
{
    QCache<int, Foo> cache;
    cache.setMaxCost(2);
    cache.insert(1, new Foo);
    cache.insert(2, new Foo);
    QCOMPARE(cache.totalCost(), 2);
    QCOMPARE(Foo::count, 2);

    cache.insert(3, new Foo);
    QCOMPARE(cache.totalCost(), 2);
    QCOMPARE(Foo::count, 2);

    cache.setMaxCost(3);
    QCOMPARE(cache.totalCost(), 2);
    QCOMPARE(Foo::count, 2);

    cache.setMaxCost(2);
    QCOMPARE(cache.totalCost(), 2);
    QCOMPARE(Foo::count, 2);

    cache.setMaxCost(1);
    QCOMPARE(cache.totalCost(), 1);
    QCOMPARE(Foo::count, 1);

    cache.setMaxCost(0);
    QCOMPARE(cache.totalCost(), 0);
    QCOMPARE(Foo::count, 0);

    cache.setMaxCost(-1);
    QCOMPARE(cache.totalCost(), 0);
    QCOMPARE(Foo::count, 0);
}
Пример #4
0
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 *));
}
Пример #5
0
void tst_QCache::remove()
{
    QCache<QString, Foo> cache;
    cache.remove(QString());
    cache.remove("alpha");
    QVERIFY(cache.isEmpty());

    cache.insert("alpha", new Foo, 10);
    QCOMPARE(cache.size(), 1);

    cache.insert("beta", new Foo, 20);
    QCOMPARE(cache.size(), 2);

    for (int i = 0; i < 10; ++i) {
        cache.remove("alpha");
        QCOMPARE(cache.size(), 1);
        QCOMPARE(cache.totalCost(), 20);
    }

    cache.setMaxCost(1);
    QCOMPARE(cache.size(), 0);
    cache.remove("beta");
    QCOMPARE(cache.size(), 0);
}