Skip to content

Commit

Permalink
Test LinkedList.remove (#9377)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-chrzan authored and martinvol committed May 13, 2022
1 parent 7bb56cd commit b8aa551
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion packages/protocol/test/common/linkedlist.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertRevert } from '@celo/protocol/lib/test-utils'
import { assertRevert, assertRevertWithReason } from '@celo/protocol/lib/test-utils'
import { LinkedListTestContract, LinkedListTestInstance } from 'types'

const LinkedListTest: LinkedListTestContract = artifacts.require('LinkedListTest')
Expand Down Expand Up @@ -79,4 +79,50 @@ contract('LinkedListTest', () => {
})
})
})

describe('#remove()', () => {
const NULL_KEY = '0x00' + '00'.repeat(31)
const firstKey = '0x01' + '00'.repeat(31)
const middleKey = '0x02' + '00'.repeat(31)
const lastKey = '0x03' + '00'.repeat(31)
const keys = [firstKey, middleKey, lastKey]

describe('removing from an empty list', () => {
it('should revert', async () => {
await assertRevertWithReason(linkedListTest.remove(firstKey), 'key not in list')
})
})

describe('when removing from a list with more items', () => {
beforeEach(async () => {
await linkedListTest.insert(firstKey, NULL_KEY, NULL_KEY)
for (let i = 1; i < keys.length; i++) {
await linkedListTest.insert(keys[i], keys[i - 1], NULL_KEY)
}
})

it('should remove the first element', async () => {
await linkedListTest.remove(firstKey)
const exists = await linkedListTest.contains(firstKey)
assert.isFalse(exists)
})

it('should end with the middle element at head', async () => {
await linkedListTest.remove(firstKey)
const tail = await linkedListTest.tail()
assert.equal(tail, middleKey)
})

it('should reduce the number of list elements', async () => {
await linkedListTest.remove(firstKey)
const numElements = await linkedListTest.getNumElements()
assert.equal(numElements.toNumber(), 2)
})

it('should revert when attempting to remove a removed element', async () => {
await linkedListTest.remove(firstKey)
await assertRevertWithReason(linkedListTest.remove(firstKey), 'key not in list')
})
})
})
})

0 comments on commit b8aa551

Please sign in to comment.