Skip to content

Commit

Permalink
Test floats
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderOMara committed Nov 14, 2024
1 parent 0e2b4a8 commit 7e475dd
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 1 deletion.
79 changes: 79 additions & 0 deletions member/f32.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { assertEquals } from '@std/assert';

import { Struct } from '../struct.ts';
import { memberF32 } from './f32.ts';

Deno.test('memberF32', () => {
const v = new DataView(new ArrayBuffer(4));
for (
const f64 of [
0,
1,
-1,
Math.PI,
-Math.PI,
Math.E,
-Math.E,
Number.EPSILON,
-Number.EPSILON,
Number.MAX_SAFE_INTEGER,
Number.MIN_SAFE_INTEGER,
Number.MAX_VALUE,
Number.MIN_VALUE,
Infinity,
-Infinity,
NaN,
]
) {
v.setFloat32(0, f64, true);
const f32 = v.getFloat32(0, true);

class Test extends Struct {
declare public readonly ['constructor']: typeof Test;

declare public alpha: number;

declare public beta: number;

declare public gamma: number;

public static override readonly BYTE_LENGTH: number = ((o) => {
o += memberF32(this, o, 'alpha', true);
o += memberF32(this, o, 'beta', false);
o += memberF32(this, o, 'gamma');
return o;
})(super.BYTE_LENGTH);
}

assertEquals(Test.BYTE_LENGTH, 12);

const data = new Uint8Array(Test.BYTE_LENGTH);
const view = new DataView(data.buffer);
{
const test = new Test(data.buffer, 0, false);
test.alpha = f32;
test.beta = f32;
test.gamma = f32;

assertEquals(test.alpha, f32);
assertEquals(test.beta, f32);
assertEquals(test.gamma, f32);
assertEquals(view.getFloat32(0, true), f32);
assertEquals(view.getFloat32(4, false), f32);
assertEquals(view.getFloat32(8, false), f32);
}
{
const test = new Test(data.buffer, 0, true);
test.alpha = f32;
test.beta = f32;
test.gamma = f32;

assertEquals(test.alpha, f32);
assertEquals(test.beta, f32);
assertEquals(test.gamma, f32);
assertEquals(view.getFloat32(0, true), f32);
assertEquals(view.getFloat32(4, false), f32);
assertEquals(view.getFloat32(8, true), f32);
}
}
});
75 changes: 75 additions & 0 deletions member/f64.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { assertEquals } from '@std/assert';

import { Struct } from '../struct.ts';
import { memberF64 } from './f64.ts';

Deno.test('memberF64', () => {
for (
const f64 of [
0,
1,
-1,
Math.PI,
-Math.PI,
Math.E,
-Math.E,
Number.EPSILON,
-Number.EPSILON,
Number.MAX_SAFE_INTEGER,
Number.MIN_SAFE_INTEGER,
Number.MAX_VALUE,
Number.MIN_VALUE,
Infinity,
-Infinity,
NaN,
]
) {
class Test extends Struct {
declare public readonly ['constructor']: typeof Test;

declare public alpha: number;

declare public beta: number;

declare public gamma: number;

public static override readonly BYTE_LENGTH: number = ((o) => {
o += memberF64(this, o, 'alpha', true);
o += memberF64(this, o, 'beta', false);
o += memberF64(this, o, 'gamma');
return o;
})(super.BYTE_LENGTH);
}

assertEquals(Test.BYTE_LENGTH, 24);

const data = new Uint8Array(Test.BYTE_LENGTH);
const view = new DataView(data.buffer);
{
const test = new Test(data.buffer, 0, false);
test.alpha = f64;
test.beta = f64;
test.gamma = f64;

assertEquals(test.alpha, f64);
assertEquals(test.beta, f64);
assertEquals(test.gamma, f64);
assertEquals(view.getFloat64(0, true), f64);
assertEquals(view.getFloat64(8, false), f64);
assertEquals(view.getFloat64(16, false), f64);
}
{
const test = new Test(data.buffer, 0, true);
test.alpha = f64;
test.beta = f64;
test.gamma = f64;

assertEquals(test.alpha, f64);
assertEquals(test.beta, f64);
assertEquals(test.gamma, f64);
assertEquals(view.getFloat64(0, true), f64);
assertEquals(view.getFloat64(8, false), f64);
assertEquals(view.getFloat64(16, true), f64);
}
}
});
2 changes: 1 addition & 1 deletion member/f64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ export function memberF64<T extends typeof Struct>(
);
},
});
return 4;
return 8;
}

0 comments on commit 7e475dd

Please sign in to comment.