import { describe, it, expect } from 'vitest';
import { smod } from './0x07_SMOD.js';
describe('SMOD (0x07)', () => {
const MIN_INT = 1n << 255n;
const MAX_UINT = (1n << 256n) - 1n;
const toSigned = (n: bigint) => n < 0n ? (1n << 256n) + n : n;
it('computes positive modulo', () => {
const frame = createFrame([10n, 3n]);
expect(smod(frame)).toBeNull();
expect(frame.stack).toEqual([1n]);
});
it('handles negative dividend', () => {
const frame = createFrame([toSigned(-10n), 3n]);
expect(smod(frame)).toBeNull();
expect(frame.stack).toEqual([toSigned(-1n)]);
});
it('handles negative modulus', () => {
const frame = createFrame([10n, toSigned(-3n)]);
expect(smod(frame)).toBeNull();
expect(frame.stack).toEqual([1n]);
});
it('handles both negative', () => {
const frame = createFrame([toSigned(-10n), toSigned(-3n)]);
expect(smod(frame)).toBeNull();
expect(frame.stack).toEqual([toSigned(-1n)]);
});
it('handles MIN_INT % -1', () => {
const frame = createFrame([MIN_INT, MAX_UINT]);
expect(smod(frame)).toBeNull();
expect(frame.stack).toEqual([0n]);
});
it('handles modulo by zero', () => {
const frame = createFrame([toSigned(-10n), 0n]);
expect(smod(frame)).toBeNull();
expect(frame.stack).toEqual([0n]);
});
it('result sign matches dividend', () => {
// -7 % 2 = -1 (not 1)
const frame = createFrame([toSigned(-7n), 2n]);
expect(smod(frame)).toBeNull();
expect(frame.stack).toEqual([toSigned(-1n)]);
});
});