-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParityAlgsTests.cpp
126 lines (101 loc) · 3.58 KB
/
ParityAlgsTests.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
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
#include "ParityAlgsTests.hpp"
vector<bool> PartityAlgsTests::evenParity(vector<bool> frame) {
Logger::logInfo(("Adicionando o bit de paridade"), "PARIDADE-PAR");
Logger::logInfo(("[" + Logger::bitsAsString(frame) + "]"));
frame.insert(frame.begin(), PartityAlgsTests::evenTest(frame));
Logger::logInfo(("[" + Logger::bitsAsString(frame) + "]"));
return frame;
}
vector<bool> PartityAlgsTests::oddParity(vector<bool> frame) {
Logger::logInfo(("Adicionando o bit de paridade"), "PARIDADE-IMPAR");
Logger::logInfo(("[" + Logger::bitsAsString(frame) + "]"));
frame.insert(frame.begin(), !PartityAlgsTests::evenTest(frame));
Logger::logInfo(("[" + Logger::bitsAsString(frame) + "]"));
return frame;
}
vector<bool> PartityAlgsTests::crc32(vector<bool> frame) {
Logger::logInfo(("Executando CRC32"), "CRC32");
vector<bool> remaining = frame;
remaining.resize(remaining.size() + 32);
// 100000100110000010001110110110111 -> x32 + x26 + x23 + x22 + x16 + x12 +
// x11 + x10 + x8 + x7 + x5 + x4 + x2 + x1 + x0
vector<bool> polynomial{1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1,
0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1};
Logger::logInfo("[" + Logger::bitsAsString(polynomial) + "]", "Polinomio");
for (int i = 0; i < remaining.size() - 32; i++) {
if (remaining[i]) {
for (int j = 0; j < polynomial.size(); j++) {
remaining[i + j] = remaining[i + j] ^ polynomial[j];
}
}
remaining[i] = frame[i];
}
Logger::logInfo("[" + Logger::bitsAsString(remaining) + "]", "Resto");
return remaining;
}
bool PartityAlgsTests::evenTest(vector<bool> frame) { // true if even
bool even = false;
for (auto bit : frame) {
if (bit) {
even = !even;
}
}
return !even;
}
bool PartityAlgsTests::evenParityTest(std::vector<bool>& frame) {
bool parity = frame[0];
frame.erase(frame.begin());
if (parity) {
if (PartityAlgsTests::evenTest(frame)) {
return false;
} else {
return true;
}
} else {
if (PartityAlgsTests::evenTest(frame)) {
return true;
} else {
return false;
}
}
}
bool PartityAlgsTests::oddParityTest(std::vector<bool>& frame) {
bool parity = frame[0];
frame.erase(frame.begin());
if (parity) {
if (!PartityAlgsTests::evenTest(frame)) {
return false;
} else {
return true;
}
} else {
if (!PartityAlgsTests::evenTest(frame)) {
return true;
} else {
return false;
}
}
}
bool PartityAlgsTests::crc32Test(std::vector<bool>& frame) {
std::vector<bool> remaining = frame;
std::vector<bool> polinomio{1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1,
0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1,
1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1};
remaining.resize(remaining.size());
for (int i = 0; i < remaining.size() - 32; i++) {
if (remaining[i]) {
for (int j = 0; j < polinomio.size(); j++) {
remaining[i + j] = remaining[i + j] ^ polinomio[j];
}
}
remaining[i] = frame[i];
}
Logger::logInfo("[" + Logger::bitsAsString(remaining) + "]", "Resto");
bool hasError = false;
for (int i = 0; i < polinomio.size() - 1; i++) {
frame.erase(frame.end());
if (!hasError) hasError = remaining[remaining.size()];
remaining.erase(remaining.end());
}
return hasError;
}