@Test
  public void andtest4() {
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();
    final MutableRoaringBitmap rb2 = new MutableRoaringBitmap();

    for (int i = 0; i < 200000; i += 4) rb2.add(i);
    for (int i = 200000; i < 400000; i += 14) rb2.add(i);

    // check or against an empty bitmap
    final MutableRoaringBitmap andresult = MutableRoaringBitmap.and(rb, rb2);
    final MutableRoaringBitmap off = MutableRoaringBitmap.and(rb2, rb);
    Assert.assertTrue(andresult.equals(off));

    Assert.assertEquals(0, andresult.getCardinality());

    for (int i = 500000; i < 600000; i += 14) rb.add(i);
    for (int i = 200000; i < 400000; i += 3) rb2.add(i);
    // check or against an empty bitmap
    final MutableRoaringBitmap andresult2 = MutableRoaringBitmap.and(rb, rb2);
    Assert.assertEquals(0, andresult.getCardinality());

    Assert.assertEquals(0, andresult2.getCardinality());
    for (int i = 0; i < 200000; i += 4) rb.add(i);
    for (int i = 200000; i < 400000; i += 14) rb.add(i);
    Assert.assertEquals(0, andresult.getCardinality());
    final MutableRoaringBitmap rc = MutableRoaringBitmap.and(rb, rb2);
    rb.and(rb2);
    Assert.assertEquals(rc.getCardinality(), rb.getCardinality());
  }
  @Test
  public void ANDtest() {
    final MutableRoaringBitmap rr = new MutableRoaringBitmap();
    for (int k = 4000; k < 4256; ++k) rr.add(k);
    for (int k = 65536; k < 65536 + 4000; ++k) rr.add(k);
    for (int k = 3 * 65536; k < 3 * 65536 + 9000; ++k) rr.add(k);
    for (int k = 4 * 65535; k < 4 * 65535 + 7000; ++k) rr.add(k);
    for (int k = 6 * 65535; k < 6 * 65535 + 10000; ++k) rr.add(k);
    for (int k = 8 * 65535; k < 8 * 65535 + 1000; ++k) rr.add(k);
    for (int k = 9 * 65535; k < 9 * 65535 + 30000; ++k) rr.add(k);

    final MutableRoaringBitmap rr2 = new MutableRoaringBitmap();
    for (int k = 4000; k < 4256; ++k) {
      rr2.add(k);
    }
    for (int k = 65536; k < 65536 + 4000; ++k) {
      rr2.add(k);
    }
    for (int k = 3 * 65536 + 2000; k < 3 * 65536 + 6000; ++k) {
      rr2.add(k);
    }
    for (int k = 6 * 65535; k < 6 * 65535 + 1000; ++k) {
      rr2.add(k);
    }
    for (int k = 7 * 65535; k < 7 * 65535 + 1000; ++k) {
      rr2.add(k);
    }
    for (int k = 10 * 65535; k < 10 * 65535 + 5000; ++k) {
      rr2.add(k);
    }
    final MutableRoaringBitmap correct = MutableRoaringBitmap.and(rr, rr2);
    rr.and(rr2);
    Assert.assertTrue(correct.equals(rr));
  }
  @Test
  public void andtest() {
    final MutableRoaringBitmap rr = new MutableRoaringBitmap();
    for (int k = 0; k < 4000; ++k) {
      rr.add(k);
    }
    rr.add(100000);
    rr.add(110000);
    final MutableRoaringBitmap rr2 = new MutableRoaringBitmap();
    rr2.add(13);
    final MutableRoaringBitmap rrand = MutableRoaringBitmap.and(rr, rr2);
    int[] array = rrand.toArray();

    Assert.assertEquals(array.length, 1);
    Assert.assertEquals(array[0], 13);
    rr.and(rr2);
    array = rr.toArray();
    Assert.assertEquals(array.length, 1);
    Assert.assertEquals(array[0], 13);
  }
  @Test
  public void flipTestBigA() {
    final int numCases = 1000;
    final BitSet bs = new BitSet();
    final Random r = new Random(3333);
    int checkTime = 2;
    MutableRoaringBitmap rb1 = new MutableRoaringBitmap(), rb2 = null; // alternate
    // between
    // them

    for (int i = 0; i < numCases; ++i) {
      final int start = r.nextInt(65536 * 20);
      int end = r.nextInt(65536 * 20);
      if (r.nextDouble() < 0.1) end = start + r.nextInt(100);

      if ((i & 1) == 0) {
        rb2 = MutableRoaringBitmap.flip(rb1, start, end);
        // tweak the other, catch bad sharing
        rb1.flip(r.nextInt(65536 * 20), r.nextInt(65536 * 20));
      } else {
        rb1 = MutableRoaringBitmap.flip(rb2, start, end);
        rb2.flip(r.nextInt(65536 * 20), r.nextInt(65536 * 20));
      }

      if (start < end) bs.flip(start, end); // throws exception
      // otherwise
      // insert some more ANDs to keep things sparser
      if (r.nextDouble() < 0.2 && (i & 1) == 0) {
        final MutableRoaringBitmap mask = new MutableRoaringBitmap();
        final BitSet mask1 = new BitSet();
        final int startM = r.nextInt(65536 * 20);
        final int endM = startM + 100000;
        mask.flip(startM, endM);
        mask1.flip(startM, endM);
        mask.flip(0, 65536 * 20 + 100000);
        mask1.flip(0, 65536 * 20 + 100000);
        rb2.and(mask);
        bs.and(mask1);
      }

      if (i > checkTime) {
        System.out.println("check after " + i + ", card = " + rb2.getCardinality());
        final MutableRoaringBitmap rb = (i & 1) == 0 ? rb2 : rb1;
        final boolean status = equals(bs, rb);
        Assert.assertTrue(status);
        checkTime *= 1.5;
      }
    }
  }
  @Test
  public void andtest3() {
    final int[] arrayand = new int[11256];
    int pos = 0;
    final MutableRoaringBitmap rr = new MutableRoaringBitmap();
    for (int k = 4000; k < 4256; ++k) rr.add(k);
    for (int k = 65536; k < 65536 + 4000; ++k) rr.add(k);
    for (int k = 3 * 65536; k < 3 * 65536 + 1000; ++k) rr.add(k);
    for (int k = 3 * 65536 + 1000; k < 3 * 65536 + 7000; ++k) rr.add(k);
    for (int k = 3 * 65536 + 7000; k < 3 * 65536 + 9000; ++k) rr.add(k);
    for (int k = 4 * 65536; k < 4 * 65536 + 7000; ++k) rr.add(k);
    for (int k = 6 * 65536; k < 6 * 65536 + 10000; ++k) rr.add(k);
    for (int k = 8 * 65536; k < 8 * 65536 + 1000; ++k) rr.add(k);
    for (int k = 9 * 65536; k < 9 * 65536 + 30000; ++k) rr.add(k);

    final MutableRoaringBitmap rr2 = new MutableRoaringBitmap();
    for (int k = 4000; k < 4256; ++k) {
      rr2.add(k);
      arrayand[pos++] = k;
    }
    for (int k = 65536; k < 65536 + 4000; ++k) {
      rr2.add(k);
      arrayand[pos++] = k;
    }
    for (int k = 3 * 65536 + 1000; k < 3 * 65536 + 7000; ++k) {
      rr2.add(k);
      arrayand[pos++] = k;
    }
    for (int k = 6 * 65536; k < 6 * 65536 + 1000; ++k) {
      rr2.add(k);
      arrayand[pos++] = k;
    }
    for (int k = 7 * 65536; k < 7 * 65536 + 1000; ++k) {
      rr2.add(k);
    }
    for (int k = 10 * 65536; k < 10 * 65536 + 5000; ++k) {
      rr2.add(k);
    }

    final MutableRoaringBitmap rrand = MutableRoaringBitmap.and(rr, rr2);

    final int[] arrayres = rrand.toArray();

    for (int i = 0; i < arrayres.length; i++)
      if (arrayres[i] != arrayand[i]) System.out.println(arrayres[i]);

    Assert.assertTrue(Arrays.equals(arrayand, arrayres));
  }
  /** Test massive and. */
  @Test
  public void testMassiveAnd() {
    System.out.println("testing massive logical and");
    MutableRoaringBitmap[] ewah = new MutableRoaringBitmap[1024];
    for (int k = 0; k < ewah.length; ++k) ewah[k] = new MutableRoaringBitmap();
    int howmany = 1000000;
    for (int k = 0; k < howmany; ++k) {
      ewah[Math.abs(k + 2 * k * k) % ewah.length].add(k);
    }
    for (int k = 3; k < ewah.length; k += 3) ewah[k].flip(13, howmany / 2);
    for (int N = 2; N < ewah.length; ++N) {
      MutableRoaringBitmap answer = ewah[0];
      for (int k = 1; k < N; ++k) answer = MutableRoaringBitmap.and(answer, ewah[k]);

      MutableRoaringBitmap answer2 = BufferFastAggregation.and(Arrays.copyOf(ewah, N));
      Assert.assertTrue(answer.equals(answer2));
    }
  }
  @Test
  public void flipTestBig() {
    final int numCases = 1000;
    System.out.println("flipTestBig for " + numCases + " tests");
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();
    final BitSet bs = new BitSet();
    final Random r = new Random(3333);
    int checkTime = 2;

    for (int i = 0; i < numCases; ++i) {
      final int start = r.nextInt(65536 * 20);
      int end = r.nextInt(65536 * 20);
      if (r.nextDouble() < 0.1) end = start + r.nextInt(100);
      rb.flip(start, end);
      if (start < end) bs.flip(start, end); // throws exception
      // otherwise
      // insert some more ANDs to keep things sparser
      if (r.nextDouble() < 0.2) {
        final MutableRoaringBitmap mask = new MutableRoaringBitmap();
        final BitSet mask1 = new BitSet();
        final int startM = r.nextInt(65536 * 20);
        final int endM = startM + 100000;
        mask.flip(startM, endM);
        mask1.flip(startM, endM);
        mask.flip(0, 65536 * 20 + 100000);
        mask1.flip(0, 65536 * 20 + 100000);
        rb.and(mask);
        bs.and(mask1);
      }
      // see if we can detect incorrectly shared containers
      if (r.nextDouble() < 0.1) {
        final MutableRoaringBitmap irrelevant = MutableRoaringBitmap.flip(rb, 10, 100000);
        irrelevant.flip(5, 200000);
        irrelevant.flip(190000, 260000);
      }
      if (i > checkTime) {
        Assert.assertTrue(equals(bs, rb));
        checkTime *= 1.5;
      }
    }
  }
  @Test
  public void clearTest() {
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();
    for (int i = 0; i < 200000; i += 7)
      // dense
      rb.add(i);
    for (int i = 200000; i < 400000; i += 177)
      // sparse
      rb.add(i);

    final MutableRoaringBitmap rb2 = new MutableRoaringBitmap();
    final MutableRoaringBitmap rb3 = new MutableRoaringBitmap();
    for (int i = 0; i < 200000; i += 4) rb2.add(i);
    for (int i = 200000; i < 400000; i += 14) rb2.add(i);

    rb.clear();
    Assert.assertEquals(0, rb.getCardinality());
    Assert.assertTrue(0 != rb2.getCardinality());

    rb.add(4);
    rb3.add(4);
    final MutableRoaringBitmap andresult = MutableRoaringBitmap.and(rb, rb2);
    final MutableRoaringBitmap orresult = MutableRoaringBitmap.or(rb, rb2);

    Assert.assertEquals(1, andresult.getCardinality());
    Assert.assertEquals(rb2.getCardinality(), orresult.getCardinality());

    for (int i = 0; i < 200000; i += 4) {
      rb.add(i);
      rb3.add(i);
    }
    for (int i = 200000; i < 400000; i += 114) {
      rb.add(i);
      rb3.add(i);
    }

    final int[] arrayrr = rb.toArray();
    final int[] arrayrr3 = rb3.toArray();

    Assert.assertTrue(Arrays.equals(arrayrr, arrayrr3));
  }
  @Test
  public void cardinalityTest() {
    final int N = 1024;
    for (int gap = 7; gap < 100000; gap *= 10) {
      for (int offset = 2; offset <= 1024; offset *= 2) {
        final MutableRoaringBitmap rb = new MutableRoaringBitmap();
        // check the add of new values
        for (int k = 0; k < N; k++) {
          rb.add(k * gap);
          Assert.assertEquals(rb.getCardinality(), k + 1);
        }
        Assert.assertEquals(rb.getCardinality(), N);
        // check the add of existing values
        for (int k = 0; k < N; k++) {
          rb.add(k * gap);
          Assert.assertEquals(rb.getCardinality(), N);
        }

        final MutableRoaringBitmap rb2 = new MutableRoaringBitmap();

        for (int k = 0; k < N; k++) {
          rb2.add(k * gap * offset);
          Assert.assertEquals(rb2.getCardinality(), k + 1);
        }

        Assert.assertEquals(rb2.getCardinality(), N);

        for (int k = 0; k < N; k++) {
          rb2.add(k * gap * offset);
          Assert.assertEquals(rb2.getCardinality(), N);
        }
        Assert.assertEquals(MutableRoaringBitmap.and(rb, rb2).getCardinality(), N / offset);
        Assert.assertEquals(MutableRoaringBitmap.or(rb, rb2).getCardinality(), 2 * N - N / offset);
        Assert.assertEquals(
            MutableRoaringBitmap.xor(rb, rb2).getCardinality(), 2 * N - 2 * N / offset);
      }
    }
  }
  public void rTest(final int N) {
    System.out.println("rtest N=" + N);
    for (int gap = 1; gap <= 65536; gap *= 2) {
      final BitSet bs1 = new BitSet();
      final MutableRoaringBitmap rb1 = new MutableRoaringBitmap();
      for (int x = 0; x <= N; x += gap) {
        bs1.set(x);
        rb1.add(x);
      }
      if (bs1.cardinality() != rb1.getCardinality()) throw new RuntimeException("different card");
      if (!equals(bs1, rb1)) throw new RuntimeException("basic  bug");
      for (int offset = 1; offset <= gap; offset *= 2) {
        final BitSet bs2 = new BitSet();
        final MutableRoaringBitmap rb2 = new MutableRoaringBitmap();
        for (int x = 0; x <= N; x += gap) {
          bs2.set(x + offset);
          rb2.add(x + offset);
        }
        if (bs2.cardinality() != rb2.getCardinality()) throw new RuntimeException("different card");
        if (!equals(bs2, rb2)) throw new RuntimeException("basic  bug");

        BitSet clonebs1;
        // testing AND
        clonebs1 = (BitSet) bs1.clone();
        clonebs1.and(bs2);
        if (!equals(clonebs1, MutableRoaringBitmap.and(rb1, rb2)))
          throw new RuntimeException("bug and");
        {
          final MutableRoaringBitmap t = rb1.clone();
          t.and(rb2);
          if (!equals(clonebs1, t)) throw new RuntimeException("bug inplace and");
          if (!t.equals(MutableRoaringBitmap.and(rb1, rb2))) {
            System.out.println(
                t.highLowContainer.getContainerAtIndex(0).getClass().getCanonicalName());
            System.out.println(
                MutableRoaringBitmap.and(rb1, rb2)
                    .highLowContainer
                    .getContainerAtIndex(0)
                    .getClass()
                    .getCanonicalName());

            throw new RuntimeException("bug inplace and");
          }
        }

        // testing OR
        clonebs1 = (BitSet) bs1.clone();
        clonebs1.or(bs2);

        if (!equals(clonebs1, MutableRoaringBitmap.or(rb1, rb2)))
          throw new RuntimeException("bug or");
        {
          final MutableRoaringBitmap t = rb1.clone();
          t.or(rb2);
          if (!equals(clonebs1, t)) throw new RuntimeException("bug or");
          if (!t.equals(MutableRoaringBitmap.or(rb1, rb2))) throw new RuntimeException("bug or");
          if (!t.toString().equals(MutableRoaringBitmap.or(rb1, rb2).toString()))
            throw new RuntimeException("bug or");
        }
        // testing XOR
        clonebs1 = (BitSet) bs1.clone();
        clonebs1.xor(bs2);
        if (!equals(clonebs1, MutableRoaringBitmap.xor(rb1, rb2))) {
          throw new RuntimeException("bug xor");
        }
        {
          final MutableRoaringBitmap t = rb1.clone();
          t.xor(rb2);
          if (!equals(clonebs1, t)) throw new RuntimeException("bug xor");

          if (!t.equals(MutableRoaringBitmap.xor(rb1, rb2))) {
            System.out.println(t);
            System.out.println(MutableRoaringBitmap.xor(rb1, rb2));
            System.out.println(
                Arrays.equals(t.toArray(), MutableRoaringBitmap.xor(rb1, rb2).toArray()));
            throw new RuntimeException("bug xor");
          }
        }
        // testing NOTAND
        clonebs1 = (BitSet) bs1.clone();
        clonebs1.andNot(bs2);
        if (!equals(clonebs1, MutableRoaringBitmap.andNot(rb1, rb2))) {
          throw new RuntimeException("bug andnot");
        }
        clonebs1 = (BitSet) bs2.clone();
        clonebs1.andNot(bs1);
        if (!equals(clonebs1, MutableRoaringBitmap.andNot(rb2, rb1))) {
          throw new RuntimeException("bug andnot");
        }
        {
          final MutableRoaringBitmap t = rb2.clone();
          t.andNot(rb1);
          if (!equals(clonebs1, t)) {
            throw new RuntimeException("bug inplace andnot");
          }
          final MutableRoaringBitmap g = MutableRoaringBitmap.andNot(rb2, rb1);
          if (!equals(clonebs1, g)) {
            throw new RuntimeException("bug andnot");
          }
          if (!t.equals(g)) throw new RuntimeException("bug");
        }
        clonebs1 = (BitSet) bs1.clone();
        clonebs1.andNot(bs2);
        if (!equals(clonebs1, MutableRoaringBitmap.andNot(rb1, rb2))) {
          throw new RuntimeException("bug andnot");
        }
        {
          final MutableRoaringBitmap t = rb1.clone();
          t.andNot(rb2);
          if (!equals(clonebs1, t)) {
            throw new RuntimeException("bug andnot");
          }
          final MutableRoaringBitmap g = MutableRoaringBitmap.andNot(rb1, rb2);
          if (!equals(clonebs1, g)) {
            throw new RuntimeException("bug andnot");
          }
          if (!t.equals(g)) throw new RuntimeException("bug");
        }
      }
    }
  }