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; 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.Categories;
import io.nosqlbench.virtdata.api.annotations.Category; import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example; 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.HashSet;
import java.util.function.LongFunction; import java.util.function.LongFunction;
import java.util.function.LongToIntFunction; import java.util.function.LongToIntFunction;
import java.util.function.LongUnaryOperator;
/** /**
* Create a {@code Set} from a long input based on two functions, * Create a {@code Set} from a long input based on two functions, the first to determine the set size, and the second to
* the first to determine the set size, and the second to populate * populate the set with object values. The input fed to the second function is incremented between elements.
* the set with object values. The input fed to the second function * <p>
* is incremented between elements. * To create Sets of Strings from the String version of the same mapping functions, simply use {@link StringSet}
* * instead.
* To create Sets of Strings from the String version of the same
* mapping functions, simply use {@link StringSet} instead.
*/ */
@Categories({Category.collections}) @Categories({Category.collections})
@ThreadSafeMapper @ThreadSafeMapper
@ -31,13 +31,61 @@ public class Set implements LongFunction<java.util.Set<Object>> {
this.sizeFunc = sizeFunc; this.sizeFunc = sizeFunc;
this.valueFunc = valueFunc; 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 @Override
public java.util.Set<Object> apply(long value) { public java.util.Set<Object> apply(long value) {
int size = sizeFunc.applyAsInt(value); int size = sizeFunc.applyAsInt(value);
java.util.Set<Object> set = new HashSet<>(size); java.util.Set<Object> set = new HashSet<>(size);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
set.add(valueFunc.apply(value+i)); set.add(valueFunc.apply(value + i));
} }
return set; return set;
} }

View File

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

View File

@ -166,24 +166,24 @@ public class IntegratedComposerLibraryTest {
@Test @Test
public void testChainedHashRanges() { public void testChainedHashRanges() {
final int initialCycle = 0; final int initialCycle = 0;
final int intermediateCycle = 39; final int intermediateCycle = 52;
final int finalCycle = 81; final int finalCycle = 81;
Object intermediateValue = assertMapper("compose HashRange(0,100) -> int", initialCycle); Object intermediateValue = assertMapper("compose HashRange(0,100) -> int", 0);
assertInteger(intermediateValue, intermediateCycle); assertThat(intermediateValue).isEqualTo(52);
Object finalValue = assertMapper("compose HashRange(0,100) -> int", intermediateCycle); Object finalValue = assertMapper("compose HashRange(0,100) -> int", intermediateCycle);
assertInteger(finalValue, finalCycle); assertThat(finalValue).isEqualTo(16);
Object finalChainedValue = assertMapper("compose HashRange(0,100); HashRange(0,100) -> int", initialCycle); Object finalChainedValue = assertMapper("compose HashRange(0,100); HashRange(0,100) -> int", initialCycle);
assertInteger(finalChainedValue, finalCycle); assertThat(finalChainedValue).isEqualTo(16);
} }
@Test @Test
public void testLeadingIdentityDoesNotImpactTypes() public void testLeadingIdentityDoesNotImpactTypes()
{ {
final int initialCycle = 0; final int initialCycle = 0;
final int finalCycle = 167; final int finalCycle = 160;
Object o1 = assertMapper("compose HashRange(0,1000); HashRange(0,1000) -> int", initialCycle); Object o1 = assertMapper("compose HashRange(0,1000); HashRange(0,1000) -> int", initialCycle);
assertInteger(o1, finalCycle); assertInteger(o1, finalCycle);

View File

@ -102,7 +102,7 @@ public class IntegratedComposerLogicTest {
DataMapper<Long> longDataMapper = mo.get(); DataMapper<Long> longDataMapper = mo.get();
Long result = longDataMapper.get(5L); Long result = longDataMapper.get(5L);
assertThat(result).isNotNull(); assertThat(result).isNotNull();
assertThat(result).isEqualTo(1398623797L); assertThat(result).isEqualTo(359183748L);
} }
@Test @Test

View File

@ -35,7 +35,7 @@ public class IntegratedTemporalExamplesTest {
); );
UUID uuid1 = uuidgen.get(1L); UUID uuid1 = uuidgen.get(1L);
System.out.println(uuid1); System.out.println(uuid1);
assertThat(uuid1).isEqualTo(UUID.fromString("998ccf20-ee50-1398-8000-000000000000")); assertThat(uuid1).isEqualTo(UUID.fromString("826476e0-ee50-1398-8000-000000000000"));
} }
/** /**
@ -87,7 +87,7 @@ public class IntegratedTemporalExamplesTest {
); );
Date date = dateMapper.get(3L); Date date = dateMapper.get(3L);
assertThat(date).isEqualTo(new Date(2527683L)); assertThat(date).isEqualTo(new Date(1539133L));
} }
/** /**
@ -107,10 +107,10 @@ public class IntegratedTemporalExamplesTest {
1970, 1970,
1, 1,
1, 1,
0,
2, 2,
43, 27,
10, 245,
106,
DateTimeZone.UTC) DateTimeZone.UTC)
); );
} }