Packing.test.js 1.0 KB

123456789101112131415161718192021222324252627
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { generators } = require('../helpers/random');
  5. async function fixture() {
  6. return { mock: await ethers.deployContract('$Packing') };
  7. }
  8. describe('Packing', function () {
  9. beforeEach(async function () {
  10. Object.assign(this, await loadFixture(fixture));
  11. });
  12. it('Uint128x2', async function () {
  13. const first = generators.uint256() % 2n ** 128n;
  14. const second = generators.uint256() % 2n ** 128n;
  15. const packed = ethers.hexlify(ethers.toBeArray((first << 128n) | second));
  16. expect(await this.mock.$asUint128x2(packed)).to.equal(packed);
  17. expect(await this.mock.$asBytes32(packed)).to.equal(packed);
  18. expect(await this.mock.$pack(first, second)).to.equal(packed);
  19. expect(await this.mock.$split(packed)).to.deep.equal([first, second]);
  20. expect(await this.mock.$first(packed)).to.equal(first);
  21. expect(await this.mock.$second(packed)).to.equal(second);
  22. });
  23. });