@Test
  public void xortest4() {
    final RoaringBitmap rb = new RoaringBitmap();
    final RoaringBitmap rb2 = new RoaringBitmap();

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

    // check or against an empty bitmap
    final RoaringBitmap xorresult = RoaringBitmap.xor(rb, rb2);
    final RoaringBitmap off = RoaringBitmap.or(rb2, rb);
    Assert.assertTrue(xorresult.equals(off));

    Assert.assertEquals(rb2card, xorresult.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 RoaringBitmap xorresult2 = RoaringBitmap.xor(rb, rb2);
    Assert.assertEquals(rb2card, xorresult.getCardinality());

    Assert.assertEquals(rb2.getCardinality() + rb.getCardinality(), xorresult2.getCardinality());
    rb.xor(rb2);
    Assert.assertTrue(xorresult2.equals(rb));
  }
  @Test
  public void XORtest() {
    final RoaringBitmap rr = new RoaringBitmap();
    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 RoaringBitmap rr2 = new RoaringBitmap();
    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 RoaringBitmap correct = RoaringBitmap.xor(rr, rr2);
    rr.xor(rr2);
    Assert.assertTrue(correct.equals(rr));
  }
  @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 RoaringBitmap rb = new RoaringBitmap();
        // 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 RoaringBitmap rb2 = new RoaringBitmap();

        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(RoaringBitmap.and(rb, rb2).getCardinality(), N / offset);
        Assert.assertEquals(RoaringBitmap.or(rb, rb2).getCardinality(), 2 * N - N / offset);
        Assert.assertEquals(RoaringBitmap.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 RoaringBitmap rb1 = new RoaringBitmap();
      for (int x = 0; x <= N; ++x) {
        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 RoaringBitmap rb2 = new RoaringBitmap();
        for (int x = 0; x <= N; ++x) {
          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, RoaringBitmap.and(rb1, rb2))) throw new RuntimeException("bug and");
        {
          final RoaringBitmap t = rb1.clone();
          t.and(rb2);
          if (!equals(clonebs1, t)) throw new RuntimeException("bug inplace and");
          if (!t.equals(RoaringBitmap.and(rb1, rb2))) {
            System.out.println(
                t.highlowcontainer.getContainerAtIndex(0).getClass().getCanonicalName());
            System.out.println(
                RoaringBitmap.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, RoaringBitmap.or(rb1, rb2))) throw new RuntimeException("bug or");
        {
          final RoaringBitmap t = rb1.clone();
          t.or(rb2);
          if (!equals(clonebs1, t)) throw new RuntimeException("bug or");
          if (!t.equals(RoaringBitmap.or(rb1, rb2))) throw new RuntimeException("bug or");
          if (!t.toString().equals(RoaringBitmap.or(rb1, rb2).toString()))
            throw new RuntimeException("bug or");
        }
        // testing XOR
        clonebs1 = (BitSet) bs1.clone();
        clonebs1.xor(bs2);
        if (!equals(clonebs1, RoaringBitmap.xor(rb1, rb2))) {
          throw new RuntimeException("bug xor");
        }
        {
          final RoaringBitmap t = rb1.clone();
          t.xor(rb2);
          if (!equals(clonebs1, t)) throw new RuntimeException("bug xor");
          if (!t.equals(RoaringBitmap.xor(rb1, rb2))) throw new RuntimeException("bug xor");
        }
        // testing NOTAND
        clonebs1 = (BitSet) bs1.clone();
        clonebs1.andNot(bs2);
        if (!equals(clonebs1, RoaringBitmap.andNot(rb1, rb2))) {
          throw new RuntimeException("bug andnot");
        }
        clonebs1 = (BitSet) bs2.clone();
        clonebs1.andNot(bs1);
        if (!equals(clonebs1, RoaringBitmap.andNot(rb2, rb1))) {
          throw new RuntimeException("bug andnot");
        }
        {
          final RoaringBitmap t = rb2.clone();
          t.andNot(rb1);
          if (!equals(clonebs1, t)) {
            throw new RuntimeException("bug inplace andnot");
          }
          final RoaringBitmap g = RoaringBitmap.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, RoaringBitmap.andNot(rb1, rb2))) {
          throw new RuntimeException("bug andnot");
        }
        {
          final RoaringBitmap t = rb1.clone();
          t.andNot(rb2);
          if (!equals(clonebs1, t)) {
            throw new RuntimeException("bug andnot");
          }
          final RoaringBitmap g = RoaringBitmap.andNot(rb1, rb2);
          if (!equals(clonebs1, g)) {
            throw new RuntimeException("bug andnot");
          }
          if (!t.equals(g)) throw new RuntimeException("bug");
        }
      }
    }
  }
  @Test
  public void xortest1() {
    final HashSet<Integer> V1 = new HashSet<Integer>();
    final HashSet<Integer> V2 = new HashSet<Integer>();

    final RoaringBitmap rr = new RoaringBitmap();
    final RoaringBitmap rr2 = new RoaringBitmap();
    // For the first 65536: rr2 has a bitmap container, and rr has
    // an array container.
    // We will check the union between a BitmapCintainer and an
    // arrayContainer
    for (int k = 0; k < 4000; ++k) {
      rr2.add(k);
      if (k < 3500) V1.add(new Integer(k));
    }
    for (int k = 3500; k < 4500; ++k) {
      rr.add(k);
    }
    for (int k = 4000; k < 65000; ++k) {
      rr2.add(k);
      if (k >= 4500) V1.add(new Integer(k));
    }

    // In the second node of each roaring bitmap, we have two bitmap
    // containers.
    // So, we will check the union between two BitmapContainers
    for (int k = 65536; k < 65536 + 30000; ++k) {
      rr.add(k);
    }

    for (int k = 65536; k < 65536 + 50000; ++k) {
      rr2.add(k);
      if (k >= 65536 + 30000) V1.add(new Integer(k));
    }

    // In the 3rd node of each Roaring Bitmap, we have an
    // ArrayContainer. So, we will try the union between two
    // ArrayContainers.
    for (int k = 4 * 65535; k < 4 * 65535 + 1000; ++k) {
      rr.add(k);
      if (k >= 4 * 65535 + 800) V1.add(new Integer(k));
    }

    for (int k = 4 * 65535; k < 4 * 65535 + 800; ++k) {
      rr2.add(k);
    }

    // For the rest, we will check if the union will take them in
    // the result
    for (int k = 6 * 65535; k < 6 * 65535 + 1000; ++k) {
      rr.add(k);
      V1.add(new Integer(k));
    }

    for (int k = 7 * 65535; k < 7 * 65535 + 2000; ++k) {
      rr2.add(k);
      V1.add(new Integer(k));
    }

    final RoaringBitmap rrxor = RoaringBitmap.xor(rr, rr2);
    boolean valide = true;

    // Si tous les elements de rror sont dans V1 et que tous les
    // elements de
    // V1 sont dans rror(V2)
    // alors V1 == rror
    final Object[] tab = V1.toArray();
    final Vector<Integer> vector = new Vector<Integer>();
    for (int i = 0; i < tab.length; i++) vector.add((Integer) tab[i]);

    for (final int i : rrxor.toArray()) {
      if (!vector.contains(new Integer(i))) {
        valide = false;
      }
      V2.add(new Integer(i));
    }
    for (int i = 0; i < V1.size(); i++)
      if (!V2.contains(vector.elementAt(i))) {
        valide = false;
      }

    Assert.assertEquals(valide, true);
  }