BitMap.test.js 4.79 KB
Newer Older
vicotor's avatar
vicotor committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 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 142 143 144 145
const { BN } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');

const BitMap = artifacts.require('BitMapMock');

contract('BitMap', function (accounts) {
  const keyA = new BN('7891');
  const keyB = new BN('451');
  const keyC = new BN('9592328');

  beforeEach(async function () {
    this.bitmap = await BitMap.new();
  });

  it('starts empty', async function () {
    expect(await this.bitmap.get(keyA)).to.equal(false);
    expect(await this.bitmap.get(keyB)).to.equal(false);
    expect(await this.bitmap.get(keyC)).to.equal(false);
  });

  describe('setTo', function () {
    it('set a key to true', async function () {
      await this.bitmap.setTo(keyA, true);
      expect(await this.bitmap.get(keyA)).to.equal(true);
      expect(await this.bitmap.get(keyB)).to.equal(false);
      expect(await this.bitmap.get(keyC)).to.equal(false);
    });

    it('set a key to false', async function () {
      await this.bitmap.setTo(keyA, true);
      await this.bitmap.setTo(keyA, false);
      expect(await this.bitmap.get(keyA)).to.equal(false);
      expect(await this.bitmap.get(keyB)).to.equal(false);
      expect(await this.bitmap.get(keyC)).to.equal(false);
    });

    it('set several consecutive keys', async function () {
      await this.bitmap.setTo(keyA.addn(0), true);
      await this.bitmap.setTo(keyA.addn(1), true);
      await this.bitmap.setTo(keyA.addn(2), true);
      await this.bitmap.setTo(keyA.addn(3), true);
      await this.bitmap.setTo(keyA.addn(4), true);
      await this.bitmap.setTo(keyA.addn(2), false);
      await this.bitmap.setTo(keyA.addn(4), false);
      expect(await this.bitmap.get(keyA.addn(0))).to.equal(true);
      expect(await this.bitmap.get(keyA.addn(1))).to.equal(true);
      expect(await this.bitmap.get(keyA.addn(2))).to.equal(false);
      expect(await this.bitmap.get(keyA.addn(3))).to.equal(true);
      expect(await this.bitmap.get(keyA.addn(4))).to.equal(false);
    });
  });

  describe('set', function () {
    it('adds a key', async function () {
      await this.bitmap.set(keyA);
      expect(await this.bitmap.get(keyA)).to.equal(true);
      expect(await this.bitmap.get(keyB)).to.equal(false);
      expect(await this.bitmap.get(keyC)).to.equal(false);
    });

    it('adds several keys', async function () {
      await this.bitmap.set(keyA);
      await this.bitmap.set(keyB);
      expect(await this.bitmap.get(keyA)).to.equal(true);
      expect(await this.bitmap.get(keyB)).to.equal(true);
      expect(await this.bitmap.get(keyC)).to.equal(false);
    });

    it('adds several consecutive keys', async function () {
      await this.bitmap.set(keyA.addn(0));
      await this.bitmap.set(keyA.addn(1));
      await this.bitmap.set(keyA.addn(3));
      expect(await this.bitmap.get(keyA.addn(0))).to.equal(true);
      expect(await this.bitmap.get(keyA.addn(1))).to.equal(true);
      expect(await this.bitmap.get(keyA.addn(2))).to.equal(false);
      expect(await this.bitmap.get(keyA.addn(3))).to.equal(true);
      expect(await this.bitmap.get(keyA.addn(4))).to.equal(false);
    });
  });

  describe('unset', function () {
    it('removes added keys', async function () {
      await this.bitmap.set(keyA);
      await this.bitmap.set(keyB);
      await this.bitmap.unset(keyA);
      expect(await this.bitmap.get(keyA)).to.equal(false);
      expect(await this.bitmap.get(keyB)).to.equal(true);
      expect(await this.bitmap.get(keyC)).to.equal(false);
    });

    it('removes consecutive added keys', async function () {
      await this.bitmap.set(keyA.addn(0));
      await this.bitmap.set(keyA.addn(1));
      await this.bitmap.set(keyA.addn(3));
      await this.bitmap.unset(keyA.addn(1));
      expect(await this.bitmap.get(keyA.addn(0))).to.equal(true);
      expect(await this.bitmap.get(keyA.addn(1))).to.equal(false);
      expect(await this.bitmap.get(keyA.addn(2))).to.equal(false);
      expect(await this.bitmap.get(keyA.addn(3))).to.equal(true);
      expect(await this.bitmap.get(keyA.addn(4))).to.equal(false);
    });

    it('adds and removes multiple keys', async function () {
      // []

      await this.bitmap.set(keyA);
      await this.bitmap.set(keyC);

      // [A, C]

      await this.bitmap.unset(keyA);
      await this.bitmap.unset(keyB);

      // [C]

      await this.bitmap.set(keyB);

      // [C, B]

      await this.bitmap.set(keyA);
      await this.bitmap.unset(keyC);

      // [A, B]

      await this.bitmap.set(keyA);
      await this.bitmap.set(keyB);

      // [A, B]

      await this.bitmap.set(keyC);
      await this.bitmap.unset(keyA);

      // [B, C]

      await this.bitmap.set(keyA);
      await this.bitmap.unset(keyB);

      // [A, C]

      expect(await this.bitmap.get(keyA)).to.equal(true);
      expect(await this.bitmap.get(keyB)).to.equal(false);
      expect(await this.bitmap.get(keyC)).to.equal(true);
    });
  });
});