|
//// SPDX-License-Identifier: MIT |
|
pragma solidity >=0.8.13; |
|
import "../bubblesort.sol"; |
|
import "../../lib/forge-std/src/Test.sol"; |
|
|
|
contract BubbleSortTest is Test { |
|
function testBubbleSortAsmHardcoded() public { |
|
unchecked { |
|
uint256[] memory b = new uint256[](6); |
|
b[0] = 1; |
|
b[1] = 3; |
|
b[2] = 2; |
|
b[3] = 5; |
|
b[4] = 6; |
|
b[5] = 4; |
|
BubbleSort.bubbleSort(b, 6); |
|
emit log_array(b); |
|
|
|
uint256[] memory bSorted = new uint256[](6); |
|
bSorted[0] = 1; |
|
bSorted[1] = 2; |
|
bSorted[2] = 3; |
|
bSorted[3] = 4; |
|
bSorted[4] = 5; |
|
bSorted[5] = 6; |
|
assertEq(b, bSorted); |
|
bytes memory _b = abi.encodePacked(b); |
|
for (uint256 i = 1; i < b.length; ++i) { |
|
assertTrue(b[i - 1] <= b[i]); |
|
} |
|
} |
|
} |
|
|
|
function testBubbleSortNormal() public { |
|
unchecked { |
|
uint256[] memory b = new uint256[](6); |
|
b[0] = 1; |
|
b[1] = 3; |
|
b[2] = 2; |
|
b[3] = 5; |
|
b[4] = 6; |
|
b[5] = 4; |
|
BubbleSort.normBubbleSort(b, 6); |
|
for (uint256 i = 1; i < b.length; ++i) { |
|
assertTrue(b[i - 1] <= b[i]); |
|
} |
|
bytes memory _b = abi.encodePacked(b); |
|
// console.logBytes(_b); |
|
} |
|
} |
|
|
|
function testBubbleSortAssembly() public { |
|
unchecked { |
|
uint256[] memory b = new uint256[](6); |
|
uint8[6] memory a = [1, 3, 2, 5, 6, 4]; |
|
b[0] = 1; |
|
b[1] = 3; |
|
b[2] = 2; |
|
b[3] = 5; |
|
b[4] = 6; |
|
b[5] = 4; |
|
BubbleSort.bubbleSort(b, 6); |
|
for (uint256 i = 1; i < b.length; ++i) { |
|
assertTrue(b[i - 1] <= b[i]); |
|
} |
|
bytes memory _b = abi.encodePacked(b); |
|
// console.logBytes(_b); |
|
} |
|
} |
|
|
|
function testBubbleSortRandom(uint256[] memory a) public { |
|
unchecked { |
|
vm.assume(a.length < 7); |
|
|
|
BubbleSort.bubbleSort(a, 6); |
|
for (uint256 i = 1; i < a.length; ++i) { |
|
assertTrue(a[i - 1] <= a[i]); |
|
} |
|
bytes memory _b = abi.encodePacked(a); |
|
// console.logBytes(_b); |
|
} |
|
} |
|
|
|
function testSortChecksumed(uint256[] memory a) public { |
|
unchecked { |
|
vm.assume(a.length < 2048); |
|
uint256 checksum; |
|
for (uint256 i = 0; i < a.length; ++i) { |
|
checksum += a[i]; |
|
} |
|
BubbleSort.bubbleSort(a, a.length); |
|
uint256 checksumAfterSort; |
|
for (uint256 i = 0; i < a.length; ++i) { |
|
checksumAfterSort += a[i]; |
|
} |
|
assertEq(checksum, checksumAfterSort); |
|
for (uint256 i = 1; i < a.length; ++i) { |
|
assertTrue(a[i - 1] <= a[i]); |
|
} |
|
} |
|
} |
|
|
|
function testSortDifferential(uint256[] memory a) public { |
|
unchecked { |
|
vm.assume(a.length < 128); |
|
// Make a copy of the `a` and perform insertion sort on it. |
|
uint256[] memory aCopy = new uint256[](a.length); |
|
for (uint256 i = 0; i < a.length; ++i) { |
|
aCopy[i] = a[i]; |
|
} |
|
for (uint256 i = 1; i < aCopy.length; ++i) { |
|
uint256 key = aCopy[i]; |
|
uint256 j = i; |
|
while (j != 0 && aCopy[j - 1] > key) { |
|
aCopy[j] = aCopy[j - 1]; |
|
--j; |
|
} |
|
aCopy[j] = key; |
|
} |
|
BubbleSort.bubbleSort(a, a.length); |
|
// Compare the results. |
|
for (uint256 i = 0; i < a.length; ++i) { |
|
assertEq(a[i], aCopy[i]); |
|
} |
|
} |
|
} |
|
|
|
function testSort(uint256[] memory a) public { |
|
unchecked { |
|
vm.assume(a.length < 2048); |
|
BubbleSort.bubbleSort(a, a.length); |
|
for (uint256 i = 1; i < a.length; ++i) { |
|
assertTrue(a[i - 1] <= a[i]); |
|
} |
|
} |
|
} |
|
|
|
function testSortBasicCase() public { |
|
unchecked { |
|
uint256[] memory a = new uint256[](2); |
|
a[0] = 3; |
|
a[1] = 0; |
|
BubbleSort.bubbleSort(a, a.length); |
|
for (uint256 i = 1; i < a.length; ++i) { |
|
assertTrue(a[i - 1] <= a[i]); |
|
} |
|
} |
|
} |
|
|
|
function testSortPsuedorandom() public { |
|
unchecked { |
|
uint256[] memory a = new uint256[](100); |
|
uint256 lcg = 123456789; |
|
for (uint256 i; i < a.length; ++i) { |
|
a[i] = lcg; |
|
lcg = (lcg * 1664525 + 1013904223) & 0xFFFFFFFF; |
|
} |
|
BubbleSort.bubbleSort(a, a.length); |
|
for (uint256 i = 1; i < a.length; ++i) { |
|
assertTrue(a[i - 1] <= a[i]); |
|
} |
|
} |
|
} |
|
|
|
function testSortSorted() public { |
|
unchecked { |
|
uint256[] memory a = new uint256[](100); |
|
for (uint256 i; i < a.length; ++i) { |
|
a[i] = i; |
|
} |
|
BubbleSort.bubbleSort(a, a.length); |
|
for (uint256 i = 1; i < a.length; ++i) { |
|
assertTrue(a[i - 1] <= a[i]); |
|
} |
|
} |
|
} |
|
|
|
function testSortReversed() public { |
|
unchecked { |
|
uint256[] memory a = new uint256[](100); |
|
for (uint256 i; i < a.length; ++i) { |
|
a[i] = 999 - i; |
|
} |
|
BubbleSort.bubbleSort(a, a.length); |
|
for (uint256 i = 1; i < a.length; ++i) { |
|
assertTrue(a[i - 1] <= a[i]); |
|
} |
|
} |
|
} |
|
} |