Skip to content

Commit

Permalink
Fix x-y swap in rect function
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazerbeak12345 committed May 7, 2022
1 parent ba65795 commit 52139ea
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
23 changes: 22 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pixelmanipulator",
"version": "5.5.1",
"version": "5.5.2",
"description": "Run any cellular automata on an html5 canvas.",
"main": "dist/main.js",
"browser": "dist/browser.js",
Expand Down Expand Up @@ -68,6 +68,7 @@
"@typescript-eslint/eslint-plugin": "^4.0.1",
"@typescript-eslint/parser": "^4.0.0",
"ava": "^4.2.0",
"ava-fast-check": "^5.0.0",
"bootstrap-dark-5": "^1.1.3",
"browserify": "^17.0.0",
"c8": "^7.11.2",
Expand All @@ -78,6 +79,7 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1 || ^5.0.0",
"eslint-plugin-tsdoc": "^0.2.16",
"fast-check": "^2.25.0",
"fps-control": "^1.0.0",
"gh-pages": "^3.2.3",
"parcel": "^2.5.0",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/neighborhoods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type Hitbox=Location[]
*/
export function rect (topLeft: Location, bottomRight: Location): Location[] {
const output: Hitbox = []
for (let x = topLeft.x; x <= bottomRight.y; x++) {
for (let x = topLeft.x; x <= bottomRight.x; x++) {
for (let y = topLeft.y; y <= bottomRight.y; y++) {
output.push({ x, y })
}
Expand Down
59 changes: 59 additions & 0 deletions tests/lib/test-neighborhoods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { testProp, fc } from 'ava-fast-check'
import { neighborhoods } from '../../src/lib/pixelmanipulator'
const { rect } = neighborhoods

testProp(
'rect\'s size output is the area',
// Maximums provided to ensure reasonable time and memory is spent
[fc.integer(), fc.integer(), fc.nat({ max: 25 }), fc.nat({ max: 25 })],
(t, x, y, w, h) => {
const topLeft = {
x,
y
}
const bottomRight = {
x: x + w,
y: y + h
}
const expectedArea = (w + 1) * (h + 1) // (0,0) to (1,1) is area of 4
const list = rect(topLeft, bottomRight)
t.is(list.length, expectedArea)
}
)
testProp(
'return from rect has no duplicates',
[fc.integer(), fc.integer(), fc.nat({ max: 25 }), fc.nat({ max: 25 })],
(t, x, y, w, h) => {
const topLeft = {
x,
y
}
const bottomRight = {
x: x + w,
y: y + h
}
const list = rect(topLeft, bottomRight).map(pos => JSON.stringify(pos))
const set = new Set(list)
t.is(list.length, set.size)
}
)
testProp(
'return from rect keeps all values within bounds',
[fc.integer(), fc.integer(), fc.nat({ max: 25 }), fc.nat({ max: 25 })],
(t, x, y, w, h) => {
const topLeft = {
x,
y
}
const bottomRight = {
x: x + w,
y: y + h
}
const list = rect(topLeft, bottomRight)
t.plan(list.length * 2)
list.forEach(pos => {
t.true(topLeft.x <= pos.x && pos.x <= bottomRight.x)
t.true(topLeft.y <= pos.y && pos.y <= bottomRight.y)
})
}
)

0 comments on commit 52139ea

Please sign in to comment.