Newer
Older
#ifndef CACHETEST_H_
#define CACHETEST_H_
#include <cxxtest/TestSuite.h>
Gigg, Martyn Anthony
committed
// The test requires the hit/miss stats. Note that this only works because the Cache class
// is header only
#define USE_CACHE_STATS
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "MantidKernel/Cache.h"
using namespace Mantid::Kernel;
class CacheTest : public CxxTest::TestSuite
{
public:
void testConstructor()
{
Cache<bool,double> c;
// Test that all the base class member variables are correctly assigned to
TS_ASSERT_EQUALS(c.size(), 0);
TS_ASSERT_EQUALS(c.hitCount(), 0);
TS_ASSERT_EQUALS(c.missCount(), 0);
TS_ASSERT_EQUALS(c.hitRatio(), 0);
}
void testSetCache()
{
Cache<int,int> c;
c.setCache(1,1);
TS_ASSERT_EQUALS(c.size(), 1);
TS_ASSERT_EQUALS(c.hitCount(), 0);
TS_ASSERT_EQUALS(c.missCount(), 0);
TS_ASSERT_EQUALS(c.hitRatio(), 0);
}
void testSetCacheOverwrite()
{
Cache<int,int> c;
c.setCache(1,1);
TS_ASSERT_EQUALS(c.size(), 1);
TS_ASSERT_EQUALS(c.hitCount(), 0);
TS_ASSERT_EQUALS(c.missCount(), 0);
TS_ASSERT_EQUALS(c.hitRatio(), 0);
//overwrite
c.setCache(1,1);
TS_ASSERT_EQUALS(c.size(), 1);
TS_ASSERT_EQUALS(c.hitCount(), 0);
TS_ASSERT_EQUALS(c.missCount(), 0);
TS_ASSERT_EQUALS(c.hitRatio(), 0);
}
void testClear()
{
Cache<int,int> c;
c.setCache(1,1);
c.setCache(2,1);
TS_ASSERT_EQUALS(c.size(), 2);
c.clear();
TS_ASSERT_EQUALS(c.size(), 0);
TS_ASSERT_EQUALS(c.hitCount(), 0);
TS_ASSERT_EQUALS(c.missCount(), 0);
TS_ASSERT_EQUALS(c.hitRatio(), 0);
}
void testgetCache()
{
//set up cache
Cache<int,int> c;
c.setCache(1,1);
c.setCache(2,2);
c.setCache(3,3);
c.setCache(4,4);
TS_ASSERT_EQUALS(c.size(), 4);
TS_ASSERT_EQUALS(c.hitCount(), 0);
TS_ASSERT_EQUALS(c.missCount(), 0);
TS_ASSERT_EQUALS(c.hitRatio(), 0);
//test hit
TS_ASSERT_EQUALS(c.getCache(1,value), true);
TS_ASSERT_EQUALS(value, 1);
TS_ASSERT_EQUALS(c.hitCount(), 1);
TS_ASSERT_EQUALS(c.missCount(), 0);
TS_ASSERT_DELTA(c.hitRatio(), 100, 1e-6);
//test miss
TS_ASSERT_EQUALS(c.getCache(5,value), false);
TS_ASSERT_EQUALS(value, 1);
TS_ASSERT_EQUALS(c.hitCount(), 1);
TS_ASSERT_EQUALS(c.missCount(), 1);
TS_ASSERT_DELTA(c.hitRatio(), 50, 1e-6);
//test hit
TS_ASSERT_EQUALS(c.getCache(4,value), true);
TS_ASSERT_EQUALS(value, 4);
TS_ASSERT_EQUALS(c.hitCount(), 2);
TS_ASSERT_EQUALS(c.missCount(), 1);
TS_ASSERT_DELTA(c.hitRatio(), 66.666666, 1e-6);
}
};
Gigg, Martyn Anthony
committed
// Remove the define here
#undef USE_CACHE_STATS
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class CacheTestPerformance : public CxxTest::TestSuite
{
public:
void setUp()
{
m_nelements = 250000;
for(int i = 0; i < m_nelements; ++i)
{
m_cacheGet.setCache(i, 1.5);
}
}
void test_get_performance()
{
double value(0.0);
for(int i = 0; i < m_nelements; ++i)
{
m_cacheGet.getCache(i, value);
}
}
void test_set_performance()
{
for(int i = 0; i < m_nelements; ++i)
{
m_cacheSet.setCache(i, 1.5);
}
}
int m_nelements;
Cache<int,double> m_cacheGet;
Cache<int,double> m_cacheSet;
};