Skip to content

test_swapn_valid_immediates()

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

Generate fixtures for these test cases for Amsterdam with:

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

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

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py
 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
@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 1 fork.