test,buffer: refactor redeclarations

Many variables in the buffer tests are redeclared. Change them so that
they are scoped appropriately.

PR-URL: https://github.com/nodejs/node/pull/4893
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Rich Trott 2016-01-26 16:47:10 -08:00
parent 2ac47f87a4
commit 9429685d48
4 changed files with 632 additions and 534 deletions

View File

@ -161,7 +161,7 @@ assert(!mixedByteStringUtf8.includes('\u0396'));
// Test complex string includes algorithms. Only trigger for long strings. // Test complex string includes algorithms. Only trigger for long strings.
// Long string that isn't a simple repeat of a shorter string. // Long string that isn't a simple repeat of a shorter string.
var longString = 'A'; var longString = 'A';
for (var i = 66; i < 76; i++) { // from 'B' to 'K' for (let i = 66; i < 76; i++) { // from 'B' to 'K'
longString = longString + String.fromCharCode(i) + longString; longString = longString + String.fromCharCode(i) + longString;
} }
@ -169,7 +169,7 @@ const longBufferString = new Buffer(longString);
// pattern of 15 chars, repeated every 16 chars in long // pattern of 15 chars, repeated every 16 chars in long
var pattern = 'ABACABADABACABA'; var pattern = 'ABACABADABACABA';
for (var i = 0; i < longBufferString.length - pattern.length; i += 7) { for (let i = 0; i < longBufferString.length - pattern.length; i += 7) {
const includes = longBufferString.includes(pattern, i); const includes = longBufferString.includes(pattern, i);
assert(includes, 'Long ABACABA...-string at index ' + i); assert(includes, 'Long ABACABA...-string at index ' + i);
} }
@ -188,7 +188,7 @@ assert(asciiString.includes('leb', 0));
// Search in string containing many non-ASCII chars. // Search in string containing many non-ASCII chars.
const allCodePoints = []; const allCodePoints = [];
for (var i = 0; i < 65536; i++) allCodePoints[i] = i; for (let i = 0; i < 65536; i++) allCodePoints[i] = i;
const allCharsString = String.fromCharCode.apply(String, allCodePoints); const allCharsString = String.fromCharCode.apply(String, allCodePoints);
const allCharsBufferUtf8 = new Buffer(allCharsString); const allCharsBufferUtf8 = new Buffer(allCharsString);
const allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2'); const allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2');
@ -201,10 +201,10 @@ assert(!allCharsBufferUcs2.includes('notfound'));
// Find substrings in Utf8. // Find substrings in Utf8.
var lengths = [1, 3, 15]; // Single char, simple and complex. var lengths = [1, 3, 15]; // Single char, simple and complex.
var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b]; var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b];
for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
for (var i = 0; i < indices.length; i++) { for (let i = 0; i < indices.length; i++) {
const index = indices[i]; const index = indices[i];
var length = lengths[lengthIndex]; let length = lengths[lengthIndex];
if (index + length > 0x7F) { if (index + length > 0x7F) {
length = 2 * length; length = 2 * length;
@ -229,10 +229,10 @@ for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
// Find substrings in Usc2. // Find substrings in Usc2.
lengths = [2, 4, 16]; // Single char, simple and complex. lengths = [2, 4, 16]; // Single char, simple and complex.
indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0]; indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0];
for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
for (var i = 0; i < indices.length; i++) { for (let i = 0; i < indices.length; i++) {
const index = indices[i] * 2; const index = indices[i] * 2;
var length = lengths[lengthIndex]; const length = lengths[lengthIndex];
const patternBufferUcs2 = const patternBufferUcs2 =
allCharsBufferUcs2.slice(index, index + length); allCharsBufferUcs2.slice(index, index + length);

View File

@ -124,16 +124,17 @@ assert.equal(
assert.equal(new Buffer('aaaa0').indexOf('30', 'hex'), 4); assert.equal(new Buffer('aaaa0').indexOf('30', 'hex'), 4);
assert.equal(new Buffer('aaaa00a').indexOf('3030', 'hex'), 4); assert.equal(new Buffer('aaaa00a').indexOf('3030', 'hex'), 4);
{
// test usc2 encoding
const twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
// test usc2 encoding assert.equal(8, twoByteString.indexOf('\u0395', 4, 'ucs2'));
var twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); assert.equal(6, twoByteString.indexOf('\u03a3', -4, 'ucs2'));
assert.equal(4, twoByteString.indexOf('\u03a3', -6, 'ucs2'));
assert.equal(8, twoByteString.indexOf('\u0395', 4, 'ucs2')); assert.equal(4, twoByteString.indexOf(
assert.equal(6, twoByteString.indexOf('\u03a3', -4, 'ucs2')); new Buffer('\u03a3', 'ucs2'), -6, 'ucs2'));
assert.equal(4, twoByteString.indexOf('\u03a3', -6, 'ucs2')); assert.equal(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2'));
assert.equal(4, twoByteString.indexOf( }
new Buffer('\u03a3', 'ucs2'), -6, 'ucs2'));
assert.equal(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2'));
var mixedByteStringUcs2 = var mixedByteStringUcs2 =
new Buffer('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2'); new Buffer('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2');
@ -148,25 +149,27 @@ assert.equal(
assert.equal( assert.equal(
-1, mixedByteStringUcs2.indexOf(new Buffer('\u0396', 'ucs2'), 0, 'ucs2')); -1, mixedByteStringUcs2.indexOf(new Buffer('\u0396', 'ucs2'), 0, 'ucs2'));
var twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); {
const twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
// Test single char pattern // Test single char pattern
assert.equal(0, twoByteString.indexOf('\u039a', 0, 'ucs2')); assert.equal(0, twoByteString.indexOf('\u039a', 0, 'ucs2'));
assert.equal(2, twoByteString.indexOf('\u0391', 0, 'ucs2'), 'Alpha'); assert.equal(2, twoByteString.indexOf('\u0391', 0, 'ucs2'), 'Alpha');
assert.equal(4, twoByteString.indexOf('\u03a3', 0, 'ucs2'), 'First Sigma'); assert.equal(4, twoByteString.indexOf('\u03a3', 0, 'ucs2'), 'First Sigma');
assert.equal(6, twoByteString.indexOf('\u03a3', 6, 'ucs2'), 'Second Sigma'); assert.equal(6, twoByteString.indexOf('\u03a3', 6, 'ucs2'), 'Second Sigma');
assert.equal(8, twoByteString.indexOf('\u0395', 0, 'ucs2'), 'Epsilon'); assert.equal(8, twoByteString.indexOf('\u0395', 0, 'ucs2'), 'Epsilon');
assert.equal(-1, twoByteString.indexOf('\u0392', 0, 'ucs2'), 'Not beta'); assert.equal(-1, twoByteString.indexOf('\u0392', 0, 'ucs2'), 'Not beta');
// Test multi-char pattern // Test multi-char pattern
assert.equal( assert.equal(
0, twoByteString.indexOf('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha'); 0, twoByteString.indexOf('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha');
assert.equal( assert.equal(
2, twoByteString.indexOf('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma'); 2, twoByteString.indexOf('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma');
assert.equal( assert.equal(
4, twoByteString.indexOf('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma'); 4, twoByteString.indexOf('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma');
assert.equal( assert.equal(
6, twoByteString.indexOf('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon'); 6, twoByteString.indexOf('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon');
}
var mixedByteStringUtf8 = new Buffer('\u039a\u0391abc\u03a3\u03a3\u0395'); var mixedByteStringUtf8 = new Buffer('\u039a\u0391abc\u03a3\u03a3\u0395');
assert.equal(5, mixedByteStringUtf8.indexOf('bc')); assert.equal(5, mixedByteStringUtf8.indexOf('bc'));
@ -179,7 +182,7 @@ assert.equal(-1, mixedByteStringUtf8.indexOf('\u0396'));
// Test complex string indexOf algorithms. Only trigger for long strings. // Test complex string indexOf algorithms. Only trigger for long strings.
// Long string that isn't a simple repeat of a shorter string. // Long string that isn't a simple repeat of a shorter string.
var longString = 'A'; var longString = 'A';
for (var i = 66; i < 76; i++) { // from 'B' to 'K' for (let i = 66; i < 76; i++) { // from 'B' to 'K'
longString = longString + String.fromCharCode(i) + longString; longString = longString + String.fromCharCode(i) + longString;
} }
@ -187,8 +190,8 @@ var longBufferString = new Buffer(longString);
// pattern of 15 chars, repeated every 16 chars in long // pattern of 15 chars, repeated every 16 chars in long
var pattern = 'ABACABADABACABA'; var pattern = 'ABACABADABACABA';
for (var i = 0; i < longBufferString.length - pattern.length; i += 7) { for (let i = 0; i < longBufferString.length - pattern.length; i += 7) {
var index = longBufferString.indexOf(pattern, i); const index = longBufferString.indexOf(pattern, i);
assert.equal((i + 15) & ~0xf, index, 'Long ABACABA...-string at index ' + i); assert.equal((i + 15) & ~0xf, index, 'Long ABACABA...-string at index ' + i);
} }
assert.equal(510, longBufferString.indexOf('AJABACA'), 'Long AJABACA, First J'); assert.equal(510, longBufferString.indexOf('AJABACA'), 'Long AJABACA, First J');
@ -209,7 +212,7 @@ assert.equal(3, asciiString.indexOf('leb', 0));
// Search in string containing many non-ASCII chars. // Search in string containing many non-ASCII chars.
var allCodePoints = []; var allCodePoints = [];
for (var i = 0; i < 65536; i++) allCodePoints[i] = i; for (let i = 0; i < 65536; i++) allCodePoints[i] = i;
var allCharsString = String.fromCharCode.apply(String, allCodePoints); var allCharsString = String.fromCharCode.apply(String, allCodePoints);
var allCharsBufferUtf8 = new Buffer(allCharsString); var allCharsBufferUtf8 = new Buffer(allCharsString);
var allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2'); var allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2');
@ -219,50 +222,54 @@ var allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2');
assert.equal(-1, allCharsBufferUtf8.indexOf('notfound')); assert.equal(-1, allCharsBufferUtf8.indexOf('notfound'));
assert.equal(-1, allCharsBufferUcs2.indexOf('notfound')); assert.equal(-1, allCharsBufferUcs2.indexOf('notfound'));
// Find substrings in Utf8. {
var lengths = [1, 3, 15]; // Single char, simple and complex. // Find substrings in Utf8.
var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b]; const lengths = [1, 3, 15]; // Single char, simple and complex.
for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { const indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b];
for (var i = 0; i < indices.length; i++) { for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
var index = indices[i]; for (let i = 0; i < indices.length; i++) {
var length = lengths[lengthIndex]; const index = indices[i];
let length = lengths[lengthIndex];
if (index + length > 0x7F) { if (index + length > 0x7F) {
length = 2 * length; length = 2 * length;
}
if (index + length > 0x7FF) {
length = 3 * length;
}
if (index + length > 0xFFFF) {
length = 4 * length;
}
var patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length);
assert.equal(index, allCharsBufferUtf8.indexOf(patternBufferUtf8));
var patternStringUtf8 = patternBufferUtf8.toString();
assert.equal(index, allCharsBufferUtf8.indexOf(patternStringUtf8));
} }
if (index + length > 0x7FF) {
length = 3 * length;
}
if (index + length > 0xFFFF) {
length = 4 * length;
}
var patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length);
assert.equal(index, allCharsBufferUtf8.indexOf(patternBufferUtf8));
var patternStringUtf8 = patternBufferUtf8.toString();
assert.equal(index, allCharsBufferUtf8.indexOf(patternStringUtf8));
} }
} }
// Find substrings in Usc2. {
var lengths = [2, 4, 16]; // Single char, simple and complex. // Find substrings in Usc2.
var indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0]; const lengths = [2, 4, 16]; // Single char, simple and complex.
for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { const indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0];
for (var i = 0; i < indices.length; i++) { for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
var index = indices[i] * 2; for (let i = 0; i < indices.length; i++) {
var length = lengths[lengthIndex]; const index = indices[i] * 2;
const length = lengths[lengthIndex];
var patternBufferUcs2 = var patternBufferUcs2 =
allCharsBufferUcs2.slice(index, index + length); allCharsBufferUcs2.slice(index, index + length);
assert.equal( assert.equal(
index, allCharsBufferUcs2.indexOf(patternBufferUcs2, 0, 'ucs2')); index, allCharsBufferUcs2.indexOf(patternBufferUcs2, 0, 'ucs2'));
var patternStringUcs2 = patternBufferUcs2.toString('ucs2'); var patternStringUcs2 = patternBufferUcs2.toString('ucs2');
assert.equal( assert.equal(
index, allCharsBufferUcs2.indexOf(patternStringUcs2, 0, 'ucs2')); index, allCharsBufferUcs2.indexOf(patternStringUcs2, 0, 'ucs2'));
}
} }
} }

View File

@ -50,7 +50,7 @@ assert.deepEqual(arr, [0, 1, 2, 3, 4]);
arr = []; arr = [];
for (var b of buffer.entries()) for (b of buffer.entries())
arr.push(b); arr.push(b);
assert.deepEqual(arr, [ assert.deepEqual(arr, [

File diff suppressed because it is too large Load Diff