net.java.quickcheck.Generator.next()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(10.3k)|赞(0)|评价(0)|浏览(124)

本文整理了Java中net.java.quickcheck.Generator.next()方法的一些代码示例,展示了Generator.next()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Generator.next()方法的具体详情如下:
包路径:net.java.quickcheck.Generator
类名称:Generator
方法名:next

Generator.next介绍

[英]Generates the next instance.
[中]生成下一个实例。

代码示例

代码示例来源:origin: lenskit/lenskit

@Override
  public Rating next() {
    long id = ids.next();
    while (usedIds.contains(id)) {
      id = ids.next();
    }
    usedIds.add(id);
    RatingBuilder rb = Rating.newBuilder()
                 .setId(id)
                 .setUserId(userIds.next())
                 .setItemId(itemIds.next())
                 .setRating(values.next());
    if (timestamps != null) {
      rb.setTimestamp(timestamps.next());
    }
    return rb.build();
  }
}

代码示例来源:origin: lenskit/lenskit

private List<Rating> makeRatings() {
  Generator<Rating> rgen = ratings();
  int n = integers(10, 1000).next();
  return Stream.generate(rgen::next)
         .limit(n)
         .collect(Collectors.toList());
}

代码示例来源:origin: lenskit/lenskit

@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));
  }
}

代码示例来源:origin: lenskit/lenskit

@Test
  public void testParallelCollect() {
    for (List<Long> ids: someLists(longs(), 100, 10000)) {
      List<Double> values = lists(doubles(), ids.size()).next();
      ResultList results = IntStream.range(0, ids.size())
                     .parallel()
                     .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));
    }
  }
}

代码示例来源:origin: lenskit/lenskit

@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)));
  }
}

代码示例来源:origin: lenskit/lenskit

@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)));
    }
  }
}

代码示例来源:origin: lenskit/lenskit

/**
 * Use random lists to test that we always find the *first* matching element.
 */
@Test
public void testRandomSearchesFindFirst() {
  for (List<String> keys: someNonEmptyLists(strings())) {
    Collections.sort(keys);
    // pick an element to duplicate
    int toDup = integers(0, keys.size() - 1).next();
    // and # of times to duplicate
    int ndups = integers(0, 10).next();
    String dupKey = keys.get(toDup);
    for (int i = 0; i < ndups; i++) {
      keys.add(toDup, dupKey);
    }
    BinarySearch search = BinarySearch.forList(dupKey, keys);
    int rv = search.search(0, keys.size());
    assertThat(rv, greaterThanOrEqualTo(0));
    assertThat(search.search(0, keys.size()),
          lessThanOrEqualTo(Collections.binarySearch(keys, dupKey)));
    assertThat(keys.get(rv), equalTo(dupKey));
    if (rv > 0) {
      // this is the first one
      assertThat(keys.get(rv-1), lessThan(dupKey));
    }
  }
}

代码示例来源:origin: lenskit/lenskit

byte key = bytes().next();
BinarySearch search = BinarySearch.forList(key, keys);
int start = integers(0, keys.size()).next();
int end = integers(start, keys.size()).next();

代码示例来源:origin: lenskit/lenskit

@Test
public void testRandomMaps() {
  for (Map<Long,Double> map: someMaps(longs(), doubles())) {
    Long2DoubleSortedArrayMap vec = Long2DoubleSortedArrayMap.create(map);
    Set<Long> picked = sets(map.keySet()).next();
    Set<Long> extra = sets(longs()).next();
    LongSortedSet wanted = LongUtils.setUnion(LongUtils.asLongSet(picked), LongUtils.asLongSet(extra));
    Long2DoubleSortedMap sv = vec.subMap(wanted);
    assertThat(sv.keySet(), everyItem(isIn(wanted)));
    assertThat(sv.keySet(), containsInAnyOrder(picked.toArray()));
    assertThat(sv.entrySet(), everyItem(isIn(map.entrySet())));
  }
}

代码示例来源:origin: net.java.quickcheck/quickcheck

@Override
  public T next() {
    Generator<T> generator = generators.get(index++ % generators.size());
    return generator.next();
  }
}

代码示例来源:origin: lenskit/lenskit

