本文整理汇总了C++中QCache::size方法的典型用法代码示例。如果您正苦于以下问题:C++ QCache::size方法的具体用法?C++ QCache::size怎么用?C++ QCache::size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QCache
的用法示例。
在下文中一共展示了QCache::size方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: insert
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);
}
示例2: totalCost
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);
}
示例3: take
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);
}
示例4: remove
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);
}