|
1 | 1 | const { ethers } = require('hardhat');
|
2 | 2 | const { expect } = require('chai');
|
3 | 3 | const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
| 4 | +const { MAX_UINT128, MAX_UINT64, MAX_UINT32, MAX_UINT16 } = require('../helpers/constants'); |
| 5 | + |
| 6 | +// Helper functions for fixed bytes types |
| 7 | +const bytes32 = value => ethers.toBeHex(value, 32); |
| 8 | +const bytes16 = value => ethers.toBeHex(value, 16); |
| 9 | +const bytes8 = value => ethers.toBeHex(value, 8); |
| 10 | +const bytes4 = value => ethers.toBeHex(value, 4); |
| 11 | +const bytes2 = value => ethers.toBeHex(value, 2); |
4 | 12 |
|
5 | 13 | async function fixture() {
|
6 | 14 | const mock = await ethers.deployContract('$Bytes');
|
@@ -85,4 +93,120 @@ describe('Bytes', function () {
|
85 | 93 | }
|
86 | 94 | });
|
87 | 95 | });
|
| 96 | + |
| 97 | + describe('reverseBits', function () { |
| 98 | + describe('reverseBytes32', function () { |
| 99 | + it('reverses bytes correctly', async function () { |
| 100 | + await expect(this.mock.$reverseBytes32(bytes32(0))).to.eventually.equal(bytes32(0)); |
| 101 | + await expect(this.mock.$reverseBytes32(bytes32(ethers.MaxUint256))).to.eventually.equal( |
| 102 | + bytes32(ethers.MaxUint256), |
| 103 | + ); |
| 104 | + |
| 105 | + // Test complex pattern that clearly shows byte reversal |
| 106 | + await expect( |
| 107 | + this.mock.$reverseBytes32('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'), |
| 108 | + ).to.eventually.equal('0xefcdab8967452301efcdab8967452301efcdab8967452301efcdab8967452301'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('double reverse returns original', async function () { |
| 112 | + const values = [0n, 1n, 0x12345678n, ethers.MaxUint256]; |
| 113 | + for (const value of values) { |
| 114 | + const reversed = await this.mock.$reverseBytes32(bytes32(value)); |
| 115 | + await expect(this.mock.$reverseBytes32(reversed)).to.eventually.equal(bytes32(value)); |
| 116 | + } |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('reverseBytes16', function () { |
| 121 | + it('reverses bytes correctly', async function () { |
| 122 | + await expect(this.mock.$reverseBytes16(bytes16(0))).to.eventually.equal(bytes16(0)); |
| 123 | + await expect(this.mock.$reverseBytes16(bytes16(MAX_UINT128))).to.eventually.equal(bytes16(MAX_UINT128)); |
| 124 | + |
| 125 | + // Test complex pattern that clearly shows byte reversal |
| 126 | + await expect(this.mock.$reverseBytes16('0x0123456789abcdef0123456789abcdef')).to.eventually.equal( |
| 127 | + '0xefcdab8967452301efcdab8967452301', |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('double reverse returns original', async function () { |
| 132 | + const values = [0n, 1n, 0x12345678n, MAX_UINT128]; |
| 133 | + for (const value of values) { |
| 134 | + const reversed = await this.mock.$reverseBytes16(bytes16(value)); |
| 135 | + // Cast back to uint128 for comparison since function returns uint256 |
| 136 | + await expect(this.mock.$reverseBytes16(reversed)).to.eventually.equal(bytes16(value & MAX_UINT128)); |
| 137 | + } |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe('reverseBytes8', function () { |
| 142 | + it('reverses bytes correctly', async function () { |
| 143 | + await expect(this.mock.$reverseBytes8(bytes8(0))).to.eventually.equal(bytes8(0)); |
| 144 | + await expect(this.mock.$reverseBytes8(bytes8(MAX_UINT64))).to.eventually.equal(bytes8(MAX_UINT64)); |
| 145 | + |
| 146 | + // Test known pattern: 0x123456789ABCDEF0 -> 0xF0DEBC9A78563412 |
| 147 | + await expect(this.mock.$reverseBytes8('0x123456789abcdef0')).to.eventually.equal('0xf0debc9a78563412'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('double reverse returns original', async function () { |
| 151 | + const values = [0n, 1n, 0x12345678n, MAX_UINT64]; |
| 152 | + for (const value of values) { |
| 153 | + const reversed = await this.mock.$reverseBytes8(bytes8(value)); |
| 154 | + // Cast back to uint64 for comparison since function returns uint256 |
| 155 | + await expect(this.mock.$reverseBytes8(reversed)).to.eventually.equal(bytes8(value & MAX_UINT64)); |
| 156 | + } |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + describe('reverseBytes4', function () { |
| 161 | + it('reverses bytes correctly', async function () { |
| 162 | + await expect(this.mock.$reverseBytes4(bytes4(0))).to.eventually.equal(bytes4(0)); |
| 163 | + await expect(this.mock.$reverseBytes4(bytes4(MAX_UINT32))).to.eventually.equal(bytes4(MAX_UINT32)); |
| 164 | + |
| 165 | + // Test known pattern: 0x12345678 -> 0x78563412 |
| 166 | + await expect(this.mock.$reverseBytes4(bytes4(0x12345678))).to.eventually.equal(bytes4(0x78563412)); |
| 167 | + }); |
| 168 | + |
| 169 | + it('double reverse returns original', async function () { |
| 170 | + const values = [0n, 1n, 0x12345678n, MAX_UINT32]; |
| 171 | + for (const value of values) { |
| 172 | + const reversed = await this.mock.$reverseBytes4(bytes4(value)); |
| 173 | + // Cast back to uint32 for comparison since function returns uint256 |
| 174 | + await expect(this.mock.$reverseBytes4(reversed)).to.eventually.equal(bytes4(value & MAX_UINT32)); |
| 175 | + } |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + describe('reverseBytes2', function () { |
| 180 | + it('reverses bytes correctly', async function () { |
| 181 | + await expect(this.mock.$reverseBytes2(bytes2(0))).to.eventually.equal(bytes2(0)); |
| 182 | + await expect(this.mock.$reverseBytes2(bytes2(MAX_UINT16))).to.eventually.equal(bytes2(MAX_UINT16)); |
| 183 | + |
| 184 | + // Test known pattern: 0x1234 -> 0x3412 |
| 185 | + await expect(this.mock.$reverseBytes2(bytes2(0x1234))).to.eventually.equal(bytes2(0x3412)); |
| 186 | + }); |
| 187 | + |
| 188 | + it('double reverse returns original', async function () { |
| 189 | + const values = [0n, 1n, 0x1234n, MAX_UINT16]; |
| 190 | + for (const value of values) { |
| 191 | + const reversed = await this.mock.$reverseBytes2(bytes2(value)); |
| 192 | + // Cast back to uint16 for comparison since function returns uint256 |
| 193 | + await expect(this.mock.$reverseBytes2(reversed)).to.eventually.equal(bytes2(value & MAX_UINT16)); |
| 194 | + } |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + describe('edge cases', function () { |
| 199 | + it('handles single byte values', async function () { |
| 200 | + await expect(this.mock.$reverseBytes2(bytes2(0x00ff))).to.eventually.equal(bytes2(0xff00)); |
| 201 | + await expect(this.mock.$reverseBytes4(bytes4(0x000000ff))).to.eventually.equal(bytes4(0xff000000)); |
| 202 | + }); |
| 203 | + |
| 204 | + it('handles alternating patterns', async function () { |
| 205 | + await expect(this.mock.$reverseBytes2(bytes2(0xaaaa))).to.eventually.equal(bytes2(0xaaaa)); |
| 206 | + await expect(this.mock.$reverseBytes2(bytes2(0x5555))).to.eventually.equal(bytes2(0x5555)); |
| 207 | + await expect(this.mock.$reverseBytes4(bytes4(0xaaaaaaaa))).to.eventually.equal(bytes4(0xaaaaaaaa)); |
| 208 | + await expect(this.mock.$reverseBytes4(bytes4(0x55555555))).to.eventually.equal(bytes4(0x55555555)); |
| 209 | + }); |
| 210 | + }); |
| 211 | + }); |
88 | 212 | });
|
0 commit comments