Skip to content

test_swapn_valid_immediates()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py::test_swapn_valid_immediates@ffe48572.

Generate fixtures for these test cases for Bogota with:

fill -v tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py::test_swapn_valid_immediates --fork Bogota

Test SWAPN with valid immediate values (0-90 and 128-255).

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py
@EIPChecklist.Opcode.Test.DataPortion.AllZeros()
@EIPChecklist.Opcode.Test.DataPortion.MaxValue()
@pytest.mark.parametrize(
    "immediate",
    [0, 45, 90, 128, 200, 255],
    ids=lambda x: f"swapn_imm_{x}",
)
def test_swapn_valid_immediates(
    immediate: int,
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """Test SWAPN with valid immediate values (0-90 and 128-255)."""
    sender = pre.fund_eoa()

    # Decode the immediate to get the stack index
    # SWAPN with decoded value n swaps position 1 with position (n+1)
    stack_index = decode_single(immediate)
    stack_height = stack_index + 1
    top_value = 0xAAAA
    swap_target_value = 0xBBBB

    # Build stack
    code = Bytecode()

    # First push ends up at position (stack_index+1) from top
    code += Op.PUSH2(swap_target_value)

    # Pushes in-between
    for i in range(1, stack_height - 1):
        code += Op.PUSH2(0x1000 + i)

    # Last push ends up at top
    code += Op.PUSH2(top_value)

    # Pass immediate as bytes (raw immediate byte for testing)
    code += Op.SWAPN[immediate.to_bytes(1, "big")]

    # Store the new top value
    code += Op.PUSH1(0) + Op.SSTORE
    code += Op.STOP

    contract_address = pre.deploy_contract(code=code)

    tx = Transaction(to=contract_address, sender=sender)

    post = {
        contract_address: Account(
            storage={
                0: swap_target_value,  # Top now has the swapped value
            }
        )
    }

    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 6 parametrized test cases across 2 forks.