Skip to content

Commit

Permalink
PB-538 : add unit test to cover the split chunk function
Browse files Browse the repository at this point in the history
  • Loading branch information
pakb committed May 30, 2024
1 parent 62b938f commit 3c15287
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
44 changes: 44 additions & 0 deletions src/api/__tests__/profile.api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, it } from 'vitest'

import ElevationProfile from '@/api/profile/ElevationProfile.class'
import ElevationProfileSegment from '@/api/profile/ElevationProfileSegment.class'
import { splitIfTooManyPoints } from '@/api/profile/profile.api.js'

const testProfile = new ElevationProfile([
new ElevationProfileSegment([
Expand Down Expand Up @@ -73,3 +74,46 @@ describe('Profile calculation', () => {
expect(testProfile.slopeDistance).to.approximately(397.86, 0.01)
})
})

describe('splitIfTooManyPoints', () => {
/**
* @param {Number} pointsCount
* @returns {CoordinatesChunk}
*/
function generateChunkWith(pointsCount) {
const coordinates = []
for (let i = 0; i < pointsCount; i++) {
coordinates.push([0, i])
}
return {
coordinates,
isWithinBounds: true,
}
}

it('does not split a segment that does not contain more point than the limit', () => {
const result = splitIfTooManyPoints([generateChunkWith(3000)])
expect(result).to.be.an('Array').lengthOf(1)
expect(result[0].coordinates).to.be.an('Array').lengthOf(3000)
})
it('splits if one coordinates above the limit', () => {
const result = splitIfTooManyPoints([generateChunkWith(3001)])
expect(result).to.be.an('Array').lengthOf(2)
expect(result[0].coordinates).to.be.an('Array').lengthOf(3000)
expect(result[1].coordinates).to.be.an('Array').lengthOf(1)
})
it('creates as many sub-chunks as necessary', () => {
const result = splitIfTooManyPoints([generateChunkWith(3000 * 4 + 123)])
expect(result).to.be.an('Array').lengthOf(5)
for (let i = 0; i < 4; i++) {
expect(result[i].coordinates).to.be.an('Array').lengthOf(3000)
}
expect(result[4].coordinates).to.be.an('Array').lengthOf(123)
})
it('does not fail if the given chunk is empty or invalid', () => {
expect(splitIfTooManyPoints(null)).to.be.null
expect(splitIfTooManyPoints(undefined)).to.be.null
expect(splitIfTooManyPoints({})).to.be.null
expect(splitIfTooManyPoints([])).to.be.an('Array').lengthOf(0)
})
})
4 changes: 2 additions & 2 deletions src/api/profile/profile.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export class ProfileError {
const MAX_CHUNK_LENGTH = 3000

/**
* @param {CoordinatesChunk | null} [chunks]
* @param {CoordinatesChunk[] | null} [chunks]
* @returns {null | CoordinatesChunk[]}
*/
function splitIfTooManyPoints(chunks = null) {
export function splitIfTooManyPoints(chunks = null) {
if (!Array.isArray(chunks)) {
return null
}
Expand Down

0 comments on commit 3c15287

Please sign in to comment.