Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
LibCtPopTest:testCTPOPReference(uint256) (runs: 2048, μ: 3604, ~: 3605)
LibCtPopTest:testCTPOPShuffled(uint8,bytes32) (runs: 2048, μ: 149131, ~: 149131)
LibCtPopTest:testCTPOPUnshuffled(uint8) (runs: 2048, μ: 4146, ~: 4146)
LibCtPopTest:testCtPop0() (gas: 741)
LibCtPopTest:testCtPop1() (gas: 764)
LibCtPopTest:testCtPop256EdgeCase() (gas: 677)
LibCtPopTest:testCtPopReference(uint256) (runs: 2048, μ: 789, ~: 790)
LibCtPopTest:testCtPopShuffled(uint8,bytes32) (runs: 2048, μ: 144902, ~: 144902)
LibCtPopTest:testCtPopUnshuffled(uint8) (runs: 2048, μ: 989, ~: 989)
31 changes: 27 additions & 4 deletions test/src/lib/LibCtPop.ctpop.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
pragma solidity =0.8.25;

import {Test} from "forge-std/Test.sol";

import {LibCtPop} from "src/lib/LibCtPop.sol";

/// @title LibCtPopTest
Expand All @@ -13,7 +12,7 @@ import {LibCtPop} from "src/lib/LibCtPop.sol";
contract LibCtPopTest is Test {
/// We should be able to count the number of bits set when we simply set a
/// sequence of bits from the low bit to some mid bit.
function testCTPOPUnshuffled(uint8 n) external pure {
function testCtPopUnshuffled(uint8 n) external pure {
uint256 x = (1 << n) - 1;
uint256 ct = LibCtPop.ctpop(x);
assertEq(n, ct);
Expand All @@ -22,7 +21,7 @@ contract LibCtPopTest is Test {
}

/// The distribution of bits in the underlying `uint256` should not matter.
function testCTPOPShuffled(uint8 n, bytes32 rand) external pure {
function testCtPopShuffled(uint8 n, bytes32 rand) external pure {
uint256 x = (1 << n) - 1;
uint256 y = 0;

Expand All @@ -44,8 +43,32 @@ contract LibCtPopTest is Test {
assertEq(ct, ctSlow);
}

function testCTPOPReference(uint256 x) external pure {
function testCtPopReference(uint256 x) external pure {
uint256 ct = LibCtPop.ctpop(x);
uint256 ctSlow = LibCtPop.ctpopSlow(x);
assertEq(ct, ctSlow);
}

function testCtPop256EdgeCase() external pure {
uint256 x = type(uint256).max;
uint256 ct = LibCtPop.ctpop(x);
assertEq(256, ct);
uint256 ctSlow = LibCtPop.ctpopSlow(x);
assertEq(ct, ctSlow);
}

function testCtPop0() external pure {
uint256 x = 0;
uint256 ct = LibCtPop.ctpop(x);
assertEq(0, ct);
uint256 ctSlow = LibCtPop.ctpopSlow(x);
assertEq(ct, ctSlow);
}

function testCtPop1() external pure {
uint256 x = 1;
uint256 ct = LibCtPop.ctpop(x);
assertEq(1, ct);
uint256 ctSlow = LibCtPop.ctpopSlow(x);
assertEq(ct, ctSlow);
}
Expand Down