use of org.apache.rocketmq.filter.util.BitsArray in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class BitsArrayTest method testOr.
@Test
public void testOr() {
BitsArray b1 = BitsArray.create(new byte[] { (byte) 0xff, 0x00 });
BitsArray b2 = BitsArray.create(new byte[] { 0x00, (byte) 0xff });
b1.or(b2);
for (int i = 0; i < b1.bitLength(); i++) {
assertThat(b1.getBit(i)).isTrue();
}
}
use of org.apache.rocketmq.filter.util.BitsArray in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class BitsArrayTest method testConstructor.
@Test
public void testConstructor() {
BitsArray bitsArray = BitsArray.create(8);
assertThat(bitsArray.byteLength() == 1 && bitsArray.bitLength() == 8).isTrue();
bitsArray = BitsArray.create(9);
assertThat(bitsArray.byteLength() == 2 && bitsArray.bitLength() == 9).isTrue();
bitsArray = BitsArray.create(7);
assertThat(bitsArray.byteLength() == 1 && bitsArray.bitLength() == 7).isTrue();
}
use of org.apache.rocketmq.filter.util.BitsArray in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class BloomFilterTest method testHashTo.
@Test
public void testHashTo() {
String cid = "CID_abc_efg";
BloomFilter bloomFilter = BloomFilter.createByFn(10, 20);
BitsArray bits = BitsArray.create(bloomFilter.getM());
int[] bitPos = bloomFilter.calcBitPositions(cid);
bloomFilter.hashTo(cid, bits);
for (int bit : bitPos) {
assertThat(bits.getBit(bit)).isTrue();
}
}
use of org.apache.rocketmq.filter.util.BitsArray in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class BloomFilterTest method testIsHit.
@Test
public void testIsHit() {
String cid = "CID_abc_efg";
String cid2 = "CID_abc_123";
BloomFilter bloomFilter = BloomFilter.createByFn(10, 20);
BitsArray bits = BitsArray.create(bloomFilter.getM());
bloomFilter.hashTo(cid, bits);
assertThat(bloomFilter.isHit(cid, bits)).isTrue();
assertThat(!bloomFilter.isHit(cid2, bits)).isTrue();
bloomFilter.hashTo(cid2, bits);
assertThat(bloomFilter.isHit(cid, bits)).isTrue();
assertThat(bloomFilter.isHit(cid2, bits)).isTrue();
}
use of org.apache.rocketmq.filter.util.BitsArray in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class BloomFilterTest method testCheckFalseHit.
@Test
public void testCheckFalseHit() {
BloomFilter bloomFilter = BloomFilter.createByFn(1, 300);
BitsArray bits = BitsArray.create(bloomFilter.getM());
int falseHit = 0;
for (int i = 0; i < bloomFilter.getN(); i++) {
String str = randomString((new Random(System.nanoTime())).nextInt(127) + 10);
int[] bitPos = bloomFilter.calcBitPositions(str);
if (bloomFilter.checkFalseHit(bitPos, bits)) {
falseHit++;
}
bloomFilter.hashTo(bitPos, bits);
}
assertThat(falseHit).isLessThanOrEqualTo(bloomFilter.getF() * bloomFilter.getN() / 100);
}
Aggregations