-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
11,043 additions
and
4,176 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,2 +1,3 @@ | ||
coverage | ||
dist | ||
node_modules |
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,87 @@ | ||
/** | ||
* Copyright 2020 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { ArrayOfStructsView } from "./aosv.js"; | ||
|
||
describe("ArrayOfStructsView", function() { | ||
it("calculates length correctly", function() { | ||
// Add one stray byte | ||
const { buffer } = new Uint8Array([0, 0, 1, 0, 2, 0, 1]); | ||
const aosv = new ArrayOfStructsView(buffer, { | ||
id: ArrayOfStructsView.Uint8(), | ||
_: ArrayOfStructsView.reserved(1) | ||
}); | ||
expect(aosv.length).toBe(3); | ||
}); | ||
|
||
it("decodes items correctly", function() { | ||
const buffer = new ArrayBuffer(22 * 2); | ||
const dataView = new DataView(buffer); | ||
dataView.setUint8(0 + 0, 1); | ||
dataView.setUint8(22 + 0, 2); | ||
dataView.setFloat64(0 + 1, 20, false); | ||
dataView.setFloat64(0 + 9, 30, true); | ||
dataView.setFloat64(22 + 1, 40, false); | ||
dataView.setFloat64(22 + 9, 50, true); | ||
dataView.setInt32(0 + 17, 9); | ||
dataView.setInt32(22 + 17, 10); | ||
const aosv = new ArrayOfStructsView(buffer, { | ||
id: ArrayOfStructsView.Uint8(), | ||
x: ArrayOfStructsView.Float64({ endianess: "big" }), | ||
y: ArrayOfStructsView.Float64({ endianess: "little" }), | ||
texture: ArrayOfStructsView.Int32(), | ||
_: ArrayOfStructsView.reserved(1) | ||
}); | ||
expect(aosv[0].id).toBe(1); | ||
expect(aosv[1].id).toBe(2); | ||
expect(aosv[0].x).toBe(20); | ||
expect(aosv[1].x).toBe(40); | ||
expect(aosv[0].y).toBe(30); | ||
expect(aosv[1].y).toBe(50); | ||
expect(aosv[0].texture).toBe(9); | ||
expect(aosv[1].texture).toBe(10); | ||
}); | ||
|
||
it("can return the buffer", function() { | ||
const buffer = new ArrayBuffer(22); | ||
const aosv = new ArrayOfStructsView(buffer, { | ||
x: ArrayOfStructsView.Uint8() | ||
}); | ||
expect(aosv.buffer).toBe(buffer); | ||
}); | ||
|
||
it("can decode to JSON", function() { | ||
const { buffer } = new Uint8Array([0, 0, 1, 0, 2, 0, 1]); | ||
const aosv = new ArrayOfStructsView(buffer, { | ||
id: ArrayOfStructsView.Uint8(), | ||
_: ArrayOfStructsView.reserved(1) | ||
}); | ||
expect(JSON.stringify(aosv)).toBe( | ||
JSON.stringify([{ id: 0 }, { id: 1 }, { id: 2 }]) | ||
); | ||
}); | ||
|
||
it("can write items", function() { | ||
const buffer = new ArrayBuffer(10 * 2); | ||
const dataView = new DataView(buffer); | ||
const aosv = new ArrayOfStructsView(buffer, { | ||
id: ArrayOfStructsView.Uint8(), | ||
x: ArrayOfStructsView.Float64(), | ||
_: ArrayOfStructsView.reserved(1) | ||
}); | ||
aosv[0].x = 10; | ||
aosv[1].x = 20; | ||
expect(dataView.getFloat64(1)).toBe(10); | ||
expect(dataView.getFloat64(11)).toBe(20); | ||
}); | ||
}); |
Oops, something went wrong.