@Before
public void initialize() throws IOException {
  predictOutputFile = folder.newFile("predictions.csv");
  List<Rating> ratings = new ArrayList<>();
  Generator<Rating> rgen = LenskitGenerators.ratings();
  Set<Pair<Long,Long>> used = new HashSet<>();
  while (ratings.size() < RATING_COUNT) {
    Rating r = rgen.next();
    long uid = r.getUserId() % 5;
    Pair<Long,Long> ui = ImmutablePair.of(uid, r.getItemId());
    if (used.contains(ui)) {
      continue;
    }
    used.add(ui);
    Rating r2 = r.copyBuilder()
           .setUserId(r.getUserId() % 5)
           .build();
    ratings.add(r2);
  }
  assumeThat(ratings, hasSize(RATING_COUNT));
  dao = StaticDataSource.fromList(ratings).get();
  LenskitConfiguration config = new LenskitConfiguration();
  config.bind(ItemScorer.class).to(UserMeanItemScorer.class);
  config.bind(UserMeanBaseline.class, ItemScorer.class).to(ItemMeanRatingItemScorer.class);
  tempEval.setRebuildPeriod(1L);
  tempEval.setDataSource(dao);
  tempEval.setAlgorithm("UserMeanBaseline", config);
  tempEval.setOutputFile(predictOutputFile);
}

代码示例来源:origin: lenskit/lenskit

@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));
    }
  }
}

代码示例来源:origin: net.java.quickcheck/quickcheck

@Override
public String next() {
  int size = length.next();
  StringBuilder builder = new StringBuilder();
  for (int count = 0; count < size; count++) {
    builder.append(characters.next());
  }
  return builder.toString();
}

代码示例来源:origin: lenskit/lenskit

/**
 * Method to compute the data points. Split out so that errors it throws get reported.
 */
@BeforeClass
public static void makeDataPoints() {
  KeyData[] data = new KeyData[10];
  Generator<Long> intGen = longs(Integer.MIN_VALUE, Integer.MAX_VALUE);
  Generator<Long> longGen = longs(Integer.MAX_VALUE + 1L, Long.MAX_VALUE);
  for (int i = 0; i < 10; i++) {
    Generator<List<Long>> listGen;
    if (i % 2 == 0) {
      // generate ints
      listGen = sortedLists(uniqueValues(intGen), 25, 25);
    } else {
      // generate longs
      listGen = sortedLists(uniqueValues(longGen), 25, 25);
    }
    List<Long> nums = listGen.next();
    data[i] = new KeyData(nums);
  }
  DATA_POINTS = data;
}

代码示例来源:origin: lenskit/lenskit

@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));
        }
      }
    }
  }
}

代码示例来源:origin: lenskit/lenskit

@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));
        }
      }
    }
  }
}

代码示例来源:origin: lenskit/lenskit

@Test
public void testAddScalarSorted() {
  for (Map<Long,Double> map: someMaps(longs(), doubles(-1000, 1000))) {
    double scalar = doubles(-250, 250).next();
    Long2DoubleMap m = Long2DoubleSortedArrayMap.create(map);
    Long2DoubleMap result = Vectors.addScalar(m, scalar);
    assertThat(Vectors.sum(result), closeTo(Vectors.sum(m) + m.size() * scalar, 1.0e-6));
    for (long key: result.keySet()) {
      assertThat(result.get(key), closeTo(map.get(key) + scalar, 1.0e-6));
    }
  }
}

代码示例来源:origin: lenskit/lenskit

@Test
public void testAddScalar() {
  for (Map<Long,Double> map: someMaps(longs(), doubles(-1000, 1000))) {
    double scalar = doubles(-250, 250).next();
    Long2DoubleMap m = new Long2DoubleOpenHashMap(map);
    Long2DoubleMap result = Vectors.addScalar(m, scalar);
    assertThat(Vectors.sum(result), closeTo(Vectors.sum(m) + m.size() * scalar, 1.0e-6));
    for (long key: result.keySet()) {
      assertThat(result.get(key), closeTo(map.get(key) + scalar, 1.0e-6));
    }
  }
}

代码示例来源:origin: net.java.quickcheck/quickcheck

@Override
public T next() throws GeneratorException {
  for(int idx = 0; idx < maxTries; idx++) {
    T value = generator.next();
    if(tryValue(value)) return value;
  }
  throw new GeneratorException(format("Failed to generate another value after [%s] tries (generator: %s)",
      maxTries, generator), generator);
}

代码示例来源:origin: net.java.quickcheck/quickcheck

@Override public Set<T> next() {
  Collections.shuffle(superset);
  int size = sizes.next();
  greaterOrEqual(minSize(superset), size , "subset size");
  lessOrEqual(maxSize(superset), size , "subset size");
  return new HashSet<T>(superset.subList(0, size));
}

相关文章

Generator类方法