Skip to content

Commit 7a10228

Browse files
author
Beau Shinkle
committed
Refactor the child tests in positionTest.js
1 parent f6f71f0 commit 7a10228

1 file changed

Lines changed: 53 additions & 3 deletions

File tree

test/positionTest.js

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,59 @@ describe("Position", async () => {
111111
})
112112

113113
describe("child", async () => {
114-
it("Returns the child address", async () => {
115-
const result = await positionInstance.child(parentPosition, childPosition)
116-
expect(result).to.be.equal(fullPosition)
114+
it("Returns the position of the child given the position and slot of the parent", async () => {
115+
// Sample Cases
116+
//
117+
// The first position gets the first slot in the first parent, the
118+
// second gets the second slot in the first parent, and so on, until
119+
// the 9th, which gets the first slot in the second parent. Working
120+
// backwards, that means that if we know the parent's position and
121+
// child's slot, the child's position is `parentPosition * 8 + slot`
122+
const testData = [
123+
{
124+
position: 24603,
125+
slot: 7,
126+
child: 196831,
127+
},
128+
{
129+
position: 29871,
130+
slot: 0,
131+
child: 238968,
132+
},
133+
{
134+
position: 3875,
135+
slot: 2,
136+
child: 31002,
137+
},
138+
{
139+
position: 202242,
140+
slot: 4,
141+
child: 1617940,
142+
},
143+
]
144+
for (let i = 0; i < testData.length; i++) {
145+
const test = testData[i]
146+
const child = await positionInstance.child(test.position, test.slot)
147+
expect(child).to.equal(
148+
test.child,
149+
`unexpected result for test index ${i}`,
150+
)
151+
}
152+
153+
// Generative Testing
154+
const numSamples = 100
155+
const maxParentIndex = Math.pow(8, 6) // the largest possible parent index
156+
for (let i = 0; i < numSamples; i++) {
157+
// generate a random position in [0, maxParentIndex)
158+
const position = Math.floor(Math.random() * maxParentIndex)
159+
const slot = Math.floor(Math.random() * 8) // slots range from 0 to 7
160+
const child = await positionInstance.child(position, slot)
161+
const expectation = position * 8 + slot
162+
expect(child).to.equal(
163+
expectation,
164+
`unexpected result for position ${position}`,
165+
)
166+
}
117167
})
118168
})
119169
})

0 commit comments

Comments
 (0)