Skip to content

Commit 14f5bb7

Browse files
Trottrvagg
authored andcommittedFeb 8, 2016
test,buffer: refactor redeclarations
Many variables in the buffer tests are redeclared. Change them so that they are scoped appropriately. PR-URL: #4893 Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b752630 commit 14f5bb7

File tree

4 files changed

+661
-563
lines changed

4 files changed

+661
-563
lines changed
 

‎test/parallel/test-buffer-includes.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ assert(!mixedByteStringUtf8.includes('\u0396'));
161161
// Test complex string includes algorithms. Only trigger for long strings.
162162
// Long string that isn't a simple repeat of a shorter string.
163163
var longString = 'A';
164-
for (var i = 66; i < 76; i++) { // from 'B' to 'K'
164+
for (let i = 66; i < 76; i++) { // from 'B' to 'K'
165165
longString = longString + String.fromCharCode(i) + longString;
166166
}
167167

168168
const longBufferString = new Buffer(longString);
169169

170170
// pattern of 15 chars, repeated every 16 chars in long
171171
var pattern = 'ABACABADABACABA';
172-
for (var i = 0; i < longBufferString.length - pattern.length; i += 7) {
172+
for (let i = 0; i < longBufferString.length - pattern.length; i += 7) {
173173
const includes = longBufferString.includes(pattern, i);
174174
assert(includes, 'Long ABACABA...-string at index ' + i);
175175
}
@@ -188,7 +188,7 @@ assert(asciiString.includes('leb', 0));
188188

189189
// Search in string containing many non-ASCII chars.
190190
const allCodePoints = [];
191-
for (var i = 0; i < 65536; i++) allCodePoints[i] = i;
191+
for (let i = 0; i < 65536; i++) allCodePoints[i] = i;
192192
const allCharsString = String.fromCharCode.apply(String, allCodePoints);
193193
const allCharsBufferUtf8 = new Buffer(allCharsString);
194194
const allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2');
@@ -201,10 +201,10 @@ assert(!allCharsBufferUcs2.includes('notfound'));
201201
// Find substrings in Utf8.
202202
var lengths = [1, 3, 15]; // Single char, simple and complex.
203203
var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b];
204-
for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
205-
for (var i = 0; i < indices.length; i++) {
204+
for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
205+
for (let i = 0; i < indices.length; i++) {
206206
const index = indices[i];
207-
var length = lengths[lengthIndex];
207+
let length = lengths[lengthIndex];
208208

209209
if (index + length > 0x7F) {
210210
length = 2 * length;
@@ -229,10 +229,10 @@ for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
229229
// Find substrings in Usc2.
230230
lengths = [2, 4, 16]; // Single char, simple and complex.
231231
indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0];
232-
for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
233-
for (var i = 0; i < indices.length; i++) {
232+
for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
233+
for (let i = 0; i < indices.length; i++) {
234234
const index = indices[i] * 2;
235-
var length = lengths[lengthIndex];
235+
const length = lengths[lengthIndex];
236236

237237
const patternBufferUcs2 =
238238
allCharsBufferUcs2.slice(index, index + length);

‎test/parallel/test-buffer-indexof.js

+76-69
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,17 @@ assert.equal(
110110
Buffer(b.toString('binary'), 'binary')
111111
.indexOf(Buffer('d', 'binary'), 0, 'binary'), 3);
112112

113-
114-
// test usc2 encoding
115-
var twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
116-
117-
assert.equal(8, twoByteString.indexOf('\u0395', 4, 'ucs2'));
118-
assert.equal(6, twoByteString.indexOf('\u03a3', -4, 'ucs2'));
119-
assert.equal(4, twoByteString.indexOf('\u03a3', -6, 'ucs2'));
120-
assert.equal(4, twoByteString.indexOf(
121-
new Buffer('\u03a3', 'ucs2'), -6, 'ucs2'));
122-
assert.equal(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2'));
113+
{
114+
// test usc2 encoding
115+
const twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
116+
117+
assert.equal(8, twoByteString.indexOf('\u0395', 4, 'ucs2'));
118+
assert.equal(6, twoByteString.indexOf('\u03a3', -4, 'ucs2'));
119+
assert.equal(4, twoByteString.indexOf('\u03a3', -6, 'ucs2'));
120+
assert.equal(4, twoByteString.indexOf(
121+
new Buffer('\u03a3', 'ucs2'), -6, 'ucs2'));
122+
assert.equal(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2'));
123+
}
123124

124125
var mixedByteStringUcs2 =
125126
new Buffer('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2');
@@ -134,25 +135,27 @@ assert.equal(
134135
assert.equal(
135136
-1, mixedByteStringUcs2.indexOf(new Buffer('\u0396', 'ucs2'), 0, 'ucs2'));
136137

137-
var twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
138-
139-
// Test single char pattern
140-
assert.equal(0, twoByteString.indexOf('\u039a', 0, 'ucs2'));
141-
assert.equal(2, twoByteString.indexOf('\u0391', 0, 'ucs2'), 'Alpha');
142-
assert.equal(4, twoByteString.indexOf('\u03a3', 0, 'ucs2'), 'First Sigma');
143-
assert.equal(6, twoByteString.indexOf('\u03a3', 6, 'ucs2'), 'Second Sigma');
144-
assert.equal(8, twoByteString.indexOf('\u0395', 0, 'ucs2'), 'Epsilon');
145-
assert.equal(-1, twoByteString.indexOf('\u0392', 0, 'ucs2'), 'Not beta');
146-
147-
// Test multi-char pattern
148-
assert.equal(
149-
0, twoByteString.indexOf('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha');
150-
assert.equal(
151-
2, twoByteString.indexOf('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma');
152-
assert.equal(
153-
4, twoByteString.indexOf('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma');
154-
assert.equal(
155-
6, twoByteString.indexOf('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon');
138+
{
139+
const twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
140+
141+
// Test single char pattern
142+
assert.equal(0, twoByteString.indexOf('\u039a', 0, 'ucs2'));
143+
assert.equal(2, twoByteString.indexOf('\u0391', 0, 'ucs2'), 'Alpha');
144+
assert.equal(4, twoByteString.indexOf('\u03a3', 0, 'ucs2'), 'First Sigma');
145+
assert.equal(6, twoByteString.indexOf('\u03a3', 6, 'ucs2'), 'Second Sigma');
146+
assert.equal(8, twoByteString.indexOf('\u0395', 0, 'ucs2'), 'Epsilon');
147+
assert.equal(-1, twoByteString.indexOf('\u0392', 0, 'ucs2'), 'Not beta');
148+
149+
// Test multi-char pattern
150+
assert.equal(
151+
0, twoByteString.indexOf('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha');
152+
assert.equal(
153+
2, twoByteString.indexOf('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma');
154+
assert.equal(
155+
4, twoByteString.indexOf('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma');
156+
assert.equal(
157+
6, twoByteString.indexOf('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon');
158+
}
156159

157160
var mixedByteStringUtf8 = new Buffer('\u039a\u0391abc\u03a3\u03a3\u0395');
158161
assert.equal(5, mixedByteStringUtf8.indexOf('bc'));
@@ -165,16 +168,16 @@ assert.equal(-1, mixedByteStringUtf8.indexOf('\u0396'));
165168
// Test complex string indexOf algorithms. Only trigger for long strings.
166169
// Long string that isn't a simple repeat of a shorter string.
167170
var longString = 'A';
168-
for (var i = 66; i < 76; i++) { // from 'B' to 'K'
171+
for (let i = 66; i < 76; i++) { // from 'B' to 'K'
169172
longString = longString + String.fromCharCode(i) + longString;
170173
}
171174

172175
var longBufferString = new Buffer(longString);
173176

174177
// pattern of 15 chars, repeated every 16 chars in long
175178
var pattern = 'ABACABADABACABA';
176-
for (var i = 0; i < longBufferString.length - pattern.length; i += 7) {
177-
var index = longBufferString.indexOf(pattern, i);
179+
for (let i = 0; i < longBufferString.length - pattern.length; i += 7) {
180+
const index = longBufferString.indexOf(pattern, i);
178181
assert.equal((i + 15) & ~0xf, index, 'Long ABACABA...-string at index ' + i);
179182
}
180183
assert.equal(510, longBufferString.indexOf('AJABACA'), 'Long AJABACA, First J');
@@ -195,7 +198,7 @@ assert.equal(3, asciiString.indexOf('leb', 0));
195198

196199
// Search in string containing many non-ASCII chars.
197200
var allCodePoints = [];
198-
for (var i = 0; i < 65536; i++) allCodePoints[i] = i;
201+
for (let i = 0; i < 65536; i++) allCodePoints[i] = i;
199202
var allCharsString = String.fromCharCode.apply(String, allCodePoints);
200203
var allCharsBufferUtf8 = new Buffer(allCharsString);
201204
var allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2');
@@ -205,50 +208,54 @@ var allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2');
205208
assert.equal(-1, allCharsBufferUtf8.indexOf('notfound'));
206209
assert.equal(-1, allCharsBufferUcs2.indexOf('notfound'));
207210

208-
// Find substrings in Utf8.
209-
var lengths = [1, 3, 15]; // Single char, simple and complex.
210-
var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b];
211-
for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
212-
for (var i = 0; i < indices.length; i++) {
213-
var index = indices[i];
214-
var length = lengths[lengthIndex];
211+
{
212+
// Find substrings in Utf8.
213+
const lengths = [1, 3, 15]; // Single char, simple and complex.
214+
const indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b];
215+
for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
216+
for (let i = 0; i < indices.length; i++) {
217+
const index = indices[i];
218+
let length = lengths[lengthIndex];
215219

216-
if (index + length > 0x7F) {
217-
length = 2 * length;
218-
}
220+
if (index + length > 0x7F) {
221+
length = 2 * length;
222+
}
219223

220-
if (index + length > 0x7FF) {
221-
length = 3 * length;
222-
}
224+
if (index + length > 0x7FF) {
225+
length = 3 * length;
226+
}
223227

224-
if (index + length > 0xFFFF) {
225-
length = 4 * length;
226-
}
228+
if (index + length > 0xFFFF) {
229+
length = 4 * length;
230+
}
227231

228-
var patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length);
229-
assert.equal(index, allCharsBufferUtf8.indexOf(patternBufferUtf8));
232+
var patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length);
233+
assert.equal(index, allCharsBufferUtf8.indexOf(patternBufferUtf8));
230234

231-
var patternStringUtf8 = patternBufferUtf8.toString();
232-
assert.equal(index, allCharsBufferUtf8.indexOf(patternStringUtf8));
235+
var patternStringUtf8 = patternBufferUtf8.toString();
236+
assert.equal(index, allCharsBufferUtf8.indexOf(patternStringUtf8));
237+
}
233238
}
234239
}
235240

236-
// Find substrings in Usc2.
237-
var lengths = [2, 4, 16]; // Single char, simple and complex.
238-
var indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0];
239-
for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
240-
for (var i = 0; i < indices.length; i++) {
241-
var index = indices[i] * 2;
242-
var length = lengths[lengthIndex];
243-
244-
var patternBufferUcs2 =
245-
allCharsBufferUcs2.slice(index, index + length);
246-
assert.equal(
247-
index, allCharsBufferUcs2.indexOf(patternBufferUcs2, 0, 'ucs2'));
248-
249-
var patternStringUcs2 = patternBufferUcs2.toString('ucs2');
250-
assert.equal(
251-
index, allCharsBufferUcs2.indexOf(patternStringUcs2, 0, 'ucs2'));
241+
{
242+
// Find substrings in Usc2.
243+
const lengths = [2, 4, 16]; // Single char, simple and complex.
244+
const indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0];
245+
for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
246+
for (let i = 0; i < indices.length; i++) {
247+
const index = indices[i] * 2;
248+
const length = lengths[lengthIndex];
249+
250+
var patternBufferUcs2 =
251+
allCharsBufferUcs2.slice(index, index + length);
252+
assert.equal(
253+
index, allCharsBufferUcs2.indexOf(patternBufferUcs2, 0, 'ucs2'));
254+
255+
var patternStringUcs2 = patternBufferUcs2.toString('ucs2');
256+
assert.equal(
257+
index, allCharsBufferUcs2.indexOf(patternStringUcs2, 0, 'ucs2'));
258+
}
252259
}
253260
}
254261

‎test/parallel/test-buffer-iterator.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ assert.deepEqual(arr, [0, 1, 2, 3, 4]);
5050

5151
arr = [];
5252

53-
for (var b of buffer.entries())
53+
for (b of buffer.entries())
5454
arr.push(b);
5555

5656
assert.deepEqual(arr, [

‎test/parallel/test-buffer.js

+575-484
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.