-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTests.cpp
More file actions
161 lines (136 loc) · 5.1 KB
/
Copy pathTests.cpp
File metadata and controls
161 lines (136 loc) · 5.1 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
//
// Created by Mindaugas on 2020-10-08.
//
#include <iostream>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <ctime>
#include <functional>
#include "Tests.h"
#include "sha256.h"
using namespace std;
float Tests::BinaryDifference(Converter conv1, Converter conv2) {
int BitDiffPercent = 0;
vector<byte> firstVector = conv1.getOutput();
vector<byte> secondVector = conv2.getOutput();
vector<byte> diff;
set_difference(firstVector.begin(), firstVector.end(), secondVector.begin(), secondVector.end(),
inserter(diff, diff.begin()));
float percent = (float) diff.size() / firstVector.size();
cout << "Baitu vektoriai skiriasi per: " << diff.size() << " elementu. Is viso elementu vektoriuose: " << firstVector.size() <<
". Procentinis skirtumas: " << percent << endl;
return percent;
}
float Tests::HexDifference(Converter conv1, Converter conv2) {
vector<byte> firstVector = conv1.getOutput();
vector<byte> secondVector = conv2.getOutput();
stringstream first, second;
for(byte b : firstVector){
first << std::hex << static_cast<int>(b);
}
for(byte b : secondVector){
second << std::hex << static_cast<int>(b);
}
string diff;
string string_F = first.str();
string string_S = second.str();
set_difference(string_F.begin(), string_F.end(), string_S.begin(), string_S.end(),
inserter(diff, diff.begin()));
float percent = (float) diff.size() / string_F.size();
cout << "Hexas skiriasi per: " << diff.size() << " elementu. Procentinis skirtumas" << percent << "% " << endl;
return percent;
}
void Tests::generateSymbols1000() {
ofstream testFile("1000simboliu.txt");
srand(time(NULL));
for(int i = 0; i < 1000; i++){
char randomChar = (rand() % 26) + 'a';
testFile << randomChar;
}
testFile.close();
}
void Tests::checkCollission(vector<string> allHashes) {
int collission = 0;
for(string hashCode : allHashes){
int num = std::count(allHashes.begin(), allHashes.end(), hashCode);
if(num > 1){
cout << "Kolizija surasta: " << hashCode << ", kartojasi: "<< num
<< " kartu "<<endl;
collission++;
}
}
cout << "Is viso koliziju: " << collission << " is 100000\n";
}
void Tests::compareTwo(Converter conv1, Converter conv2) {
BinaryDifference(conv1, conv2);
HexDifference(conv1, conv2);
}
void Tests::generatePairs(ofstream& basicOfstream, int length) {
auto gen = std::bind(std::uniform_int_distribution<>(0,26),std::default_random_engine(time(NULL)));
for(int j = 0; j < 25000; j++){
for(int i = 0; i < length; i++){
char randomChar = gen() + 'a';
if(i == length/2){
basicOfstream << ' ';
} else {
basicOfstream << randomChar;
}
}
basicOfstream << "\n";
}
}
void Tests::makePairsFiles() {
ofstream pairsFile("poros.txt");
generatePairs(pairsFile, 10);
generatePairs(pairsFile, 100);
generatePairs(pairsFile, 500);
generatePairs(pairsFile, 1000);
pairsFile.close();
}
void Tests::MeasureSha256(string value) {
sha256(value);
}
void Tests::generatePairsDiff() {
ofstream similiarPairsFile("panasios_poros.txt");
makePairsSimiliar(similiarPairsFile, 10);
makePairsSimiliar(similiarPairsFile, 100);
makePairsSimiliar(similiarPairsFile, 500);
makePairsSimiliar(similiarPairsFile, 1000);
similiarPairsFile.close();
}
void Tests::makePairsSimiliar(ofstream &file, int length) {
auto gen = std::bind(std::uniform_int_distribution<>(0,26),std::default_random_engine(time(NULL)));
for(int j = 0; j < 25000; j++){
stringstream first;
string second;
for(int i = 0; i < length/2; i++){
char randomChar = gen() + 'a';
first << randomChar;
}
file << first.str();
file << " ";
second = first.str();
do {
auto pickRandom = std::bind(std::uniform_int_distribution<>(0, second.length() - 1),
std::default_random_engine(time(NULL)));
second[rand() % second.length()] = gen() + 'a';
} while (second == first.str());
file << second;
file << "\n";
}
}
void Tests::checkDiff(vector<pair<Converter, Converter>> allPairs) {
vector<float> totalHexDiff;
vector<float> totalBinaryDiff;
for(pair<Converter, Converter> comparable : allPairs){
totalHexDiff.push_back(HexDifference(comparable.first, comparable.second));
totalBinaryDiff.push_back(BinaryDifference(comparable.first, comparable.second));
}
float binaryDiff = 0.0f;
binaryDiff = accumulate(totalBinaryDiff.begin(), totalBinaryDiff.end(), 0.0) / totalBinaryDiff.size();
float hexDiff = 0.0f;
hexDiff = accumulate(totalHexDiff.begin(), totalHexDiff.end(), 0.0) / totalHexDiff.size();
cout << "Galutinis procentinis skirtingumas bitu lygmenyje: " << binaryDiff*100 << "% " << endl
<< "Galutinis procentinis skirtingumas hexu lygmenyje: " << hexDiff*100 << "% " << endl;
}