new tests for the UniqueAccess caches

This commit is contained in:
Peter Schojer 2007-10-29 14:48:39 +00:00
parent 65579e4612
commit 383929fb39
4 changed files with 109 additions and 5 deletions

View File

@ -1,7 +1,7 @@
//
// UniqueExpireCacheTest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/UniqueExpireCacheTest.cpp#4 $
// $Id: //poco/Main/Foundation/testsuite/src/UniqueExpireCacheTest.cpp#5 $
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
@ -35,7 +35,9 @@
#include "CppUnit/TestSuite.h"
#include "Poco/Exception.h"
#include "Poco/UniqueExpireCache.h"
#include "Poco/UniqueAccessExpireCache.h"
#include "Poco/ExpirationDecorator.h"
#include "Poco/AccessExpirationDecorator.h"
#include "Poco/Bugcheck.h"
#include "Poco/Thread.h"
@ -58,6 +60,7 @@ struct IntVal
}
};
typedef AccessExpirationDecorator<int> DIntVal;
#define DURSLEEP 250
#define DURHALFSLEEP DURSLEEP / 2
@ -93,6 +96,52 @@ void UniqueExpireCacheTest::testClear()
}
void UniqueExpireCacheTest::testAccessClear()
{
UniqueAccessExpireCache<int, DIntVal> aCache;
aCache.add(1, DIntVal(2, DURSLEEP));
aCache.add(3, DIntVal(4, DURSLEEP));
aCache.add(5, DIntVal(6, DURSLEEP));
assert (aCache.has(1));
assert (aCache.has(3));
assert (aCache.has(5));
assert (aCache.get(1)->value() == 2);
assert (aCache.get(3)->value() == 4);
assert (aCache.get(5)->value() == 6);
aCache.clear();
assert (!aCache.has(1));
assert (!aCache.has(3));
assert (!aCache.has(5));
}
void UniqueExpireCacheTest::testAccessUpdate()
{
UniqueAccessExpireCache<int, DIntVal> aCache;
aCache.add(1, DIntVal(2, DURSLEEP));
aCache.add(3, DIntVal(4, DURSLEEP));
aCache.add(5, DIntVal(6, DURSLEEP));
assert (aCache.has(1));
assert (aCache.has(3));
assert (aCache.has(5));
assert (aCache.get(1)->value() == 2);
Thread::sleep(DURSLEEP/2);
assert (aCache.get(1)->value() == 2);
Thread::sleep(DURSLEEP/2);
assert (aCache.get(1)->value() == 2);
Thread::sleep(DURSLEEP/2);
assert (aCache.get(1)->value() == 2);
assert (!aCache.has(3));
assert (!aCache.has(5));
Thread::sleep(DURSLEEP*2);
assert (!aCache.has(1));
assert (!aCache.has(3));
assert (!aCache.has(5));
aCache.remove(666); //must work too
}
void UniqueExpireCacheTest::testExpire0()
{
UniqueExpireCache<int, IntVal> aCache;
@ -101,6 +150,15 @@ void UniqueExpireCacheTest::testExpire0()
}
void UniqueExpireCacheTest::testAccessExpire0()
{
UniqueAccessExpireCache<int, DIntVal> aCache;
aCache.add(1, DIntVal(2, Timespan(0, 0)));
assert (!aCache.has(1));
}
void UniqueExpireCacheTest::testExpireN()
{
// 3-1 represents the cache sorted by age, elements get replaced at the end of the list
@ -165,6 +223,18 @@ void UniqueExpireCacheTest::testDuplicateAdd()
}
void UniqueExpireCacheTest::testAccessDuplicateAdd()
{
UniqueAccessExpireCache<int, DIntVal> aCache;
aCache.add(1, DIntVal(2, DURSLEEP)); // 1
assert (aCache.has(1));
assert (aCache.get(1)->value() == 2);
aCache.add(1, DIntVal(3, DURSLEEP));
assert (aCache.has(1));
assert (aCache.get(1)->value() == 3);
}
void UniqueExpireCacheTest::testExpirationDecorator()
{
typedef ExpirationDecorator<int> ExpireInt;
@ -193,9 +263,13 @@ CppUnit::Test* UniqueExpireCacheTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("UniqueExpireCacheTest");
CppUnit_addTest(pSuite, UniqueExpireCacheTest, testClear);
CppUnit_addTest(pSuite, UniqueExpireCacheTest, testAccessClear);
CppUnit_addTest(pSuite, UniqueExpireCacheTest, testAccessUpdate);
CppUnit_addTest(pSuite, UniqueExpireCacheTest, testExpire0);
CppUnit_addTest(pSuite, UniqueExpireCacheTest, testAccessExpire0);
CppUnit_addTest(pSuite, UniqueExpireCacheTest, testExpireN);
CppUnit_addTest(pSuite, UniqueExpireCacheTest, testDuplicateAdd);
CppUnit_addTest(pSuite, UniqueExpireCacheTest, testAccessDuplicateAdd);
CppUnit_addTest(pSuite, UniqueExpireCacheTest, testExpirationDecorator);
return pSuite;

View File

@ -1,7 +1,7 @@
//
// UniqueExpireCacheTest.h
//
// $Id: //poco/Main/Foundation/testsuite/src/UniqueExpireCacheTest.h#2 $
// $Id: //poco/Main/Foundation/testsuite/src/UniqueExpireCacheTest.h#3 $
//
// Tests for ExpireCache
//
@ -46,11 +46,14 @@ public:
~UniqueExpireCacheTest();
void testClear();
void testAccessClear();
void testDuplicateAdd();
void testAccessDuplicateAdd();
void testExpire0();
void testAccessExpire0();
void testExpireN();
void testExpirationDecorator();
void testAccessUpdate();
void setUp();
void tearDown();

View File

@ -1,7 +1,7 @@
//
// UniqueExpireLRUCacheTest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/UniqueExpireLRUCacheTest.cpp#2 $
// $Id: //poco/Main/Foundation/testsuite/src/UniqueExpireLRUCacheTest.cpp#3 $
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
@ -35,6 +35,8 @@
#include "CppUnit/TestSuite.h"
#include "Poco/Exception.h"
#include "Poco/UniqueExpireLRUCache.h"
#include "Poco/UniqueAccessExpireLRUCache.h"
#include "Poco/AccessExpirationDecorator.h"
#include "Poco/Bugcheck.h"
#include "Poco/Thread.h"
@ -57,6 +59,10 @@ struct IntVal
}
};
typedef AccessExpirationDecorator<int> DIntVal;
#define DURSLEEP 250
#define DURHALFSLEEP DURSLEEP / 2
#define DURWAIT 300
@ -91,6 +97,25 @@ void UniqueExpireLRUCacheTest::testClear()
}
void UniqueExpireLRUCacheTest::testAccessClear()
{
UniqueAccessExpireLRUCache<int, DIntVal> aCache;
aCache.add(1, DIntVal(2, DURSLEEP));
aCache.add(3, DIntVal(4, DURSLEEP));
aCache.add(5, DIntVal(6, DURSLEEP));
assert (aCache.has(1));
assert (aCache.has(3));
assert (aCache.has(5));
assert (aCache.get(1)->value() == 2);
assert (aCache.get(3)->value() == 4);
assert (aCache.get(5)->value() == 6);
aCache.clear();
assert (!aCache.has(1));
assert (!aCache.has(3));
assert (!aCache.has(5));
}
void UniqueExpireLRUCacheTest::testExpire0()
{
UniqueExpireLRUCache<int, IntVal> aCache;
@ -309,6 +334,7 @@ CppUnit::Test* UniqueExpireLRUCacheTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("UniqueExpireLRUCacheTest");
CppUnit_addTest(pSuite, UniqueExpireLRUCacheTest, testClear);
CppUnit_addTest(pSuite, UniqueExpireLRUCacheTest, testAccessClear);
CppUnit_addTest(pSuite, UniqueExpireLRUCacheTest, testExpire0);
CppUnit_addTest(pSuite, UniqueExpireLRUCacheTest, testExpireN);
CppUnit_addTest(pSuite, UniqueExpireLRUCacheTest, testCacheSize0);

View File

@ -1,7 +1,7 @@
//
// UniqueExpireLRUCacheTest.h
//
// $Id: //poco/Main/Foundation/testsuite/src/UniqueExpireLRUCacheTest.h#1 $
// $Id: //poco/Main/Foundation/testsuite/src/UniqueExpireLRUCacheTest.h#2 $
//
// Tests for UniqueExpireLRUCache
//
@ -46,6 +46,7 @@ public:
~UniqueExpireLRUCacheTest();
void testClear();
void testAccessClear();
void testExpire0();
void testExpireN();
void testCacheSize0();