GMTROM/BSI_Tests.cxx

506 lines
14 KiB
C++

#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;
const int ARRAY_SIZE = 20000;
const int ARRAY_SIZE2 = 256 * 65536;
int bitzahl = 0;
char BitFeldA[ARRAY_SIZE2];
const int bitbreite = 256;
class BSI_Tests{
public:
char BitFeldB[ARRAY_SIZE];
int letzterwert = 0;
int letzteselement = -1;
public:
void readBinaryFile(string fileName, int bufferSize) {
ifstream input(fileName, ios::binary);
if (input) {
input.read(BitFeldA, bufferSize);
input.close();
}
}
//In Bearbeitung
void neuesfeldb(char* BitFeldA, char* BitFeldB, int durchlauf) {
int grundelement = 0;
if (durchlauf % (bitbreite + 1) == 0) {
std::cout << ("Teste im Block.", false, 1);
for (int i = 0; i < 20000; i++) {
BitFeldB[i] = BitFeldA[i + letzteselement + 1];
}
letzteselement += teilenaufrunden(20000, bitbreite) * bitbreite;
}
else {
grundelement = (durchlauf % (bitbreite + 1)) + letzteselement;
for (int i = 0; i < 20000; i++) {
BitFeldB[i] = BitFeldA[grundelement + (i * bitbreite)];
}
if (durchlauf % (bitbreite + 1) == bitbreite) {
letzteselement += 20000 * bitbreite;
}
}
}
int teilenaufrunden(int a, int b) {
if ((a % b) == 0) {
return (int)(a / b);
}
else {
return (((int)(a / b)) + 1);
}
}
public:
bool test0(int c, int bitbreite) {
int i, j;
bool ok;
long u0 = (long)0;
long u1 = (long)1;
long WFeld[65536];
for (i = 0; i < 65536; i++) {
WFeld[i] = u0;
for (j = 0; j < 48; j++) {
WFeld[i] += (u1 << j) * BitFeldA[(c * bitbreite) * i + j];
}
}
std::sort(WFeld, WFeld + 65536);
ok = true;
for (i = 1; i < 65536; i++) {
if (WFeld[i - 1] == WFeld[i]) {
ok = false;
}
}
return ok;
}
public:
bool test1() {
int i, einsen, mg = ARRAY_SIZE;
bool ok;
einsen = 0;
for (i = 0; i < mg; i++) {
einsen += BitFeldB[i];
}
if ((einsen > 9654) && (einsen < 10346)) {
ok = true;
} else {
ok = false;
}
std::cout << "Anzahl Einsen: " << einsen << std::endl;
std::cout << "Zulässiger Bereich: [9655; 10345]" << std::endl;
return ok;
}
public:
bool test2() {
int i, j, index;
int Hfg[16];
bool ok;
double testgroesse;
for (i = 0; i < 16; i++) {
Hfg[i] = 0;
}
for (i = 0; i < 5000; i++) {
index = 0;
for (j = 0; j < 4; j++) {
index += (1 << j) * BitFeldB[4 * i + j];
}
Hfg[index]++;
}
testgroesse = 0.0;
for (i = 0; i < 16; i++) {
testgroesse += Hfg[i] * Hfg[i];
}
testgroesse = testgroesse * (16.0 / 5000.0) - 5000.0;
if ((testgroesse > 1.03) && (testgroesse < 57.4)) {
ok = true;
} else {
ok = false;
}
return ok;
}
bool test3() {
int i, run;
int Run0Feld[7];
int Run1Feld[7];
bool ok;
int UGrenze[] = {0, 2267, 1079, 502, 223, 90, 90};
int OGrenze[] = {0, 2733, 1421, 748, 402, 223, 223};
for (i = 0; i < 7; i++) {
Run0Feld[i] = Run1Feld[i] = 0;
}
run = 1;
for (i = 1; i < 20000; i++) {
if (BitFeldB[i - 1] == BitFeldB[i]) {
run++;
} else {
if (run > 6) {
run = 6;
}
if (BitFeldB[i - 1] == 1) {
Run1Feld[run]++;
} else {
Run0Feld[run]++;
}
run = 1;
}
}
if (run > 6) {
run = 6;
}
if (BitFeldB[i - 1] == 1) {
Run1Feld[run]++;
} else {
Run0Feld[run]++;
}
ok = true;
for (i = 1; i <= 6; i++) {
if ((Run0Feld[i] >= UGrenze[i]) && (Run0Feld[i] <= OGrenze[i])) {
cout << "0-Runs[" << i << "] = " << Run0Feld[i] << "; zulässiges Intervall: [" << UGrenze[i] << "; " << OGrenze[i] << "]" << endl;
} else {
ok = false;
cout << "FEHLER: 0-Runs[" << i << "] = " << Run0Feld[i] << "; zulässiges Intervall: [" << UGrenze[i] << "; " << OGrenze[i] << "]" << endl;
}
if ((Run1Feld[i] >= UGrenze[i]) && (Run1Feld[i] <= OGrenze[i])) {
cout << "1-Runs[" << i << "] = " << Run1Feld[i] << "; zulässiges Intervall: [" << UGrenze[i] << "; " << OGrenze[i] << "]" << endl;
} else {
ok = false;
cout << "FEHLER: 1-Runs[" << i << "] = " << Run1Feld[i] << "; zulässiges Intervall: [" << UGrenze[i] << "; " << OGrenze[i] << "]" << endl;
}
}
return ok;
}
bool test4() {
int i, run;
bool ok;
run = 1;
ok = true;
for (i = 1; i < 20000; i++) {
if (BitFeldB[i - 1] == BitFeldB[i]) {
run++;
if (run >= 34) {
ok = false;
cout << "Long Run aufgetreten (Wert: " << BitFeldB[i] << "). Erste Bitposition: " << (i - 33) << "." << endl;
}
}
else {
run = 1;
}
}
return ok;
}
bool test5() {
int i, j, k, tau, Z_tau, max;
bool ok;
int ShiftFeld[5000];
int MaxKorrFeld[5000];
for (tau = 1; tau <= 5000; tau++) {
Z_tau = 0;
for (i = 0; i < 5000; i++) {
Z_tau += (BitFeldB[i] ^ BitFeldB[i + tau]);
}
ShiftFeld[tau - 1] = Z_tau;
}
max = 0;
for (tau = 0; tau < 5000; tau++) {
if (abs(ShiftFeld[tau] - 2500) > max) {
max = abs(ShiftFeld[tau] - 2500);
}
}
j = 0;
for (tau = 0; tau < 5000; tau++) {
if (abs(ShiftFeld[tau] - 2500) == max) {
MaxKorrFeld[j] = tau;
j++;
}
}
cout << "Maximale Z_tau-Abweichung von 2500: " << max << endl;
cout << "Aufgetreten für Shifts: " << endl;
for (k = 0; k < j; k++) {
cout << "Shift: " << (MaxKorrFeld[k] + 1) << endl;
}
tau = MaxKorrFeld[0];
Z_tau = 0;
for (i = 10000; i < 15000; i++) {
Z_tau += (BitFeldB[i] ^ BitFeldB[i + tau + 1]);
}
tau++;
cout << "Nochmaliger Autokorrelationstest mit Shift: " << tau << " auf Bits 10.000 bis 14.999" << endl;
cout << "Z_" << tau << " = " << Z_tau << endl;
if ((Z_tau > 2326) && (Z_tau < 2674))
ok = true;
else
ok = false;
return ok;
}
bool test6a() {
bool ok;
double* prob = new double[1];
double absdiff = 0.0;
int groesse = 100000;
int einsen = 0;
for (int i = 0; i < groesse; i++) einsen += BitFeldA[i];
prob[0] = (double)einsen / (double)groesse;
absdiff = fabs(prob[0] - 0.5);
if (absdiff < 0.025) {
ok = true;
}
else {
ok = false;
}
letzterwert = groesse;
cout << ("|P(1) - 0.5| = " + std::to_string(absdiff), false, 2);
cout << ("Letztes Element: " + std::to_string(letzterwert), false, 2);
delete[] prob;
return ok;
}
bool test6b() {
int groesse = 100000;
int counter[2] = { 0, 0 };
int einsen[2] = { 0, 0 };
int voll[2] = { 0, 0 };
int i = 0;
double prob[2];
double absdiff = 0.0;
while ((voll[0] + voll[1] < 2) && ((letzterwert + 2 * i + 1) < bitzahl)) {
if (voll[BitFeldA[letzterwert + 2 * i]] == 1);
else {
counter[BitFeldA[letzterwert + 2 * i]] += 1;
if (counter[BitFeldA[letzterwert + 2 * i]] == groesse) voll[BitFeldA[letzterwert + 2 * i]] = 1;
einsen[BitFeldA[letzterwert + 2 * i]] += BitFeldA[letzterwert + 2 * i + 1];
}
i++;
}
letzterwert += 2 * i;
if (voll[0] + voll[1] < 2) {
cout << ("Inputdatei zu klein....Kriterium P2.i)(vii.b) konnte nicht geprueft werden.", true, 2);
return false;
}
else {
for (i = 0; i < 2; i++) prob[i] = (double)einsen[i] / groesse;
absdiff = abs(prob[0] - prob[1]);
cout << ("p(01) = " + to_string(prob[0]), false, 2);
cout << ("p(11) = " + to_string(prob[1]), false, 2);
cout << ("|p_(01) - p_(11)| = " + to_string(absdiff), false, 2);
cout << ("Letztes Element: " + to_string(letzterwert), false, 2);
if (absdiff < 0.02) return true;
else return false;
}
}
bool test7a() {
int groesse = 100000;
int hilf;
int voll[4] = { 0, 0, 0, 0 };
int counter[4] = { 0, 0, 0, 0 };
int einsen[4] = { 0, 0, 0, 0 };
int nullen[4] = { 0, 0, 0, 0 };
double sum = 0;
int i = 0;
while ((voll[0] + voll[1] + voll[2] + voll[3] < 4) && ((letzterwert + 3 * i) < bitzahl)) {
hilf = 2 * BitFeldA[letzterwert + 3 * i + 1] + BitFeldA[letzterwert + 3 * i];
if (voll[hilf] == 1);
else {
counter[hilf] += 1;
if (counter[hilf] == groesse) voll[hilf] = 1;
einsen[hilf] += BitFeldA[letzterwert + 3 * i + 2];
}
i++;
}
letzterwert += 3 * i;
if (voll[0] + voll[1] + voll[2] + voll[3] < 4) {
cout << ("Inputdatei zu klein....Kriterium P2.i)(vii.c) konnte nicht geprueft werden", true, 2);
return false;
}
else {
for (i = 0; i < 4; i++) nullen[i] = groesse - einsen[i];
bool ok = true;
for (i = 0; i < 2; i++) {
sum = ((double)((nullen[2 * i] - nullen[2 * i + 1]) * (nullen[2 * i] - nullen[2 * i + 1])) / (nullen[2 * i] + nullen[2 * i + 1])
+ (double)((einsen[2 * i] - einsen[2 * i + 1]) * (einsen[2 * i] - einsen[2 * i + 1])) / (einsen[2 * i] + einsen[2 * i + 1]));
if (sum > 15.13) ok = false;
cout << ("Testgröße[" + to_string(i) + "] = " + to_string(sum), false, 2);
}
cout << ("Letztes Element: " + to_string(letzterwert), false, 2);
return ok;
}
}
bool test7b() {
bool ok;
int hilf;
int voll[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
int counter[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
int einsen[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
int nullen[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
double sum = 0;
int groesse = 100000;
int i = 0;
while ((voll[0] + voll[1] + voll[2] + voll[3] + voll[4] + voll[5] + voll[6] + voll[7] < 8) && ((letzterwert + 4 * i) < bitzahl)) {
hilf = 4 * BitFeldA[letzterwert + 4 * i + 2] + 2 * BitFeldA[letzterwert + 4 * i + 1] + BitFeldA[letzterwert + 4 * i];
if (voll[hilf] == 1);
else {
counter[hilf] += 1;
if (counter[hilf] == groesse) voll[hilf] = 1;
einsen[hilf] += BitFeldA[letzterwert + 4 * i + 3];
}
i++;
}
letzterwert += 4 * i;
if (voll[0] + voll[1] + voll[2] + voll[3] + voll[4] + voll[5] + voll[6] + voll[7] < 8) {
cout << ("Inputdatei zu klein....Kriterium P2.i)(vii.d) konnte nicht geprueft werden", true, 2);
ok = false;
}
else {
for (i = 0; i < 8; i++) nullen[i] = groesse - einsen[i];
ok = true;
for (i = 0; i < 4; i++) {
sum = (double)((nullen[2 * i] - nullen[2 * i + 1]) * (nullen[2 * i] - nullen[2 * i + 1])) / (nullen[2 * i] + nullen[2 * i + 1])
+ (double)((einsen[2 * i] - einsen[2 * i + 1]) * (einsen[2 * i] - einsen[2 * i + 1])) / (einsen[2 * i] + einsen[2 * i + 1]);
if (sum > 15.13) ok = false;
cout << ("Testgröße[" + to_string(i) + "] = " + to_string(sum), false, 2);
}
}
cout << ("Letztes Element: " + to_string(letzterwert), false, 2);
return ok;
}
bool test8() {
const int L = 8;
const int Q = 2560;
const int K = 256000;
int hilf = 0;
int counter = 0;
int letzteposition[256];
for (int i = 0; i < 256; i++) {
letzteposition[i] = -1;
}
int W[Q + K];
double TG;
double G[K + Q + 1];
int abstand;
G[1] = 0.0;
for (int i = 1; i <= K + Q - 1; i++) G[i + 1] = G[i] + 1.0 / (i);
for (int i = 1; i <= K + Q; i++) G[i] /= log(2.0);
for (int i = 0; i < Q; i++) {
hilf = 0;
for (int j = 0; j < 8; j++) {
hilf += BitFeldA[letzterwert + 8 * i + j] << j;
}
letzteposition[hilf] = i;
}
TG = 0.0;
for (int i = Q; i < K + Q; i++) {
hilf = 0;
for (int j = 0; j < 8; j++) {
hilf += BitFeldA[letzterwert + 8 * i + j] << j;
}
abstand = i - letzteposition[hilf];
letzteposition[hilf] = i;
TG += G[abstand];
}
TG /= (double)K;
cout << ("Testgröße = " + 1, false, 2);
cout << TG;
letzterwert += 8 * (2560 + 256000);
cout << ("Letztes Element: " + letzterwert, false, 2);
if (TG > 7.976) return true;
else return false;
}
public:
void T1() {
bool result;
bitzahl = teilenaufrunden(257, (bitbreite + 1)) * ((teilenaufrunden(20000, bitbreite)) * bitbreite + (bitbreite * 20000));
for (int durchlauf = 0; durchlauf < 257; durchlauf++) {
neuesfeldb(BitFeldA, BitFeldB, durchlauf);
result = test1();
std::cout << "Test1 result: " << result << std::endl;
result = test2();
std::cout << "Test2 result: " << result << std::endl;
result = test3();
std::cout << "Test3 result: " << result << std::endl;
result = test4();
std::cout << "Test4 result: " << result << std::endl;
result = test5();
std::cout << "Test5 result: " << result << std::endl;
}
}
public:
void T0() {
bitzahl = bitbreite * teilenaufrunden(48, bitbreite) * 65536;
bool result = test0(teilenaufrunden(48, bitbreite), bitbreite);
std::cout << "Test0 result: " << result << std::endl;
}
public:
void T2() {
bool result;
bitzahl = 7200000;
result = test6a();
std::cout << "Test6a result: " << result << std::endl;
result = test6b();
std::cout << "Test6b result: " << result << std::endl;
result = test7a();
std::cout << "Test7a result: " << result << std::endl;
result = test7b();
std::cout << "Test7b result: " << result << std::endl;
result = test8();
std::cout << "Test8 result: " << result << std::endl;
}
};
int main() {
BSI_Tests t1;
t1.readBinaryFile("file.txt", ARRAY_SIZE2);
//t1.T0();
t1.T1();
//t1.T2();
return 0;
}