diff options
| author | kj_sh604 | 2026-03-15 16:19:35 -0400 |
|---|---|---|
| committer | kj_sh604 | 2026-03-15 16:19:35 -0400 |
| commit | 6ec259a0e71174651bae95d4628138bf6fd68742 (patch) | |
| tree | 5e33c6a5ec091ecabfcb257fdc7b6a88ed8754ac /packages/math/range.test.ts | |
| parent | 16c8578b15c727f22921f8a80a56ee4d4e7f2272 (diff) | |
refactor: packages/
Diffstat (limited to 'packages/math/range.test.ts')
| -rw-r--r-- | packages/math/range.test.ts | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/packages/math/range.test.ts b/packages/math/range.test.ts new file mode 100644 index 0000000..fb4b6a3 --- /dev/null +++ b/packages/math/range.test.ts @@ -0,0 +1,51 @@ +import { rangeInclusive, rangeIntersection, rangesOverlap } from "./range"; + +describe("range overlap", () => { + const range1_4 = rangeInclusive(1, 4); + + it("should overlap when range a contains range b", () => { + expect(rangesOverlap(range1_4, rangeInclusive(2, 3))).toBe(true); + expect(rangesOverlap(range1_4, range1_4)).toBe(true); + expect(rangesOverlap(range1_4, rangeInclusive(1, 3))).toBe(true); + expect(rangesOverlap(range1_4, rangeInclusive(2, 4))).toBe(true); + }); + + it("should overlap when range b contains range a", () => { + expect(rangesOverlap(rangeInclusive(2, 3), range1_4)).toBe(true); + expect(rangesOverlap(rangeInclusive(1, 3), range1_4)).toBe(true); + expect(rangesOverlap(rangeInclusive(2, 4), range1_4)).toBe(true); + }); + + it("should overlap when range a and b intersect", () => { + expect(rangesOverlap(range1_4, rangeInclusive(2, 5))).toBe(true); + }); +}); + +describe("range intersection", () => { + const range1_4 = rangeInclusive(1, 4); + + it("should intersect completely with itself", () => { + expect(rangeIntersection(range1_4, range1_4)).toEqual(range1_4); + }); + + it("should intersect irrespective of order", () => { + expect(rangeIntersection(range1_4, rangeInclusive(2, 3))).toEqual([2, 3]); + expect(rangeIntersection(rangeInclusive(2, 3), range1_4)).toEqual([2, 3]); + expect(rangeIntersection(range1_4, rangeInclusive(3, 5))).toEqual( + rangeInclusive(3, 4), + ); + expect(rangeIntersection(rangeInclusive(3, 5), range1_4)).toEqual( + rangeInclusive(3, 4), + ); + }); + + it("should intersect at the edge", () => { + expect(rangeIntersection(range1_4, rangeInclusive(4, 5))).toEqual( + rangeInclusive(4, 4), + ); + }); + + it("should not intersect", () => { + expect(rangeIntersection(range1_4, rangeInclusive(5, 7))).toEqual(null); + }); +}); |
