From 3787dbbc3a69bc6bc2a627c439efc137f499fb37 Mon Sep 17 00:00:00 2001 From: Jared Miller Date: Wed, 11 Mar 2026 14:05:17 -0400 Subject: [PATCH] Add Margolus block offset utility with tests --- src/__tests__/margolus.test.ts | 28 ++++++++++++++++++++++++++++ src/sand/margolus.ts | 4 ++++ 2 files changed, 32 insertions(+) create mode 100644 src/__tests__/margolus.test.ts create mode 100644 src/sand/margolus.ts diff --git a/src/__tests__/margolus.test.ts b/src/__tests__/margolus.test.ts new file mode 100644 index 0000000..f6705e7 --- /dev/null +++ b/src/__tests__/margolus.test.ts @@ -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 }); + }); +}); diff --git a/src/sand/margolus.ts b/src/sand/margolus.ts new file mode 100644 index 0000000..0345a40 --- /dev/null +++ b/src/sand/margolus.ts @@ -0,0 +1,4 @@ +export function getBlockOffset(frame: number): { x: number; y: number } { + const parity = frame & 1; + return { x: parity, y: parity }; +}