|
| 1 | +import assert from 'assert'; |
| 2 | +import { describe, it } from 'mocha'; |
| 3 | + |
| 4 | +import { DynamicBuffer } from '../src'; |
| 5 | + |
| 6 | +describe('IndexOf tests', () => { |
| 7 | + it('Test indexOf with empty buffer and empty value.', () => { |
| 8 | + const buf = new DynamicBuffer(); |
| 9 | + |
| 10 | + assert.equal(buf.indexOf('Hello'), -1); |
| 11 | + assert.equal(buf.indexOf(''), 0); |
| 12 | + assert.equal(buf.indexOf(0), -1); |
| 13 | + assert.equal(buf.indexOf(Buffer.alloc(0)), 0); |
| 14 | + assert.equal(buf.indexOf(new Uint8Array()), 0); |
| 15 | + assert.equal(buf.indexOf(new DynamicBuffer()), 0); |
| 16 | + }); |
| 17 | + |
| 18 | + it('Test indexOf with empty value.', () => { |
| 19 | + const buf = new DynamicBuffer(); |
| 20 | + buf.append('Hello world'); |
| 21 | + |
| 22 | + assert.equal(buf.indexOf(''), 0); |
| 23 | + assert.equal(buf.indexOf(0), -1); |
| 24 | + assert.equal(buf.indexOf(Buffer.alloc(0)), 0); |
| 25 | + assert.equal(buf.indexOf(new Uint8Array()), 0); |
| 26 | + assert.equal(buf.indexOf(new DynamicBuffer()), 0); |
| 27 | + }); |
| 28 | + |
| 29 | + it('Test indexOf with string.', () => { |
| 30 | + const buf = new DynamicBuffer(); |
| 31 | + buf.append('Hello world'); |
| 32 | + |
| 33 | + assert.equal(buf.indexOf('Hello'), 0); |
| 34 | + assert.equal(buf.indexOf('world'), 6); |
| 35 | + assert.equal(buf.indexOf('no'), -1); |
| 36 | + }); |
| 37 | + |
| 38 | + it('Test indexOf with number.', () => { |
| 39 | + const buf = new DynamicBuffer(); |
| 40 | + buf.append('Hello world'); |
| 41 | + |
| 42 | + assert.equal(buf.indexOf(72), 0); // H |
| 43 | + assert.equal(buf.indexOf(119), 6); // w |
| 44 | + assert.equal(buf.indexOf(97), -1); // a |
| 45 | + }); |
| 46 | + |
| 47 | + it('Test indexOf with Buffer.', () => { |
| 48 | + const buf = new DynamicBuffer(); |
| 49 | + buf.append('Hello world'); |
| 50 | + |
| 51 | + assert.equal(buf.indexOf(Buffer.from('Hello')), 0); |
| 52 | + assert.equal(buf.indexOf(Buffer.from('world')), 6); |
| 53 | + assert.equal(buf.indexOf(Buffer.from('no')), -1); |
| 54 | + }); |
| 55 | + |
| 56 | + it('Test indexOf with Uint8Array.', () => { |
| 57 | + const buf = new DynamicBuffer(); |
| 58 | + buf.append('Hello world'); |
| 59 | + |
| 60 | + assert.equal(buf.indexOf(new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f])), 0); |
| 61 | + assert.equal(buf.indexOf(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64])), 6); |
| 62 | + assert.equal(buf.indexOf(new Uint8Array([0x6e, 0x6c])), -1); |
| 63 | + }); |
| 64 | + |
| 65 | + it('Test indexOf with DynamicBuffer.', () => { |
| 66 | + const buf = new DynamicBuffer(); |
| 67 | + buf.append('Hello world'); |
| 68 | + |
| 69 | + const buf1 = new DynamicBuffer(); |
| 70 | + buf1.append('Hello'); |
| 71 | + assert.equal(buf.indexOf(buf1), 0); |
| 72 | + |
| 73 | + const buf2 = new DynamicBuffer(); |
| 74 | + buf2.append('world'); |
| 75 | + assert.equal(buf.indexOf(buf2), 6); |
| 76 | + |
| 77 | + const buf3 = new DynamicBuffer(); |
| 78 | + buf3.append('no'); |
| 79 | + assert.equal(buf.indexOf(buf3), -1); |
| 80 | + }); |
| 81 | + |
| 82 | + it('Test indexOf with byteOffset parameter.', () => { |
| 83 | + const buf = new DynamicBuffer(); |
| 84 | + buf.append('Hello world'); |
| 85 | + |
| 86 | + assert.equal(buf.indexOf('Hello'), 0); |
| 87 | + assert.equal(buf.indexOf('Hello', 0), 0); |
| 88 | + assert.equal(buf.indexOf('Hello', 1), -1); |
| 89 | + assert.equal(buf.indexOf('Hello', -1), -1); |
| 90 | + assert.equal(buf.indexOf('Hello', -11), 0); // -11 equals 0 |
| 91 | + assert.equal(buf.indexOf('Hello', 32), -1); |
| 92 | + assert.equal(buf.indexOf('', 32), 0); |
| 93 | + }); |
| 94 | +}); |
0 commit comments