binutils64 0.1.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,93 +0,0 @@
1
- ---
2
- name: add-binary-type
3
- description: Add a new matched Read/Write method pair to binutils.js following the library's exact ES5 conventions, then update README.md and add a round-trip test. Use when asked to support a new numeric/binary type (e.g. a new width, a string type, a boolean) in the BinaryReader/BinaryWriter classes.
4
- ---
5
-
6
- # Adding a binary type to binutils64
7
-
8
- This library exposes mirrored methods on `BinaryReader` and `BinaryWriter` in the
9
- single file `binutils.js`. Every supported type has a `ReadX` and a matching
10
- `WriteX`. Adding a type means editing both prototypes, updating `README.md`, and
11
- adding a round-trip test. Follow the templates below **exactly** — style
12
- deviations and width mismatches are the main source of bugs here (this library
13
- previously shipped a low-32-bit width bug in the 64-bit writers — don't repeat it).
14
-
15
- ## Hard rules (match existing code)
16
-
17
- - ES5 only: `var`, prototype assignment. No `let`/`const`/classes/arrow functions.
18
- - Method names are `PascalCase`: `ReadUInt24`, `WriteBool`, etc.
19
- - Parameters are prefixed `p_` (`p_Value`); locals are prefixed `s_` (`s_Val`,
20
- `s_TempBuffer`).
21
- - Keep `new Buffer(...)` — the package targets `node >=0.12`. Do not switch to
22
- `Buffer.alloc`/`Buffer.from` unless `package.json` `engines.node` is also raised.
23
- - **The write width MUST equal the read width.** When the type is wider than 32
24
- bits, use the correct wide Buffer method (`writeBigUInt64LE`, etc.), NOT
25
- `writeUInt32LE` — using a 32-bit write into an 8-byte buffer was the original
26
- 64-bit writer bug (now fixed). 64-bit values are `BigInt`; coerce with
27
- `BigInt(p_Value)` so both `Number` and `BigInt` arguments work.
28
-
29
- ## Reader method template
30
-
31
- Out-of-range reads return `0` (or `0.0` / empty `Buffer`) — they do not throw.
32
- Reads are destructive: slice the consumed bytes off `this.ByteBuffer` and advance
33
- `this.Position` by the byte width `N`.
34
-
35
- ```javascript
36
- ReadTYPE: function() {
37
- if (this.ByteBuffer.length < N) {
38
- return 0;
39
- }
40
-
41
- var s_Val = (this.Endianness == 'little') ? this.ByteBuffer.readTYPELE(0) : this.ByteBuffer.readTYPEBE(0);
42
- this.ByteBuffer = this.ByteBuffer.slice(N);
43
- this.Position += N;
44
- return s_Val;
45
- },
46
- ```
47
-
48
- For a single-byte type there is no endianness branch (see `ReadUInt8`/`ReadInt8`),
49
- and use `++this.Position;`.
50
-
51
- ## Writer method template
52
-
53
- Allocate an `N`-byte temp buffer, write with the correct-width method honoring
54
- endianness, grow `this.Length` by `N`, and concat.
55
-
56
- ```javascript
57
- WriteTYPE: function(p_Value) {
58
- var s_TempBuffer = new Buffer(N);
59
- if (this.Endianness == 'little') {
60
- s_TempBuffer.writeTYPELE(p_Value, 0);
61
- } else {
62
- s_TempBuffer.writeTYPEBE(p_Value, 0);
63
- }
64
- this.Length += N;
65
- this.ByteBuffer = Buffer.concat([this.ByteBuffer, s_TempBuffer], this.Length);
66
- },
67
- ```
68
-
69
- For a single-byte type, drop the endianness branch (see `WriteUInt8`/`WriteInt8`).
70
-
71
- ## Placement
72
-
73
- Keep the read/write methods grouped by family and ordered as in the existing file
74
- (unsigned ascending width, then signed ascending width, then float/double, then
75
- bytes). Add the new method next to its siblings, not at the end.
76
-
77
- ## After editing binutils.js
78
-
79
- 1. **README.md** — add a `### ReadX(...)` and `### WriteX(value)` entry in the
80
- matching BinaryReader / BinaryWriter sections, mirroring the existing phrasing
81
- ("Reads/Writes a … and advances the current position by N bytes").
82
- 2. **Test** — add a round-trip test (see the `test-binutils` skill): write a value
83
- in both `'big'` and `'little'`, read it back, assert equality. Include a min/max
84
- or negative boundary value for signed/wide types. For 64-bit values assert the
85
- returned type is `bigint`.
86
- 3. Run the tests: `node --test`.
87
-
88
- ## Verify before finishing
89
-
90
- - Write width == read width (round-trip test passes for both endiannesses).
91
- - `Position`/`Length` advance by exactly `N`.
92
- - Out-of-range read returns the documented zero value, not a throw.
93
- - README entries added; method placed beside its family.
@@ -1,91 +0,0 @@
1
- ---
2
- name: test-binutils
3
- description: Author and run tests for the binutils64 library using node:test and node:assert. Use when asked to add test coverage, write tests for a Read/Write method, verify a change, or set up the test suite for this project.
4
- ---
5
-
6
- # Testing binutils64
7
-
8
- The library ships with no test framework today. Use the built-in `node:test`
9
- runner + `node:assert/strict` — zero dependencies. (The library's runtime floor
10
- stays `node >=0.12`; `node:test` only affects the dev/test environment, which
11
- needs Node >= 18.)
12
-
13
- ## Layout & running
14
-
15
- - Put tests in `test/`, named `*.test.js`.
16
- - Run the whole suite: `node --test`.
17
- - Add to `package.json` if missing:
18
- ```json
19
- "scripts": { "test": "node --test" }
20
- ```
21
-
22
- ## Test file skeleton
23
-
24
- ```javascript
25
- var test = require('node:test');
26
- var assert = require('node:assert/strict');
27
- var binutils = require('../binutils.js');
28
-
29
- test('UInt32 round-trips both endiannesses', function() {
30
- ['big', 'little'].forEach(function(endian) {
31
- var w = new binutils.BinaryWriter(endian);
32
- w.WriteUInt32(0xDEADBEEF);
33
- var r = new binutils.BinaryReader(w.ByteBuffer, endian);
34
- assert.equal(r.ReadUInt32(), 0xDEADBEEF);
35
- });
36
- });
37
- ```
38
-
39
- Keep test code ES5-compatible in spirit (`var`, `function`) to match repo style,
40
- though the test runner itself requires modern Node.
41
-
42
- ## Coverage priorities (highest value first)
43
-
44
- ### 1. Round-trips — write then read back
45
- For every type, both `'big'` and `'little'`: write a value, feed `writer.ByteBuffer`
46
- into a reader, assert the read value equals the written one.
47
- - `UInt8/16/32`, `Int8/16/32`, `Float`, `Double`, `Bytes`.
48
- - `UInt64/Int64`: assert against the correct BigInt value. The writers accept a
49
- `Number` or a `BigInt` and emit all 8 bytes; the reader returns a `BigInt`, so
50
- compare against a `BigInt` literal (`123n`), not a `Number` (`123n !== 123` under
51
- strict equality).
52
- - Signed boundaries: `Int32` min `-2147483648` and max `2147483647`, negatives for
53
- `Int8/16`.
54
- - `Float`: compare with tolerance (`assert.ok(Math.abs(got - want) < 1e-6)`) due to
55
- precision loss. `Double` is exact for representable values.
56
-
57
- ### 2. BinaryReader behavior
58
- - **Endianness**: identical bytes read as `'big'` vs `'little'` produce the expected
59
- swapped values; default (no arg) is `'big'`.
60
- - **Invariants**: after reads, `Position` advanced by the right byte count, `Length`
61
- unchanged at the original size, `ByteBuffer` shrank by the consumed bytes.
62
- - **Out-of-range** reads return `0` / `0.0` / empty `Buffer` (never throw) — read
63
- past the end for each method.
64
- - **Constructor inputs**: `Buffer`, `Array`, and `string` (+ encoding) all build;
65
- an invalid input (e.g. a number) throws.
66
- - **Constructor copies input**: mutate the caller's original buffer after
67
- constructing and confirm reader output is unaffected.
68
- - `ReadUInt64`/`ReadInt64` return a `bigint` (`assert.equal(typeof v, 'bigint')`).
69
- - README reader example: bytes `[1,0,2,0,0,0,3,1,2,3,4,5,6]` yield `1`, `2`, `3`,
70
- a 6-byte buffer, then `Position` and `Length` both `13`.
71
-
72
- ### 3. BinaryWriter behavior
73
- - Defaults: endianness `'big'`, encoding `'ascii'`.
74
- - `Length` increments by the correct width per write; emitted bytes match expected
75
- hex for both endiannesses.
76
- - `WriteBytes` accepts `string`, `Array`, and `Buffer`, producing identical bytes,
77
- and throws on any other input type.
78
- - README writer example yields `<Buffer ff ff 00 00 00 00 ff ff ff ff 05 04 03 02 01>`,
79
- `Length` 15.
80
-
81
- ### 4. Maintaining bug-documentation tests
82
- There are no open known bugs at the moment. When you discover one, pin its current
83
- behavior with a clearly-commented test so regressions are noticed. When a bug is
84
- fixed, delete its pinned test and convert any matching TODO round-trip into a normal
85
- assertion — this is how the former 64-bit writer bug (low-32-bit truncation) and the
86
- `WriteBytes` guard bug (`!p_Value instanceof Buffer` mis-parsing, which let invalid
87
- input through) were retired.
88
-
89
- ## Verify before finishing
90
- - `node --test` passes (except intentionally-`todo` bug tests).
91
- - New types added via the `add-binary-type` skill have a matching round-trip test.
@@ -1,22 +0,0 @@
1
- name: Tests
2
-
3
- on:
4
- push:
5
- branches: [master]
6
- pull_request:
7
-
8
- jobs:
9
- test:
10
- runs-on: ubuntu-latest
11
- strategy:
12
- fail-fast: false
13
- matrix:
14
- node: [20, 22, 24]
15
- name: Node ${{ matrix.node }}
16
- steps:
17
- - uses: actions/checkout@v6
18
- - uses: actions/setup-node@v6
19
- with:
20
- node-version: ${{ matrix.node }}
21
- # No install step: the package has zero dependencies and no lockfile.
22
- - run: npm test
package/CLAUDE.md DELETED
@@ -1,54 +0,0 @@
1
- # CLAUDE.md
2
-
3
- Guidance for working in this repository.
4
-
5
- ## What this is
6
-
7
- `binutils64` — a small npm package providing .NET-style `BinaryReader` and
8
- `BinaryWriter` classes for Node.js, with selectable endianness (`'big'` default
9
- or `'little'`). All logic lives in a single file: `binutils.js`. Published to npm
10
- as `binutils64`.
11
-
12
- ## Layout
13
-
14
- - `binutils.js` — the entire library. Two ES5 prototype-based "classes"
15
- (`BinaryReader`, `BinaryWriter`) exported via `module.exports`.
16
- - `README.md` — the public API reference (keep in sync when adding/changing methods).
17
- - No `src/`, no build step, no dependencies, no test suite.
18
-
19
- ## Running / testing
20
-
21
- There is no test framework, lint config, or build. To verify a change, write an
22
- ad-hoc script and run it with node, e.g.:
23
-
24
- ```bash
25
- node -e "var b=require('./binutils.js'); var w=new b.BinaryWriter('little'); w.WriteUInt32(3); console.log(w.ByteBuffer);"
26
- ```
27
-
28
- When you add or change a public method, update the matching section in `README.md`.
29
-
30
- ## Code conventions (match the existing style exactly)
31
-
32
- - ES5 only: `var`, prototype assignment, no classes/arrow functions/`let`/`const`.
33
- - Method and property names are `PascalCase` (`ReadUInt32`, `ByteBuffer`, `Position`).
34
- - Function parameters are prefixed `p_` (`p_InputBuffer`, `p_Value`).
35
- - Local variables are prefixed `s_` (`s_Val`, `s_TempBuffer`).
36
- - Targets `node >=0.12`, so `new Buffer(...)` is used deliberately despite being
37
- deprecated in modern Node. Don't "fix" it to `Buffer.alloc`/`Buffer.from` unless
38
- the engines floor is also raised.
39
- - Commit messages are short, lowercase, imperative (e.g. `add read/write 64 bit values`,
40
- `fix read int64`). No AI/Claude attribution.
41
-
42
- ## Behavior to preserve
43
-
44
- - **Reads are destructive.** Each `Read*` slices the consumed bytes off
45
- `this.ByteBuffer` and advances `this.Position`. `Length` stays at the original
46
- buffer length; `ByteBuffer` shrinks as you read.
47
- - **Out-of-range reads return `0` (or `0.0`/empty Buffer), they do not throw.**
48
- - `ReadUInt64`/`ReadInt64` return a `BigInt` (via `readBigUInt64*`/`readBigInt64*`).
49
- - The constructor copies the input buffer (`new Buffer(p_InputBuffer)`) so the
50
- caller's buffer is not mutated — preserve this.
51
- - `WriteUInt64`/`WriteInt64` accept a `Number` or `BigInt` (coerced via
52
- `BigInt(p_Value)`) and emit all 8 bytes via `writeBigUInt64*`/`writeBigInt64*`.
53
- - `WriteBytes` accepts a `Buffer`, `Array`, or `string`, and throws on any other
54
- input type.
@@ -1,76 +0,0 @@
1
- var test = require('node:test');
2
- var assert = require('node:assert/strict');
3
- var binutils = require('../binutils.js');
4
- var BinaryReader = binutils.BinaryReader;
5
-
6
- test('constructor accepts a Buffer', function() {
7
- var r = new BinaryReader(Buffer.from([1, 2, 3]));
8
- assert.equal(r.Length, 3);
9
- assert.equal(r.ReadUInt8(), 1);
10
- });
11
-
12
- test('constructor accepts an Array', function() {
13
- var r = new BinaryReader([1, 2, 3]);
14
- assert.equal(r.Length, 3);
15
- assert.equal(r.ReadUInt8(), 1);
16
- });
17
-
18
- test('constructor accepts a string with encoding', function() {
19
- var r = new BinaryReader('ABC', 'big', 'ascii');
20
- assert.equal(r.Length, 3);
21
- assert.equal(r.ReadUInt8(), 65);
22
- });
23
-
24
- test('constructor rejects invalid input types', function() {
25
- assert.throws(function() { new BinaryReader(42); }, /Invalid buffer input/);
26
- assert.throws(function() { new BinaryReader({}); }, /Invalid buffer input/);
27
- assert.throws(function() { new BinaryReader(null); }, /Invalid buffer input/);
28
- });
29
-
30
- test('constructor copies the input buffer (no aliasing)', function() {
31
- var src = Buffer.from([1, 2, 3]);
32
- var r = new BinaryReader(src);
33
- src[0] = 99; // mutate the caller's buffer after construction
34
- assert.equal(r.ReadUInt8(), 1, 'reader is unaffected by later mutation of the source');
35
- });
36
-
37
- test('out-of-range integer reads return 0 without advancing Position', function() {
38
- var r = new BinaryReader([0x01], 'big'); // only 1 byte available
39
- assert.equal(r.ReadUInt16(), 0);
40
- assert.equal(r.ReadUInt32(), 0);
41
- assert.equal(r.ReadUInt64(), 0);
42
- assert.equal(r.ReadInt16(), 0);
43
- assert.equal(r.ReadInt32(), 0);
44
- assert.equal(r.ReadInt64(), 0);
45
- assert.equal(r.Position, 0, 'failed reads do not advance Position');
46
- assert.equal(r.ByteBuffer.length, 1, 'failed reads do not consume bytes');
47
- });
48
-
49
- test('out-of-range float/double reads return 0', function() {
50
- var r = new BinaryReader([0x01], 'big');
51
- assert.equal(r.ReadFloat(), 0);
52
- assert.equal(r.ReadDouble(), 0);
53
- });
54
-
55
- test('ReadUInt8 on an empty buffer returns 0', function() {
56
- var r = new BinaryReader([], 'big');
57
- assert.equal(r.Length, 0);
58
- assert.equal(r.ReadUInt8(), 0);
59
- assert.equal(r.ReadInt8(), 0);
60
- });
61
-
62
- test('ReadBytes returns an empty Buffer when count exceeds remaining', function() {
63
- var r = new BinaryReader([1, 2, 3], 'big');
64
- var out = r.ReadBytes(5);
65
- assert.ok(Buffer.isBuffer(out));
66
- assert.equal(out.length, 0);
67
- assert.equal(r.Position, 0, 'over-long ReadBytes does not advance');
68
- assert.equal(r.ByteBuffer.length, 3);
69
- });
70
-
71
- test('ReadBytes reading exactly the remaining length succeeds', function() {
72
- var r = new BinaryReader([1, 2, 3], 'big');
73
- assert.deepEqual(Array.from(r.ReadBytes(3)), [1, 2, 3]);
74
- assert.equal(r.Position, 3);
75
- assert.equal(r.ByteBuffer.length, 0);
76
- });
@@ -1,106 +0,0 @@
1
- var test = require('node:test');
2
- var assert = require('node:assert/strict');
3
- var binutils = require('../binutils.js');
4
- var BinaryReader = binutils.BinaryReader;
5
-
6
- function reader(arr, endian) {
7
- return new BinaryReader(Buffer.from(arr), endian);
8
- }
9
-
10
- test('BinaryReader: constructor defaults', function() {
11
- var r = reader([1, 2, 3]);
12
- assert.equal(r.Endianness, 'big', 'defaults to big-endian');
13
- assert.equal(r.Encoding, 'ascii', 'defaults to ascii encoding');
14
- assert.equal(r.Length, 3, 'Length is the original buffer length');
15
- assert.equal(r.Position, 0, 'Position starts at 0');
16
- assert.equal(r.ByteBuffer.length, 3);
17
- });
18
-
19
- test('BinaryReader: ReadUInt8', function() {
20
- var r = reader([200, 5]);
21
- assert.equal(r.ReadUInt8(), 200);
22
- assert.equal(r.ReadUInt8(), 5);
23
- });
24
-
25
- test('BinaryReader: ReadInt8 (signed)', function() {
26
- var r = reader([0xFF, 0x80, 0x7F]);
27
- assert.equal(r.ReadInt8(), -1);
28
- assert.equal(r.ReadInt8(), -128);
29
- assert.equal(r.ReadInt8(), 127);
30
- });
31
-
32
- test('BinaryReader: ReadUInt16 honors endianness', function() {
33
- assert.equal(reader([0x12, 0x34], 'big').ReadUInt16(), 0x1234);
34
- assert.equal(reader([0x12, 0x34], 'little').ReadUInt16(), 0x3412);
35
- });
36
-
37
- test('BinaryReader: ReadInt16 (signed)', function() {
38
- assert.equal(reader([0xFF, 0xFF], 'big').ReadInt16(), -1);
39
- assert.equal(reader([0x80, 0x00], 'big').ReadInt16(), -32768);
40
- assert.equal(reader([0x00, 0x80], 'little').ReadInt16(), -32768);
41
- });
42
-
43
- test('BinaryReader: ReadUInt32 honors endianness', function() {
44
- assert.equal(reader([0x12, 0x34, 0x56, 0x78], 'big').ReadUInt32(), 0x12345678);
45
- assert.equal(reader([0x78, 0x56, 0x34, 0x12], 'little').ReadUInt32(), 0x12345678);
46
- });
47
-
48
- test('BinaryReader: ReadInt32 (signed)', function() {
49
- assert.equal(reader([0xFF, 0xFF, 0xFF, 0xFF], 'big').ReadInt32(), -1);
50
- assert.equal(reader([0x80, 0x00, 0x00, 0x00], 'big').ReadInt32(), -2147483648);
51
- });
52
-
53
- test('BinaryReader: ReadUInt64 returns a BigInt', function() {
54
- var r = reader([0, 0, 0, 0, 0, 0, 0, 5], 'big');
55
- var v = r.ReadUInt64();
56
- assert.equal(typeof v, 'bigint');
57
- assert.equal(v, 5n);
58
- assert.equal(reader([5, 0, 0, 0, 0, 0, 0, 0], 'little').ReadUInt64(), 5n);
59
- });
60
-
61
- test('BinaryReader: ReadInt64 returns a signed BigInt', function() {
62
- assert.equal(reader([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], 'big').ReadInt64(), -1n);
63
- });
64
-
65
- test('BinaryReader: ReadFloat', function() {
66
- // 1.5 as IEEE-754 single precision = 0x3FC00000
67
- assert.equal(reader([0x3F, 0xC0, 0x00, 0x00], 'big').ReadFloat(), 1.5);
68
- assert.equal(reader([0x00, 0x00, 0xC0, 0x3F], 'little').ReadFloat(), 1.5);
69
- });
70
-
71
- test('BinaryReader: ReadDouble', function() {
72
- // 1.5 as IEEE-754 double precision = 0x3FF8000000000000
73
- assert.equal(reader([0x3F, 0xF8, 0, 0, 0, 0, 0, 0], 'big').ReadDouble(), 1.5);
74
- });
75
-
76
- test('BinaryReader: ReadBytes returns a Buffer of the requested length', function() {
77
- var r = reader([1, 2, 3, 4, 5, 6]);
78
- var out = r.ReadBytes(4);
79
- assert.ok(Buffer.isBuffer(out));
80
- assert.deepEqual(Array.from(out), [1, 2, 3, 4]);
81
- assert.equal(r.Position, 4);
82
- assert.equal(r.ByteBuffer.length, 2);
83
- });
84
-
85
- test('BinaryReader: Position advances and Length stays constant; ByteBuffer shrinks', function() {
86
- var r = reader([1, 2, 3, 4, 5, 6, 7, 8], 'big');
87
-
88
- assert.equal(r.ReadUInt8(), 1);
89
- assert.equal(r.Position, 1);
90
- assert.equal(r.Length, 8, 'Length never changes on reads');
91
- assert.equal(r.ByteBuffer.length, 7, 'ByteBuffer shrinks by consumed bytes');
92
-
93
- assert.equal(r.ReadUInt16(), 0x0203);
94
- assert.equal(r.Position, 3);
95
- assert.equal(r.ByteBuffer.length, 5);
96
-
97
- assert.equal(r.ReadUInt32(), 0x04050607);
98
- assert.equal(r.Position, 7);
99
- assert.equal(r.ByteBuffer.length, 1);
100
- });
101
-
102
- test('BinaryReader: same bytes decode differently per endianness', function() {
103
- var b = [0x00, 0x00, 0x00, 0x01];
104
- assert.equal(reader(b, 'big').ReadUInt32(), 1);
105
- assert.equal(reader(b, 'little').ReadUInt32(), 0x01000000);
106
- });
@@ -1,33 +0,0 @@
1
- var test = require('node:test');
2
- var assert = require('node:assert/strict');
3
- var binutils = require('../binutils.js');
4
-
5
- // These mirror the worked examples in README.md. If the documented output ever
6
- // changes, the docs and these tests must change together.
7
-
8
- test('README reader example', function() {
9
- var buffer = Buffer.from([1, 0, 2, 0, 0, 0, 3, 1, 2, 3, 4, 5, 6]);
10
- var reader = new binutils.BinaryReader(buffer);
11
-
12
- assert.equal(reader.ReadUInt8(), 1);
13
- assert.equal(reader.ReadUInt16(), 2);
14
- assert.equal(reader.ReadUInt32(), 3);
15
- assert.deepEqual(Array.from(reader.ReadBytes(6)), [1, 2, 3, 4, 5, 6]);
16
- assert.equal(reader.Position, 13);
17
- assert.equal(reader.Length, 13);
18
- });
19
-
20
- test('README writer example', function() {
21
- var writer = new binutils.BinaryWriter();
22
-
23
- writer.WriteUInt16(65535);
24
- writer.WriteUInt32(0);
25
- writer.WriteInt32(-1);
26
- writer.WriteBytes([5, 4, 3, 2, 1]);
27
-
28
- assert.deepEqual(
29
- Array.from(writer.ByteBuffer),
30
- [0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 5, 4, 3, 2, 1]
31
- );
32
- assert.equal(writer.Length, 15);
33
- });
@@ -1,120 +0,0 @@
1
- var test = require('node:test');
2
- var assert = require('node:assert/strict');
3
- var binutils = require('../binutils.js');
4
- var BinaryReader = binutils.BinaryReader;
5
- var BinaryWriter = binutils.BinaryWriter;
6
-
7
- // Write a value with WriteMethod, then read it back with ReadMethod, asserting
8
- // equality across both endiannesses.
9
- function roundTrip(writeMethod, readMethod, value, compare) {
10
- ['big', 'little'].forEach(function(endian) {
11
- var w = new BinaryWriter(endian);
12
- w[writeMethod](value);
13
- var r = new BinaryReader(w.ByteBuffer, endian);
14
- var got = r[readMethod]();
15
- if (compare) {
16
- compare(got, value, endian);
17
- } else {
18
- assert.equal(got, value, writeMethod + '/' + readMethod + ' (' + endian + ')');
19
- }
20
- });
21
- }
22
-
23
- test('round-trip: UInt8', function() {
24
- roundTrip('WriteUInt8', 'ReadUInt8', 0);
25
- roundTrip('WriteUInt8', 'ReadUInt8', 200);
26
- roundTrip('WriteUInt8', 'ReadUInt8', 255);
27
- });
28
-
29
- test('round-trip: Int8', function() {
30
- roundTrip('WriteInt8', 'ReadInt8', -128);
31
- roundTrip('WriteInt8', 'ReadInt8', -1);
32
- roundTrip('WriteInt8', 'ReadInt8', 127);
33
- });
34
-
35
- test('round-trip: UInt16', function() {
36
- roundTrip('WriteUInt16', 'ReadUInt16', 0);
37
- roundTrip('WriteUInt16', 'ReadUInt16', 0xBEEF);
38
- roundTrip('WriteUInt16', 'ReadUInt16', 0xFFFF);
39
- });
40
-
41
- test('round-trip: Int16', function() {
42
- roundTrip('WriteInt16', 'ReadInt16', -32768);
43
- roundTrip('WriteInt16', 'ReadInt16', -1);
44
- roundTrip('WriteInt16', 'ReadInt16', 32767);
45
- });
46
-
47
- test('round-trip: UInt32', function() {
48
- roundTrip('WriteUInt32', 'ReadUInt32', 0);
49
- roundTrip('WriteUInt32', 'ReadUInt32', 0xDEADBEEF);
50
- roundTrip('WriteUInt32', 'ReadUInt32', 0xFFFFFFFF);
51
- });
52
-
53
- test('round-trip: Int32', function() {
54
- roundTrip('WriteInt32', 'ReadInt32', -2147483648);
55
- roundTrip('WriteInt32', 'ReadInt32', -1);
56
- roundTrip('WriteInt32', 'ReadInt32', 2147483647);
57
- });
58
-
59
- test('round-trip: Float (within single-precision tolerance)', function() {
60
- roundTrip('WriteFloat', 'ReadFloat', 1.5);
61
- roundTrip('WriteFloat', 'ReadFloat', 3.14, function(got, want, endian) {
62
- assert.ok(Math.abs(got - want) < 1e-5, 'Float ~3.14 (' + endian + '), got ' + got);
63
- });
64
- });
65
-
66
- test('round-trip: Double (exact)', function() {
67
- roundTrip('WriteDouble', 'ReadDouble', Math.PI);
68
- roundTrip('WriteDouble', 'ReadDouble', -123456.789);
69
- });
70
-
71
- test('round-trip: Bytes', function() {
72
- ['big', 'little'].forEach(function(endian) {
73
- var payload = [1, 2, 3, 4, 5];
74
- var w = new BinaryWriter(endian);
75
- w.WriteBytes(payload);
76
- var r = new BinaryReader(w.ByteBuffer, endian);
77
- assert.deepEqual(Array.from(r.ReadBytes(payload.length)), payload);
78
- });
79
- });
80
-
81
- // 64-bit round-trips. The writers accept a Number or a BigInt and emit all 8
82
- // bytes; the reader always returns a BigInt.
83
-
84
- test('round-trip: UInt64 (BigInt argument)', function() {
85
- var value = 0x1234567890ABCDEFn;
86
- ['big', 'little'].forEach(function(endian) {
87
- var w = new BinaryWriter(endian);
88
- w.WriteUInt64(value);
89
- var r = new BinaryReader(w.ByteBuffer, endian);
90
- assert.equal(r.ReadUInt64(), value);
91
- });
92
- });
93
-
94
- test('round-trip: UInt64 (Number argument is coerced)', function() {
95
- ['big', 'little'].forEach(function(endian) {
96
- var w = new BinaryWriter(endian);
97
- w.WriteUInt64(123);
98
- var r = new BinaryReader(w.ByteBuffer, endian);
99
- assert.equal(r.ReadUInt64(), 123n);
100
- });
101
- });
102
-
103
- test('round-trip: Int64 (BigInt argument)', function() {
104
- var value = -81985529216486896n;
105
- ['big', 'little'].forEach(function(endian) {
106
- var w = new BinaryWriter(endian);
107
- w.WriteInt64(value);
108
- var r = new BinaryReader(w.ByteBuffer, endian);
109
- assert.equal(r.ReadInt64(), value);
110
- });
111
- });
112
-
113
- test('round-trip: Int64 (negative Number argument is coerced)', function() {
114
- ['big', 'little'].forEach(function(endian) {
115
- var w = new BinaryWriter(endian);
116
- w.WriteInt64(-123);
117
- var r = new BinaryReader(w.ByteBuffer, endian);
118
- assert.equal(r.ReadInt64(), -123n);
119
- });
120
- });