|
| 1 | +import assert from 'assert'; |
| 2 | +import { describe, it } from 'mocha'; |
| 3 | + |
| 4 | +import { DynamicBuffer } from '../src'; |
| 5 | + |
| 6 | +describe('Comparison tests', () => { |
| 7 | + it('Compare with other DynamicBuffers', () => { |
| 8 | + const buf1 = new DynamicBuffer(); |
| 9 | + const buf2 = new DynamicBuffer(); |
| 10 | + const buf3 = new DynamicBuffer(); |
| 11 | + |
| 12 | + buf1.append('ABC'); |
| 13 | + buf2.append('BCD'); |
| 14 | + buf3.append('ABCD'); |
| 15 | + |
| 16 | + assert.equal(buf1.compare(buf1), 0, 'expected ABC == ABC'); |
| 17 | + assert.equal(buf1.compare(buf2), -1, 'expect ABC < BCD'); |
| 18 | + assert.equal(buf1.compare(buf3), -1, 'expect ABC < ABCD'); |
| 19 | + assert.equal(buf2.compare(buf1), 1, 'expect BCD > ABC'); |
| 20 | + assert.equal(buf2.compare(buf3), 1, 'expect BCD > ABCD'); |
| 21 | + assert.equal(buf3.compare(buf1), 1, 'expect ABCD > ABC'); |
| 22 | + assert.equal(buf3.compare(buf2), -1, 'expect ABCD < BCD'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('Compare with other Buffer (1)', () => { |
| 26 | + const buf1 = new DynamicBuffer(); |
| 27 | + buf1.append('ABC'); |
| 28 | + |
| 29 | + const buf2 = Buffer.from('BCD'); |
| 30 | + const buf3 = Buffer.from('ABCD'); |
| 31 | + |
| 32 | + assert.equal(buf1.compare(buf1), 0, 'expected ABC == ABC'); |
| 33 | + assert.equal(buf1.compare(buf2), -1, 'expect ABC < BCD'); |
| 34 | + assert.equal(buf1.compare(buf3), -1, 'expect ABC < ABCD'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('Compare with other Buffer (2)', () => { |
| 38 | + const buf1 = new DynamicBuffer(); |
| 39 | + buf1.append('BCD'); |
| 40 | + |
| 41 | + const buf2 = Buffer.from('ABC'); |
| 42 | + const buf3 = Buffer.from('ABCD'); |
| 43 | + |
| 44 | + assert.equal(buf1.compare(buf2), 1, 'expect BCD > ABC'); |
| 45 | + assert.equal(buf1.compare(buf3), 1, 'expect BCD > ABCD'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('Compare with other Buffer (3)', () => { |
| 49 | + const buf1 = new DynamicBuffer(); |
| 50 | + buf1.append('ABCD'); |
| 51 | + |
| 52 | + const buf2 = Buffer.from('ABC'); |
| 53 | + const buf3 = Buffer.from('BCD'); |
| 54 | + |
| 55 | + assert.equal(buf1.compare(buf2), 1, 'expect ABCD > ABC'); |
| 56 | + assert.equal(buf1.compare(buf3), -1, 'expect ABCD < BCD'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('Compare with other Uint8Array (1)', () => { |
| 60 | + const buf1 = new DynamicBuffer(); |
| 61 | + buf1.append('ABC'); |
| 62 | + |
| 63 | + const buf2 = new Uint8Array([0x42, 0x43, 0x44]); |
| 64 | + const buf3 = new Uint8Array([0x41, 0x42, 0x43, 0x44]); |
| 65 | + |
| 66 | + assert.equal(buf1.compare(buf1), 0, 'expected ABC == ABC'); |
| 67 | + assert.equal(buf1.compare(buf2), -1, 'expect ABC < BCD'); |
| 68 | + assert.equal(buf1.compare(buf3), -1, 'expect ABC < ABCD'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('Compare with other Uint8Array (2)', () => { |
| 72 | + const buf1 = new DynamicBuffer(); |
| 73 | + buf1.append('BCD'); |
| 74 | + |
| 75 | + const buf2 = new Uint8Array([0x41, 0x42, 0x43]); |
| 76 | + const buf3 = new Uint8Array([0x41, 0x42, 0x43, 0x44]); |
| 77 | + |
| 78 | + assert.equal(buf1.compare(buf2), 1, 'expect BCD > ABC'); |
| 79 | + assert.equal(buf1.compare(buf3), 1, 'expect BCD > ABCD'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('Compare with other Uint8Array (3)', () => { |
| 83 | + const buf1 = new DynamicBuffer(); |
| 84 | + buf1.append('ABCD'); |
| 85 | + |
| 86 | + const buf2 = new Uint8Array([0x41, 0x42, 0x43]); |
| 87 | + const buf3 = new Uint8Array([0x42, 0x43, 0x44]); |
| 88 | + |
| 89 | + assert.equal(buf1.compare(buf2), 1, 'expect ABCD > ABC'); |
| 90 | + assert.equal(buf1.compare(buf3), -1, 'expect ABCD < BCD'); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +describe('Equality tests', () => { |
| 95 | + it('Compare equals with other DynamicBuffers', () => { |
| 96 | + const buf1 = new DynamicBuffer(); |
| 97 | + const buf2 = new DynamicBuffer(); |
| 98 | + const buf3 = new DynamicBuffer(); |
| 99 | + const buf4 = new DynamicBuffer(); |
| 100 | + |
| 101 | + buf1.append('ABC'); |
| 102 | + buf2.append('ABC'); |
| 103 | + buf3.append('BCD'); |
| 104 | + buf4.append('ABCD'); |
| 105 | + |
| 106 | + assert.equal(buf1.equals(buf1), true, 'expected ABC == ABC'); |
| 107 | + assert.equal(buf1.equals(buf2), true, 'expected ABC == ABC'); |
| 108 | + assert.equal(buf1.equals(buf3), false, 'expected ABC != ABC'); |
| 109 | + assert.equal(buf1.equals(buf4), false, 'expected ABC != ABCD'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('Compare equals with other Buffer', () => { |
| 113 | + const buf1 = new DynamicBuffer(); |
| 114 | + buf1.append('ABC'); |
| 115 | + |
| 116 | + const buf2 = Buffer.from('ABC'); |
| 117 | + const buf3 = Buffer.from('BCD'); |
| 118 | + const buf4 = Buffer.from('ABCD'); |
| 119 | + |
| 120 | + assert.equal(buf1.equals(buf1), true, 'expected ABC == ABC'); |
| 121 | + assert.equal(buf1.equals(buf2), true, 'expected ABC == ABC'); |
| 122 | + assert.equal(buf1.equals(buf3), false, 'expected ABC != ABC'); |
| 123 | + assert.equal(buf1.equals(buf4), false, 'expected ABC != ABCD'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('Compare equals with other Uint8Array', () => { |
| 127 | + const buf1 = new DynamicBuffer(); |
| 128 | + buf1.append('ABC'); |
| 129 | + |
| 130 | + const buf2 = new Uint8Array([0x41, 0x42, 0x43]); |
| 131 | + const buf3 = new Uint8Array([0x42, 0x43, 0x44]); |
| 132 | + const buf4 = new Uint8Array([0x41, 0x42, 0x43, 0x44]); |
| 133 | + |
| 134 | + assert.equal(buf1.equals(buf1), true, 'expected ABC == ABC'); |
| 135 | + assert.equal(buf1.equals(buf2), true, 'expected ABC == ABC'); |
| 136 | + assert.equal(buf1.equals(buf3), false, 'expected ABC != ABC'); |
| 137 | + assert.equal(buf1.equals(buf4), false, 'expected ABC != ABCD'); |
| 138 | + }); |
| 139 | +}); |
0 commit comments