Basic loop
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import im from 'immutable'
|
||||
import { setPointsInTree } from './tree'
|
||||
|
||||
it('sets points on an empty tree', () => {
|
||||
const tree = im.List()
|
||||
expect(setPointsInTree(tree, 2, 5).toJS()).toEqual([0, 0, 5])
|
||||
})
|
||||
|
||||
it('sets points in the end of the current range', () => {
|
||||
const tree = im.List([0, 1])
|
||||
expect(setPointsInTree(tree, 2, 5).toJS()).toEqual([0, 1, 5])
|
||||
})
|
||||
it('sets points in the middle of the current range', () => {
|
||||
const tree = im.List([0, 0, 0, 0, 0, 0, 5])
|
||||
expect(setPointsInTree(tree, 2, 5).toJS()).toEqual([0, 0, 5, 0, 0, 0, 5])
|
||||
})
|
||||
|
||||
it('does not mutate the tree for points already set', () => {
|
||||
const tree = im.List([0, 3, 2, 0, 5])
|
||||
expect(setPointsInTree(tree, 1, 3)).toStrictEqual(tree)
|
||||
})
|
||||
@@ -0,0 +1,9 @@
|
||||
import im from 'immutable'
|
||||
|
||||
export function setPointsInTree(tree: im.List<number>, index: number, points: number) {
|
||||
// Ensure all values until `index` are set, otherwise set to 0
|
||||
for (let i = tree.size; i <= index; i++) {
|
||||
tree = tree.set(i, i === index ? points : 0)
|
||||
}
|
||||
return tree
|
||||
}
|
||||
Reference in New Issue
Block a user