Skip to content

Commit c6d6bdd

Browse files
committed
coverage
1 parent 41b586b commit c6d6bdd

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

contracts/utils/Bytes.sol

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,15 @@ library Bytes {
7979
* @dev Count number of occurrences of `search` in `buffer`, starting from position `offset`.
8080
*/
8181
function countConsecutive(bytes memory buffer, uint256 offset, bytes1 search) internal pure returns (uint256 i) {
82+
uint256 length = buffer.length;
83+
if (offset > length) return 0;
84+
8285
assembly ("memory-safe") {
8386
let chunk
84-
let length := sub(mload(buffer), offset)
87+
let end := sub(length, offset)
8588
for {
8689
i := 0
87-
} lt(i, length) {
90+
} lt(i, end) {
8891
i := add(i, 1)
8992
} {
9093
// every 32 bytes, load a new chunk

test/utils/Base58.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ describe('Base58', function () {
2020
const hex = ethers.hexlify(buffer);
2121
const b58 = ethers.encodeBase58(buffer);
2222

23-
expect(await this.mock.$encode(hex)).to.equal(b58);
24-
expect(await this.mock.$decode(b58)).to.equal(hex);
23+
await expect(this.mock.$encode(hex)).to.eventually.equal(b58);
24+
await expect(this.mock.$decode(b58)).to.eventually.equal(hex);
2525
});
2626
});
2727

test/utils/Base64.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ describe('Base64', function () {
2727
])
2828
it(title, async function () {
2929
const buffer = Buffer.from(input, 'ascii');
30-
expect(await this.mock.$encode(buffer)).to.equal(ethers.encodeBase64(buffer));
31-
expect(await this.mock.$encode(buffer)).to.equal(expected);
30+
await expect(this.mock.$encode(buffer)).to.eventually.equal(ethers.encodeBase64(buffer));
31+
await expect(this.mock.$encode(buffer)).to.eventually.equal(expected);
3232
});
3333
});
3434

@@ -43,8 +43,8 @@ describe('Base64', function () {
4343
])
4444
it(title, async function () {
4545
const buffer = Buffer.from(input, 'ascii');
46-
expect(await this.mock.$encodeURL(buffer)).to.equal(base64toBase64Url(ethers.encodeBase64(buffer)));
47-
expect(await this.mock.$encodeURL(buffer)).to.equal(expected);
46+
await expect(this.mock.$encodeURL(buffer)).to.eventually.equal(base64toBase64Url(ethers.encodeBase64(buffer)));
47+
await expect(this.mock.$encodeURL(buffer)).to.eventually.equal(expected);
4848
});
4949
});
5050

@@ -53,7 +53,7 @@ describe('Base64', function () {
5353
const buffer32 = ethers.id('example');
5454
const buffer31 = buffer32.slice(0, -2);
5555

56-
expect(await mock.encode(buffer31)).to.equal(ethers.encodeBase64(buffer31));
57-
expect(await mock.encode(buffer32)).to.equal(ethers.encodeBase64(buffer32));
56+
await expect(mock.encode(buffer31)).to.eventually.equal(ethers.encodeBase64(buffer31));
57+
await expect(mock.encode(buffer32)).to.eventually.equal(ethers.encodeBase64(buffer32));
5858
});
5959
});

test/utils/Bytes.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,35 @@ describe('Bytes', function () {
5656
});
5757
});
5858

59+
describe('countConsecutive', function () {
60+
it('empty buffer', async function () {
61+
await expect(this.mock.$countConsecutive('0x', 0, '0x00')).to.eventually.equal(0);
62+
});
63+
64+
it('no occurrence', async function () {
65+
await expect(this.mock.$countConsecutive('0xa4f678', 0, '0x00')).to.eventually.equal(0);
66+
await expect(this.mock.$countConsecutive('0x000000', 0, '0x01')).to.eventually.equal(0);
67+
});
68+
69+
it('single occurrence', async function () {
70+
await expect(this.mock.$countConsecutive('0xa4f678', 0, '0xa4')).to.eventually.equal(1);
71+
await expect(this.mock.$countConsecutive('0xa4f678', 1, '0xf6')).to.eventually.equal(1);
72+
await expect(this.mock.$countConsecutive('0xa4f678', 2, '0x78')).to.eventually.equal(1);
73+
});
74+
75+
it('multiple occurrence', async function () {
76+
await expect(this.mock.$countConsecutive('0xa4a4f6f6f6f678', 0, '0xa4')).to.eventually.equal(2);
77+
await expect(this.mock.$countConsecutive('0xa4a4f6f6f6f678', 2, '0xf6')).to.eventually.equal(4);
78+
await expect(this.mock.$countConsecutive('0x78787878787878', 0, '0x78')).to.eventually.equal(7);
79+
await expect(this.mock.$countConsecutive('0x78787878787878', 3, '0x78')).to.eventually.equal(4);
80+
});
81+
82+
it('out of bound offset', async function () {
83+
await expect(this.mock.$countConsecutive('0x000000', 3, '0x00')).to.eventually.equal(0);
84+
await expect(this.mock.$countConsecutive('0x000000', 42, '0x00')).to.eventually.equal(0);
85+
});
86+
});
87+
5988
describe('slice & splice', function () {
6089
describe('slice(bytes, uint256) & splice(bytes, uint256)', function () {
6190
for (const [descr, start] of Object.entries({

0 commit comments

Comments
 (0)