Add Margolus block offset utility with tests

This commit is contained in:
Jared Miller 2026-03-11 14:05:17 -04:00
parent 89f963f9a6
commit 3787dbbc3a
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
2 changed files with 32 additions and 0 deletions

View 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
View file

@ -0,0 +1,4 @@
export function getBlockOffset(frame: number): { x: number; y: number } {
const parity = frame & 1;
return { x: parity, y: parity };
}