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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#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
int value;
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