Skip to content

Commit

Permalink
Strengthen test
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Aug 31, 2023
1 parent 312295f commit 8c688fb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 41 deletions.
6 changes: 3 additions & 3 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ DateTimeLibTest:testNthWeekdayInMonthOfYearTimestamp() (gas: 12031)
DateTimeLibTest:testNthWeekdayInMonthOfYearTimestamp(uint256,uint256,uint256,uint256) (runs: 256, μ: 3440, ~: 3480)
DateTimeLibTest:testWeekday() (gas: 705)
DateTimeLibTest:test__codesize() (gas: 20051)
DynamicBufferLibTest:testDynamicBuffer(bytes[],uint256) (runs: 256, μ: 913970, ~: 800618)
DynamicBufferLibTest:testDynamicBuffer(uint256) (runs: 256, μ: 890877, ~: 757860)
DynamicBufferLibTest:testDynamicBuffer(bytes[],uint256) (runs: 256, μ: 857036, ~: 762581)
DynamicBufferLibTest:testDynamicBuffer(uint256) (runs: 256, μ: 882828, ~: 739342)
DynamicBufferLibTest:testDynamicBufferChaining() (gas: 26188)
DynamicBufferLibTest:testJoinWithConcat() (gas: 31407)
DynamicBufferLibTest:testJoinWithDynamicBuffer() (gas: 10784)
DynamicBufferLibTest:test__codesize() (gas: 9305)
DynamicBufferLibTest:test__codesize() (gas: 9606)
ECDSATest:testBytes32ToEthSignedMessageHash() (gas: 381)
ECDSATest:testBytesToEthSignedMessageHash() (gas: 11588890)
ECDSATest:testBytesToEthSignedMessageHashEmpty() (gas: 556)
Expand Down
93 changes: 55 additions & 38 deletions test/DynamicBufferLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pragma solidity ^0.8.4;

import "./utils/SoladyTest.sol";
import {DynamicBufferLib} from "../src/utils/DynamicBufferLib.sol";
import {LibString} from "../src/utils/LibString.sol";

