@Test
  @SuppressWarnings("deprecation")
  public void testDeprecatedIteratorXor() {

    MutableRoaringBitmap rb1 = new MutableRoaringBitmap();
    MutableRoaringBitmap rb2 = new MutableRoaringBitmap();

    List<MutableRoaringBitmap> list = new ArrayList<>();
    list.add(rb1);
    list.add(rb2);

    rb1.add(200000L, 400000L); // two normal positive ranges
    rb2.add(300000L, 500000L); // full XOR is 200000 to 299999, 400000-4999999

    MutableRoaringBitmap result = ImmutableRoaringBitmap.xor(list.iterator(), 250000L, 450000L);
    MutableRoaringBitmap resultInt = ImmutableRoaringBitmap.xor(list.iterator(), 250000, 450000);

    Assert.assertTrue(result.equals(resultInt));
    Assert.assertEquals(100000, result.getCardinality());

    // empty ranges get empty result
    resultInt = ImmutableRoaringBitmap.xor(list.iterator(), 300000, 200000);
    result = ImmutableRoaringBitmap.xor(list.iterator(), 300000L, 200000L);
    Assert.assertTrue(result.equals(resultInt));
    Assert.assertEquals(0, resultInt.getCardinality());
  }
  @Test
  public void andnottest4A() {
    final MutableRoaringBitmap rb = new MutableRoaringBitmap();
    final MutableRoaringBitmap rb2 = new MutableRoaringBitmap();

    for (int i = 0; i < 200000; i++) {
      if (i % 9 < 6) {
        rb2.add(i);
      }
    }
    for (int i = 200000; i < 400000; i += 14) {
      rb2.add(i);
    }
    rb2.getCardinality();
    rb2.runOptimize();

    // check against an empty bitmap
    final MutableRoaringBitmap andNotresult = MutableRoaringBitmap.andNot(rb, rb2);
    final MutableRoaringBitmap off = MutableRoaringBitmap.andNot(rb2, rb);

    Assert.assertEquals(rb, andNotresult);
    Assert.assertEquals(rb2, off);
    rb2.andNot(rb);
    Assert.assertEquals(rb2, off);
  }
  @SuppressWarnings("resource")
  @Test
  public void testProperSerializationA() throws IOException {
    // denser, so we should have run containers
    final int SIZE = 500;
    final Random rand = new Random(0);
    for (int i = 0; i < SIZE; ++i) {
      MutableRoaringBitmap r = new MutableRoaringBitmap();
      for (int k = 0; k < 500000; ++k) {
        if (rand.nextDouble() < .9) {
          r.add(k);
        }
      }
      r.runOptimize();
      ByteBuffer b = ByteBuffer.allocate(r.serializedSizeInBytes());
      r.serialize(
          new DataOutputStream(
              new OutputStream() {
                ByteBuffer mBB;

                @Override
                public void close() {}

                @Override
                public void flush() {}

                OutputStream init(final ByteBuffer mbb) {
                  mBB = mbb;
                  return this;
                }

                @Override
                public void write(final byte[] b) {
                  mBB.put(b);
                }

                @Override
                public void write(final byte[] b, final int off, final int l) {
                  mBB.put(b, off, l);
                }

                @Override
                public void write(final int b) {
                  mBB.put((byte) b);
                }
              }.init(b)));
      b.flip();
      ImmutableRoaringBitmap irb = new ImmutableRoaringBitmap(b);
      Assert.assertTrue(irb.hashCode() == r.hashCode());
      Assert.assertTrue(irb.getCardinality() == r.getCardinality());
    }
  }
  @Test
  public void MappeableContainersAccessTestA() throws IOException {
    // run containers too
    MutableRoaringBitmap mr = new MutableRoaringBitmap();
    for (int k = 4000; k < 4256; ++k) {
      mr.add(k);
    }
    for (int k = (1 << 16); k < (1 << 16) + 4000; ++k) {
      mr.add(k);
    }
    for (int k = (1 << 18); k < (1 << 18) + 9000; ++k) {
      mr.add(k);
    }
    for (int k = (1 << 19); k < (1 << 19) + 7000; ++k) {
      mr.add(k);
    }
    for (int k = (1 << 20); k < (1 << 20) + 10000; ++k) {
      mr.add(k);
    }
    for (int k = (1 << 23); k < (1 << 23) + 1000; ++k) {
      mr.add(k);
    }
    for (int k = (1 << 24); k < (1 << 24) + 30000; ++k) {
      mr.add(k);
    }
    mr.runOptimize();
    ByteBuffer buffer = serializeRoaring(mr);
    buffer.rewind();
    ImmutableRoaringBitmap ir = new ImmutableRoaringBitmap(buffer);
    mr = ir.toMutableRoaringBitmap();
    Assert.assertTrue(
        (mr.getMappeableRoaringArray().getContainerAtIndex(0)) instanceof MappeableRunContainer);
    Assert.assertTrue(
        (mr.getMappeableRoaringArray().getContainerAtIndex(1)) instanceof MappeableRunContainer);
    Assert.assertTrue(
        (mr.getMappeableRoaringArray().getContainerAtIndex(2)) instanceof MappeableRunContainer);
    Assert.assertTrue(
        (mr.getMappeableRoaringArray().getContainerAtIndex(3)) instanceof MappeableRunContainer);
    Assert.assertTrue(
        (mr.getMappeableRoaringArray().getContainerAtIndex(4)) instanceof MappeableRunContainer);
    Assert.assertTrue(
        (mr.getMappeableRoaringArray().getContainerAtIndex(5)) instanceof MappeableRunContainer);
    Assert.assertTrue(
        (mr.getMappeableRoaringArray().getContainerAtIndex(6)) instanceof MappeableRunContainer);

    Assert.assertTrue(mr.getMappeableRoaringArray().getContainerAtIndex(0).getCardinality() == 256);
    Assert.assertTrue(
        mr.getMappeableRoaringArray().getContainerAtIndex(1).getCardinality() == 4000);
    Assert.assertTrue(
        mr.getMappeableRoaringArray().getContainerAtIndex(2).getCardinality() == 9000);
    Assert.assertTrue(
        mr.getMappeableRoaringArray().getContainerAtIndex(3).getCardinality() == 7000);
    Assert.assertTrue(
        mr.getMappeableRoaringArray().getContainerAtIndex(4).getCardinality() == 10000);
    Assert.assertTrue(
        mr.getMappeableRoaringArray().getContainerAtIndex(5).getCardinality() == 1000);
    Assert.assertTrue(
        mr.getMappeableRoaringArray().getContainerAtIndex(6).getCardinality() == 30000);

    MutableRoaringBitmap mr2 = new MutableRoaringBitmap();
    for (int k = 4000; k < 4256; ++k) {
      mr2.add(k);
    }
    for (int k = (1 << 16); k < (1 << 16) + 4000; ++k) {
      mr2.add(k);
    }
    for (int k = (1 << 18) + 2000; k < (1 << 18) + 8000; ++k) {
      mr2.add(k);
    }
    for (int k = (1 << 21); k < (1 << 21) + 1000; ++k) {
      mr2.add(k);
    }
    for (int k = (1 << 22); k < (1 << 22) + 2000; ++k) {
      mr2.add(k);
    }
    for (int k = (1 << 25); k < (1 << 25) + 5000; ++k) {
      mr2.add(k);
    }
    mr2.runOptimize();
    buffer = serializeRoaring(mr2);
    buffer.rewind();
    ImmutableRoaringBitmap ir2 = new ImmutableRoaringBitmap(buffer);
    mr2 = ir2.toMutableRoaringBitmap();
    Assert.assertTrue(
        (mr2.getMappeableRoaringArray().getContainerAtIndex(0)) instanceof MappeableRunContainer);
    Assert.assertTrue(
        (mr2.getMappeableRoaringArray().getContainerAtIndex(1)) instanceof MappeableRunContainer);
    Assert.assertTrue(
        (mr2.getMappeableRoaringArray().getContainerAtIndex(2)) instanceof MappeableRunContainer);
    Assert.assertTrue(
        (mr2.getMappeableRoaringArray().getContainerAtIndex(3)) instanceof MappeableRunContainer);
    Assert.assertTrue(
        (mr2.getMappeableRoaringArray().getContainerAtIndex(4)) instanceof MappeableRunContainer);
    Assert.assertTrue(
        (mr2.getMappeableRoaringArray().getContainerAtIndex(5)) instanceof MappeableRunContainer);

    Assert.assertTrue(
        mr2.getMappeableRoaringArray().getContainerAtIndex(0).getCardinality() == 256);
    Assert.assertTrue(
        mr2.getMappeableRoaringArray().getContainerAtIndex(1).getCardinality() == 4000);
    Assert.assertTrue(
        mr2.getMappeableRoaringArray().getContainerAtIndex(2).getCardinality() == 6000);
    Assert.assertTrue(
        mr2.getMappeableRoaringArray().getContainerAtIndex(3).getCardinality() == 1000);
    Assert.assertTrue(
        mr2.getMappeableRoaringArray().getContainerAtIndex(4).getCardinality() == 2000);
    Assert.assertTrue(
        mr2.getMappeableRoaringArray().getContainerAtIndex(5).getCardinality() == 5000);

    MutableRoaringBitmap mr3 = new MutableRoaringBitmap();
    buffer = serializeRoaring(mr3);
    buffer.rewind();
    ImmutableRoaringBitmap ir3 = new ImmutableRoaringBitmap(buffer);
    mr3 = ir3.toMutableRoaringBitmap();

    Assert.assertTrue(mr3.getCardinality() == 0);
  }