import { describe, it, expect } from 'vitest';
import { mload } from './0x51_MLOAD.js';
describe('MLOAD (0x51)', () => {
it('loads 32 bytes from memory', () => {
const frame = createFrame();
for (let i = 0; i < 32; i++) {
frame.memory.set(i, i + 1);
}
frame.stack.push(0n);
expect(mload(frame)).toBeNull();
expect(frame.stack[0]).toBe(0x0102030405...n);
expect(frame.pc).toBe(1);
});
it('loads from uninitialized memory as zero', () => {
const frame = createFrame();
frame.stack.push(0n);
mload(frame);
expect(frame.stack[0]).toBe(0n);
expect(frame.memorySize).toBe(32);
});
it('expands memory to word boundary', () => {
const frame = createFrame();
frame.stack.push(1n); // Offset 1 -> bytes 1-32 = 2 words
mload(frame);
expect(frame.memorySize).toBe(64);
});
it('charges correct gas for expansion', () => {
const frame = createFrame({ gasRemaining: 1000n });
frame.stack.push(0n);
mload(frame);
// 3 base + memory expansion
expect(frame.gasRemaining).toBeLessThan(1000n);
});
it('returns StackUnderflow when empty', () => {
const frame = createFrame();
expect(mload(frame)).toEqual({ type: "StackUnderflow" });
});
it('returns OutOfGas when insufficient', () => {
const frame = createFrame({ gasRemaining: 2n });
frame.stack.push(0n);
expect(mload(frame)).toEqual({ type: "OutOfGas" });
});
});