contract DynamicBufferLibTest is SoladyTest {
using DynamicBufferLib for DynamicBufferLib.DynamicBuffer;
Expand All @@ -13,68 +12,86 @@ contract DynamicBufferLibTest is SoladyTest {
if (_random() % 8 == 0) _misalignFreeMemoryPointer();
DynamicBufferLib.DynamicBuffer memory bufferA;
DynamicBufferLib.DynamicBuffer memory bufferB;
if (_random() % 8 == 0) bufferA.reserve(_random() % 1024);
if (_random() % 8 == 0) bufferB.reserve(_random() % 1024);
uint256 mode;
uint256 z = _bound(_random(), 32, 4096);
if (_random() % 8 == 0) bufferA.reserve(_random() % z);
if (_random() % 8 == 0) bufferB.reserve(_random() % z);
uint256 r = _random() % 3;
uint256 o = _bound(_random(), 0, 32);
uint256 n = _bound(_random(), 5, _random() % 8 == 0 ? 64 : 8);
z = z + z;

if (r == 0) {
for (uint256 i; i != 20; ++i) {
bytes memory s = bytes(LibString.toString(i > 10 ? 1 << i : i));
if (_random() % 8 == 0) bufferA.reserve(_random() % 2048);
bufferA.append(s);
for (uint256 i; i != n; ++i) {
if (_random() % 8 == 0) bufferA.reserve(_random() % z);
bufferA.append(_generateRandomBytes(i + o, i + z));
}
for (uint256 i; i != 20; ++i) {
bytes memory s = bytes(LibString.toString(i > 10 ? 1 << i : i));
if (_random() % 8 == 0) bufferB.reserve(_random() % 2048);
bufferB.append(s);
for (uint256 i; i != n; ++i) {
if (_random() % 8 == 0) bufferB.reserve(_random() % z);
bufferB.append(_generateRandomBytes(i + o, i + z));
}
} else if (r == 1) {
for (uint256 i; i != 20; ++i) {
bytes memory s = bytes(LibString.toString(i > 10 ? 1 << i : i));
if (_random() % 8 == 0) bufferB.reserve(_random() % 2048);
bufferB.append(s);
for (uint256 i; i != n; ++i) {
if (_random() % 8 == 0) bufferB.reserve(_random() % z);
bufferB.append(_generateRandomBytes(i + o, i + z));
}
for (uint256 i; i != 20; ++i) {
bytes memory s = bytes(LibString.toString(i > 10 ? 1 << i : i));
if (_random() % 8 == 0) bufferA.reserve(_random() % 2048);
bufferA.append(s);
for (uint256 i; i != n; ++i) {
if (_random() % 8 == 0) bufferA.reserve(_random() % z);
bufferA.append(_generateRandomBytes(i + o, i + z));
}
} else {
for (uint256 i; i != 20; ++i) {
bytes memory s = bytes(LibString.toString(i > 10 ? 1 << i : i));
uint256 mode;
for (uint256 i; i != n; ++i) {
if (_random() % 8 == 0) mode ^= 1;
if (mode == 0) {
if (_random() % 8 == 0) bufferA.reserve(_random() % 2048);
bufferA.append(s);
if (_random() % 8 == 0) bufferB.reserve(_random() % 2048);
bufferB.append(s);
if (_random() % 8 == 0) bufferA.reserve(_random() % z);
bufferA.append(_generateRandomBytes(i + o, i + z));
if (_random() % 8 == 0) bufferB.reserve(_random() % z);
bufferB.append(_generateRandomBytes(i + o, i + z));
} else {
if (_random() % 8 == 0) bufferB.reserve(_random() % 2048);
bufferB.append(s);
if (_random() % 8 == 0) bufferA.reserve(_random() % 2048);
bufferA.append(s);
if (_random() % 8 == 0) bufferB.reserve(_random() % z);
bufferB.append(_generateRandomBytes(i + o, i + z));
if (_random() % 8 == 0) bufferA.reserve(_random() % z);
bufferA.append(_generateRandomBytes(i + o, i + z));
}
}
}

bytes memory expected;
for (uint256 i; i != 20; ++i) {
bytes memory s = bytes(LibString.toString(i > 10 ? 1 << i : i));
expected = abi.encodePacked(expected, s);
for (uint256 i; i != n; ++i) {
expected = bytes.concat(expected, _generateRandomBytes(i + o, i + z));
}
assertEq(bufferA.data, expected);
assertEq(bufferB.data, expected);
}
}

function _generateRandomBytes(uint256 n, uint256 seed)
internal
pure
returns (bytes memory result)
{
/// @solidity memory-safe-assembly
assembly {
if n {
result := mload(0x40)
mstore(result, n)
mstore(0x00, seed)
for { let i := 0 } lt(i, n) { i := add(i, 0x20) } {
mstore(0x20, i)
mstore(add(add(result, 0x20), i), keccak256(0x00, 0x40))
}
mstore(0x40, add(add(result, 0x20), n))
}
}
}

function testDynamicBuffer(bytes[] memory inputs, uint256 randomness) public brutalizeMemory {
_boundInputs(inputs);

if (_random() % 8 == 0) _misalignFreeMemoryPointer();
if ((randomness >> 16) % 8 == 0) _misalignFreeMemoryPointer();
DynamicBufferLib.DynamicBuffer memory buffer;
if (_random() % 4 == 0) {
buffer.reserve(_random() % 1024);
if ((randomness >> 32) % 4 == 0) {
buffer.reserve((randomness >> 128) % 1024);
}

unchecked {
Expand All @@ -100,8 +117,8 @@ contract DynamicBufferLibTest is SoladyTest {
mstore(0x40, add(corruptCheckSlot, 0x20))
}
buffer.append(inputs[i]);
if (_random() % 8 == 0 && expectedLength != 0) {
buffer.reserve(_random() % (expectedLength * 2));
if ((randomness >> 48) % 8 == 0 && expectedLength != 0) {
buffer.reserve((randomness >> 160) % (expectedLength * 2));
}
assertEq(buffer.data.length, expectedLength);
_checkMemory(buffer.data);
Expand Down

0 comments on commit 8c688fb

Please sign in to comment.