Skip to content

Commit 4197148

Browse files
authored
Merge pull request #590 from apache/bf_cross_lang
bloom filter cross language tests
2 parents 8bf32c5 + ca53791 commit 4197148

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.datasketches.filters.bloomfilter;
21+
22+
import static org.apache.datasketches.common.TestUtil.CHECK_CPP_FILES;
23+
import static org.apache.datasketches.common.TestUtil.GENERATE_JAVA_FILES;
24+
import static org.apache.datasketches.common.TestUtil.cppPath;
25+
import static org.apache.datasketches.common.TestUtil.javaPath;
26+
import static org.testng.Assert.assertEquals;
27+
import static org.testng.Assert.assertTrue;
28+
29+
import java.io.IOException;
30+
import java.nio.file.Files;
31+
32+
import org.apache.datasketches.memory.Memory;
33+
import org.testng.annotations.Test;
34+
35+
/**
36+
* Serialize binary sketches to be tested by C++ code.
37+
* Test deserialization of binary sketches serialized by C++ code.
38+
*/
39+
public class BloomFilterCorssLanguageTest {
40+
41+
@Test(groups = {GENERATE_JAVA_FILES})
42+
public void generatBloomFilterBinariesForCompatibilityTesting() throws IOException {
43+
final int[] nArr = {0, 10_000, 2_000_000, 300_000_00};
44+
final short[] hArr = {3, 5};
45+
for (int n : nArr) {
46+
for (short numHashes : hArr) {
47+
final long configBits = Math.max(n, 1000L); // so empty still has valid bit size
48+
BloomFilter bf = BloomFilterBuilder.createBySize(configBits, numHashes);
49+
for (int i = 0; i < n / 10; ++i) {
50+
bf.update(i);
51+
}
52+
if (n > 0) { bf.update(Float.NaN); }
53+
assertEquals(bf.isEmpty(), n == 0);
54+
assertTrue(bf.isEmpty() || (bf.getBitsUsed() > (n / 10)));
55+
Files.newOutputStream(javaPath.resolve("bf_n" + n + "_h" + numHashes + "_java.sk")).write(bf.toByteArray());
56+
}
57+
}
58+
}
59+
60+
@Test(groups = {CHECK_CPP_FILES})
61+
public void readBloomFilterBinariesForCompatibilityTesting() throws IOException {
62+
final int[] nArr = {0, 10_000, 2_000_000, 300_000_00};
63+
final short[] hArr = {3, 5};
64+
for (int n : nArr) {
65+
for (short numHashes : hArr) {
66+
final byte[] bytes = Files.readAllBytes(cppPath.resolve("bf_n" + n + "_h" + numHashes + "_cpp.sk"));
67+
final BloomFilter bf = BloomFilter.heapify(Memory.wrap(bytes));
68+
assertEquals(bf.isEmpty(), n == 0);
69+
assertTrue(bf.isEmpty() || (bf.getBitsUsed() > (n / 10)));
70+
71+
for (int i = 0; i < n / 10; ++i) {
72+
assertTrue(bf.query(i));
73+
}
74+
if (n > 0) {
75+
assert(bf.query(Double.NaN));
76+
}
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)