new tests for the UniqueAccess caches

This commit is contained in:
Peter Schojer 2007-10-29 14:50:09 +00:00
parent 383929fb39
commit 6987146b6c
4 changed files with 90 additions and 4 deletions

View File

@ -1,7 +1,7 @@
//
// ExpireCacheTest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/ExpireCacheTest.cpp#9 $
// $Id: //poco/Main/Foundation/testsuite/src/ExpireCacheTest.cpp#10 $
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
@ -35,6 +35,7 @@
#include "CppUnit/TestSuite.h"
#include "Poco/Exception.h"
#include "Poco/ExpireCache.h"
#include "Poco/AccessExpireCache.h"
#include "Poco/Bugcheck.h"
#include "Poco/Thread.h"
@ -155,6 +156,46 @@ void ExpireCacheTest::testDuplicateAdd()
}
void ExpireCacheTest::testAccessExpireN()
{
// 3-1 represents the cache sorted by age, elements get replaced at the end of the list
// 3-1|5 -> 5 gets removed
AccessExpireCache<int, int> aCache(DURSLEEP);
aCache.add(1, 2); // 1
assert (aCache.has(1));
SharedPtr<int> tmp = aCache.get(1);
assert (!tmp.isNull());
assert (*tmp == 2);
assert (aCache.size() == 1);
Thread::sleep(DURWAIT);
assert (aCache.size() == 0);
assert (!aCache.has(1));
// tmp must still be valid, access it
assert (*tmp == 2);
tmp = aCache.get(1);
assert (!tmp);
aCache.add(1, 2); // 1
Thread::sleep(DURHALFSLEEP);
aCache.add(3, 4); // 3-1
assert (aCache.has(1));
assert (aCache.has(3));
Thread::sleep(DURHALFSLEEP+50); //3|1
assert (!aCache.has(1));
assert (*aCache.get(3) == 4);
Thread::sleep(DURHALFSLEEP+25); //3|1
assert (*aCache.get(3) == 4);
// removing illegal entries should work too
aCache.remove(666);
aCache.clear();
assert (!aCache.has(5));
assert (!aCache.has(3));
}
void ExpireCacheTest::setUp()
{
}
@ -173,6 +214,7 @@ CppUnit::Test* ExpireCacheTest::suite()
CppUnit_addTest(pSuite, ExpireCacheTest, testExpire0);
CppUnit_addTest(pSuite, ExpireCacheTest, testExpireN);
CppUnit_addTest(pSuite, ExpireCacheTest, testDuplicateAdd);
CppUnit_addTest(pSuite, ExpireCacheTest, testAccessExpireN);
return pSuite;
}

View File

@ -1,7 +1,7 @@
//
// ExpireCacheTest.h
//
// $Id: //poco/Main/Foundation/testsuite/src/ExpireCacheTest.h#5 $
// $Id: //poco/Main/Foundation/testsuite/src/ExpireCacheTest.h#6 $
//
// Tests for ExpireCache
//
@ -49,6 +49,7 @@ public:
void testDuplicateAdd();
void testExpire0();
void testExpireN();
void testAccessExpireN();
void setUp();

View File

@ -1,7 +1,7 @@
//
// ExpireLRUCacheTest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/ExpireLRUCacheTest.cpp#8 $
// $Id: //poco/Main/Foundation/testsuite/src/ExpireLRUCacheTest.cpp#9 $
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
@ -35,6 +35,7 @@
#include "CppUnit/TestSuite.h"
#include "Poco/Exception.h"
#include "Poco/ExpireLRUCache.h"
#include "Poco/AccessExpireLRUCache.h"
#include "Poco/Bugcheck.h"
#include "Poco/Thread.h"
@ -141,6 +142,46 @@ void ExpireLRUCacheTest::testExpireN()
}
void ExpireLRUCacheTest::testAccessExpireN()
{
// 3-1 represents the cache sorted by age, elements get replaced at the end of the list
// 3-1|5 -> 5 gets removed
AccessExpireLRUCache<int, int> aCache(3, DURSLEEP);
aCache.add(1, 2); // 1
assert (aCache.has(1));
SharedPtr<int> tmp = aCache.get(1);
assert (!tmp.isNull());
assert (*tmp == 2);
assert (aCache.size() == 1);
Thread::sleep(DURWAIT);
assert (aCache.size() == 0);
assert (!aCache.has(1));
// tmp must still be valid, access it
assert (*tmp == 2);
tmp = aCache.get(1);
assert (!tmp);
aCache.add(1, 2); // 1
Thread::sleep(DURHALFSLEEP);
aCache.add(3, 4); // 3-1
assert (aCache.has(1));
assert (aCache.has(3));
Thread::sleep(DURHALFSLEEP+50); //3|1
assert (!aCache.has(1));
assert (*aCache.get(3) == 4);
Thread::sleep(DURHALFSLEEP+25); //3|1
assert (*aCache.get(3) == 4);
// removing illegal entries should work too
aCache.remove(666);
aCache.clear();
assert (!aCache.has(5));
assert (!aCache.has(3));
}
void ExpireLRUCacheTest::testCacheSize0()
{
// cache size 0 is illegal
@ -301,6 +342,7 @@ CppUnit::Test* ExpireLRUCacheTest::suite()
CppUnit_addTest(pSuite, ExpireLRUCacheTest, testClear);
CppUnit_addTest(pSuite, ExpireLRUCacheTest, testExpire0);
CppUnit_addTest(pSuite, ExpireLRUCacheTest, testExpireN);
CppUnit_addTest(pSuite, ExpireLRUCacheTest, testAccessExpireN);
CppUnit_addTest(pSuite, ExpireLRUCacheTest, testCacheSize0);
CppUnit_addTest(pSuite, ExpireLRUCacheTest, testCacheSize1);
CppUnit_addTest(pSuite, ExpireLRUCacheTest, testCacheSize2);

View File

@ -1,7 +1,7 @@
//
// ExpireLRUCacheTest.h
//
// $Id: //poco/Main/Foundation/testsuite/src/ExpireLRUCacheTest.h#5 $
// $Id: //poco/Main/Foundation/testsuite/src/ExpireLRUCacheTest.h#6 $
//
// Tests for ExpireLRUCache
//
@ -48,6 +48,7 @@ public:
void testClear();
void testExpire0();
void testExpireN();
void testAccessExpireN();
void testCacheSize0();
void testCacheSize1();
void testCacheSize2();