-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: bitstring improvement and test coverage (#270)
Signed-off-by: Francisco Javier Ribo Labrador <elribonazo@gmail.com>
- Loading branch information
1 parent
8a1ed3f
commit dce65b5
Showing
3 changed files
with
156 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,87 @@ | ||
import { base64url } from 'multiformats/bases/base64'; | ||
import { gzip, ungzip } from 'pako'; | ||
|
||
export class Bitstring { | ||
bits: Uint8Array; | ||
length: number; | ||
leftToRightIndexing: boolean; | ||
|
||
constructor({ | ||
buffer, | ||
leftToRightIndexing | ||
}: { | ||
buffer: Uint8Array; | ||
public bits: Uint8Array; | ||
public length: number; | ||
private leftToRightIndexing: boolean; | ||
|
||
constructor(options: { | ||
length?: number; | ||
buffer?: Uint8Array; | ||
leftToRightIndexing?: boolean; | ||
}) { | ||
if (!buffer) { | ||
littleEndianBits?: boolean; | ||
} = {}) { | ||
const { length, buffer, leftToRightIndexing, littleEndianBits } = options; | ||
|
||
if (length && buffer) { | ||
throw new Error('Only one of "length" or "buffer" must be given.'); | ||
} | ||
this.bits = new Uint8Array(buffer.buffer); | ||
this.length = buffer.length * 8; | ||
this.leftToRightIndexing = leftToRightIndexing ?? false; | ||
|
||
if (littleEndianBits !== undefined) { | ||
if (leftToRightIndexing !== undefined) { | ||
throw new Error( | ||
'Using both "littleEndianBits" and "leftToRightIndexing" is not allowed.' | ||
); | ||
} | ||
this.leftToRightIndexing = littleEndianBits; | ||
} else { | ||
this.leftToRightIndexing = leftToRightIndexing ?? true; | ||
} | ||
|
||
if (length) { | ||
this.bits = new Uint8Array(Math.ceil(length / 8)); | ||
this.length = length; | ||
} else if (buffer) { | ||
this.bits = new Uint8Array(buffer.buffer); | ||
this.length = buffer.length * 8; | ||
} else { | ||
throw new Error('Either "length" or "buffer" must be provided.'); | ||
} | ||
} | ||
|
||
set(position: number, on: boolean): void { | ||
const { length, leftToRightIndexing } = this; | ||
const { index, bit } = _parsePosition(position, length, leftToRightIndexing); | ||
const { index, bit } = this.parsePosition(position); | ||
if (on) { | ||
this.bits[index] |= bit; | ||
} else { | ||
this.bits[index] &= 0xff ^ bit; | ||
this.bits[index] &= 0xFF ^ bit; | ||
} | ||
} | ||
|
||
get(position: number): boolean { | ||
const { length, leftToRightIndexing } = this; | ||
const { index, bit } = _parsePosition(position, length, leftToRightIndexing); | ||
const { index, bit } = this.parsePosition(position); | ||
return !!(this.bits[index] & bit); | ||
} | ||
|
||
async encodeBits(): Promise<string> { | ||
return base64url.baseEncode(gzip(this.bits)); | ||
} | ||
|
||
static async decodeBits(encoded: string): Promise<Uint8Array> { | ||
return ungzip(base64url.baseDecode(encoded)); | ||
} | ||
|
||
async compressBits(): Promise<Uint8Array> { | ||
return gzip(this.bits); | ||
} | ||
|
||
static async uncompressBits(compressed: Uint8Array): Promise<Uint8Array> { | ||
return ungzip(compressed); | ||
} | ||
|
||
private parsePosition(position: number): { index: number; bit: number } { | ||
if (position < 0 || position >= this.length) { | ||
throw new Error( | ||
`Position "${position}" is out of range "0-${this.length - 1}".` | ||
); | ||
} | ||
|
||
} | ||
const index = Math.floor(position / 8); | ||
const rem = position % 8; | ||
const shift = this.leftToRightIndexing ? 7 - rem : rem; | ||
const bit = 1 << shift; | ||
|
||
function _parsePosition( | ||
position: number, | ||
length: number, | ||
leftToRightIndexing: boolean | ||
): { index: number; bit: number } { | ||
if (position >= length) { | ||
throw new Error( | ||
`Position "${position}" is out of range "0-${length - 1}".` | ||
); | ||
return { index, bit }; | ||
} | ||
const index = Math.floor(position / 8); | ||
const rem = position % 8; | ||
const shift = leftToRightIndexing ? 7 - rem : rem; | ||
const bit = 1 << shift; | ||
return { index, bit }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { expect } from 'chai'; | ||
import { Bitstring } from '../../src/pollux/utils/Bitstring'; | ||
|
||
describe('Bitstring', () => { | ||
describe('constructor', () => { | ||
it('should create a Bitstring with all indexes false for [0, 0]', () => { | ||
const buffer = new Uint8Array([0, 0]); | ||
const bitstring = new Bitstring({ buffer }); | ||
for (let i = 0; i < 16; i++) { | ||
expect(bitstring.get(i)).to.be.false; | ||
} | ||
}); | ||
|
||
it('should create a Bitstring with indexes 0, 14, 15 true for [128,3]', () => { | ||
const buffer = Uint8Array.from([128, 3]); | ||
const bitstring = new Bitstring({ buffer }); | ||
const validIndexes = [0, 14, 15]; | ||
for (let i = 0; i < 16; i++) { | ||
const bitstringIndex = bitstring.get(i); | ||
if (validIndexes.includes(i)) { | ||
console.log("Index should be true ", i) | ||
expect(bitstringIndex).to.be.true; | ||
} else { | ||
console.log("Index should be false ", i) | ||
expect(bitstringIndex).to.be.false; | ||
} | ||
} | ||
}); | ||
|
||
it('should create a Bitstring with indexes 6 true for [2,0]', () => { | ||
const buffer = Uint8Array.from([2, 0]); | ||
const bitstring = new Bitstring({ buffer }); | ||
const validIndexes = [6]; | ||
for (let i = 0; i < 16; i++) { | ||
const bitstringIndex = bitstring.get(i); | ||
if (validIndexes.includes(i)) { | ||
console.log("Index should be true ", i) | ||
expect(bitstringIndex).to.be.true; | ||
} else { | ||
console.log("Index should be false ", i) | ||
expect(bitstringIndex).to.be.false; | ||
} | ||
} | ||
}); | ||
|
||
it('should set a bit to true on default instance', async () => { | ||
const buffer = Uint8Array.from([0b00000000]); | ||
const bitstring = new Bitstring({ buffer }); | ||
bitstring.set(4, true); | ||
expect(bitstring.get(0)).to.equal(false); | ||
expect(bitstring.get(1)).to.equal(false); | ||
expect(bitstring.get(2)).to.equal(false); | ||
expect(bitstring.get(3)).to.equal(false); | ||
expect(bitstring.get(4)).to.equal(true); | ||
expect(bitstring.get(5)).to.equal(false); | ||
expect(bitstring.get(6)).to.equal(false); | ||
expect(bitstring.get(7)).to.equal(false); | ||
expect(bitstring.bits).to.have.length(1); | ||
expect(bitstring.length).to.equal(8); | ||
// left-to-right bit order | ||
expect(bitstring.bits[0]).to.equal(0b00001000); | ||
}); | ||
|
||
}); | ||
|
||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters