@Test public void testAddABunchOfStrings() { for (List<String> strings: someLists(nullsAnd(strings(), 20), integers(2 * Shard.SHARD_SIZE + 20, 10 * Shard.SHARD_SIZE))) { AttrStoreBuilder asb = new AttrStoreBuilder(); strings.forEach(asb::add); assertThat(asb.size(), equalTo(strings.size())); AttrStore store = asb.build(); assertThat(store.size(), equalTo(strings.size())); assertThat(IntStream.range(0, strings.size()) .mapToObj(store::get) .collect(Collectors.toList()), contains(strings.toArray())); } }
@Test public void testCollectMany() { for (List<Long> ids: someLists(longs(), 10, 100)) { List<Double> values = lists(doubles(), ids.size()).next(); ResultList results = IntStream.range(0, ids.size()) .mapToObj(i -> Results.create(ids.get(i), values.get(i))) .collect(Results.listCollector()); assertThat(results.stream().map(Result::getId).collect(Collectors.toList()), equalTo(ids)); assertThat(results.stream().map(Result::getScore).collect(Collectors.toList()), equalTo(values)); } }
/** * Create a new long generator which creates long values ranging from 1 to * {@link Long#MAX_VALUE}. */ public static Generator<Long> positiveLongs() { return positiveLongs(Long.MAX_VALUE); }
@Test public void testRandomSearchesPresent() { for (List<Long> keys: someSortedLists(longs())) { List<Long> deduped = Lists.newArrayList(Sets.newLinkedHashSet(keys)); long key = integers(0, deduped.size()).next(); BinarySearch search = BinarySearch.forList(key, deduped); assertThat(search.search(0, keys.size()), equalTo(Collections.binarySearch(deduped, key))); } }
@Test public void testAddABunchOfDoubles() { for (List<Double> strings: someLists(nullsAnd(doubles(), 20), integers(2 * Shard.SHARD_SIZE + 20, 10 * Shard.SHARD_SIZE))) { AttrStoreBuilder asb = new AttrStoreBuilder(DoubleShard::create); strings.forEach(asb::add); assertThat(asb.size(), equalTo(strings.size())); AttrStore store = asb.build(); assertThat(store.size(), equalTo(strings.size())); assertThat(IntStream.range(0, strings.size()) .mapToObj(store::get) .collect(Collectors.toList()), contains(strings.toArray())); } }
private List<Rating> makeRatings() { Generator<Rating> rgen = ratings(); int n = integers(10, 1000).next(); return Stream.generate(rgen::next) .limit(n) .collect(Collectors.toList()); }
@Test public void testCreateWithLists() { for (Set<Long> keys: someSets(longs(), integers(0, 500))) { LongSortedSet sorted = LongUtils.packedSet(keys); SortedKeyIndex dom = SortedKeyIndex.fromCollection(keys); double[] values = new double[dom.size()]; for (int i = 0; i < dom.size(); i++) { values[i] = doubles().next(); } Long2DoubleSortedMap map = new Long2DoubleSortedArrayMap(dom, values); assertThat(map.size(), equalTo(dom.size())); assertThat(map.size(), equalTo(keys.size())); if (map.size() > 0) { assertThat(map.entrySet().first().getKey(), equalTo(sorted.firstLong())); assertThat(map.entrySet().last().getKey(), equalTo(sorted.lastLong())); assertThat(map.firstLongKey(), equalTo(sorted.firstLong())); assertThat(map.lastLongKey(), equalTo(sorted.lastLong())); } assertThat(map.keySet(), equalTo(sorted)); for (Long k: keys) { assertThat(map.containsKey(k), equalTo(true)); } } }
@Test public void testManyItems() { Generator<Double> globals = doubles(); for (Map<Long,Double> map: someMaps(positiveLongs(), doubles())) { double bias = globals.next(); Long2DoubleMap itemBiases = Long2DoubleSortedArrayMap.create(map); BiasModel model = new UserItemBiasModel(bias, Long2DoubleMaps.EMPTY_MAP, itemBiases); assertThat(model.getIntercept(), equalTo(bias)); assertThat(model.getItemBiases(itemBiases.keySet()), equalTo(itemBiases)); for (Set<Long> users : someSets(positiveLongs())) { Long2DoubleMap biases = model.getItemBiases(LongUtils.packedSet(users)); for (long user: users) { if (itemBiases.containsKey(user)) { assertThat(biases.get(user), equalTo(itemBiases.get(user))); } else { assertThat(biases.get(user), equalTo(0.0)); } } } } }
public FieldGenerator(ColumnSchema schema) { switch (schema.getType()) { case ULONG: case LONG: case TIME: { generator = new LongBytesGenerator(); break; } case DOUBLE: { generator = new DoubleBytesGenerator(); break; } case BINARY: { generator = CombinedGenerators.byteArrays( PrimitiveGenerators.integers(0, schema.getMaxLength())); break; } case STRING: { generator = new StringBytesGenerator(schema.getMaxLength()); break; } default: generator = CombinedGenerators.byteArrays( PrimitiveGenerators.fixedValues(32)); } if (schema.getIsNullable()) { generator = CombinedGenerators.nullsAnd(generator, 10); } }
@Test public void testRandomSearches() { for (List<Long> keys: someSortedLists(longs())) { List<Long> deduped = Lists.newArrayList(Sets.newLinkedHashSet(keys)); long key = longs().next(); BinarySearch search = BinarySearch.forList(key, deduped); int rv = search.search(0, keys.size()); assertThat(search.search(0, keys.size()), equalTo(Collections.binarySearch(deduped, key))); int idx = BinarySearch.resultToIndex(rv); if (deduped.isEmpty()) { assertThat(idx, equalTo(0)); } else if (idx == deduped.size()) { assertThat(key, greaterThan(deduped.get(deduped.size() - 1))); } else { assertThat(key, lessThanOrEqualTo(deduped.get(idx))); } } }
/** * Create a fixed value generator returning one of the values from the * values array. */ public static <T> Generator<T> fixedValues(T... values) { return fixedValues(Arrays.asList(values)); }
/** * Create a new string generator for strings that are not empty. */ public static ExtendibleGenerator<Character, String> nonEmptyStrings() { return strings(1, StringGenerator.MAX_LENGTH); }
@Test public void testStorableInts() { for (int n: toIterable(integers(-512, 512), 10000)) { double d = n; assertThat(String.format("value %f should be %s", d, Math.abs(n) < 64 ? "storable" : "unstorable"), DoubleShard.Compact.isStorable(d), equalTo(n > -64 && n < 64)); } }
@Test public void testAddABunchOfSmallLongs() { for (List<Long> strings: someLists(nullsAnd(longs(Short.MIN_VALUE, Short.MAX_VALUE), 20), integers(2 * Shard.SHARD_SIZE + 20, 10 * Shard.SHARD_SIZE))) { AttrStoreBuilder asb = new AttrStoreBuilder(LongShard::create); strings.forEach(asb::add); assertThat(asb.size(), equalTo(strings.size())); AttrStore store = asb.build(); assertThat(store.size(), equalTo(strings.size())); assertThat(IntStream.range(0, strings.size()) .mapToObj(store::get) .collect(Collectors.toList()), contains(strings.toArray())); } }
@Test public void testManyUsers() { Generator<Double> globals = doubles(); for (Map<Long,Double> map: someMaps(positiveLongs(), doubles())) { double bias = globals.next(); Long2DoubleMap userBiases = Long2DoubleSortedArrayMap.create(map); BiasModel model = new UserItemBiasModel(bias, userBiases, Long2DoubleMaps.EMPTY_MAP); assertThat(model.getIntercept(), equalTo(bias)); assertThat(model.getUserBiases(userBiases.keySet()), equalTo(userBiases)); for (Set<Long> users : someSets(positiveLongs())) { Long2DoubleMap biases = model.getUserBiases(LongUtils.packedSet(users)); for (long user: users) { if (userBiases.containsKey(user)) { assertThat(biases.get(user), equalTo(userBiases.get(user))); } else { assertThat(biases.get(user), equalTo(0.0)); } } } } }
@Test public void testStorableDoubles() { for (double d: toIterable(doubles(), 10000)) { assertThat(DoubleShard.Compact.isStorable(d), equalTo(d > -64 && d < 64 && Scalars.isZero(Math.IEEEremainder(d, 0.5)))); } } }
/** * Create a new long generator which creates long values ranging from 1 to * hi. */ public static Generator<Long> positiveLongs(long hi) { return longs(1, hi); }