(index-query) Add some tests for the QueryFilter code
This commit is contained in:
parent
6c3b49417f
commit
300b1a1b84
@ -1,25 +1,24 @@
|
||||
package nu.marginalia.index.query.filter;
|
||||
|
||||
import nu.marginalia.array.buffer.LongQueryBuffer;
|
||||
|
||||
public class QueryFilterLetThrough implements QueryFilterStepIf {
|
||||
static final QueryFilterStepIf instance = new QueryFilterLetThrough();
|
||||
|
||||
@Override
|
||||
public boolean test(long value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(LongQueryBuffer buffer) {
|
||||
buffer.retainAll();
|
||||
buffer.finalizeFiltering();
|
||||
}
|
||||
|
||||
public double cost() {
|
||||
return 0.;
|
||||
}
|
||||
|
||||
public int retainDestructive(long[] items, int max) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int retainReorder(long[] items, int start, int max) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String describe() {
|
||||
return "[PassThrough]";
|
||||
}
|
||||
|
@ -36,13 +36,5 @@ public interface QueryFilterStepIf extends Comparable<QueryFilterStepIf> {
|
||||
buffer.finalizeFiltering();
|
||||
}
|
||||
|
||||
static QueryFilterStepIf noPass() {
|
||||
return QueryFilterNoPass.instance;
|
||||
}
|
||||
static QueryFilterStepIf anyOf(List<? extends QueryFilterStepIf> steps) {
|
||||
return new QueryFilterAnyOf(steps);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,67 @@
|
||||
package nu.marginalia.index.query.filter;
|
||||
|
||||
import nu.marginalia.array.buffer.LongQueryBuffer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class QueryFilterStepIfTest {
|
||||
|
||||
private LongQueryBuffer createBuffer(long... data) {
|
||||
return new LongQueryBuffer(data, data.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPassThrough() {
|
||||
var buffer = createBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
var filter = new QueryFilterLetThrough();
|
||||
filter.apply(buffer);
|
||||
assertArrayEquals(new long[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, buffer.copyData());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoPass() {
|
||||
var buffer = createBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
var filter = new QueryFilterNoPass();
|
||||
filter.apply(buffer);
|
||||
assertArrayEquals(new long[]{}, buffer.copyData());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncludePredicate() {
|
||||
var buffer = createBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
var filter = new QueryFilterStepFromPredicate(value -> value % 2 == 0);
|
||||
filter.apply(buffer);
|
||||
assertArrayEquals(new long[]{2, 4, 6, 8, 10}, buffer.copyData());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExcludePredicate() {
|
||||
var buffer = createBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
var filter = new QueryFilterStepExcludeFromPredicate(value -> value % 2 == 1);
|
||||
filter.apply(buffer);
|
||||
assertArrayEquals(new long[]{2, 4, 6, 8, 10}, buffer.copyData());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccessiveApplication() {
|
||||
var buffer = createBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
var filter1 = new QueryFilterStepFromPredicate(value -> value % 2 == 0);
|
||||
var filter2 = new QueryFilterStepExcludeFromPredicate(value -> value <= 6);
|
||||
filter1.apply(buffer);
|
||||
filter2.apply(buffer);
|
||||
assertArrayEquals(new long[]{8, 10}, buffer.copyData());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCombinedApplication() {
|
||||
var buffer = createBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
var filter1 = new QueryFilterStepFromPredicate(value -> value % 3 == 0);
|
||||
var filter2 = new QueryFilterStepFromPredicate(value -> value % 5 == 0);
|
||||
var filter = new QueryFilterAnyOf(List.of(filter1, filter2));
|
||||
filter.apply(buffer);
|
||||
assertArrayEquals(new long[]{3, 5, 6, 9, 10}, buffer.copyData());
|
||||
}
|
||||
}
|
@ -89,6 +89,15 @@ public class LongQueryBuffer {
|
||||
return ++read < end;
|
||||
}
|
||||
|
||||
/** Retains all values in the buffer, and updates
|
||||
* the read and write pointer to the end pointer,
|
||||
* as though all values were retained.
|
||||
*/
|
||||
public void retainAll() {
|
||||
write = end;
|
||||
read = end;
|
||||
}
|
||||
|
||||
public boolean hasMore() {
|
||||
return read < end;
|
||||
}
|
||||
@ -147,4 +156,5 @@ public class LongQueryBuffer {
|
||||
",data = [" + Arrays.toString(Arrays.copyOf(data, end)) + "]]";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user