Skip to content

Commit 6584ea0

Browse files
dcposchMylesBorins
authored andcommitted
test: update Buffer.lastIndexOf
Test type coercion for non-number offset arguments. Verify that Buffer and String behave the same way. PR-URL: #10162 Fixes: #9801 Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent b662c11 commit 6584ea0

File tree

1 file changed

+64
-7
lines changed

1 file changed

+64
-7
lines changed

test/parallel/test-buffer-indexof.js

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const buf_f = Buffer.from('f');
1111
const buf_z = Buffer.from('z');
1212
const buf_empty = Buffer.from('');
1313

14+
const s = 'abcdef';
15+
1416
assert.strictEqual(b.indexOf('a'), 0);
1517
assert.strictEqual(b.indexOf('a', 1), -1);
1618
assert.strictEqual(b.indexOf('a', -1), -1);
@@ -347,6 +349,37 @@ assert.throws(function() {
347349
b.indexOf([]);
348350
});
349351

352+
// Test weird offset arguments.
353+
// The following offsets coerce to NaN or 0, searching the whole Buffer
354+
assert.strictEqual(b.indexOf('b', undefined), 1);
355+
assert.strictEqual(b.indexOf('b', {}), 1);
356+
assert.strictEqual(b.indexOf('b', 0), 1);
357+
assert.strictEqual(b.indexOf('b', null), 1);
358+
assert.strictEqual(b.indexOf('b', []), 1);
359+
360+
// The following offset coerces to 2, in other words +[2] === 2
361+
assert.strictEqual(b.indexOf('b', [2]), -1);
362+
363+
// Behavior should match String.indexOf()
364+
assert.strictEqual(
365+
b.indexOf('b', undefined),
366+
s.indexOf('b', undefined));
367+
assert.strictEqual(
368+
b.indexOf('b', {}),
369+
s.indexOf('b', {}));
370+
assert.strictEqual(
371+
b.indexOf('b', 0),
372+
s.indexOf('b', 0));
373+
assert.strictEqual(
374+
b.indexOf('b', null),
375+
s.indexOf('b', null));
376+
assert.strictEqual(
377+
b.indexOf('b', []),
378+
s.indexOf('b', []));
379+
assert.strictEqual(
380+
b.indexOf('b', [2]),
381+
s.indexOf('b', [2]));
382+
350383
// All code for handling encodings is shared between Buffer.indexOf and
351384
// Buffer.lastIndexOf, so only testing the separate lastIndexOf semantics.
352385

@@ -401,13 +434,37 @@ assert.equal(b.lastIndexOf(0x61, Infinity), 0);
401434
assert.equal(b.lastIndexOf(0x0), -1);
402435

403436
// Test weird offset arguments.
404-
// Behaviour should match String.lastIndexOf:
405-
assert.equal(b.lastIndexOf('b', 0), -1);
406-
assert.equal(b.lastIndexOf('b', undefined), 1);
407-
assert.equal(b.lastIndexOf('b', null), -1);
408-
assert.equal(b.lastIndexOf('b', {}), 1);
409-
assert.equal(b.lastIndexOf('b', []), -1);
410-
assert.equal(b.lastIndexOf('b', [2]), 1);
437+
// The following offsets coerce to NaN, searching the whole Buffer
438+
assert.strictEqual(b.lastIndexOf('b', undefined), 1);
439+
assert.strictEqual(b.lastIndexOf('b', {}), 1);
440+
441+
// The following offsets coerce to 0
442+
assert.strictEqual(b.lastIndexOf('b', 0), -1);
443+
assert.strictEqual(b.lastIndexOf('b', null), -1);
444+
assert.strictEqual(b.lastIndexOf('b', []), -1);
445+
446+
// The following offset coerces to 2, in other words +[2] === 2
447+
assert.strictEqual(b.lastIndexOf('b', [2]), 1);
448+
449+
// Behavior should match String.lastIndexOf()
450+
assert.strictEqual(
451+
b.lastIndexOf('b', undefined),
452+
s.lastIndexOf('b', undefined));
453+
assert.strictEqual(
454+
b.lastIndexOf('b', {}),
455+
s.lastIndexOf('b', {}));
456+
assert.strictEqual(
457+
b.lastIndexOf('b', 0),
458+
s.lastIndexOf('b', 0));
459+
assert.strictEqual(
460+
b.lastIndexOf('b', null),
461+
s.lastIndexOf('b', null));
462+
assert.strictEqual(
463+
b.lastIndexOf('b', []),
464+
s.lastIndexOf('b', []));
465+
assert.strictEqual(
466+
b.lastIndexOf('b', [2]),
467+
s.lastIndexOf('b', [2]));
411468

412469
// Test needles longer than the haystack.
413470
assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'ucs2'), -1);

0 commit comments

Comments
 (0)