-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properties are now stored in insertion-order.
- Loading branch information
Showing
14 changed files
with
156 additions
and
108 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,19 @@ | ||
import { BufferReader, BufferWriter } from '../io'; | ||
import { ISchema } from '../structure/types'; | ||
import { Parsed } from '../utilityTypes'; | ||
|
||
export function makeIO(bufferSize: number) { | ||
const buffer = Buffer.alloc(bufferSize); | ||
return { | ||
output: new BufferWriter(buffer), | ||
input: new BufferReader(buffer), | ||
}; | ||
} | ||
|
||
export function encodeAndDecode<T extends ISchema<Parsed<T>>>(schema: T, value: Parsed<T>): Parsed<T> { | ||
const buffer = Buffer.alloc(schema.sizeOf(value)); | ||
|
||
schema.write(new BufferWriter(buffer), value); | ||
|
||
return schema.read(new BufferReader(buffer)); | ||
} |
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
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,13 @@ | ||
import * as chai from 'chai'; | ||
import { BOOL} from '../structure'; | ||
import { encodeAndDecode } from './_mock.test'; | ||
|
||
const expect = chai.expect; | ||
describe('BoolSchema', () => { | ||
it('should encode and decode a bool value', () => { | ||
const value = Math.random() < 0.5; | ||
const decoded = encodeAndDecode(BOOL, value); | ||
|
||
expect(decoded).to.equal(value); | ||
}); | ||
}); |
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,14 @@ | ||
import * as chai from 'chai'; | ||
import { randIntBetween } from './random'; | ||
import { BYTE } from '../structure'; | ||
import { encodeAndDecode } from './_mock.test'; | ||
|
||
const expect = chai.expect; | ||
describe('ByteSchema', () => { | ||
it('should encode and decode a byte value', () => { | ||
const value = randIntBetween(0, 256); | ||
const decoded = encodeAndDecode(BYTE, value); | ||
|
||
expect(decoded).to.equal(value); | ||
}); | ||
}); |
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
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,14 @@ | ||
import * as chai from 'chai'; | ||
import { randBetween } from './random'; | ||
import { FLOAT } from '../structure'; | ||
import { encodeAndDecode } from './_mock.test'; | ||
|
||
const expect = chai.expect; | ||
describe('FloatSchema', () => { | ||
it('should encode and decode a float value', () => { | ||
const value = randBetween(-100, 100); | ||
const decoded = encodeAndDecode(FLOAT, value); | ||
|
||
expect(decoded).to.closeTo(value, 0.01); | ||
}); | ||
}); |
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,14 @@ | ||
import * as chai from 'chai'; | ||
import { randIntBetween } from './random'; | ||
import { INT } from '../structure'; | ||
import { encodeAndDecode } from './_mock.test'; | ||
|
||
const expect = chai.expect; | ||
describe('IntSchema', () => { | ||
it('should encode and decode an int value', () => { | ||
const value = randIntBetween(-100, 100); | ||
const decoded = encodeAndDecode(INT, value); | ||
|
||
expect(decoded).to.equal(value); | ||
}); | ||
}); |
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
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
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
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,26 @@ | ||
import * as chai from 'chai'; | ||
import { randIntBetween } from './random'; | ||
import { STRING, } from '../structure'; | ||
import { encodeAndDecode } from './_mock.test'; | ||
|
||
const expect = chai.expect; | ||
describe('StringSchema', () => { | ||
it('should encode and decode an empty string value', () => { | ||
const decoded = encodeAndDecode(STRING, ''); | ||
|
||
expect(decoded).to.equal(''); | ||
}); | ||
|
||
it('should encode and decode an alphanumerical string', () => { | ||
const length = randIntBetween(0, 100); | ||
let value = ''; | ||
const ranges = [['A', 'Z'], ['a', 'z'], ['0', '9']]; | ||
for (let i = 0; i < length; ++i) { | ||
const range = ranges[randIntBetween(0, ranges.length) % ranges.length]; | ||
value += String.fromCharCode(randIntBetween(range[0].charCodeAt(0), range[1].charCodeAt(0))); | ||
} | ||
|
||
const decoded = encodeAndDecode(STRING, value); | ||
expect(decoded).to.equal(value); | ||
}); | ||
}); |