@Test public void ortest4() { 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); final int rb2card = rb2.getCardinality(); // check or against an empty bitmap final MutableRoaringBitmap orresult = MutableRoaringBitmap.or(rb, rb2); final MutableRoaringBitmap off = MutableRoaringBitmap.or(rb2, rb); Assert.assertTrue(orresult.equals(off)); Assert.assertEquals(rb2card, orresult.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 orresult2 = MutableRoaringBitmap.or(rb, rb2); Assert.assertEquals(rb2card, orresult.getCardinality()); Assert.assertEquals(rb2.getCardinality() + rb.getCardinality(), orresult2.getCardinality()); rb.or(rb2); Assert.assertTrue(rb.equals(orresult2)); }
@Test public void ortest2() { final int[] arrayrr = new int[4000 + 4000 + 2]; int pos = 0; final MutableRoaringBitmap rr = new MutableRoaringBitmap(); for (int k = 0; k < 4000; ++k) { rr.add(k); arrayrr[pos++] = k; } rr.add(100000); rr.add(110000); final MutableRoaringBitmap rr2 = new MutableRoaringBitmap(); for (int k = 4000; k < 8000; ++k) { rr2.add(k); arrayrr[pos++] = k; } arrayrr[pos++] = 100000; arrayrr[pos++] = 110000; final MutableRoaringBitmap rror = MutableRoaringBitmap.or(rr, rr2); final int[] arrayor = rror.toArray(); Assert.assertTrue(Arrays.equals(arrayor, arrayrr)); }
@Test public void testHash() { MutableRoaringBitmap rbm1 = new MutableRoaringBitmap(); rbm1.add(17); MutableRoaringBitmap rbm2 = new MutableRoaringBitmap(); rbm2.add(17); Assert.assertTrue(rbm1.hashCode() == rbm2.hashCode()); rbm2 = rbm1.clone(); Assert.assertTrue(rbm1.hashCode() == rbm2.hashCode()); }
@Test public void andtest2() { 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); final int[] array = rrand.toArray(); Assert.assertEquals(array.length, 1); Assert.assertEquals(array[0], 13); }
@Test public void simplecardinalityTest() { final int N = 512; final int gap = 70; final MutableRoaringBitmap rb = new MutableRoaringBitmap(); for (int k = 0; k < N; k++) { rb.add(k * gap); Assert.assertEquals(rb.getCardinality(), k + 1); } Assert.assertEquals(rb.getCardinality(), N); for (int k = 0; k < N; k++) { rb.add(k * gap); Assert.assertEquals(rb.getCardinality(), N); } }
@Test public void andnottest4() { 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); rb2.getCardinality(); // check or 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); }
@Test public void testSerialization3() throws IOException, ClassNotFoundException { final MutableRoaringBitmap rr = new MutableRoaringBitmap(); for (int k = 65000; k < 2 * 65000; ++k) rr.add(k); rr.add(1444000); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); // Note: you could use a file output steam instead of // ByteArrayOutputStream int howmuch = rr.serializedSizeInBytes(); final DataOutputStream oo = new DataOutputStream(bos); rr.serialize(oo); oo.close(); Assert.assertEquals(howmuch, bos.toByteArray().length); final MutableRoaringBitmap rrback = new MutableRoaringBitmap(); final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); rrback.deserialize(new DataInputStream(bis)); Assert.assertEquals(rr.getCardinality(), rrback.getCardinality()); Assert.assertTrue(rr.equals(rrback)); }
@Test public void basictest() { final MutableRoaringBitmap rr = new MutableRoaringBitmap(); final int N = 4000; final int[] a = new int[N + 2]; int pos = 0; for (int k = 0; k < N; ++k) { rr.add(k); a[pos++] = k; } rr.add(100000); a[pos++] = 100000; rr.add(110000); a[pos++] = 110000; final int[] array = rr.toArray(); for (int i = 0; i < array.length; i++) if (array[i] != a[i]) System.out.println("rr : " + array[i] + " a : " + a[i]); Assert.assertTrue(Arrays.equals(array, a)); }
@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 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 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); } } }
@Test public void testIterator() { MutableRoaringBitmap rb = new MutableRoaringBitmap(); for (int k = 0; k < 4000; ++k) rb.add(k); for (int k = 0; k < 1000; ++k) rb.add(k * 100); MutableRoaringBitmap copy1 = new MutableRoaringBitmap(); for (int x : rb) { copy1.add(x); } Assert.assertTrue(copy1.equals(rb)); MutableRoaringBitmap copy2 = new MutableRoaringBitmap(); IntIterator i = rb.getIntIterator(); Iterator<Integer> is = rb.iterator(); while (i.hasNext()) { if (!is.hasNext()) throw new RuntimeException("bug"); int x = i.next(); copy2.add(x); int xs = is.next(); if (x != xs) throw new RuntimeException("values differ " + x + " " + xs); } if (is.hasNext()) throw new RuntimeException("bug: more data available"); Assert.assertTrue(copy2.equals(rb)); }
@Test public void ortest() { 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(); for (int k = 0; k < 4000; ++k) { rr2.add(k); } final MutableRoaringBitmap rror = MutableRoaringBitmap.or(rr, rr2); final int[] array = rror.toArray(); final int[] arrayrr = rr.toArray(); Assert.assertTrue(Arrays.equals(array, arrayrr)); rr.or(rr2); final int[] arrayirr = rr.toArray(); Assert.assertTrue(Arrays.equals(array, arrayirr)); }
@Test public void flipTest2A() { final MutableRoaringBitmap rb = new MutableRoaringBitmap(); final MutableRoaringBitmap rb1 = MutableRoaringBitmap.flip(rb, 100000, 100000); rb.add(1); // will not affect rb1 (no shared container) final int rbcard = rb1.getCardinality(); Assert.assertEquals(0, rbcard); Assert.assertEquals(1, rb.getCardinality()); final BitSet bs = new BitSet(); Assert.assertTrue(equals(bs, rb1)); bs.set(1); Assert.assertTrue(equals(bs, rb)); }
@Test public void testSerialization2() throws IOException, ClassNotFoundException { final MutableRoaringBitmap rr = new MutableRoaringBitmap(); for (int k = 200; k < 400; ++k) rr.add(k); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); // Note: you could use a file output steam instead of // ByteArrayOutputStream final ObjectOutputStream oo = new ObjectOutputStream(bos); rr.writeExternal(oo); oo.close(); final MutableRoaringBitmap rrback = new MutableRoaringBitmap(); final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); rrback.readExternal(new ObjectInputStream(bis)); Assert.assertEquals(rr.getCardinality(), rrback.getCardinality()); Assert.assertTrue(rr.equals(rrback)); }
@Test public void testContains() throws IOException { System.out.println("test contains"); MutableRoaringBitmap rbm1 = new MutableRoaringBitmap(); for (int k = 0; k < 1000; ++k) { rbm1.add(17 * k); } for (int k = 0; k < 17 * 1000; ++k) { Assert.assertTrue(rbm1.contains(k) == (k / 17 * 17 == k)); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); rbm1.serialize(dos); dos.close(); ByteBuffer bb = ByteBuffer.allocateDirect(bos.size()); bb.put(bos.toByteArray()); bb.flip(); ImmutableRoaringBitmap rrback1 = new ImmutableRoaringBitmap(bb); for (int k = 0; k < 17 * 1000; ++k) { Assert.assertTrue(rrback1.contains(k) == (k / 17 * 17 == k)); } }
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"); } } } }
@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 public void ortest3() { final HashSet<Integer> V1 = new HashSet<Integer>(); final HashSet<Integer> V2 = new HashSet<Integer>(); final MutableRoaringBitmap rr = new MutableRoaringBitmap(); final MutableRoaringBitmap rr2 = new MutableRoaringBitmap(); // 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); V1.add(k); } for (int k = 3500; k < 4500; ++k) { rr.add(k); V1.add(k); } for (int k = 4000; k < 65000; ++k) { rr2.add(k); V1.add(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 + 10000; ++k) { rr.add(k); V1.add(k); } for (int k = 65536; k < 65536 + 14000; ++k) { rr2.add(k); V1.add(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); V1.add(k); } for (int k = 4 * 65535; k < 4 * 65535 + 800; ++k) { rr2.add(k); V1.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(k); } for (int k = 7 * 65535; k < 7 * 65535 + 2000; ++k) { rr2.add(k); V1.add(k); } final MutableRoaringBitmap rror = MutableRoaringBitmap.or(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 (Object aTab : tab) vector.add((Integer) aTab); for (final int i : rror.toArray()) { if (!vector.contains(i)) { valide = false; } V2.add(i); } for (int i = 0; i < V1.size(); i++) if (!V2.contains(vector.elementAt(i))) { valide = false; } Assert.assertEquals(valide, true); }
@Test public void ORtest() { 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.or(rr, rr2); rr.or(rr2); Assert.assertTrue(correct.equals(rr)); }