123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- const { ethers } = require('hardhat');
- const { expect } = require('chai');
- const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
- const { MAX_UINT128, MAX_UINT64, MAX_UINT32, MAX_UINT16 } = require('../helpers/constants');
- // Helper functions for fixed bytes types
- const bytes32 = value => ethers.toBeHex(value, 32);
- const bytes16 = value => ethers.toBeHex(value, 16);
- const bytes8 = value => ethers.toBeHex(value, 8);
- const bytes4 = value => ethers.toBeHex(value, 4);
- const bytes2 = value => ethers.toBeHex(value, 2);
- async function fixture() {
- const mock = await ethers.deployContract('$Bytes');
- return { mock };
- }
- const lorem = ethers.toUtf8Bytes(
- 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
- );
- const present = lorem.at(1);
- const absent = 255;
- describe('Bytes', function () {
- before(async function () {
- Object.assign(this, await loadFixture(fixture));
- });
- describe('indexOf', function () {
- it('first', async function () {
- await expect(this.mock.$indexOf(lorem, ethers.toBeHex(present))).to.eventually.equal(lorem.indexOf(present));
- });
- it('from index', async function () {
- for (const start in Array(lorem.length + 10).fill()) {
- const index = lorem.indexOf(present, start);
- const result = index === -1 ? ethers.MaxUint256 : index;
- await expect(
- this.mock.$indexOf(lorem, ethers.toBeHex(present), ethers.Typed.uint256(start)),
- ).to.eventually.equal(result);
- }
- });
- it('absent', async function () {
- await expect(this.mock.$indexOf(lorem, ethers.toBeHex(absent))).to.eventually.equal(ethers.MaxUint256);
- });
- it('empty buffer', async function () {
- await expect(this.mock.$indexOf('0x', '0x00')).to.eventually.equal(ethers.MaxUint256);
- await expect(this.mock.$indexOf('0x', '0x00', ethers.Typed.uint256(17))).to.eventually.equal(ethers.MaxUint256);
- });
- });
- describe('lastIndexOf', function () {
- it('first', async function () {
- await expect(this.mock.$lastIndexOf(lorem, ethers.toBeHex(present))).to.eventually.equal(
- lorem.lastIndexOf(present),
- );
- });
- it('from index', async function () {
- for (const start in Array(lorem.length + 10).fill()) {
- const index = lorem.lastIndexOf(present, start);
- const result = index === -1 ? ethers.MaxUint256 : index;
- await expect(
- this.mock.$lastIndexOf(lorem, ethers.toBeHex(present), ethers.Typed.uint256(start)),
- ).to.eventually.equal(result);
- }
- });
- it('absent', async function () {
- await expect(this.mock.$lastIndexOf(lorem, ethers.toBeHex(absent))).to.eventually.equal(ethers.MaxUint256);
- });
- it('empty buffer', async function () {
- await expect(this.mock.$lastIndexOf('0x', '0x00')).to.eventually.equal(ethers.MaxUint256);
- await expect(this.mock.$lastIndexOf('0x', '0x00', ethers.Typed.uint256(17))).to.eventually.equal(
- ethers.MaxUint256,
- );
- });
- });
- describe('slice & splice', function () {
- describe('slice(bytes, uint256) & splice(bytes, uint256)', function () {
- for (const [descr, start] of Object.entries({
- 'start = 0': 0,
- 'start within bound': 10,
- 'start out of bound': 1000,
- })) {
- it(descr, async function () {
- const result = ethers.hexlify(lorem.slice(start));
- await expect(this.mock.$slice(lorem, start)).to.eventually.equal(result);
- await expect(this.mock.$splice(lorem, start)).to.eventually.equal(result);
- });
- }
- });
- describe('slice(bytes, uint256, uint256) & splice(bytes, uint256, uint256)', function () {
- for (const [descr, [start, end]] of Object.entries({
- 'start = 0': [0, 42],
- 'start and end within bound': [17, 42],
- 'end out of bound': [42, 1000],
- 'start = end': [17, 17],
- 'start > end': [42, 17],
- })) {
- it(descr, async function () {
- const result = ethers.hexlify(lorem.slice(start, end));
- await expect(this.mock.$slice(lorem, start, ethers.Typed.uint256(end))).to.eventually.equal(result);
- await expect(this.mock.$splice(lorem, start, ethers.Typed.uint256(end))).to.eventually.equal(result);
- });
- }
- });
- });
- describe('reverseBits', function () {
- describe('reverseBytes32', function () {
- it('reverses bytes correctly', async function () {
- await expect(this.mock.$reverseBytes32(bytes32(0))).to.eventually.equal(bytes32(0));
- await expect(this.mock.$reverseBytes32(bytes32(ethers.MaxUint256))).to.eventually.equal(
- bytes32(ethers.MaxUint256),
- );
- // Test complex pattern that clearly shows byte reversal
- await expect(
- this.mock.$reverseBytes32('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'),
- ).to.eventually.equal('0xefcdab8967452301efcdab8967452301efcdab8967452301efcdab8967452301');
- });
- it('double reverse returns original', async function () {
- const values = [0n, 1n, 0x12345678n, ethers.MaxUint256];
- for (const value of values) {
- const reversed = await this.mock.$reverseBytes32(bytes32(value));
- await expect(this.mock.$reverseBytes32(reversed)).to.eventually.equal(bytes32(value));
- }
- });
- });
- describe('reverseBytes16', function () {
- it('reverses bytes correctly', async function () {
- await expect(this.mock.$reverseBytes16(bytes16(0))).to.eventually.equal(bytes16(0));
- await expect(this.mock.$reverseBytes16(bytes16(MAX_UINT128))).to.eventually.equal(bytes16(MAX_UINT128));
- // Test complex pattern that clearly shows byte reversal
- await expect(this.mock.$reverseBytes16('0x0123456789abcdef0123456789abcdef')).to.eventually.equal(
- '0xefcdab8967452301efcdab8967452301',
- );
- });
- it('double reverse returns original', async function () {
- const values = [0n, 1n, 0x12345678n, MAX_UINT128];
- for (const value of values) {
- const reversed = await this.mock.$reverseBytes16(bytes16(value));
- // Cast back to uint128 for comparison since function returns uint256
- await expect(this.mock.$reverseBytes16(reversed)).to.eventually.equal(bytes16(value & MAX_UINT128));
- }
- });
- });
- describe('reverseBytes8', function () {
- it('reverses bytes correctly', async function () {
- await expect(this.mock.$reverseBytes8(bytes8(0))).to.eventually.equal(bytes8(0));
- await expect(this.mock.$reverseBytes8(bytes8(MAX_UINT64))).to.eventually.equal(bytes8(MAX_UINT64));
- // Test known pattern: 0x123456789ABCDEF0 -> 0xF0DEBC9A78563412
- await expect(this.mock.$reverseBytes8('0x123456789abcdef0')).to.eventually.equal('0xf0debc9a78563412');
- });
- it('double reverse returns original', async function () {
- const values = [0n, 1n, 0x12345678n, MAX_UINT64];
- for (const value of values) {
- const reversed = await this.mock.$reverseBytes8(bytes8(value));
- // Cast back to uint64 for comparison since function returns uint256
- await expect(this.mock.$reverseBytes8(reversed)).to.eventually.equal(bytes8(value & MAX_UINT64));
- }
- });
- });
- describe('reverseBytes4', function () {
- it('reverses bytes correctly', async function () {
- await expect(this.mock.$reverseBytes4(bytes4(0))).to.eventually.equal(bytes4(0));
- await expect(this.mock.$reverseBytes4(bytes4(MAX_UINT32))).to.eventually.equal(bytes4(MAX_UINT32));
- // Test known pattern: 0x12345678 -> 0x78563412
- await expect(this.mock.$reverseBytes4(bytes4(0x12345678))).to.eventually.equal(bytes4(0x78563412));
- });
- it('double reverse returns original', async function () {
- const values = [0n, 1n, 0x12345678n, MAX_UINT32];
- for (const value of values) {
- const reversed = await this.mock.$reverseBytes4(bytes4(value));
- // Cast back to uint32 for comparison since function returns uint256
- await expect(this.mock.$reverseBytes4(reversed)).to.eventually.equal(bytes4(value & MAX_UINT32));
- }
- });
- });
- describe('reverseBytes2', function () {
- it('reverses bytes correctly', async function () {
- await expect(this.mock.$reverseBytes2(bytes2(0))).to.eventually.equal(bytes2(0));
- await expect(this.mock.$reverseBytes2(bytes2(MAX_UINT16))).to.eventually.equal(bytes2(MAX_UINT16));
- // Test known pattern: 0x1234 -> 0x3412
- await expect(this.mock.$reverseBytes2(bytes2(0x1234))).to.eventually.equal(bytes2(0x3412));
- });
- it('double reverse returns original', async function () {
- const values = [0n, 1n, 0x1234n, MAX_UINT16];
- for (const value of values) {
- const reversed = await this.mock.$reverseBytes2(bytes2(value));
- // Cast back to uint16 for comparison since function returns uint256
- await expect(this.mock.$reverseBytes2(reversed)).to.eventually.equal(bytes2(value & MAX_UINT16));
- }
- });
- });
- describe('edge cases', function () {
- it('handles single byte values', async function () {
- await expect(this.mock.$reverseBytes2(bytes2(0x00ff))).to.eventually.equal(bytes2(0xff00));
- await expect(this.mock.$reverseBytes4(bytes4(0x000000ff))).to.eventually.equal(bytes4(0xff000000));
- });
- it('handles alternating patterns', async function () {
- await expect(this.mock.$reverseBytes2(bytes2(0xaaaa))).to.eventually.equal(bytes2(0xaaaa));
- await expect(this.mock.$reverseBytes2(bytes2(0x5555))).to.eventually.equal(bytes2(0x5555));
- await expect(this.mock.$reverseBytes4(bytes4(0xaaaaaaaa))).to.eventually.equal(bytes4(0xaaaaaaaa));
- await expect(this.mock.$reverseBytes4(bytes4(0x55555555))).to.eventually.equal(bytes4(0x55555555));
- });
- });
- });
- });
|