This commit is contained in:
2025-09-03 19:18:44 +01:00
parent eb620087c9
commit 695964a636
29 changed files with 631 additions and 571 deletions

View File

@@ -1,19 +1,19 @@
import { numberToUint16BE, numberToUint32BE } from '../../src/utilities/number'
import { numberToUint16BE, numberToUint32BE } from "../../src/utilities/number";
test('Test number conversion to Uint16 big endian buffer.', () => {
test("Test number conversion to Uint16 big endian buffer.", () => {
// When
const result = numberToUint16BE(1234)
const result = numberToUint16BE(1234);
// Then
const expectedResult = new Uint8Array([0x04, 0xd2])
expect(result).toMatchObject(expectedResult)
})
const expectedResult = new Uint8Array([0x04, 0xd2]);
expect(result).toMatchObject(expectedResult);
});
test('Test number conversion to Uint32 big endian buffer.', () => {
test("Test number conversion to Uint32 big endian buffer.", () => {
// When
const result = numberToUint32BE(123456)
const result = numberToUint32BE(123456);
// Then
const expectedResult = new Uint8Array([0x00, 0x01, 0xE2, 0x40])
expect(result).toMatchObject(expectedResult)
})
const expectedResult = new Uint8Array([0x00, 0x01, 0xe2, 0x40]);
expect(result).toMatchObject(expectedResult);
});

View File

