Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

edge-math.test.ts

293 lines8.0 KB

1import { describe, expect, it } from "vitest"
2import {
3 BOX_COUNT,
4 EDGE_COUNT,
5 H,
6 HORIZ_COUNT,
7 VERT_COUNT,
8 W,
9 boxCoordsToId,
10 boxIdToCoords,
11 coordsToEdgeId,
12 edgeIdToCoords,
13 getAdjacentBoxes,
14 getBoxEdges,
15 isHorizontal,
16 isValidEdgeId,
17} from "../../../shared/edge-math"
18​
19describe(`edge-math constants`, () => {
20 it(`has correct grid dimensions`, () => {
21 expect(W).toBe(1000)
22 expect(H).toBe(1000)
23 })
24​
25 it(`has correct edge counts`, () => {
26 expect(HORIZ_COUNT).toBe(1000 * 1001) // W * (H+1) = 1,001,000
27 expect(VERT_COUNT).toBe(1001 * 1000) // (W+1) * H = 1,001,000
28 expect(EDGE_COUNT).toBe(2002000)
29 })
30​
31 it(`has correct box count`, () => {
32 expect(BOX_COUNT).toBe(1000000) // W * H
33 })
34})
35​
36describe(`edgeIdToCoords`, () => {
37 it(`converts horizontal edge at origin (0,0)`, () => {
38 expect(edgeIdToCoords(0)).toEqual({ x: 0, y: 0, horizontal: true })
39 })
40​
41 it(`converts horizontal edge at end of first row`, () => {
42 expect(edgeIdToCoords(999)).toEqual({ x: 999, y: 0, horizontal: true })
43 })
44​
45 it(`converts horizontal edge at start of second row`, () => {
46 expect(edgeIdToCoords(1000)).toEqual({ x: 0, y: 1, horizontal: true })
47 })
48​
49 it(`converts last horizontal edge`, () => {
50 expect(edgeIdToCoords(HORIZ_COUNT - 1)).toEqual({
51 x: W - 1,
52 y: H,
53 horizontal: true,
54 })
55 })
56​
57 it(`converts first vertical edge`, () => {
58 expect(edgeIdToCoords(HORIZ_COUNT)).toEqual({
59 x: 0,
60 y: 0,
61 horizontal: false,
62 })
63 })
64​
65 it(`converts second vertical edge`, () => {
66 expect(edgeIdToCoords(HORIZ_COUNT + 1)).toEqual({
67 x: 1,
68 y: 0,
69 horizontal: false,
70 })
71 })
72​
73 it(`converts last vertical edge in first row`, () => {
74 expect(edgeIdToCoords(HORIZ_COUNT + W)).toEqual({
75 x: W,
76 y: 0,
77 horizontal: false,
78 })
79 })
80​
81 it(`converts first vertical edge in second row`, () => {
82 expect(edgeIdToCoords(HORIZ_COUNT + W + 1)).toEqual({
83 x: 0,
84 y: 1,
85 horizontal: false,
86 })
87 })
88​
89 it(`converts last vertical edge`, () => {
90 expect(edgeIdToCoords(EDGE_COUNT - 1)).toEqual({
91 x: W,
92 y: H - 1,
93 horizontal: false,
94 })
95 })
96})
97​
98describe(`coordsToEdgeId`, () => {
99 it(`converts horizontal edge at origin`, () => {
100 expect(coordsToEdgeId(0, 0, true)).toBe(0)
101 })
102​
103 it(`converts horizontal edge at (500, 500)`, () => {
104 expect(coordsToEdgeId(500, 500, true)).toBe(500 * W + 500)
105 })
106​
107 it(`converts first vertical edge`, () => {
108 expect(coordsToEdgeId(0, 0, false)).toBe(HORIZ_COUNT)
109 })
110​
111 it(`converts vertical edge at (500, 500)`, () => {
112 expect(coordsToEdgeId(500, 500, false)).toBe(HORIZ_COUNT + 500 * (W + 1) + 500)
113 })
114})
115​
116describe(`edgeIdToCoords / coordsToEdgeId round-trip`, () => {
117 const testCases = [
118 0,
119 1,
120 999,
121 1000,
122 50000,
123 HORIZ_COUNT - 1,
124 HORIZ_COUNT,
125 HORIZ_COUNT + 1,
126 HORIZ_COUNT + 1000,
127 EDGE_COUNT - 1,
128 ]
129​
130 testCases.forEach((id) => {
131 it(`round-trips edge ID ${id}`, () => {
132 const coords = edgeIdToCoords(id)
133 const back = coordsToEdgeId(coords.x, coords.y, coords.horizontal)
134 expect(back).toBe(id)
135 })
136 })
137})
138​
139describe(`isHorizontal`, () => {
140 it(`returns true for horizontal edges`, () => {
141 expect(isHorizontal(0)).toBe(true)
142 expect(isHorizontal(HORIZ_COUNT - 1)).toBe(true)
143 })
144​
145 it(`returns false for vertical edges`, () => {
146 expect(isHorizontal(HORIZ_COUNT)).toBe(false)
147 expect(isHorizontal(EDGE_COUNT - 1)).toBe(false)
148 })
149})
150​
151describe(`getAdjacentBoxes`, () => {
152 describe(`horizontal edges`, () => {
153 it(`returns 1 box for top boundary (y=0)`, () => {
154 const boxes = getAdjacentBoxes(0) // h(0, 0)
155 expect(boxes).toHaveLength(1)
156 expect(boxes[0]).toEqual({ x: 0, y: 0 })
157 })
158​
159 it(`returns 1 box for bottom boundary (y=H)`, () => {
160 const edge = coordsToEdgeId(0, H, true) // h(0, 1000)
161 const boxes = getAdjacentBoxes(edge)
162 expect(boxes).toHaveLength(1)
163 expect(boxes[0]).toEqual({ x: 0, y: H - 1 })
164 })
165​
166 it(`returns 2 boxes for interior horizontal edge`, () => {
167 const edge = coordsToEdgeId(500, 500, true) // h(500, 500)
168 const boxes = getAdjacentBoxes(edge)
169 expect(boxes).toHaveLength(2)
170 expect(boxes).toContainEqual({ x: 500, y: 499 })
171 expect(boxes).toContainEqual({ x: 500, y: 500 })
172 })
173 })
174​
175 describe(`vertical edges`, () => {
176 it(`returns 1 box for left boundary (x=0)`, () => {
177 const edge = coordsToEdgeId(0, 0, false) // v(0, 0)
178 const boxes = getAdjacentBoxes(edge)
179 expect(boxes).toHaveLength(1)
180 expect(boxes[0]).toEqual({ x: 0, y: 0 })
181 })
182​
183 it(`returns 1 box for right boundary (x=W)`, () => {
184 const edge = coordsToEdgeId(W, 0, false) // v(1000, 0)
185 const boxes = getAdjacentBoxes(edge)
186 expect(boxes).toHaveLength(1)
187 expect(boxes[0]).toEqual({ x: W - 1, y: 0 })
188 })
189​
190 it(`returns 2 boxes for interior vertical edge`, () => {
191 const edge = coordsToEdgeId(500, 500, false) // v(500, 500)
192 const boxes = getAdjacentBoxes(edge)
193 expect(boxes).toHaveLength(2)
194 expect(boxes).toContainEqual({ x: 499, y: 500 })
195 expect(boxes).toContainEqual({ x: 500, y: 500 })
196 })
197 })
198})
199​
200describe(`getBoxEdges`, () => {
201 it(`returns correct edges for box (0,0)`, () => {
202 const [top, bottom, left, right] = getBoxEdges(0, 0)
203​
204 // top = h(0, 0) = 0
205 expect(top).toBe(0)
206 // bottom = h(0, 1) = 1000
207 expect(bottom).toBe(W)
208 // left = v(0, 0) = HORIZ_COUNT
209 expect(left).toBe(HORIZ_COUNT)
210 // right = v(1, 0) = HORIZ_COUNT + 1
211 expect(right).toBe(HORIZ_COUNT + 1)
212 })
213​
214 it(`returns correct edges for box (500, 500)`, () => {
215 const [top, bottom, left, right] = getBoxEdges(500, 500)
216​
217 // top = h(500, 500) = 500*1000 + 500
218 expect(top).toBe(500 * W + 500)
219 // bottom = h(500, 501) = 501*1000 + 500
220 expect(bottom).toBe(501 * W + 500)
221 // left = v(500, 500) = HORIZ_COUNT + 500*1001 + 500
222 expect(left).toBe(HORIZ_COUNT + 500 * (W + 1) + 500)
223 // right = v(501, 500) = HORIZ_COUNT + 500*1001 + 501
224 expect(right).toBe(HORIZ_COUNT + 500 * (W + 1) + 501)
225 })
226​
227 it(`returns correct edges for last box (999, 999)`, () => {
228 const [top, bottom, left, right] = getBoxEdges(999, 999)
229​
230 expect(top).toBe(999 * W + 999)
231 expect(bottom).toBe(1000 * W + 999)
232 expect(left).toBe(HORIZ_COUNT + 999 * (W + 1) + 999)
233 expect(right).toBe(HORIZ_COUNT + 999 * (W + 1) + 1000)
234 })
235})
236​
237describe(`boxCoordsToId / boxIdToCoords`, () => {
238 it(`converts box (0,0) to id 0`, () => {
239 expect(boxCoordsToId(0, 0)).toBe(0)
240 expect(boxIdToCoords(0)).toEqual({ x: 0, y: 0 })
241 })
242​
243 it(`converts box (999,0) to id 999`, () => {
244 expect(boxCoordsToId(999, 0)).toBe(999)
245 expect(boxIdToCoords(999)).toEqual({ x: 999, y: 0 })
246 })
247​
248 it(`converts box (0,1) to id 1000`, () => {
249 expect(boxCoordsToId(0, 1)).toBe(1000)
250 expect(boxIdToCoords(1000)).toEqual({ x: 0, y: 1 })
251 })
252​
253 it(`converts box (500, 500) correctly`, () => {
254 const id = boxCoordsToId(500, 500)
255 expect(id).toBe(500 * W + 500)
256 expect(boxIdToCoords(id)).toEqual({ x: 500, y: 500 })
257 })
258​
259 it(`round-trips all corner boxes`, () => {
260 const corners = [
261 { x: 0, y: 0 },
262 { x: 999, y: 0 },
263 { x: 0, y: 999 },
264 { x: 999, y: 999 },
265 ]
266 for (const box of corners) {
267 const id = boxCoordsToId(box.x, box.y)
268 expect(boxIdToCoords(id)).toEqual(box)
269 }
270 })
271})
272​
273describe(`isValidEdgeId`, () => {
274 it(`returns true for valid edge IDs`, () => {
275 expect(isValidEdgeId(0)).toBe(true)
276 expect(isValidEdgeId(1000000)).toBe(true)
277 expect(isValidEdgeId(EDGE_COUNT - 1)).toBe(true)
278 })
279​
280 it(`returns false for negative edge IDs`, () => {
281 expect(isValidEdgeId(-1)).toBe(false)
282 })
283​
284 it(`returns false for edge IDs >= EDGE_COUNT`, () => {
285 expect(isValidEdgeId(EDGE_COUNT)).toBe(false)
286 expect(isValidEdgeId(EDGE_COUNT + 1)).toBe(false)
287 })
288​
289 it(`returns false for non-integer edge IDs`, () => {
290 expect(isValidEdgeId(0.5)).toBe(false)
291 expect(isValidEdgeId(NaN)).toBe(false)
292 })
293})