-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/protocol/contracts/common/test/LinkedListTest.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
pragma solidity ^0.5.8; | ||
|
||
import "../linkedlists/LinkedList.sol"; | ||
|
||
contract LinkedListTest { | ||
using LinkedList for LinkedList.List; | ||
|
||
LinkedList.List private list; | ||
|
||
function insert(bytes32 key, bytes32 previousKey, bytes32 nextKey) external { | ||
list.insert(key, previousKey, nextKey); | ||
} | ||
|
||
function update(bytes32 key, bytes32 previousKey, bytes32 nextKey) external { | ||
list.update(key, previousKey, nextKey); | ||
} | ||
|
||
function remove(bytes32 key) external { | ||
list.remove(key); | ||
} | ||
|
||
function contains(bytes32 key) external view returns (bool) { | ||
return list.contains(key); | ||
} | ||
|
||
function getNumElements() external view returns (uint256) { | ||
return list.numElements; | ||
} | ||
|
||
function getKeys() | ||
external | ||
view | ||
returns ( | ||
bytes32[] memory | ||
) | ||
{ | ||
return list.getKeys(); | ||
} | ||
|
||
function head() external view returns (bytes32) { | ||
return list.head; | ||
} | ||
|
||
function tail() external view returns (bytes32) { | ||
return list.tail; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { assertRevert } from '@celo/protocol/lib/test-utils' | ||
// import BigNumber from 'bignumber.js' | ||
import { LinkedListTestContract, LinkedListTestInstance } from 'types' | ||
|
||
const LinkedListTest: LinkedListTestContract = artifacts.require('LinkedListTest') | ||
|
||
// @ts-ignore | ||
// TODO(mcortesi): Use BN | ||
LinkedListTest.numberFormat = 'BigNumber' | ||
|
||
contract('LinkedListTest', () => { | ||
let linkedListTest: LinkedListTestInstance | ||
|
||
beforeEach(async () => { | ||
linkedListTest = await LinkedListTest.new() | ||
}) | ||
|
||
describe('#insert()', () => { | ||
const NULL_KEY = '0x00' | ||
const firstKey = '0x01' | ||
const keys = ['0x01', '0x02', '0x03'] | ||
const middleKey = '0x02' | ||
const lastKey = '0x03' | ||
const addedKey = '0x04' | ||
|
||
describe('when inserting to empty list', () => { | ||
it('should revert if previous is equal to key', async () => { | ||
await assertRevert(linkedListTest.insert(addedKey, addedKey, NULL_KEY)) | ||
}) | ||
|
||
it('should revert if next is equal to key', async () => { | ||
await assertRevert(linkedListTest.insert(addedKey, NULL_KEY, addedKey)) | ||
}) | ||
}) | ||
|
||
describe('when inserting to singleton', () => { | ||
beforeEach(async () => { | ||
await linkedListTest.insert(firstKey, NULL_KEY, NULL_KEY) | ||
}) | ||
|
||
it('should revert if next is equal to key', async () => { | ||
await assertRevert(linkedListTest.insert(addedKey, firstKey, addedKey)) | ||
}) | ||
|
||
it('should revert if previous is equal to key', async () => { | ||
await assertRevert(linkedListTest.insert(addedKey, addedKey, firstKey)) | ||
}) | ||
}) | ||
|
||
describe('when inserting to 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], NULL_KEY, keys[i - 1]) | ||
}) | ||
|
||
it('should revert if next is equal to key (beginning)', async () => { | ||
await assertRevert(linkedListTest.insert(addedKey, firstKey, addedKey)) | ||
}) | ||
|
||
it('should revert if previous is equal to key (beginning)', async () => { | ||
await assertRevert(linkedListTest.insert(addedKey, addedKey, firstKey)) | ||
}) | ||
|
||
it('should revert if next is equal to key (end)', async () => { | ||
await assertRevert(linkedListTest.insert(addedKey, lastKey, addedKey)) | ||
}) | ||
|
||
it('should revert if previous is equal to key (end)', async () => { | ||
await assertRevert(linkedListTest.insert(addedKey, addedKey, lastKey)) | ||
}) | ||
|
||
it('should revert if next is equal to key (middle)', async () => { | ||
await assertRevert(linkedListTest.insert(addedKey, middleKey, addedKey)) | ||
}) | ||
|
||
it('should revert if previous is equal to key (middle)', async () => { | ||
await assertRevert(linkedListTest.insert(addedKey, addedKey, middleKey)) | ||
}) | ||
|
||
it('should revert if next and previous equal to key', async () => { | ||
await assertRevert(linkedListTest.insert(addedKey, addedKey, addedKey)) | ||
}) | ||
}) | ||
}) | ||
}) |