Skip to content

test_clz_opcode_scenarios()

Documentation for tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py::test_clz_opcode_scenarios@892e6d1e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py::test_clz_opcode_scenarios --fork Amsterdam

Test CLZ opcode functionality.

Cases: - Format 0xb000...111: leading zeros followed by ones (2**256 - 1 >> bits) - Format 0xb010...000: single bit set at position (1 << bits)

Test coverage: - Leading zeros pattern: 0b000...111 (0 to 256 leading zeros) - Single bit pattern: 0b010...000 (bit at each possible position) - Edge cases: CLZ(0) = 256, CLZ(2^256-1) = 0

Source code in tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
@pytest.mark.valid_from("Osaka")
@pytest.mark.parametrize(
    "test_id,value,expected_clz",
    clz_parameters(),
    ids=[
        f"{test_data[0]}-expected_clz_{test_data[2]}"
        for test_data in clz_parameters()
    ],
)
def test_clz_opcode_scenarios(
    state_test: StateTestFiller,
    pre: Alloc,
    test_id: str,
    value: int,
    expected_clz: int,
) -> None:
    """
    Test CLZ opcode functionality.

    Cases:
    - Format 0xb000...111: leading zeros followed by ones
       (2**256 - 1 >> bits)
    - Format 0xb010...000: single bit set at position (1 << bits)

    Test coverage:
    - Leading zeros pattern: 0b000...111 (0 to 256 leading zeros)
    - Single bit pattern: 0b010...000 (bit at each possible position)
    - Edge cases: CLZ(0) = 256, CLZ(2^256-1) = 0
    """
    sender = pre.fund_eoa()
    contract_address = pre.deploy_contract(
        code=Op.SSTORE(0, Op.CLZ(value)),
        storage={"0x00": "0xdeadbeef"},
    )
    tx = Transaction(
        to=contract_address,
        sender=sender,
        gas_limit=200_000,
    )
    post = {
        contract_address: Account(storage={"0x00": expected_clz}),
    }
    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 522 parametrized test cases across 2 forks.