-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestRoutines.cpp
More file actions
194 lines (154 loc) · 5.66 KB
/
TestRoutines.cpp
File metadata and controls
194 lines (154 loc) · 5.66 KB
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
#include "Checksum16Bit.hpp"
#include "CRC16Bit.hpp"
#include "CRC32Bit.hpp"
#include "Packet.hpp"
#include "BernoulliErrorModel.hpp"
#include "GilbertErrorModel.hpp"
#include "PeriodicBurstErrorModel.hpp"
#include "TestRoutines.hpp"
#include <cstdint>
#include <cmath>
#include <chrono>
#include <iomanip>//setprecision setfixed
#define FIXED_FLOAT(p, x) std::fixed << std::setprecision(p) <<(x)
TestRoutines::TestRoutines(uint32_t times_, bool debug_){
this->times = times_;
this->debug = debug_;
}
void TestRoutines::compareTwoAlgorithms(ErrorModel **errs, uint8_t lenErr, VerificationAlgorithm* algA, VerificationAlgorithm* algB, RNG* rng){
for(uint8_t errCounter=0; errCounter<lenErr; errCounter++){
std::cout << std::endl;
errs[errCounter]->printAttributes();
int detectionFails[3]; //0 = PolyA, 1 = PolyB, 2 = BOTH
std::cout << "\nN(B)\tf(bit)\t%\t"
<< algA->getAlgName() << "\t"
<< algB->getAlgName() << "\t"
<< "BOTH" << std::endl;
for (uint16_t N=pow(2,3); N<pow(2,11); N*=2){
detectionFails[0] = 0;
detectionFails[1] = 0;
detectionFails[2] = 0;
uint32_t bitErrors = 0;
for(int i=0; i<times; ++i){
Packet *pkg = new Packet(N, rng);
uint32_t verCodeA = algA->generateVerificationCode(pkg);
uint32_t verCodeB = algB->generateVerificationCode(pkg);
int err = errs[errCounter]->injectErrors(pkg, true);//true=forceError
bitErrors += err;
bool algA_undetect = false;
if (err>0 && verCodeA == algA->generateVerificationCode(pkg)) {
algA_undetect = true;
detectionFails[0]++;
}
if (err>0 && verCodeB == algB->generateVerificationCode(pkg)) {
if (algA_undetect == true) detectionFails[2]++;
detectionFails[1]++;
}
delete pkg;
}
std::cout << N << "\t"
<< FIXED_FLOAT(2, (double)bitErrors/times) << "\t"
<< FIXED_FLOAT(2, (((double)bitErrors/times)*100.0)/(N*8)) << "\t"
<< detectionFails[0] << "\t"
<< detectionFails[1] << "\t"
<< detectionFails[2];
std::cout << std::endl;
}
}
}
void TestRoutines::executionTimeTest(RNG* rng, VerificationAlgorithm** algs, uint8_t lenAlg){
for(uint8_t algCounter=0; algCounter<lenAlg; algCounter++){
std::cout << std::endl;
std::cout << algs[algCounter]->getAlgName();
std::cout << std::endl;
std::cout << "N(B)" << "\t" << "Time(ms)" << std::endl;
for (int N=pow(2,3); N<pow(2,11); N*=2){
unsigned int mediaTempo = 0;
//std::chrono::high_resolution_clock mediaTempo;
Packet *pkg = new Packet(N, rng);
auto start = std::chrono::high_resolution_clock::now();
auto stop = std::chrono::high_resolution_clock::now();
for(uint32_t i=0; i<times; i++){
//Packet *pkg = new Packet(N, rng);
//start = std::chrono::high_resolution_clock::now();
algs[algCounter]->generateVerificationCode(pkg);
//stop = std::chrono::high_resolution_clock::now();
//mediaTempo += std::chrono::duration_cast<std::chrono::milliseconds>(stop-start).count();
//delete pkg;
}
mediaTempo = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now()-start).count();
std::cout << N << "\t" << mediaTempo << std::endl;
delete pkg;
}
std::cout << std::endl;
}
}
void TestRoutines::burstVerification(ErrorModel *model, int N){
long diff[50];
for (int x = 0; x<20; ++x) diff[x]=0;
RNG *rng = new RNG(N);
for(int i=0; i<times; ++i){
Packet *pkg = new Packet(N, rng);
//pkg->print('b');
Packet *pk2 = pkg->clone();
int err = model->injectErrors(pkg, true);//true=forceError
if (err>0) {
//pkg->print('b');
int k = 0;
for (uint16_t x =0; x<pkg->getLength(); ++x) {
uint16_t auxiliary = (((uint8_t*)pkg->getData())[x]^((uint8_t*)pk2->getData())[x]);
for (uint16_t y = 7; y<8;--y) {
int d = uint16_t(pow(2, y));
int r = auxiliary&d;
//cout<<bitset<16>(auxiliary)<< "- "<<bitset<16>(d)<< "- "<<bitset<16>(r)<<std::endl;
if (r >0) k++;
else {
diff[k]++;
if (k>0) diff[0]++;
k=0;
}
}
}
}
}
for (int x=0; x<20;++x) std::cout<<"["<<x<<"] = "<<diff[x]<<std::endl;
}
void TestRoutines::genericTest(VerificationAlgorithm** algs, ErrorModel** errs, uint8_t lenAlg, uint8_t lenErr, RNG* rng){
for(uint8_t errCounter=0; errCounter<lenErr; errCounter++){
std::cout << std::endl;
errs[errCounter]->printAttributes();
uint32_t DFs[lenAlg]; //detection fail for each verifiation algorithm
std::cout << "\nN(B)\tf(bit)\t%\t";
for(uint8_t i=0; i<lenAlg; i++) std::cout << algs[i]->getAlgName() << "\t";
std::cout << std::endl;
for (uint16_t N=pow(2,3); N<pow(2,11); N*=2){
for (uint8_t x=0; x<lenAlg; x++)
DFs[x] = 0;
uint16_t testsWithErrors = 0;
uint32_t bitErrors = 0;
for(uint32_t i=0; i<times; i++){
Packet *pkg = new Packet(N, rng);
uint32_t chks[lenAlg]; //checksums before error injection
for(uint8_t algCounter=0; algCounter<lenAlg; algCounter++){
chks[algCounter] = algs[algCounter]->generateVerificationCode(pkg);
}
int err = errs[errCounter]->injectErrors(pkg, true);
bitErrors += err;
if (err>0) testsWithErrors++;
for(uint8_t algCounter=0; algCounter<lenAlg; algCounter++){
uint32_t newKey = algs[algCounter]->generateVerificationCode(pkg);
if(err>0 && (newKey == chks[algCounter])){
//std::cout << newKey << " == " << chks[algCounter] << std::endl;
DFs[algCounter]++;
}
}
delete pkg;
}
std::cout << N << "\t"
<< FIXED_FLOAT(2, (double)bitErrors/times) << "\t"
<< FIXED_FLOAT(2, (((double)bitErrors/times)*100.0)/(N*8)) << "\t";
for(uint8_t i=0; i<lenAlg; i++) std::cout << DFs[i] << "\t";
std::cout << std::endl;
}
}
}