Add Margolus block offset utility with tests
This commit is contained in:
parent
89f963f9a6
commit
3787dbbc3a
2 changed files with 32 additions and 0 deletions
28
src/__tests__/margolus.test.ts
Normal file
28
src/__tests__/margolus.test.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
import { describe, expect, test } from "bun:test";
|
||||||
|
import { getBlockOffset } from "../sand/margolus";
|
||||||
|
|
||||||
|
describe("getBlockOffset", () => {
|
||||||
|
test("frame 0 returns even offset", () => {
|
||||||
|
expect(getBlockOffset(0)).toEqual({ x: 0, y: 0 });
|
||||||
|
});
|
||||||
|
|
||||||
|
test("frame 1 returns odd offset", () => {
|
||||||
|
expect(getBlockOffset(1)).toEqual({ x: 1, y: 1 });
|
||||||
|
});
|
||||||
|
|
||||||
|
test("frame 2 returns even offset", () => {
|
||||||
|
expect(getBlockOffset(2)).toEqual({ x: 0, y: 0 });
|
||||||
|
});
|
||||||
|
|
||||||
|
test("frame 3 returns odd offset", () => {
|
||||||
|
expect(getBlockOffset(3)).toEqual({ x: 1, y: 1 });
|
||||||
|
});
|
||||||
|
|
||||||
|
test("large even number returns even offset", () => {
|
||||||
|
expect(getBlockOffset(1000)).toEqual({ x: 0, y: 0 });
|
||||||
|
});
|
||||||
|
|
||||||
|
test("large odd number returns odd offset", () => {
|
||||||
|
expect(getBlockOffset(1001)).toEqual({ x: 1, y: 1 });
|
||||||
|
});
|
||||||
|
});
|
||||||
4
src/sand/margolus.ts
Normal file
4
src/sand/margolus.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
export function getBlockOffset(frame: number): { x: number; y: number } {
|
||||||
|
const parity = frame & 1;
|
||||||
|
return { x: parity, y: parity };
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue