-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec.js
206 lines (184 loc) · 4.67 KB
/
spec.js
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
195
196
197
198
199
200
201
202
203
204
205
206
const { expect } = require("chai");
const {
toBinary,
chunk,
addParityBits,
encode,
findError,
correctErrors,
join,
toAscii,
decode,
} = require("./index");
describe("Hamming", () => {
const message = "hello";
const encodedMessage = "0110100001100101011011000110110001101111";
const chunkedMessage = [
[
undefined,
undefined,
undefined,
0,
undefined,
1,
1,
0,
undefined,
1,
0,
0,
0,
0,
1,
1,
],
[
undefined,
undefined,
undefined,
0,
undefined,
0,
1,
0,
undefined,
1,
0,
1,
1,
0,
1,
1,
],
[
undefined,
undefined,
undefined,
0,
undefined,
0,
0,
1,
undefined,
1,
0,
1,
1,
0,
0,
0,
],
[
undefined,
undefined,
undefined,
1,
undefined,
1,
0,
1,
undefined,
1,
1,
1,
0,
0,
0,
0,
],
];
const hammedMessage = [
[0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1],
[0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1],
[0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0],
];
const hammedMessageWithErrors = hammedMessage.map((block) => {
const blockWithPossibleError = block.slice();
const errorPosition = Math.floor(Math.random() * block.length);
if (Math.random() > 0.5) {
blockWithPossibleError[errorPosition] =
(blockWithPossibleError[errorPosition] + 1) % 2;
}
return blockWithPossibleError;
});
describe("toBinary", () => {
it("should transform the given string into its ASCII binary representation", () => {
const actual = toBinary(message);
expect(actual).to.equal(encodedMessage);
});
});
describe("chunk", () => {
it("should break the given message into blocks of the given blockSize", () => {
const actual = chunk(encodedMessage, 16);
expect(actual).to.be.an("array").with.lengthOf(chunkedMessage.length);
for (let i = 0; i < chunkedMessage.length; i++)
expect(actual[i], `block ${i}`).to.have.ordered.members(
chunkedMessage[i]
);
});
});
describe("addParityBits", () => {
it("should calculate and add parity bits", () => {
for (let i = 0; i < chunkedMessage.length; i++) {
const actual = addParityBits(chunkedMessage[i]);
expect(actual, `block ${i}`).to.have.ordered.members(hammedMessage[i]);
}
});
});
describe("encode", () => {
it("should encode the given message for transmission", () => {
const actual = encode(message, 16);
expect(actual).to.be.an("array").with.lengthOf(hammedMessage.length);
for (let i = 0; i < hammedMessage.length; i++)
expect(actual[i], `block ${i}`).to.have.ordered.members(
hammedMessage[i]
);
});
});
describe("findError", () => {
it("should find the error in the given block", () => {
for (const block of hammedMessage) {
const errorPosition = Math.floor(Math.random() * block.length);
const blockWithError = block.slice();
blockWithError[errorPosition] = (blockWithError[errorPosition] + 1) % 2;
const actual = findError(blockWithError);
expect(actual).to.equal(errorPosition);
}
});
it("should return -1 if there is no error in the given block", () => {
for (const block of hammedMessage) {
const actual = findError(block);
expect(actual).to.equal(-1);
}
});
});
describe("correctErrors", () => {
it("should correct errors in the given message", () => {
const actual = correctErrors(hammedMessageWithErrors);
expect(actual).to.be.an("array").with.lengthOf(hammedMessage.length);
for (let i = 0; i < hammedMessage.length; i++)
expect(actual[i], `block ${i}`).to.have.ordered.members(
hammedMessage[i]
);
});
});
describe("join", () => {
it("should return a binary encoded string, with all parity bits removed", () => {
const actual = join(hammedMessage);
expect(actual).to.equal(encodedMessage);
});
});
describe("toAscii", () => {
it("should decode a binary string", () => {
const actual = toAscii(encodedMessage);
expect(actual).to.equal(message);
});
});
describe("decode", () => {
it("should decode the given message, correcting any transmission errors (up to 1 per block)", () => {
const actual = decode(hammedMessageWithErrors);
expect(actual).to.equal(message);
});
});
});