forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_opmphm_predictor.c
265 lines (231 loc) · 7.99 KB
/
test_opmphm_predictor.c
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* @file
*
* @brief
*
* @copyright BSD License (see doc/LICENSE.md or https://www.libelektra.org)
*/
#include <opmphmpredictor.c>
#include <tests_internal.h>
void test_internal_basic (void)
{
OpmphmPredictor * op = opmphmPredictorNew ();
exit_if_fail (op, "opmphmPredictorNew");
succeed_if (!op->history, "history init");
succeed_if (!op->lookupCount, "lookupCount init");
succeed_if (!op->ksSize, "lookupCount init");
succeed_if (op->size, "size init");
for (size_t i = 0; i < op->size; ++i)
{
succeed_if (!op->patternTable[i], "patternTable init");
}
opmphmPredictorDel (op);
}
void test_internal_nochange (void)
{
OpmphmPredictor * op = opmphmPredictorNew ();
exit_if_fail (op, "opmphmPredictorNew");
const size_t n = 1000;
succeed_if (!opmphmPredictor (op, n), "first prediction");
succeed_if (op->lookupCount == 1, "lookupCount");
for (size_t i = 1; i < opmphmPredictorWorthOpmphm (n) * 3; ++i)
{
// no opmphm
succeed_if (!opmphmPredictorIncCountBinarySearch (op, n), "binary search usage");
succeed_if (op->lookupCount == i + 1, "lookupCount");
}
// switch to opmphm
succeed_if (opmphmPredictorIncCountBinarySearch (op, n), "hash usage");
succeed_if (op->lookupCount == opmphmPredictorWorthOpmphm (n) * 3 + 1, "lookupCount");
opmphmPredictorDel (op);
}
/**
* @brief Switches the internal states of the OpmphmPredictor and checks switch.
*
* @param oldState old State
* @param newState new State
* @param op the OpmphmPredictor
* @param n number of elements
*/
static void test_internal_change_whitebox_set_to_state (uint8_t oldState, uint8_t newState, OpmphmPredictor * op, size_t n)
{
size_t setLookupCount;
if (oldState == 0 && newState == 0)
{
// not worth hashing
setLookupCount = opmphmPredictorWorthOpmphm (n);
}
else if (oldState == 3 && newState == 3)
{
// worth hashing
setLookupCount = opmphmPredictorWorthOpmphm (n) + 1;
}
else if (oldState + 1 == newState)
{
// worth hashing
setLookupCount = opmphmPredictorWorthOpmphm (n) + 1;
}
else if (oldState == newState + 1)
{
// not worth hashing
setLookupCount = opmphmPredictorWorthOpmphm (n);
}
else
{
exit_if_fail (0, "wrong usage");
}
/*
* Set every entry in pattern table to state
*/
for (uint16_t testHistory = 0; testHistory <= opmphmPredictorHistoryMask; ++testHistory)
{
op->history = testHistory;
// set not worth to hash
op->lookupCount = setLookupCount;
// between state 1 and 2 is the prediction reulst transition, and patterns interfere, thus no check
if (!((oldState == 1 && newState == 2) || (oldState == 2 && newState == 1)))
{
if (newState < 2)
{
succeed_if (!opmphmPredictor (op, n), "binary search usage");
}
else
{
succeed_if (opmphmPredictor (op, n), "hash usage");
}
}
else
{
// no ckeck
opmphmPredictor (op, n);
}
succeed_if (op->lookupCount == 1, "lookupCount");
}
// test pattern
for (size_t i = 0; i < op->size; ++i)
{
if (newState == 0)
{
succeed_if (op->patternTable[i] == 0x0, "patternTable state 0"); // 0x0 is 00000000 all 4 states per byte to 0
}
else if (newState == 1)
{
succeed_if (op->patternTable[i] == 0x55, "patternTable state 1"); // 0x55 is 01010101 all 4 states per byte to 1
}
else if (newState == 2)
{
succeed_if (op->patternTable[i] == 0xAA, "patternTable state 2"); // 0xAA is 10101010 all 4 states per byte to 2
}
else if (newState == 3)
{
succeed_if (op->patternTable[i] == 0xFF, "patternTable state 3"); // 0xFF is 11111111 all 4 states per byte to 3
}
}
}
void test_internal_change_whitebox (void)
{
OpmphmPredictor * op = opmphmPredictorNew ();
exit_if_fail (op, "opmphmPredictorNew");
const size_t n = 1000;
// inform predictor about size
op->ksSize = n;
// start state is 0
test_internal_change_whitebox_set_to_state (0, 0, op, n);
test_internal_change_whitebox_set_to_state (0, 1, op, n);
test_internal_change_whitebox_set_to_state (1, 2, op, n);
test_internal_change_whitebox_set_to_state (2, 3, op, n);
test_internal_change_whitebox_set_to_state (3, 3, op, n);
test_internal_change_whitebox_set_to_state (3, 2, op, n);
test_internal_change_whitebox_set_to_state (2, 1, op, n);
test_internal_change_whitebox_set_to_state (1, 0, op, n);
test_internal_change_whitebox_set_to_state (0, 0, op, n);
opmphmPredictorDel (op);
}
void test_ks_flag (void)
{
KeySet * ks = ksNew (10, KS_END);
succeed_if (ks->flags & KS_FLAG_NAME_CHANGE, "flag not set at fresh ks");
KeySet * copy = ksDup (ks);
exit_if_fail (copy, "copy");
succeed_if (copy->flags & KS_FLAG_NAME_CHANGE, "flag not set at copy ks");
ksDel (copy);
copy = ksDeepDup (ks);
exit_if_fail (copy, "copy");
succeed_if (copy->flags & KS_FLAG_NAME_CHANGE, "flag not set at copy ks");
ksDel (copy);
copy = ksNew (0, KS_END);
succeed_if (ksCopy (copy, ks) == 1, "copy");
succeed_if (copy->flags & KS_FLAG_NAME_CHANGE, "flag not set at copy ks");
ksDel (copy);
ksDel (ks);
}
void test_ks (void)
{
Key * found;
// create keyset just under opmphmPredictorActionLimit
KeySet * ks = ksNew (opmphmPredictorActionLimit, KS_END);
char name[11]; // "/test" + "10000" + "\0"
for (size_t i = 0; i < opmphmPredictorActionLimit; ++i)
{
snprintf (name, 11, "/test%zu", i);
succeed_if (ksAppendKey (ks, keyNew (name, KEY_END)) > 0, "ksAppendKey failed");
}
// predictor under limit
found = ksLookupByName (ks, "/test0", KDB_O_NONE);
succeed_if (found, "key found");
exit_if_fail (!ks->opmphmPredictor, "predictor here");
// append to be over opmphmPredictorActionLimit
snprintf (name, 11, "/test%zu", opmphmPredictorActionLimit);
succeed_if (ksAppendKey (ks, keyNew (name, KEY_END)) > 0, "ksAppendKey failed");
// predictor over limit
found = ksLookupByName (ks, "/test0", KDB_O_NOCASCADING);
succeed_if (found, "key found");
exit_if_fail (ks->opmphmPredictor, "predictor not here");
// overrule with binary search
found = ksLookupByName (ks, "/test0", KDB_O_BINSEARCH | KDB_O_NOCASCADING);
succeed_if (found, "key found");
succeed_if (ks->opmphmPredictor->lookupCount == 1, "predictor touched");
succeed_if (ks->opmphmPredictor->history == 0, "predictor touched");
// overrule with OPMPHM
found = ksLookupByName (ks, "/test0", KDB_O_OPMPHM);
succeed_if (found, "key found");
succeed_if (ks->opmphmPredictor->lookupCount == 1, "predictor touched");
succeed_if (ks->opmphmPredictor->history == 0, "predictor touched");
// use predictor again
found = ksLookupByName (ks, "/test0", KDB_O_NONE | KDB_O_NOCASCADING);
succeed_if (found, "key found");
succeed_if (ks->opmphmPredictor->lookupCount == 2, "predictor not touched");
// copy
KeySet * copy = ksDup (ks);
exit_if_fail (copy, "copy");
succeed_if (copy->opmphmPredictor->lookupCount == ks->opmphmPredictor->lookupCount, "copy predictor lookupCount");
succeed_if (copy->opmphmPredictor->history == ks->opmphmPredictor->history, "copy predictor history");
succeed_if (copy->opmphmPredictor->ksSize == ks->opmphmPredictor->ksSize, "copy predictor ksSize");
ksDel (copy);
copy = ksDeepDup (ks);
exit_if_fail (copy, "copy");
succeed_if (copy->opmphmPredictor->lookupCount == ks->opmphmPredictor->lookupCount, "copy predictor lookupCount");
succeed_if (copy->opmphmPredictor->history == ks->opmphmPredictor->history, "copy predictor history");
succeed_if (copy->opmphmPredictor->ksSize == ks->opmphmPredictor->ksSize, "copy predictor ksSize");
ksDel (copy);
copy = ksNew (0, KS_END);
succeed_if (ksCopy (copy, ks) == 1, "copy");
succeed_if (copy->opmphmPredictor->lookupCount == ks->opmphmPredictor->lookupCount, "copy predictor lookupCount");
succeed_if (copy->opmphmPredictor->history == ks->opmphmPredictor->history, "copy predictor history");
succeed_if (copy->opmphmPredictor->ksSize == ks->opmphmPredictor->ksSize, "copy predictor ksSize");
ksDel (copy);
ksDel (ks);
}
int main (int argc, char ** argv)
{
printf ("OPMPHM PREDICTOR TESTS\n");
printf ("==================\n\n");
init (argc, argv);
test_internal_basic ();
test_internal_nochange ();
test_internal_change_whitebox ();
test_ks_flag ();
test_ks ();
print_result ("test_opmphm_predictor");
return nbError;
}