Skip to content

Commit f7e53d9

Browse files
jbogacznventuro
authored andcommitted
Add Arrays library with unit tests (OpenZeppelin#1209) (OpenZeppelin#1375)
* Add Arrays library with unit tests (OpenZeppelin#1209) * prepared due to snapshot token requirements * add library with method to find upper bound * add unit test for basic and edge cases * Imporove documentation for Arrays library Simplify Arrays.test.js to use short arrays as test date * Added comment for uint256 mid variable. * Explaned why uint256 mid variable calculated as Math.average is safe to use as index of array.
1 parent 41f84f8 commit f7e53d9

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

contracts/mocks/ArraysImpl.sol

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pragma solidity ^0.4.24;
2+
3+
import "../utils/Arrays.sol";
4+
5+
contract ArraysImpl {
6+
7+
using Arrays for uint256[];
8+
9+
uint256[] private array;
10+
11+
constructor(uint256[] _array) public {
12+
array = _array;
13+
}
14+
15+
function findUpperBound(uint256 _element) external view returns (uint256) {
16+
return array.findUpperBound(_element);
17+
}
18+
}

contracts/utils/Arrays.sol

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
pragma solidity ^0.4.23;
2+
3+
import "../math/Math.sol";
4+
5+
6+
/**
7+
* @title Arrays
8+
* @dev Utility library of inline array functions
9+
*/
10+
library Arrays {
11+
12+
/**
13+
* @dev Upper bound search function which is kind of binary search algoritm. It searches sorted
14+
* array to find index of the element value. If element is found then returns it's index otherwise
15+
* it returns index of first element which is grater than searched value. If searched element is
16+
* bigger than any array element function then returns first index after last element (i.e. all
17+
* values inside the array are smaller than the target). Complexity O(log n).
18+
* @param array The array sorted in ascending order.
19+
* @param element The element's value to be find.
20+
* @return The calculated index value. Returns 0 for empty array.
21+
*/
22+
function findUpperBound(
23+
uint256[] storage array,
24+
uint256 element
25+
)
26+
internal
27+
view
28+
returns (uint256)
29+
{
30+
if (array.length == 0) {
31+
return 0;
32+
}
33+
34+
uint256 low = 0;
35+
uint256 high = array.length;
36+
37+
while (low < high) {
38+
uint256 mid = Math.average(low, high);
39+
40+
// Note that mid will always be strictly less than high (i.e. it will be a valid array index)
41+
// because Math.average rounds down (it does integer division with truncation).
42+
if (array[mid] > element) {
43+
high = mid;
44+
} else {
45+
low = mid + 1;
46+
}
47+
}
48+
49+
// At this point `low` is the exclusive upper bound. We will return the inclusive upper bound.
50+
if (low > 0 && array[low - 1] == element) {
51+
return low - 1;
52+
} else {
53+
return low;
54+
}
55+
}
56+
}

test/utils/Arrays.test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const ArraysImpl = artifacts.require('ArraysImpl');
2+
3+
const BigNumber = web3.BigNumber;
4+
5+
require('chai')
6+
.use(require('chai-bignumber')(BigNumber))
7+
.should();
8+
9+
contract('Arrays', function () {
10+
context('Even number of elements', function () {
11+
const EVEN_ELEMENTS_ARRAY = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
12+
13+
beforeEach(async function () {
14+
this.arrays = await ArraysImpl.new(EVEN_ELEMENTS_ARRAY);
15+
});
16+
17+
it('should return correct index for the basic case', async function () {
18+
(await this.arrays.findUpperBound(16)).should.be.bignumber.equal(5);
19+
});
20+
21+
it('should return 0 for the first element', async function () {
22+
(await this.arrays.findUpperBound(11)).should.be.bignumber.equal(0);
23+
});
24+
25+
it('should return index of the last element', async function () {
26+
(await this.arrays.findUpperBound(20)).should.be.bignumber.equal(9);
27+
});
28+
29+
it('should return first index after last element if searched value is over the upper boundary', async function () {
30+
(await this.arrays.findUpperBound(32)).should.be.bignumber.equal(10);
31+
});
32+
33+
it('should return 0 for the element under the lower boundary', async function () {
34+
(await this.arrays.findUpperBound(2)).should.be.bignumber.equal(0);
35+
});
36+
});
37+
38+
context('Odd number of elements', function () {
39+
const ODD_ELEMENTS_ARRAY = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21];
40+
41+
beforeEach(async function () {
42+
this.arrays = await ArraysImpl.new(ODD_ELEMENTS_ARRAY);
43+
});
44+
45+
it('should return correct index for the basic case', async function () {
46+
(await this.arrays.findUpperBound(16)).should.be.bignumber.equal(5);
47+
});
48+
49+
it('should return 0 for the first element', async function () {
50+
(await this.arrays.findUpperBound(11)).should.be.bignumber.equal(0);
51+
});
52+
53+
it('should return index of the last element', async function () {
54+
(await this.arrays.findUpperBound(21)).should.be.bignumber.equal(10);
55+
});
56+
57+
it('should return first index after last element if searched value is over the upper boundary', async function () {
58+
(await this.arrays.findUpperBound(32)).should.be.bignumber.equal(11);
59+
});
60+
61+
it('should return 0 for the element under the lower boundary', async function () {
62+
(await this.arrays.findUpperBound(2)).should.be.bignumber.equal(0);
63+
});
64+
});
65+
66+
context('Array with gap', function () {
67+
const WITH_GAP_ARRAY = [11, 12, 13, 14, 15, 20, 21, 22, 23, 24];
68+
69+
beforeEach(async function () {
70+
this.arrays = await ArraysImpl.new(WITH_GAP_ARRAY);
71+
});
72+
73+
it('should return index of first element in next filled range', async function () {
74+
(await this.arrays.findUpperBound(17)).should.be.bignumber.equal(5);
75+
});
76+
});
77+
78+
context('Empty array', function () {
79+
beforeEach(async function () {
80+
this.arrays = await ArraysImpl.new([]);
81+
});
82+
83+
it('should always return 0 for empty array', async function () {
84+
(await this.arrays.findUpperBound(10)).should.be.bignumber.equal(0);
85+
});
86+
});
87+
});

0 commit comments

Comments
 (0)