fixed integrated tests for HashRange vs HashInterval, expanded constructions for Set

This commit is contained in:
Jonathan Shook
2020-06-17 16:34:56 -05:00
parent 01da138364
commit bb57a479d6
5 changed files with 70 additions and 21 deletions

View File

@@ -1,5 +1,6 @@
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_collection;
import io.nosqlbench.nb.api.errors.BasicError;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
@@ -8,15 +9,14 @@ import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.HashSet;
import java.util.function.LongFunction;
import java.util.function.LongToIntFunction;
import java.util.function.LongUnaryOperator;
/**
* Create a {@code Set} from a long input based on two functions,
* the first to determine the set size, and the second to populate
* the set with object values. The input fed to the second function
* is incremented between elements.
*
* To create Sets of Strings from the String version of the same
* mapping functions, simply use {@link StringSet} instead.
* Create a {@code Set} from a long input based on two functions, the first to determine the set size, and the second to
* populate the set with object values. The input fed to the second function is incremented between elements.
* <p>
* To create Sets of Strings from the String version of the same mapping functions, simply use {@link StringSet}
* instead.
*/
@Categories({Category.collections})
@ThreadSafeMapper
@@ -31,13 +31,61 @@ public class Set implements LongFunction<java.util.Set<Object>> {
this.sizeFunc = sizeFunc;
this.valueFunc = valueFunc;
}
public Set(LongToIntFunction sizeFunc, LongUnaryOperator valueFunc) {
this.sizeFunc = sizeFunc;
this.valueFunc = valueFunc::applyAsLong;
}
public Set(LongToIntFunction sizeFunc, LongToIntFunction valueFunc) {
this.sizeFunc = sizeFunc;
this.valueFunc = valueFunc::applyAsInt;
}
public Set(LongFunction<Object> sizeFunc, LongFunction<Object> valueFunc) {
this.sizeFunc = checkSizeFunc(sizeFunc);
this.valueFunc = valueFunc;
}
public Set(LongFunction<Object> sizeFunc, LongUnaryOperator valueFunc) {
this.sizeFunc = checkSizeFunc(sizeFunc);
this.valueFunc = valueFunc::applyAsLong;
}
public Set(LongFunction<Object> sizeFunc, LongToIntFunction valueFunc) {
this.sizeFunc = checkSizeFunc(sizeFunc);
this.valueFunc = valueFunc::applyAsInt;
}
public Set(LongUnaryOperator sizeFunc, LongFunction<Object> valueFunc) {
this.sizeFunc = l -> (int) sizeFunc.applyAsLong(l);
this.valueFunc = valueFunc;
}
public Set(LongUnaryOperator sizeFunc, LongUnaryOperator valueFunc) {
this.sizeFunc = l -> (int) sizeFunc.applyAsLong(l);
this.valueFunc = valueFunc::applyAsLong;
}
public Set(LongUnaryOperator sizeFunc, LongToIntFunction valueFunc) {
this.sizeFunc = l -> (int) sizeFunc.applyAsLong(l);
this.valueFunc = valueFunc::applyAsInt;
}
private static LongToIntFunction checkSizeFunc(LongFunction<?> sizeFunc) {
Object sizeType = sizeFunc.apply(0);
if (int.class.isAssignableFrom(sizeType.getClass())) {
return value -> ((LongFunction<Integer>)sizeFunc).apply(value);
} else if (long.class.isAssignableFrom(sizeType.getClass())) {
return value -> ((LongFunction<Long>)sizeFunc).apply(value).intValue();
} else {
throw new BasicError("The size function produces " + sizeType.getClass().getCanonicalName() + ", which can't be used as an integer");
}
}
@Override
public java.util.Set<Object> apply(long value) {
int size = sizeFunc.applyAsInt(value);
java.util.Set<Object> set = new HashSet<>(size);
for (int i = 0; i < size; i++) {
set.add(valueFunc.apply(value+i));
set.add(valueFunc.apply(value + i));
}
return set;
}

View File

@@ -2,6 +2,7 @@ package io.nosqlbench.virtdata.library.basics.shared.from_long.to_collection;
import org.junit.Test;
import java.util.function.LongFunction;
import java.util.function.LongToIntFunction;
import static org.assertj.core.api.Assertions.assertThat;
@@ -10,7 +11,7 @@ public class SetTest {
@Test
public void testSet() {
Set set = new Set(s -> 3, e -> e);
Set set = new Set((LongToIntFunction) s -> 3, (LongFunction) e -> e);
java.util.Set<Object> s1 = set.apply(15L);
assertThat(s1).containsOnly(15L,16L,17L);
}