Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed counting bug in add() and updated tests

  • Loading branch information...
commit c176b7717e533f26b75e1cc19bac8d7b4455fe74 1 parent 4da4b1e
@MagnusS authored
View
3  src/com/skjegstad/utils/BloomFilter.java
@@ -61,7 +61,7 @@
/**
* Constructs an empty Bloom filter. The total length of the Bloom filter will be
- * c*n.
+ * c*n.
*
* @param c is the number of bits used per element.
* @param n is the expected number of elements the filter will contain.
@@ -296,7 +296,6 @@ public void clear() {
*/
public void add(E element) {
add(element.toString().getBytes(charset));
- numberOfAddedElements ++;
}
/**
View
83 test/com/skjegstad/utils/BloomFilterTest.java
@@ -15,6 +15,8 @@
package com.skjegstad.utils;
+import java.nio.charset.Charset;
+import java.util.BitSet;
import java.util.List;
import java.io.UnsupportedEncodingException;
import java.util.Random;
@@ -353,31 +355,32 @@ public void testFalsePositiveRate1() throws UnsupportedEncodingException {
for (int j = 10; j < 21; j++) {
System.out.print(j-9 + "/11");
- List<String> v = new ArrayList<String>();
+ List<byte[]> v = new ArrayList<byte[]>();
BloomFilter instance = new BloomFilter(100*j,100);
for (int i = 0; i < 100; i++) {
- v.add(UUID.randomUUID().toString());
+ byte[] bytes = new byte[100];
+ r.nextBytes(bytes);
+ v.add(bytes);
}
instance.addAll(v);
- long r = 0;
- double tests = 100000;
+ long f = 0;
+ double tests = 300000;
for (int i = 0; i < tests; i++) {
- String s = UUID.randomUUID().toString();
- if (instance.contains(s)) {
- if (!v.contains(s)) {
- r++;
+ byte[] bytes = new byte[100];
+ r.nextBytes(bytes);
+ if (instance.contains(bytes)) {
+ if (!v.contains(bytes)) {
+ f++;
}
}
}
- double ratio = r / tests;
+ double ratio = f / tests;
System.out.println(" - got " + ratio + ", math says " + instance.expectedFalsePositiveProbability());
assertEquals(instance.expectedFalsePositiveProbability(), ratio, 0.01);
-
-
}
}
@@ -421,6 +424,64 @@ public void testGetK() {
instance = new BloomFilter(12, 1);
assertEquals(8, instance.getK());
}
+
+ /**
+ * Test of contains method, of class BloomFilter.
+ */
+ @Test
+ public void testContains_GenericType() {
+ System.out.println("contains");
+ int items = 100;
+ BloomFilter<String> instance = new BloomFilter(0.01, items);
+
+ for (int i = 0; i < items; i++) {
+ String s = UUID.randomUUID().toString();
+ instance.add(s);
+ assertTrue(instance.contains(s));
+ }
+ }
+
+ /**
+ * Test of contains method, of class BloomFilter.
+ */
+ @Test
+ public void testContains_byteArr() {
+ System.out.println("contains");
+
+ int items = 100;
+ BloomFilter instance = new BloomFilter(0.01, items);
+
+ for (int i = 0; i < items; i++) {
+ byte[] bytes = new byte[500];
+ r.nextBytes(bytes);
+ instance.add(bytes);
+ assertTrue(instance.contains(bytes));
+ }
+ }
+
+ /**
+ * Test of count method, of class BloomFilter.
+ */
+ @Test
+ public void testCount() {
+ System.out.println("count");
+ int expResult = 100;
+ BloomFilter instance = new BloomFilter(0.01, expResult);
+ for (int i = 0; i < expResult; i++) {
+ byte[] bytes = new byte[100];
+ r.nextBytes(bytes);
+ instance.add(bytes);
+ }
+ int result = instance.count();
+ assertEquals(expResult, result);
+
+ instance = new BloomFilter(0.01, expResult);
+ for (int i = 0; i < expResult; i++) {
+ instance.add(UUID.randomUUID().toString());
+ }
+ result = instance.count();
+ assertEquals(expResult, result);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.