-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
6c07563
commit 7817798
Showing
1 changed file
with
114 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { assertEquals } from '@std/assert'; | ||
|
||
import { Struct } from '../struct.ts'; | ||
import { memberI32, memberU32 } from './i32.ts'; | ||
|
||
Deno.test('memberI32', () => { | ||
class Test extends Struct { | ||
declare public readonly ['constructor']: typeof Test; | ||
|
||
declare public alpha: number; | ||
|
||
declare public beta: number; | ||
|
||
declare public gamma: number; | ||
|
||
declare public delta: number; | ||
|
||
public static override readonly BYTE_LENGTH: number = ((o) => { | ||
o += memberI32(this, o, 'alpha'); | ||
o += memberI32(this, o, 'beta'); | ||
o += memberI32(this, o, 'gamma', true); | ||
o += memberI32(this, o, 'delta', false); | ||
return o; | ||
})(super.BYTE_LENGTH); | ||
} | ||
|
||
assertEquals(Test.BYTE_LENGTH, 16); | ||
|
||
const data = new Uint8Array(Test.BYTE_LENGTH); | ||
const view = new DataView(data.buffer); | ||
{ | ||
const test = new Test(data.buffer, 0, false); | ||
test.alpha = 0x7fffffff; | ||
test.beta = -2; | ||
test.gamma = -3; | ||
test.delta = -4; | ||
|
||
assertEquals(test.alpha, 0x7fffffff); | ||
assertEquals(test.beta, -2); | ||
assertEquals(view.getInt32(0, false), 0x7fffffff); | ||
assertEquals(view.getInt32(4, false), -2); | ||
assertEquals(view.getInt32(8, true), -3); | ||
assertEquals(view.getInt32(12, false), -4); | ||
} | ||
{ | ||
const test = new Test(data.buffer, 0, true); | ||
test.alpha = 0x7fffffff; | ||
test.beta = -2; | ||
test.gamma = -3; | ||
test.delta = -4; | ||
|
||
assertEquals(test.alpha, 0x7fffffff); | ||
assertEquals(test.beta, -2); | ||
assertEquals(view.getInt32(0, true), 0x7fffffff); | ||
assertEquals(view.getInt32(4, true), -2); | ||
assertEquals(view.getInt32(8, true), -3); | ||
assertEquals(view.getInt32(12, false), -4); | ||
} | ||
}); | ||
|
||
Deno.test('memberU32', () => { | ||
class Test extends Struct { | ||
declare public readonly ['constructor']: typeof Test; | ||
|
||
declare public alpha: number; | ||
|
||
declare public beta: number; | ||
|
||
declare public gamma: number; | ||
|
||
declare public delta: number; | ||
|
||
public static override readonly BYTE_LENGTH: number = ((o) => { | ||
o += memberU32(this, o, 'alpha'); | ||
o += memberU32(this, o, 'beta'); | ||
o += memberI32(this, o, 'gamma', true); | ||
o += memberI32(this, o, 'delta', false); | ||
return o; | ||
})(super.BYTE_LENGTH); | ||
} | ||
|
||
assertEquals(Test.BYTE_LENGTH, 16); | ||
|
||
const data = new Uint8Array(Test.BYTE_LENGTH); | ||
const view = new DataView(data.buffer); | ||
{ | ||
const test = new Test(data.buffer, 0, false); | ||
test.alpha = 0x7fffffff; | ||
test.beta = 0xfffffffe; | ||
test.gamma = 0xfffffffd; | ||
test.delta = 0xfffffffc; | ||
|
||
assertEquals(test.alpha, 0x7fffffff); | ||
assertEquals(test.beta, 0xfffffffe); | ||
assertEquals(view.getUint32(0, false), 0x7fffffff); | ||
assertEquals(view.getUint32(4, false), 0xfffffffe); | ||
assertEquals(view.getUint32(8, true), 0xfffffffd); | ||
assertEquals(view.getUint32(12, false), 0xfffffffc); | ||
} | ||
{ | ||
const test = new Test(data.buffer, 0, true); | ||
test.alpha = 0x7fffffff; | ||
test.beta = 0xfffffffe; | ||
test.gamma = 0xfffffffd; | ||
test.delta = 0xfffffffc; | ||
|
||
assertEquals(test.alpha, 0x7fffffff); | ||
assertEquals(test.beta, 0xfffffffe); | ||
assertEquals(view.getUint32(0, true), 0x7fffffff); | ||
assertEquals(view.getUint32(4, true), 0xfffffffe); | ||
assertEquals(view.getUint32(8, true), 0xfffffffd); | ||
assertEquals(view.getUint32(12, false), 0xfffffffc); | ||
} | ||
}); |