diff options
author | 2022-08-20 15:17:17 +0800 | |
---|---|---|
committer | 2022-08-20 00:17:17 -0700 | |
commit | 2641884342f4757867565c30c224c9a5a6b9e2d3 (patch) | |
tree | 63b78e7fcd5e80dded052c87b39b561e554f4aa1 /test/bun.js/buffer.test.js | |
parent | b1bc549cf71f58d480aeca6804cadcbc1b67d662 (diff) | |
download | bun-2641884342f4757867565c30c224c9a5a6b9e2d3.tar.gz bun-2641884342f4757867565c30c224c9a5a6b9e2d3.tar.zst bun-2641884342f4757867565c30c224c9a5a6b9e2d3.zip |
Add buffer.indexOf, includes and lastIndexOf (#1112)
* Add buffer.indexOf, includes and lastIndexOf
* use memmem
* use int64_t
* fix upon reviews
Diffstat (limited to 'test/bun.js/buffer.test.js')
-rw-r--r-- | test/bun.js/buffer.test.js | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/test/bun.js/buffer.test.js b/test/bun.js/buffer.test.js index fa086d744..0b3abd270 100644 --- a/test/bun.js/buffer.test.js +++ b/test/bun.js/buffer.test.js @@ -386,3 +386,81 @@ it("read", () => { expect(buf.readUInt8(0)).toBe(255); reset(); }); + +it("includes", () => { + const buf = Buffer.from('this is a buffer'); + + expect(buf.includes('this')).toBe(true); + expect(buf.includes('is')).toBe(true); + expect(buf.includes(Buffer.from('a buffer'))).toBe(true); + expect(buf.includes(97)).toBe(true); + expect(buf.includes(Buffer.from('a buffer example'))).toBe(false); + expect(buf.includes(Buffer.from('a buffer example').slice(0, 8))).toBe(true); + expect(buf.includes('this', 4)).toBe(false); +}); + +it("indexOf", () => { + const buf = Buffer.from('this is a buffer'); + + expect(buf.indexOf('this')).toBe(0); + expect(buf.indexOf('is')).toBe(2); + expect(buf.indexOf(Buffer.from('a buffer'))).toBe(8); + expect(buf.indexOf(97)).toBe(8); + expect(buf.indexOf(Buffer.from('a buffer example'))).toBe(-1); + expect(buf.indexOf(Buffer.from('a buffer example').slice(0, 8))).toBe(8); + + const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le'); + + expect(utf16Buffer.indexOf('\u03a3', 0, 'utf16le')).toBe(4); + expect(utf16Buffer.indexOf('\u03a3', -4, 'utf16le')).toBe(6); + + const b = Buffer.from('abcdef'); + + // Passing a value that's a number, but not a valid byte. + // Prints: 2, equivalent to searching for 99 or 'c'. + expect(b.indexOf(99.9)).toBe(2); + expect(b.indexOf(256 + 99)).toBe(2); + + // Passing a byteOffset that coerces to NaN or 0. + // Prints: 1, searching the whole buffer. + expect(b.indexOf('b', undefined)).toBe(1); + expect(b.indexOf('b', {})).toBe(1); + expect(b.indexOf('b', null)).toBe(1); + expect(b.indexOf('b', [])).toBe(1); +}); + +it("lastIndexOf", () => { + const buf = Buffer.from('this buffer is a buffer'); + + expect(buf.lastIndexOf('this')).toBe(0); + expect(buf.lastIndexOf('this', 0)).toBe(0); + expect(buf.lastIndexOf('this', -1000)).toBe(-1); + expect(buf.lastIndexOf('buffer')).toBe(17); + expect(buf.lastIndexOf(Buffer.from('buffer'))).toBe(17); + expect(buf.lastIndexOf(97)).toBe(15); + expect(buf.lastIndexOf(Buffer.from('yolo'))).toBe(-1); + expect(buf.lastIndexOf('buffer', 5)).toBe(5); + expect(buf.lastIndexOf('buffer', 4)).toBe(-1); + + const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le'); + + expect(utf16Buffer.lastIndexOf('\u03a3', undefined, 'utf16le')).toBe(6); + expect(utf16Buffer.lastIndexOf('\u03a3', -5, 'utf16le')).toBe(4); + + const b = Buffer.from('abcdef'); + + // Passing a value that's a number, but not a valid byte. + // Prints: 2, equivalent to searching for 99 or 'c'. + expect(b.lastIndexOf(99.9)).toBe(2); + expect(b.lastIndexOf(256 + 99)).toBe(2); + + // Passing a byteOffset that coerces to NaN or 0. + // Prints: 1, searching the whole buffer. + expect(b.lastIndexOf('b', undefined)).toBe(1); + expect(b.lastIndexOf('b', {})).toBe(1); + + // Passing a byteOffset that coerces to 0. + // Prints: -1, equivalent to passing 0. + expect(b.lastIndexOf('b', null)).toBe(-1); + expect(b.lastIndexOf('b', [])).toBe(-1); +}); |