@@ -1,230 +1,240 @@
import { SmartBuffer } from '../../src/utilities/smart-buffer'
import { SmartBuffer } from "../../src/utilities/smart-buffer";
test('Read a UInt16.', () => {
test("Read a UInt16.", () => {
// Given
const buffer = [0x30, 0x39]
const buffer = [0x30, 0x39];
// When
const smartBuffer = SmartBuffer.from(buffer)
const smartBuffer = SmartBuffer.from(buffer);
// Then
expect(smartBuffer.readUInt16()).toBe(12345)
})
expect(smartBuffer.readUInt16()).toBe(12345);
});
test('Read a UInt32.', () => {
test("Read a UInt32.", () => {
// Given
const buffer = [0x49, 0x96, 0x02, 0xD2]
const buffer = [0x49, 0x96, 0x02, 0xd2];
// When
const smartBuffer = SmartBuffer.from(buffer)
const smartBuffer = SmartBuffer.from(buffer);
// Then
expect(smartBuffer.readUInt32()).toBe(1234567890)
})
expect(smartBuffer.readUInt32()).toBe(1234567890);
});
test('Read a buffer.', () => {
test("Read a buffer.", () => {
// Given
const buffer = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const buffer = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
// When
const smartBuffer = SmartBuffer.from(buffer)
const smartBuffer = SmartBuffer.from(buffer);
// Then
const result = smartBuffer.readBytes(4)
expect(result).toMatchObject(new Uint8Array([0, 1, 2, 3]))
})
const result = smartBuffer.readBytes(4);
expect(result).toMatchObject(new Uint8Array([0, 1, 2, 3]));
});
test('Read a UInt16 from an offset.', () => {
test("Read a UInt16 from an offset.", () => {
// Given
const buffer = [0x00, 0x00, 0x30, 0x39]
const buffer = [0x00, 0x00, 0x30, 0x39];
// When
const smartBuffer = SmartBuffer.from(buffer)
smartBuffer.cursor = 2
const smartBuffer = SmartBuffer.from(buffer);
smartBuffer.cursor = 2;
// Then
expect(smartBuffer.readUInt16()).toBe(12345)
})
expect(smartBuffer.readUInt16()).toBe(12345);
});
test('Read a UInt32 from an offset.', () => {
test("Read a UInt32 from an offset.", () => {
// Given
const buffer = [0x00, 0x00, 0x49, 0x96, 0x02, 0xD2]
const buffer = [0x00, 0x00, 0x49, 0x96, 0x02, 0xd2];
// When
const smartBuffer = SmartBuffer.from(buffer)
smartBuffer.cursor = 2
const smartBuffer = SmartBuffer.from(buffer);
smartBuffer.cursor = 2;
// Then
expect(smartBuffer.readUInt32()).toBe(1234567890)
})
expect(smartBuffer.readUInt32()).toBe(1234567890);
});
test('Read a buffer from an offset.', () => {
test("Read a buffer from an offset.", () => {
// Given
const buffer = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const buffer = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
// When
const smartBuffer = SmartBuffer.from(buffer)
smartBuffer.cursor = 2
const smartBuffer = SmartBuffer.from(buffer);
smartBuffer.cursor = 2;
// Then
expect(smartBuffer.readBytes(4)).toMatchObject(new Uint8Array([2, 3, 4, 5]))
})
expect(smartBuffer.readBytes(4)).toMatchObject(new Uint8Array([2, 3, 4, 5]));
});
test('Write a UInt16.', () => {
test("Write a UInt16.", () => {
// Given
const smartBuffer = new SmartBuffer()
const smartBuffer = new SmartBuffer();
// When
smartBuffer.writeUInt16(12345)
smartBuffer.writeUInt16(12345);
// Then
expect(smartBuffer.data).toMatchObject(new Uint8Array([0x30, 0x39]))
})
expect(smartBuffer.data).toMatchObject(new Uint8Array([0x30, 0x39]));
});
test('Write a UInt32.', () => {
test("Write a UInt32.", () => {
// Given
const smartBuffer = new SmartBuffer()
const smartBuffer = new SmartBuffer();
// When
smartBuffer.writeUInt32(1234567890)
smartBuffer.writeUInt32(1234567890);
// Then
expect(smartBuffer.data).toMatchObject(new Uint8Array([0x49, 0x96, 0x02, 0xD2]))
})
expect(smartBuffer.data).toMatchObject(
new Uint8Array([0x49, 0x96, 0x02, 0xd2]),
);
});
test('Write a buffer.', () => {
test("Write a buffer.", () => {
// Given
const smartBuffer = new SmartBuffer()
const smartBuffer = new SmartBuffer();
// When
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
// Then
expect(smartBuffer.data).toMatchObject(new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
})
expect(smartBuffer.data).toMatchObject(
new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
);
});
test('Write a UInt16 at an offset.', () => {
test("Write a UInt16 at an offset.", () => {
// Given
const smartBuffer = new SmartBuffer()
const smartBuffer = new SmartBuffer();
// When
smartBuffer.cursor = 2
smartBuffer.writeUInt16(12345)
smartBuffer.cursor = 2;
smartBuffer.writeUInt16(12345);
// Then
expect(smartBuffer.data).toMatchObject(new Uint8Array([0x00, 0x00, 0x30, 0x39]))
})
expect(smartBuffer.data).toMatchObject(
new Uint8Array([0x00, 0x00, 0x30, 0x39]),
);
});
test('Write a UInt32 at an offset.', () => {
test("Write a UInt32 at an offset.", () => {
// Given
const smartBuffer = new SmartBuffer()
const smartBuffer = new SmartBuffer();
// When
smartBuffer.cursor = 2
smartBuffer.writeUInt32(1234567890)
smartBuffer.cursor = 2;
smartBuffer.writeUInt32(1234567890);
// Then
expect(smartBuffer.data).toMatchObject(new Uint8Array([0x00, 0x00, 0x49, 0x96, 0x02, 0xD2]))
})
expect(smartBuffer.data).toMatchObject(
new Uint8Array([0x00, 0x00, 0x49, 0x96, 0x02, 0xd2]),
);
});
test('Write a buffer at an offset.', () => {
test("Write a buffer at an offset.", () => {
// Given
const smartBuffer = new SmartBuffer()
const smartBuffer = new SmartBuffer();
// When
smartBuffer.cursor = 2
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
smartBuffer.cursor = 2;
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
// Then
expect(smartBuffer.data).toMatchObject(new Uint8Array([0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
})
expect(smartBuffer.data).toMatchObject(
new Uint8Array([0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
);
});
test('Cursor is correctly incremented after reading a UInt16.', () => {
test("Cursor is correctly incremented after reading a UInt16.", () => {
// Given
const buffer = new Uint8Array(4)
const buffer = new Uint8Array(4);
// When
const smartBuffer = SmartBuffer.from(buffer)
const smartBuffer = SmartBuffer.from(buffer);
// Then
smartBuffer.readUInt16()
expect(smartBuffer.cursor).toBe(2)
})
smartBuffer.readUInt16();
expect(smartBuffer.cursor).toBe(2);
});
test('Cursor is correctly incremented after reading a UInt32.', () => {
test("Cursor is correctly incremented after reading a UInt32.", () => {
// Given
const buffer = new Uint8Array(4)
const buffer = new Uint8Array(4);
// When
const smartBuffer = SmartBuffer.from(buffer)
const smartBuffer = SmartBuffer.from(buffer);
// Then
smartBuffer.readUInt32()
expect(smartBuffer.cursor).toBe(4)
})
smartBuffer.readUInt32();
expect(smartBuffer.cursor).toBe(4);
});
test('Cursor is correctly incremented after reading a buffer.', () => {
test("Cursor is correctly incremented after reading a buffer.", () => {
// Given
const buffer = new Uint8Array(8)
const buffer = new Uint8Array(8);
// When
const smartBuffer = SmartBuffer.from(buffer)
const smartBuffer = SmartBuffer.from(buffer);
// Then
smartBuffer.readBytes(4)
expect(smartBuffer.cursor).toBe(4)
})
smartBuffer.readBytes(4);
expect(smartBuffer.cursor).toBe(4);
});
test('Cursor is correctly incremented after writing a UInt16.', () => {
test("Cursor is correctly incremented after writing a UInt16.", () => {
// Given
const smartBuffer = new SmartBuffer()
const smartBuffer = new SmartBuffer();
// When
smartBuffer.writeUInt16(12345)
smartBuffer.writeUInt16(12345);
// Then
expect(smartBuffer.cursor).toBe(2)
})
expect(smartBuffer.cursor).toBe(2);
});
test('Cursor is correctly incremented after writing a UInt32.', () => {
test("Cursor is correctly incremented after writing a UInt32.", () => {
// Given
const smartBuffer = new SmartBuffer()
const smartBuffer = new SmartBuffer();
// When
smartBuffer.writeUInt32(1234567890)
smartBuffer.writeUInt32(1234567890);
// Then
expect(smartBuffer.cursor).toBe(4)
})
expect(smartBuffer.cursor).toBe(4);
});
test('Cursor is correctly incremented after writing a buffer.', () => {
test("Cursor is correctly incremented after writing a buffer.", () => {
// Given
const smartBuffer = new SmartBuffer()
const smartBuffer = new SmartBuffer();
// When
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
// Then
expect(smartBuffer.cursor).toBe(10)
})
expect(smartBuffer.cursor).toBe(10);
});
test('Seek to position below 0 throws range error.', () => {
test("Seek to position below 0 throws range error.", () => {
// When
const smartBuffer = new SmartBuffer()
const smartBuffer = new SmartBuffer();
// Then
expect(() => {
smartBuffer.cursor = -1
}).toThrow(RangeError)
})
smartBuffer.cursor = -1;
}).toThrow(RangeError);
});
test('Pad some data.', () => {
test("Pad some data.", () => {
// Given
const smartBuffer = new SmartBuffer()
const smartBuffer = new SmartBuffer();
// When
smartBuffer.pad(10)
smartBuffer.pad(10);
// Then
expect(smartBuffer.length).toBe(10)
})
expect(smartBuffer.length).toBe(10);
});