silphius/app/util/spatial-hash.test.js
2024-07-03 16:13:14 -05:00

126 lines
3.9 KiB
JavaScript

import {expect, test} from 'vitest';
import SpatialHash from './spatial-hash.js';
test('creates chunks', async () => {
const hash = new SpatialHash({x: 128, y: 128});
expect(hash.chunks.length)
.to.equal(2);
expect(hash.chunks[0].length)
.to.equal(2);
expect(hash.chunks[1].length)
.to.equal(2);
});
test('clamps to actual chunks', async () => {
const hash = new SpatialHash({x: 640, y: 640});
expect(hash.chunkIndex(0, 0))
.to.deep.equal({x: 0, y: 0});
expect(hash.chunkIndex(320, 320))
.to.deep.equal({x: 5, y: 5});
expect(hash.chunkIndex(1280, 1280))
.to.deep.equal({x: 9, y: 9});
});
test('updates with data', async () => {
const hash = new SpatialHash({x: 640, y: 640});
hash.update({x0: 32, x1: 96, y0: 32, y1: 96}, 'foobar');
expect(hash.data.get('foobar'))
.to.deep.equal({
bounds: {x0: 32, x1: 96, y0: 32, y1: 96},
chunks: [
{x: 0, y: 0},
{x: 1, y: 0},
{x: 0, y: 1},
{x: 1, y: 1},
],
});
expect(Array.from(hash.chunks[0][0]))
.to.deep.equal([['foobar', true]]);
expect(Array.from(hash.chunks[1][0]))
.to.deep.equal([['foobar', true]]);
expect(Array.from(hash.chunks[1][1]))
.to.deep.equal([['foobar', true]]);
expect(Array.from(hash.chunks[0][1]))
.to.deep.equal([['foobar', true]]);
hash.update({x0: 48, x1: 32, y0: 32, y1: 96}, 'foobar');
expect(hash.data.get('foobar'))
.to.deep.equal({
bounds: {x0: 32, x1: 48, y0: 32, y1: 96},
chunks: [
{x: 0, y: 0},
{x: 0, y: 1},
],
});
expect(Array.from(hash.chunks[0][0]))
.to.deep.equal([['foobar', true]]);
expect(Array.from(hash.chunks[1][0]))
.to.deep.equal([]);
expect(Array.from(hash.chunks[1][1]))
.to.deep.equal([]);
expect(Array.from(hash.chunks[0][1]))
.to.deep.equal([['foobar', true]]);
hash.update({x0: 32, x1: 160, y0: 32, y1: 160}, 'foobar');
expect(hash.data.get('foobar'))
.to.deep.equal({
bounds: {x0: 32, x1: 160, y0: 32, y1: 160},
chunks: [
{x: 0, y: 0},
{x: 1, y: 0},
{x: 2, y: 0},
{x: 0, y: 1},
{x: 1, y: 1},
{x: 2, y: 1},
{x: 0, y: 2},
{x: 1, y: 2},
{x: 2, y: 2},
],
});
expect(Array.from(hash.chunks[0][0]))
.to.deep.equal([['foobar', true]]);
expect(Array.from(hash.chunks[1][0]))
.to.deep.equal([['foobar', true]]);
expect(Array.from(hash.chunks[2][0]))
.to.deep.equal([['foobar', true]]);
expect(Array.from(hash.chunks[3][0]))
.to.deep.equal([]);
expect(Array.from(hash.chunks[0][1]))
.to.deep.equal([['foobar', true]]);
expect(Array.from(hash.chunks[1][1]))
.to.deep.equal([['foobar', true]]);
expect(Array.from(hash.chunks[2][1]))
.to.deep.equal([['foobar', true]]);
expect(Array.from(hash.chunks[3][1]))
.to.deep.equal([]);
expect(Array.from(hash.chunks[0][2]))
.to.deep.equal([['foobar', true]]);
expect(Array.from(hash.chunks[1][2]))
.to.deep.equal([['foobar', true]]);
expect(Array.from(hash.chunks[2][2]))
.to.deep.equal([['foobar', true]]);
expect(Array.from(hash.chunks[3][2]))
.to.deep.equal([]);
expect(Array.from(hash.chunks[3][3]))
.to.deep.equal([]);
expect(Array.from(hash.chunks[3][3]))
.to.deep.equal([]);
expect(Array.from(hash.chunks[3][3]))
.to.deep.equal([]);
expect(Array.from(hash.chunks[3][3]))
.to.deep.equal([]);
});
test('queries for data', async () => {
const hash = new SpatialHash({x: 640, y: 640});
hash.update({x0: 32, x1: 96, y0: 32, y1: 96}, 'foobar');
expect(Array.from(hash.within({x0: 0, x1: 16, y0: 0, y1: 16})))
.to.deep.equal([]);
expect(Array.from(hash.within({x0: 0, x1: 48, y0: 0, y1: 48})))
.to.deep.equal(['foobar']);
expect(Array.from(hash.within({x0: 48, x1: 64, y0: 48, y1: 64})))
.to.deep.equal(['foobar']);
hash.update({x0: 32, x1: 160, y0: 32, y1: 160}, 'foobar');
expect(Array.from(hash.within({x0: 80, x1: 90, y0: 80, y1: 90})))
.to.deep.equal(['foobar']);
});