-
Notifications
You must be signed in to change notification settings - Fork 7
/
binary_derivative_test.cpp
50 lines (42 loc) · 1.33 KB
/
binary_derivative_test.cpp
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
namespace test{
double binary_derivative_test(const std::vector<bool> &bits, unsigned int k){
if(bits.size()<0 || bits.size()<k){
cerr<<"Illegal parameter!"<<endl;
throw;
}
int n=bits.size();
int S=0;
double V;
double P;
vector<bool> _bits(bits);
for (int i=0; i<k; i++){
for (int j=0; j<n-i-1; j++){
_bits[j]=_bits[j] ^ _bits[j+1];
}
}
for (int i=0; i<n-k; i++)
if(_bits[i]) S++; else S--;
V = fabs(S)/sqrt(n-k);
P = erfc(V/sqrt(2));
cout<<"Binary derivative test:"<<endl;
cout<<" n = "<<n<<endl;
cout<<" k = "<<k<<endl;
cout<<" S = "<<S<<endl;
cout<<" V = "<<fixed<<setprecision(6)<<V<<endl;
cout<<" P = "<<fixed<<setprecision(4)<<P*100<<"%"<<endl;
return P;
}
class BinaryDerivativeTest: public TestMethod{
public:
unsigned int k;
void run(std::vector<bool> &bits){
P = binary_derivative_test(bits, k);
}
const char* testName(int lang = 0){
char* names[1] = {"Binary derivative test"};
return names[lang];
}
BinaryDerivativeTest(unsigned int _k) {k=_k;}
~BinaryDerivativeTest() {}
};
}