forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CacheTest.h
131 lines (114 loc) · 3.41 KB
/
CacheTest.h
1
2
3
4
5
6
7
8
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include <cxxtest/TestSuite.h>
// The test requires the hit/miss stats. Note that this only works because the
// Cache class
// is header only
#define USE_CACHE_STATS
#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 = 0;
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);
}
};
// Remove the define here
#undef USE_CACHE_STATS
class CacheTestPerformance : public CxxTest::TestSuite {
public:
void setUp() override {
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;
};