Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(PointerArray): throw error if array size > 4294967295 #168

Merged
merged 2 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions test/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,38 @@ describe('utils', function() {

describe('typed-arrays', function() {

describe('#.getPointerArray', function() {
var validatePointerArrayConstructor = function (min, max, expectedCtor) {
it(`returns ${expectedCtor} for ${min}`, () => {
assert.strictEqual(typed.getPointerArray(min), expectedCtor);
});
it(`returns ${expectedCtor} for ${(max - min) / 2}`, () => {
assert.strictEqual(typed.getPointerArray((max - min) / 2), expectedCtor);
});
it(`returns ${expectedCtor} for ${max}`, () => {
assert.strictEqual(typed.getPointerArray(max), expectedCtor);
});
};

describe('returns Uint8Array for capacity <= Math.pow(2, 8)', function() {
validatePointerArrayConstructor(0, Math.pow(2, 8), Uint8Array);
});

describe('returns Uint16Array for Math.pow(2, 8) < capacity <= Math.pow(2, 16)', function() {
validatePointerArrayConstructor(Math.pow(2, 8) + 1, Math.pow(2, 16), Uint16Array);
});

describe('returns Uint32Array for Math.pow(2, 16) < capacity <= Math.pow(2, 32)', function() {
validatePointerArrayConstructor(Math.pow(2, 16) + 1, Math.pow(2, 32), Uint32Array);
});

describe('throws error for capacity > Math.pow(2, 32)', function() {
assert.throws(function() {
typed.getPointerArray(Math.pow(2, 32) + 1);
}, /Pointer Array of size > 4294967295 is not supported/);
});
});

describe('#.getMinimalRepresentation', function() {

it('should return the correct type.', function() {
Expand Down
2 changes: 1 addition & 1 deletion utils/typed-arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ exports.getPointerArray = function(size) {
if (maxIndex <= MAX_32BIT_INTEGER)
return Uint32Array;

return Float64Array;
throw new Error('mnemonist: Pointer Array of size > 4294967295 is not supported.');
};

exports.getSignedPointerArray = function(size) {
Expand Down