Skip to content

Commit

Permalink
chore(avm-simulator): reduce boilerplate in AVM memory types (#4542)
Browse files Browse the repository at this point in the history
Uses a mixin factory to create the unsigned integral types. This also has the benefit that the mod, bitmask, etc, are created just once per type, and not once per instance.
  • Loading branch information
fcarreiro authored Feb 12, 2024
1 parent e1da2fd commit da2f5ed
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 208 deletions.
182 changes: 92 additions & 90 deletions yarn-project/simulator/src/avm/avm_memory_types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,96 +39,98 @@ describe('TaggedMemory', () => {

type IntegralClass = typeof Uint8 | typeof Uint16 | typeof Uint32 | typeof Uint64 | typeof Uint128;
describe.each([Uint8, Uint16, Uint32, Uint64, Uint128])('Integral Types', (clsValue: IntegralClass) => {
it(`Should construct a new ${clsValue.name} from a number`, () => {
const x = new clsValue(5);
expect(x.toBigInt()).toStrictEqual(5n);
});

it(`Should construct a new ${clsValue.name} from a bigint`, () => {
const x = new clsValue(5n);
expect(x.toBigInt()).toStrictEqual(5n);
});

it(`Should build a new ${clsValue.name}`, () => {
const x = new clsValue(5);
const newX = x.build(10n);
expect(newX).toStrictEqual(new clsValue(10n));
});

it(`Should add two ${clsValue.name} correctly`, () => {
const a = new clsValue(5);
const b = new clsValue(10);
const result = a.add(b);
expect(result).toStrictEqual(new clsValue(15n));
});

it(`Should subtract two ${clsValue.name} correctly`, () => {
const a = new clsValue(10);
const b = new clsValue(5);
const result = a.sub(b);
expect(result).toStrictEqual(new clsValue(5n));
});

it(`Should multiply two ${clsValue.name} correctly`, () => {
const a = new clsValue(5);
const b = new clsValue(10);
const result = a.mul(b);
expect(result).toStrictEqual(new clsValue(50n));
});

it(`Should divide two ${clsValue.name} correctly`, () => {
const a = new clsValue(10);
const b = new clsValue(5);
const result = a.div(b);
expect(result).toStrictEqual(new clsValue(2n));
});

it('Should shift right ${clsValue.name} correctly', () => {
const uintA = new clsValue(10);
const result = uintA.shr(new clsValue(1n));
expect(result).toEqual(new clsValue(5n));
});

it('Should shift left ${clsValue.name} correctly', () => {
const uintA = new clsValue(10);
const result = uintA.shl(new clsValue(1n));
expect(result).toEqual(new clsValue(20n));
});

it('Should and two ${clsValue.name} correctly', () => {
const uintA = new clsValue(10);
const uintB = new clsValue(5);
const result = uintA.and(uintB);
expect(result).toEqual(new clsValue(0n));
});

it('Should or two ${clsValue.name} correctly', () => {
const uintA = new clsValue(10);
const uintB = new clsValue(5);
const result = uintA.or(uintB);
expect(result).toEqual(new clsValue(15n));
});

it('Should xor two ${clsValue.name} correctly', () => {
const uintA = new clsValue(10);
const uintB = new clsValue(5);
const result = uintA.xor(uintB);
expect(result).toEqual(new clsValue(15n));
});

it(`Should check equality of two ${clsValue.name} correctly`, () => {
const a = new clsValue(5);
const b = new clsValue(5);
const c = new clsValue(10);
expect(a.equals(b)).toBe(true);
expect(a.equals(c)).toBe(false);
});

it(`Should check if one ${clsValue.name} is less than another correctly`, () => {
const a = new clsValue(5);
const b = new clsValue(10);
expect(a.lt(b)).toBe(true);
expect(b.lt(a)).toBe(false);
describe(`${clsValue.name}`, () => {
it(`Should construct a new ${clsValue.name} from a number`, () => {
const x = new clsValue(5);
expect(x.toBigInt()).toStrictEqual(5n);
});

it(`Should construct a new ${clsValue.name} from a bigint`, () => {
const x = new clsValue(5n);
expect(x.toBigInt()).toStrictEqual(5n);
});

it(`Should build a new ${clsValue.name}`, () => {
const x = new clsValue(5);
const newX = x.build(10n);
expect(newX).toStrictEqual(new clsValue(10n));
});

it(`Should add two ${clsValue.name} correctly`, () => {
const a = new clsValue(5);
const b = new clsValue(10);
const result = a.add(b);
expect(result).toStrictEqual(new clsValue(15n));
});

it(`Should subtract two ${clsValue.name} correctly`, () => {
const a = new clsValue(10);
const b = new clsValue(5);
const result = a.sub(b);
expect(result).toStrictEqual(new clsValue(5n));
});

it(`Should multiply two ${clsValue.name} correctly`, () => {
const a = new clsValue(5);
const b = new clsValue(10);
const result = a.mul(b);
expect(result).toStrictEqual(new clsValue(50n));
});

it(`Should divide two ${clsValue.name} correctly`, () => {
const a = new clsValue(10);
const b = new clsValue(5);
const result = a.div(b);
expect(result).toStrictEqual(new clsValue(2n));
});

it(`Should shift right ${clsValue.name} correctly`, () => {
const uintA = new clsValue(10);
const result = uintA.shr(new clsValue(1n));
expect(result).toEqual(new clsValue(5n));
});

it(`Should shift left ${clsValue.name} correctly`, () => {
const uintA = new clsValue(10);
const result = uintA.shl(new clsValue(1n));
expect(result).toEqual(new clsValue(20n));
});

it(`Should and two ${clsValue.name} correctly`, () => {
const uintA = new clsValue(10);
const uintB = new clsValue(5);
const result = uintA.and(uintB);
expect(result).toEqual(new clsValue(0n));
});

it(`Should or two ${clsValue.name} correctly`, () => {
const uintA = new clsValue(10);
const uintB = new clsValue(5);
const result = uintA.or(uintB);
expect(result).toEqual(new clsValue(15n));
});

it(`Should xor two ${clsValue.name} correctly`, () => {
const uintA = new clsValue(10);
const uintB = new clsValue(5);
const result = uintA.xor(uintB);
expect(result).toEqual(new clsValue(15n));
});

it(`Should check equality of two ${clsValue.name} correctly`, () => {
const a = new clsValue(5);
const b = new clsValue(5);
const c = new clsValue(10);
expect(a.equals(b)).toBe(true);
expect(a.equals(c)).toBe(false);
});

it(`Should check if one ${clsValue.name} is less than another correctly`, () => {
const a = new clsValue(5);
const b = new clsValue(10);
expect(a.lt(b)).toBe(true);
expect(b.lt(a)).toBe(false);
});
});
});

Expand Down
Loading

0 comments on commit da2f5ed

Please sign in to comment.