Skip to content

Commit e7c953a

Browse files
dcposchitaloacasas
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 978acd1 commit e7c953a

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

test/parallel/test-buffer-indexof.js

+60-3
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);
@@ -359,6 +361,37 @@ assert.throws(() => {
359361
b.indexOf([]);
360362
}, argumentExpected);
361363

364+
// Test weird offset arguments.
365+
// The following offsets coerce to NaN or 0, searching the whole Buffer
366+
assert.strictEqual(b.indexOf('b', undefined), 1);
367+
assert.strictEqual(b.indexOf('b', {}), 1);
368+
assert.strictEqual(b.indexOf('b', 0), 1);
369+
assert.strictEqual(b.indexOf('b', null), 1);
370+
assert.strictEqual(b.indexOf('b', []), 1);
371+
372+
// The following offset coerces to 2, in other words +[2] === 2
373+
assert.strictEqual(b.indexOf('b', [2]), -1);
374+
375+
// Behavior should match String.indexOf()
376+
assert.strictEqual(
377+
b.indexOf('b', undefined),
378+
s.indexOf('b', undefined));
379+
assert.strictEqual(
380+
b.indexOf('b', {}),
381+
s.indexOf('b', {}));
382+
assert.strictEqual(
383+
b.indexOf('b', 0),
384+
s.indexOf('b', 0));
385+
assert.strictEqual(
386+
b.indexOf('b', null),
387+
s.indexOf('b', null));
388+
assert.strictEqual(
389+
b.indexOf('b', []),
390+
s.indexOf('b', []));
391+
assert.strictEqual(
392+
b.indexOf('b', [2]),
393+
s.indexOf('b', [2]));
394+
362395
// All code for handling encodings is shared between Buffer.indexOf and
363396
// Buffer.lastIndexOf, so only testing the separate lastIndexOf semantics.
364397

@@ -413,14 +446,38 @@ assert.strictEqual(b.lastIndexOf(0x61, Infinity), 0);
413446
assert.strictEqual(b.lastIndexOf(0x0), -1);
414447

415448
// Test weird offset arguments.
416-
// Behaviour should match String.lastIndexOf:
417-
assert.strictEqual(b.lastIndexOf('b', 0), -1);
449+
// The following offsets coerce to NaN, searching the whole Buffer
418450
assert.strictEqual(b.lastIndexOf('b', undefined), 1);
419-
assert.strictEqual(b.lastIndexOf('b', null), -1);
420451
assert.strictEqual(b.lastIndexOf('b', {}), 1);
452+
453+
// The following offsets coerce to 0
454+
assert.strictEqual(b.lastIndexOf('b', 0), -1);
455+
assert.strictEqual(b.lastIndexOf('b', null), -1);
421456
assert.strictEqual(b.lastIndexOf('b', []), -1);
457+
458+
// The following offset coerces to 2, in other words +[2] === 2
422459
assert.strictEqual(b.lastIndexOf('b', [2]), 1);
423460

461+
// Behavior should match String.lastIndexOf()
462+
assert.strictEqual(
463+
b.lastIndexOf('b', undefined),
464+
s.lastIndexOf('b', undefined));
465+
assert.strictEqual(
466+
b.lastIndexOf('b', {}),
467+
s.lastIndexOf('b', {}));
468+
assert.strictEqual(
469+
b.lastIndexOf('b', 0),
470+
s.lastIndexOf('b', 0));
471+
assert.strictEqual(
472+
b.lastIndexOf('b', null),
473+
s.lastIndexOf('b', null));
474+
assert.strictEqual(
475+
b.lastIndexOf('b', []),
476+
s.lastIndexOf('b', []));
477+
assert.strictEqual(
478+
b.lastIndexOf('b', [2]),
479+
s.lastIndexOf('b', [2]));
480+
424481
// Test needles longer than the haystack.
425482
assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'ucs2'), -1);
426483
assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'utf8'), -1);

0 commit comments

Comments
 (0)