001/* 002 * Licensed to the author under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package de.cuioss.test.generator.internal.net.java.quickcheck.generator; 018 019import static de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.CharacterGenerator.BASIC_LATIN; 020import static de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.CharacterGenerator.LATIN_1_SUPPLEMENT; 021import static de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.ListGenerator.MAX_SIZE; 022import static de.cuioss.tools.base.Preconditions.checkArgument; 023import static java.util.Arrays.asList; 024import static java.util.Collections.singletonList; 025import static java.util.concurrent.TimeUnit.MILLISECONDS; 026 027import java.util.Collections; 028import java.util.Comparator; 029import java.util.Date; 030import java.util.EnumSet; 031import java.util.Iterator; 032import java.util.List; 033import java.util.Map; 034import java.util.NoSuchElementException; 035import java.util.Objects; 036import java.util.Set; 037import java.util.concurrent.TimeUnit; 038 039import de.cuioss.test.generator.internal.net.java.quickcheck.ExtendibleGenerator; 040import de.cuioss.test.generator.internal.net.java.quickcheck.FrequencyGenerator; 041import de.cuioss.test.generator.internal.net.java.quickcheck.Generator; 042import de.cuioss.test.generator.internal.net.java.quickcheck.GeneratorException; 043import de.cuioss.test.generator.internal.net.java.quickcheck.ObjectGenerator; 044import de.cuioss.test.generator.internal.net.java.quickcheck.QuickCheck; 045import de.cuioss.test.generator.internal.net.java.quickcheck.StatefulGenerator; 046import de.cuioss.test.generator.internal.net.java.quickcheck.collection.Pair; 047import de.cuioss.test.generator.internal.net.java.quickcheck.collection.Triple; 048import de.cuioss.test.generator.internal.net.java.quickcheck.generator.distribution.Distribution; 049import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.AbstractTransformerGenerator; 050import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.ArrayGenerator; 051import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.ByteArrayGenerator; 052import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.ByteGenerator; 053import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.CharacterGenerator; 054import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.CloningGenerator; 055import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.DateGenerator; 056import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.DefaultFrequencyGenerator; 057import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.DoubleGenerator; 058import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.DuplicateGenerator; 059import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.EnsuredValuesGenerator; 060import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.ExcludingGenerator; 061import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.FixedValuesGenerator; 062import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.IntegerArrayGenerator; 063import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.IntegerGenerator; 064import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.IteratorGenerator; 065import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.ListGenerator; 066import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.LongGenerator; 067import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.MapGenerator; 068import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.ObjectDefaultMappingGenerator; 069import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.ObjectGeneratorImpl; 070import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.SetGenerator; 071import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.SortedListGenerator; 072import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.StrictlyOrderedGenerator; 073import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.StringGenerator; 074import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.SubmapGenerator; 075import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.SubsetGenerator; 076import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.SubstringGenerator; 077import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.TupleGenerator; 078import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.UniqueComparableValuesGenerator; 079import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.UniqueValuesGenerator; 080import de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.VetoableGenerator; 081 082@SuppressWarnings({ "WeakerAccess", "unused" }) 083public class Generators { 084 085 public static final int DEFAULT_STRING_MAX_LENGTH = StringGenerator.MAX_LENGTH; 086 public static final int DEFAULT_COLLECTION_MAX_SIZE = ListGenerator.MAX_SIZE; 087 // TODO this could be a bit high 088 // for runs = 200 this means 20000 tries for the worst case 089 public static final int DEFAULT_MAX_TRIES = VetoableGenerator.DEFAULT_MAX_TRIES; 090 091 private Generators() { 092 } 093 094 /** 095 * Cast a generator to a super type generator. 096 * <p> 097 * This method can be used to cast a generator of type Generator<A> to a 098 * generator of type Generator<B> given that A extends B. This operator is 099 * valid as all Generator instances are covariant (are pure producers). 100 * </p> 101 */ 102 @SuppressWarnings("unchecked") 103 public static <T> Generator<T> cast(Generator<? extends T> generator) { 104 return (Generator<T>) generator; 105 } 106 107 /** 108 * Convert a generator into a {@link Iterable iterable}.<br> 109 * 110 * The generator will be run {@link QuickCheck#MAX_NUMBER_OF_RUNS} times. 111 */ 112 public static <T> Iterable<T> toIterable(final Generator<T> generator) { 113 return toIterable(generator, QuickCheck.MAX_NUMBER_OF_RUNS); 114 } 115 116 /** 117 * Convert a generator into an {@link Iterable iterable}. 118 * 119 * @param numberOfRuns to execute the runner 120 */ 121 public static <T> Iterable<T> toIterable(final Generator<T> generator, final int numberOfRuns) { 122 Objects.requireNonNull(generator, "generator"); 123 checkArgument(0.0 <= numberOfRuns, "number of runs"); 124 return () -> new Iterator<>() { 125 126 private int runs; 127 128 @Override 129 public boolean hasNext() { 130 return runs < numberOfRuns; 131 } 132 133 @Override 134 public T next() { 135 if (!hasNext()) { 136 throw new NoSuchElementException(); 137 } 138 runs++; 139 return generator.next(); 140 } 141 142 @Override 143 public void remove() { 144 throw new UnsupportedOperationException(); 145 } 146 }; 147 } 148 149 /** 150 * Create a new string generator.<br> 151 * 152 * The characters are from the Basic Latin and Latin-1 Supplement unicode 153 * blocks. 154 */ 155 public static ExtendibleGenerator<Character, String> strings() { 156 return new StringGenerator(); 157 } 158 159 /** 160 * Create a new string generator which generates strings of characters ranging 161 * from lo to hi. 162 * 163 * @param lo lower boundary character 164 * @param hi upper boundary character 165 */ 166 public static ExtendibleGenerator<Character, String> strings(char lo, char hi) { 167 return new StringGenerator(lo, hi); 168 } 169 170 /** 171 * Create a new string generator which generates strings of characters from the 172 * given string. 173 */ 174 public static ExtendibleGenerator<Character, String> strings(String allowedCharacters) { 175 return new StringGenerator(characters(allowedCharacters)); 176 } 177 178 /** 179 * Create a new string generator which generates strings of characters from the 180 * given string with a length between min and max. 181 */ 182 public static ExtendibleGenerator<Character, String> strings(String allowedCharacters, int min, int max) { 183 return new StringGenerator(new IntegerGenerator(min, max), characters(allowedCharacters)); 184 } 185 186 /** 187 * Creates a new String genearator which generates strings whose length ranges 188 * from zero to given length. 189 */ 190 public static ExtendibleGenerator<Character, String> strings(int max) { 191 return strings(0, max); 192 } 193 194 /** 195 * Create a new string generator which generates strings of sizes ranging from 196 * loLength to hiLength. 197 * 198 * @param min lower size boundary 199 * @param max upper size boundary 200 */ 201 public static ExtendibleGenerator<Character, String> strings(int min, int max) { 202 return new StringGenerator(new IntegerGenerator(min, max), new CharacterGenerator()); 203 } 204 205 /** 206 * Create a new string generator which creates strings of characters generated 207 * by the given character generator with a length generated by the length 208 * generator. 209 * 210 */ 211 public static ExtendibleGenerator<Character, String> strings(Generator<Integer> length, 212 Generator<Character> characters) { 213 return new StringGenerator(length, characters); 214 } 215 216 /** 217 * Create a new string generator which creates strings of characters generated 218 * by the given character generator. 219 * 220 */ 221 public static ExtendibleGenerator<Character, String> strings(Generator<Character> characterGenerator) { 222 return new StringGenerator(characterGenerator); 223 } 224 225 /** 226 * Create a new string generator which creates strings of characters from a-z 227 * and A-Z. 228 */ 229 public static ExtendibleGenerator<Character, String> letterStrings() { 230 return new StringGenerator(characters('a', 'z')).add(characters('A', 'Z')); 231 } 232 233 /** 234 * Create a new string generator which creates strings with sizes ranging from 235 * loLengh to hiLength of characters from a-z and A-Z. 236 */ 237 public static ExtendibleGenerator<Character, String> letterStrings(int min, int max) { 238 var generator = new StringGenerator(new IntegerGenerator(min, max), characters('a', 'z')); 239 return generator.add(characters('A', 'Z')); 240 } 241 242 /** 243 * Create a new string generator which creates strings of characters generated 244 * by {@link Generators#basicLatinCharacters()} and 245 * {@link Generators#latin1SupplementCharacters()}. 246 */ 247 public static ExtendibleGenerator<Character, String> printableStrings() { 248 return new StringGenerator(basicLatinCharacters()).add(latin1SupplementCharacters()); 249 } 250 251 /** 252 * Create a new string generator for strings that are not empty. 253 */ 254 public static ExtendibleGenerator<Character, String> nonEmptyStrings() { 255 return strings(1, StringGenerator.MAX_LENGTH); 256 } 257 258 /** 259 * Create a new string generator for substrings of a base string. 260 * 261 * <p> 262 * base.contains(generated string) will always be true. 263 * </p> 264 */ 265 public static Generator<String> substrings(String base) { 266 return substrings(base, 0, base.length()); 267 } 268 269 /** 270 * Create a new string generator for substrings of a base string. 271 * 272 * <p> 273 * base.contains(generated string) will always be true. 274 * </p> 275 * 276 * @param size of the generated string 277 */ 278 public static Generator<String> substrings(String base, int size) { 279 return substrings(base, size, size); 280 } 281 282 /** 283 * Create a new string generator for substrings of a base string. 284 * 285 * <p> 286 * base.contains(generated string) will always be true. 287 * </p> 288 * 289 * @param minSize is the minimum size of the generated string 290 * @param maxSize is the maximum size of the generated string 291 */ 292 public static Generator<String> substrings(String base, int minSize, int maxSize) { 293 return new SubstringGenerator(base, minSize, maxSize); 294 } 295 296 /** 297 * Create a new character generator which generates characters ranging from lo 298 * to hi. 299 */ 300 public static Generator<Character> characters(char lo, char hi) { 301 return new CharacterGenerator(lo, hi); 302 } 303 304 /** 305 * Create a new character generator.<br> 306 * 307 * The characters are from the Basic Latin and Latin-1 Supplement unicode 308 * blocks. 309 */ 310 public static Generator<Character> characters() { 311 return new CharacterGenerator(); 312 } 313 314 /** 315 * Create a new character generator which generates characters from the given 316 * character array. 317 */ 318 public static Generator<Character> characters(Character... chars) { 319 return characters(asList(chars)); 320 } 321 322 /** 323 * Create a new character generator which generates characters from the given 324 * string. 325 */ 326 public static Generator<Character> characters(String string) { 327 var chars = new Character[string.length()]; 328 for (var i = 0; i < chars.length; i++) { 329 chars[i] = string.charAt(i); 330 } 331 return characters(chars); 332 } 333 334 /** 335 * Create a new character generator which generates characters from the given 336 * characters. 337 */ 338 public static Generator<Character> characters(Iterable<Character> chars) { 339 return new FixedValuesGenerator<>(chars); 340 } 341 342 /** 343 * Create a new character generator which generates latin-1 supplement 344 * characters. 345 */ 346 public static Generator<Character> latin1SupplementCharacters() { 347 return characters(LATIN_1_SUPPLEMENT.getFirst(), LATIN_1_SUPPLEMENT.getSecond()); 348 } 349 350 /** 351 * Create a new character generator which generates latin characters. 352 */ 353 public static Generator<Character> basicLatinCharacters() { 354 return characters(BASIC_LATIN.getFirst(), BASIC_LATIN.getSecond()); 355 } 356 357 /** 358 * Create a new integer generator which creates integers ranging from 359 * {@link Integer#MIN_VALUE} to {@link Integer#MAX_VALUE}. 360 * 361 */ 362 public static Generator<Integer> integers() { 363 return new IntegerGenerator(); 364 } 365 366 /** 367 * Create a new integer generator which creates integers that are at equal or 368 * greater than low. 369 */ 370 public static Generator<Integer> integers(int low) { 371 return new IntegerGenerator(low, Integer.MAX_VALUE); 372 } 373 374 /** 375 * Create a new integer generator which creates integers ranging from lo to hi. 376 */ 377 public static Generator<Integer> integers(int lo, int hi) { 378 return new IntegerGenerator(lo, hi); 379 } 380 381 /** 382 * Create a new integer generator which creates integers ranging from {@code lo} 383 * to {@code hi} based on the given {@link Distribution}. 384 */ 385 public static Generator<Integer> integers(int lo, int hi, Distribution distribution) { 386 return new IntegerGenerator(lo, hi, distribution); 387 } 388 389 /** 390 * Create a new integer generator which creates integers ranging from {@code 1} 391 * to {@link Integer#MAX_VALUE}. 392 */ 393 public static Generator<Integer> positiveIntegers() { 394 return positiveIntegers(Integer.MAX_VALUE); 395 } 396 397 /** 398 * Create a new integer generator which creates integers ranging from {@code 1} 399 * to {@code max} (which must be at least 1). 400 */ 401 public static Generator<Integer> positiveIntegers(int hi) { 402 return new IntegerGenerator(1, hi); 403 } 404 405 /** 406 * Create a new byte generator which creates byte values ranging from 407 * {@link Byte#MIN_VALUE} to {@link Byte#MAX_VALUE}. 408 */ 409 public static Generator<Byte> bytes() { 410 return new ByteGenerator(); 411 } 412 413 /** 414 * Create a new byte generator which creates byte values ranging from lo to hi. 415 */ 416 public static Generator<Byte> bytes(byte lo, byte hi) { 417 return new ByteGenerator(lo, hi); 418 } 419 420 /** 421 * Create a new integer generator which creates integers ranging from lo to hi 422 * based on the given {@link Distribution}. 423 */ 424 public static Generator<Byte> bytes(byte lo, byte hi, Distribution distribution) { 425 return new ByteGenerator(lo, hi, distribution); 426 } 427 428 /** 429 * Create a new long generator which creates longs ranging from 430 * {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE}. 431 */ 432 public static Generator<Long> longs() { 433 return new LongGenerator(); 434 } 435 436 /** 437 * Create a new long generator which creates longs ranging from lo to hi. 438 */ 439 public static Generator<Long> longs(long lo, long hi) { 440 return new LongGenerator(lo, hi, Distribution.UNIFORM); 441 } 442 443 /** 444 * Create a new long generator which creates longs ranging from lo to hi based 445 * on the given {@link Distribution}. 446 */ 447 public static Generator<Long> longs(long lo, long hi, Distribution distribution) { 448 return new LongGenerator(lo, hi, distribution); 449 } 450 451 /** 452 * Create a new long generator which creates long values ranging from 1 to 453 * {@link Long#MAX_VALUE}. 454 */ 455 public static Generator<Long> positiveLongs() { 456 return positiveLongs(Long.MAX_VALUE); 457 } 458 459 /** 460 * Create a new long generator which creates long values ranging from 1 to hi. 461 */ 462 public static Generator<Long> positiveLongs(long hi) { 463 return longs(1, hi); 464 } 465 466 /** 467 * Create a new double generator which creates doubles ranging from 468 * {@link Double#MIN_VALUE} to {@link Double#MAX_VALUE}. 469 */ 470 public static Generator<Double> doubles() { 471 return new DoubleGenerator(); 472 } 473 474 /** 475 * Create a new double generator which creates doubles ranging from lo to hi. 476 */ 477 public static Generator<Double> doubles(double lo, double hi) { 478 return new DoubleGenerator(lo, hi); 479 } 480 481 /** 482 * Create a new double generator which creates doubles ranging from lo to hi 483 * based on the given {@link Distribution}. 484 */ 485 public static Generator<Double> doubles(double lo, double hi, Distribution distribution) { 486 return new DoubleGenerator(lo, hi, distribution); 487 } 488 489 /** 490 * Create a generator for boolean values. 491 */ 492 public static Generator<Boolean> booleans() { 493 return new FixedValuesGenerator<>(asList(Boolean.TRUE, Boolean.FALSE)); 494 } 495 496 /** 497 * Create a generator for null values. 498 */ 499 public static <T> Generator<T> nulls() { 500 return new FixedValuesGenerator<>(); 501 } 502 503 /** 504 * Create a generator for date values. 505 */ 506 public static Generator<Date> dates() { 507 return dates(MILLISECONDS); 508 } 509 510 /** 511 * Create a generator for date values with the given precision. 512 */ 513 public static Generator<Date> dates(TimeUnit precision) { 514 return dates(Long.MIN_VALUE, Long.MAX_VALUE, precision); 515 } 516 517 /** 518 * Create a generator for date values from low to high. 519 */ 520 public static Generator<Date> dates(Date low, Date high) { 521 return dates(low.getTime(), high.getTime()); 522 } 523 524 /** 525 * Create a generator for date values from low to high. 526 */ 527 public static Generator<Date> dates(long low, long high) { 528 return dates(low, high, MILLISECONDS); 529 } 530 531 /** 532 * Create a generator for date values from low to high with the given precision. 533 */ 534 public static Generator<Date> dates(Long low, Long high, TimeUnit precision) { 535 return new DateGenerator(precision, low, high, DEFAULT_MAX_TRIES); 536 } 537 538 /** 539 * Create a generator for fixed value generator. 540 */ 541 public static <T> Generator<T> fixedValues(T value) { 542 return new FixedValuesGenerator<>(value); 543 } 544 545 /** 546 * Create a fixed value generator returning one of the values from the values 547 * array. 548 */ 549 @SafeVarargs 550 public static <T> Generator<T> fixedValues(T... values) { 551 return fixedValues(asList(values)); 552 } 553 554 /** 555 * Create a fixed value generator returning one of the values from the values. 556 */ 557 public static <T> Generator<T> fixedValues(Iterable<T> values) { 558 return new FixedValuesGenerator<>(values); 559 } 560 561 /** 562 * A cloning generator which uses object serialization to create clones of the 563 * prototype object. For each call a new copy of the prototype will be 564 * generated. 565 */ 566 public static <T> Generator<T> clonedValues(T prototype) { 567 return new CloningGenerator<>(prototype); 568 } 569 570 /** 571 * Create a generator of enumeration values. 572 * 573 * @param <T> Type of enumerations 574 * @param enumClass class of enumeration 575 * @return generator of enum values 576 */ 577 public static <T extends Enum<T>> Generator<T> enumValues(Class<T> enumClass) { 578 return enumValues(enumClass, Collections.emptyList()); 579 } 580 581 /** 582 * Create a generator of enumeration values. 583 * 584 * @param <T> Type of enumerations 585 * @param enumClass class of enumeration 586 * @param excluded excluded values of enumeration 587 * @return generator of enum values 588 */ 589 @SafeVarargs 590 public static <T extends Enum<T>> Generator<T> enumValues(Class<T> enumClass, T... excluded) { 591 return enumValues(enumClass, asList(excluded)); 592 } 593 594 /** 595 * Create a generator of enumeration values. 596 * 597 * @param <T> Type of enumerations 598 * @param enumClass class of enumeration 599 * @param excludedValues excluded values of enumeration 600 * @return generator of enum values 601 */ 602 public static <T extends Enum<T>> Generator<T> enumValues(Class<T> enumClass, Iterable<T> excludedValues) { 603 EnumSet<T> excluded = EnumSet.noneOf(enumClass); 604 for (T e : excludedValues) { 605 excluded.add(e); 606 } 607 return new FixedValuesGenerator<>(EnumSet.complementOf(excluded)); 608 } 609 610 /** 611 * Create a generator for {@link Object java.lang.Object} instances. 612 * <p> 613 * Note: every invocation of {@link Generator#next()} creates a new instance. 614 * </p> 615 */ 616 public static Generator<Object> objects() { 617 return new de.cuioss.test.generator.internal.net.java.quickcheck.generator.support.ObjectGenerator(); 618 } 619 620 /** 621 * Create a generator from a {@link ObjectGenerator declarative object generator 622 * definition}. 623 */ 624 public static <T> ObjectGenerator<T> objects(Class<T> objectType) { 625 return new ObjectGeneratorImpl<>(objectType); 626 } 627 628 /** 629 * Create a generator from a {@link ObjectGenerator declarative object generator 630 * definition}. 631 * <p> 632 * Default values will be used for all {@link ObjectGenerator#on(Object) 633 * undefined methods}. 634 * </p> 635 */ 636 public static <T> ObjectGenerator<T> defaultObjects(Class<T> objectType) { 637 return new ObjectDefaultMappingGenerator<>(objectType); 638 } 639 640 /** 641 * <p> 642 * Create a frequency generator. The frequency of {@link Generator} usage 643 * depends on the generator weight. 644 * </p> 645 * 646 * @param generator pairs of generators and their weights used to created the 647 * values 648 * @param <T> type of values generated by the generators. 649 */ 650 public static <T> FrequencyGenerator<T> frequency(Generator<T> generator, int weight) { 651 return new DefaultFrequencyGenerator<>(generator, weight); 652 } 653 654 /** 655 * OneOf is a convenience method for 656 * {@link Generators#frequency(Generator, int)} when all generator share the 657 * same weight. 658 */ 659 public static <T> ExtendibleGenerator<T, T> oneOf(Generator<T> generator) { 660 return frequency(generator, 1); 661 } 662 663 /** 664 * Create a generator which will create vectors (here lists) of type T. 665 * 666 * @param <T> Type of the list values. 667 * @param size Number of element in the vector. 668 */ 669 public static <T> Generator<List<T>> vectors(Generator<T> content, int size) { 670 return new ListGenerator<>(content, new FixedValuesGenerator<>(size)); 671 } 672 673 /** 674 * Create a generator of pairs of type A for the left value and type B for the 675 * right value. 676 * 677 * @param <A> Type of left value. 678 * @param <B> Type of right value. 679 * @param first Generator for left values. 680 * @param second Generator for right values. 681 */ 682 @SuppressWarnings("unchecked") 683 public static <A, B> Generator<Pair<A, B>> pairs(Generator<A> first, Generator<B> second) { 684 final var generator = new TupleGenerator(first, second); 685 return () -> { 686 var next = generator.next(); 687 return new Pair<>((A) next[0], (B) next[1]); 688 }; 689 } 690 691 /** 692 * Create a generator of pairs where first value <= second value. 693 * 694 * @param <T> Type of the pair values. 695 * @param content Generator for content of the pair values. 696 */ 697 public static <T extends Comparable<T>> Generator<Pair<T, T>> sortedPairs(Generator<T> content) { 698 return new AbstractTransformerGenerator<>(sortedLists(content, 2, 2)) { 699 700 @Override 701 protected Pair<T, T> transform(Generator<List<T>> inputGenerator) { 702 var next = inputGenerator.next(); 703 return new Pair<>(next.get(0), next.get(1)); 704 } 705 }; 706 } 707 708 /** 709 * Create a generator of triples of the types A, B and C for first, second and 710 * third value. 711 * 712 * @param <A> Type of first value. 713 * @param <B> Type of second value. 714 * @param <C> Type of third value. 715 * @param first Generator for first values. 716 * @param second Generator for second values. 717 * @param third Generator for third values. 718 */ 719 @SuppressWarnings("unchecked") 720 public static <A, B, C> Generator<Triple<A, B, C>> triples(Generator<A> first, Generator<B> second, 721 Generator<C> third) { 722 final var generator = new TupleGenerator(first, second, third); 723 return () -> { 724 var next = generator.next(); 725 return new Triple<>((A) next[0], (B) next[1], (C) next[2]); 726 }; 727 } 728 729 /** 730 * Create a generator returning a combination of a null values and the given 731 * values. 732 * 733 * @param <T> Type of the values generated. 734 */ 735 @SafeVarargs 736 public static <T> Generator<T> nullsAnd(T... values) { 737 return nullsAnd(new FixedValuesGenerator<>(asList(values))); 738 } 739 740 /** 741 * Create a generator of triples where first value <= second value <= 742 * third value. 743 * 744 * @param <T> Type of the triple values. 745 * @param content Generator for content of the triple values. 746 */ 747 public static <T extends Comparable<T>> Generator<Triple<T, T, T>> sortedTriple(Generator<T> content) { 748 return new AbstractTransformerGenerator<>(sortedLists(content, 3, 3)) { 749 750 @Override 751 protected Triple<T, T, T> transform(Generator<List<T>> inputGenerator) { 752 var next = inputGenerator.next(); 753 return new Triple<>(next.get(0), next.get(1), next.get(2)); 754 } 755 }; 756 } 757 758 /** 759 * Create a generator as a combination of a null value generator and generator 760 * of type T. 761 * 762 * @param <T> Type of the values generated. 763 */ 764 public static <T> Generator<T> nullsAnd(Generator<T> generator) { 765 return nullsAnd(generator, 5); 766 } 767 768 /** 769 * Create a generator as a combination of a null value generator and generator 770 * of type T. 771 * 772 * @param <T> Type of the values generated. 773 * @param weight weight of the provided generator 774 */ 775 public static <T> Generator<T> nullsAnd(Generator<T> generator, int weight) { 776 return new DefaultFrequencyGenerator<>(Generators.<T>nulls(), 1).add(generator, weight); 777 } 778 779 /** 780 * Create a generator of sets with values from the content generator. 781 * 782 * @param <T> type of set elements generated 783 * @param content generator providing the content of sets generated 784 */ 785 public static <T> Generator<Set<T>> sets(Generator<? extends T> content) { 786 return new SetGenerator<>(content); 787 } 788 789 /** 790 * Create a generator of sets with values from the content generator. 791 * 792 * @param <T> type of set elements generated 793 * @param content generator providing the content of sets generated 794 * @param size size of the sets generated 795 */ 796 public static <T> Generator<Set<T>> sets(Generator<? extends T> content, Generator<Integer> size) { 797 return new SetGenerator<>(content, size, DEFAULT_MAX_TRIES); 798 } 799 800 /** 801 * Create a generator of sets with values from the content generator. Length is 802 * between high and low. 803 * 804 * @param <T> type of set elements generated 805 * @param content generator providing the content of sets generated 806 * @param low minimal size 807 * @param high max size 808 */ 809 public static <T> Generator<Set<T>> sets(Generator<? extends T> content, int low, int high) { 810 return new SetGenerator<>(content, integers(low, high), DEFAULT_MAX_TRIES); 811 } 812 813 /** 814 * Create a generator of sets that are not empty. 815 * 816 * @param <T> type of set elements generated 817 * @param content generator providing the content of sets generated 818 */ 819 public static <T> Generator<Set<T>> nonEmptySets(Generator<? extends T> content) { 820 return sets(content, 1, SetGenerator.MAX_SIZE); 821 } 822 823 @SafeVarargs 824 public static <T> Generator<Set<T>> sets(T... superset) { 825 return sets(asList(superset)); 826 } 827 828 /** 829 * Create a generator of subsets from a given set. 830 * 831 * @param <T> type of set elements generated 832 * @param superset of the generated set 833 */ 834 public static <T> Generator<Set<T>> sets(Iterable<T> superset) { 835 return new SubsetGenerator<>(superset); 836 } 837 838 /** 839 * Create a generator of subsets from a given set. 840 * 841 * @param <T> type of set elements generated 842 * @param superset of the generated set 843 * @param size of the generated set 844 */ 845 public static <T> Generator<Set<T>> sets(Iterable<T> superset, Generator<Integer> size) { 846 return new SubsetGenerator<>(superset, size); 847 } 848 849 /** 850 * Create a generator that produces lists of duplicates. 851 * 852 * @return a list derived from the input values. At least one input value is 853 * more than once in the resulting list. 854 */ 855 @SafeVarargs 856 public static <T> Generator<List<T>> duplicates(T... input) { 857 return new DuplicateGenerator<>(asList(input)); 858 } 859 860 /** 861 * Create a generator that produces lists of duplicates. 862 * 863 * @return a list derived from the input values. At least one input value is 864 * more than once in the resulting list. 865 */ 866 public static <T> Generator<List<T>> duplicates(Iterable<T> input) { 867 return new DuplicateGenerator<>(input); 868 } 869 870 /** 871 * Create a generator of iterators. 872 * 873 * <p> 874 * Values of the elements will be taken from the content generator. 875 * </p> 876 * 877 * @param <T> type of iterator elements generated 878 * @param content generator providing the content of iterators generated 879 */ 880 public static <T> Generator<Iterator<T>> iterators(Generator<? extends T> content) { 881 return new IteratorGenerator<>(content); 882 } 883 884 /** 885 * Create a generator of iterators. 886 * 887 * <p> 888 * Values of the elements will be taken from the content generator. The 889 * generated iterator will have at least one element. 890 * </p> 891 * 892 * @param <T> type of iterator elements generated 893 * @param content generator providing the content of iterators generated 894 */ 895 public static <T> Generator<Iterator<T>> nonEmptyIterators(Generator<T> content) { 896 return new IteratorGenerator<>(content, 1, IteratorGenerator.MAX_SIZE); 897 } 898 899 /** 900 * Create a generator of iterators. 901 * 902 * <p> 903 * Values of the elements will be taken from the content generator. The length 904 * of the iterators will be determined with the size generator. 905 * </p> 906 * 907 * @param <T> type of iterator elements generated 908 * @param content generator providing the content of iterators generated 909 * @param size used to determine the number of elements of the iterator 910 */ 911 public static <T> Generator<Iterator<T>> iterators(Generator<? extends T> content, Generator<Integer> size) { 912 return new IteratorGenerator<>(content, size); 913 } 914 915 /** 916 * Create a generator of lists with values from the content generator. Length 917 * values of lists generated will be created with {@link Distribution#UNIFORM}. 918 * 919 * @param <T> type of list elements generated 920 * @param content generator providing the content of lists generated 921 */ 922 public static <T> Generator<List<T>> lists(Generator<? extends T> content) { 923 return new ListGenerator<>(content); 924 } 925 926 /** 927 * Create a generator of non-empty lists with values from the content generator. 928 * Length values of lists generated will be created with 929 * {@link Distribution#UNIFORM}. 930 * 931 * @param <T> type of list elements generated 932 * @param content generator providing the content of lists generated 933 */ 934 public static <T> Generator<List<T>> nonEmptyLists(Generator<? extends T> content) { 935 return lists(content, positiveIntegers(MAX_SIZE)); 936 } 937 938 /** 939 * Create a generator of lists with values from the content generator. Length 940 * values of lists generated will be created with size generator. 941 * 942 * @param <T> type of list elements generated 943 * @param content generator providing the content of lists generated 944 * @param size integer used to determine the list size 945 */ 946 public static <T> Generator<List<T>> lists(Generator<? extends T> content, Generator<Integer> size) { 947 return new ListGenerator<>(content, size); 948 } 949 950 /** 951 * Create a generator of lists with values from the content generator. Length is 952 * between high and low. 953 * 954 * @param <T> type of list elements generated 955 * @param content generator providing the content of lists generated 956 * @param low minimal size 957 * @param high max size 958 */ 959 public static <T> Generator<List<T>> lists(Generator<? extends T> content, int low, int high) { 960 return lists(content, new IntegerGenerator(low, high)); 961 } 962 963 /** 964 * Create a generator of lists with values from the content generator. Length is 965 * at least low. 966 * 967 * @param <T> type of list elements generated 968 * @param content generator providing the content of lists generated 969 * @param low minimal size. If low is larger than 970 * {@link Generators#DEFAULT_COLLECTION_MAX_SIZE} then it is the 971 * upper size bound as well. 972 */ 973 public static <T> Generator<List<T>> lists(Generator<? extends T> content, int low) { 974 return lists(content, low, Math.max(low, ListGenerator.MAX_SIZE)); 975 } 976 977 /** 978 * Create a generator of sorted lists with values from the content generator. 979 * 980 * @param <T> type of list elements generated 981 * @param content generator providing the content of lists generated 982 */ 983 public static <T extends Comparable<T>> Generator<List<T>> sortedLists(Generator<T> content) { 984 return new SortedListGenerator<>(content); 985 986 } 987 988 /** 989 * Create a generator of sorted lists with values from the content generator. 990 * Length is between high and low. 991 * 992 * @param <T> type of list elements generated 993 * @param content generator providing the content of lists generated 994 * @param low minimal size 995 * @param high max size 996 */ 997 public static <T extends Comparable<T>> Generator<List<T>> sortedLists(Generator<T> content, int low, int high) { 998 return sortedLists(content, integers(low, high)); 999 1000 } 1001 1002 /** 1003 * Create a generator of sorted lists with values from the content generator. 1004 * Length is between high and low. 1005 * 1006 * @param <T> type of list elements generated 1007 * @param content generator providing the content of lists generated 1008 * @param size integer used to determine the list size 1009 */ 1010 public static <T extends Comparable<T>> Generator<List<T>> sortedLists(Generator<T> content, 1011 Generator<Integer> size) { 1012 return new SortedListGenerator<>(content, size); 1013 } 1014 1015 /** 1016 * Create a generator of arrays with values from the content generator. Length 1017 * values of array generated will be created with {@link Distribution#UNIFORM}. 1018 * 1019 * @param <T> type of arrays elements generated 1020 * @param content generator providing the content of arrays generated 1021 * @param type type of arrays generated 1022 */ 1023 public static <T> Generator<T[]> arrays(Generator<? extends T> content, Class<T> type) { 1024 return new ArrayGenerator<>(content, type); 1025 } 1026 1027 /** 1028 * Create a generator of arrays that are not empty. 1029 * 1030 * @param <T> type of arrays elements generated 1031 * @param content generator providing the content of arrays generated 1032 * @param type type of arrays generated 1033 */ 1034 public static <T> Generator<T[]> nonEmptyArrays(Generator<? extends T> content, Class<T> type) { 1035 return arrays(content, positiveIntegers(MAX_SIZE), type); 1036 } 1037 1038 /** 1039 * Create a generator of arrays with values from the content generator. Length 1040 * values of arrays generated will be created with size generator. 1041 * 1042 * @param <T> type of arrays elements generated 1043 * @param content generator providing the content of arrays generated 1044 * @param size integer used to determine the array size 1045 * @param type type of arrays generated 1046 */ 1047 public static <T> Generator<T[]> arrays(Generator<? extends T> content, Generator<Integer> size, Class<T> type) { 1048 return new ArrayGenerator<>(content, size, type); 1049 } 1050 1051 /** 1052 * Create a generator of byte arrays. The length of arrays generated will be 1053 * determined by the {@link ByteArrayGenerator#MIN_SIZE} and 1054 * {@link ByteArrayGenerator#MAX_SIZE} constants. 1055 * 1056 */ 1057 public static Generator<byte[]> byteArrays() { 1058 return new ByteArrayGenerator(); 1059 } 1060 1061 /** 1062 * Create a generator of byte arrays. Length values of arrays generated will be 1063 * created with size generator. 1064 * 1065 * @param size integer used to determine the array size 1066 */ 1067 public static Generator<byte[]> byteArrays(Generator<Integer> size) { 1068 return new ByteArrayGenerator(size); 1069 } 1070 1071 /** 1072 * Create a generator of byte arrays. Length values of arrays generated will be 1073 * created with size generator. 1074 * 1075 * @param size integer used to determine the array size 1076 * @param content generator for the byte array content 1077 */ 1078 public static Generator<byte[]> byteArrays(Generator<Byte> content, Generator<Integer> size) { 1079 return new ByteArrayGenerator(content, size); 1080 } 1081 1082 /** 1083 * Create a generator of integer arrays. 1084 * 1085 */ 1086 public static Generator<int[]> intArrays() { 1087 return new IntegerArrayGenerator(); 1088 } 1089 1090 /** 1091 * Create a generator of integer arrays. Length values of arrays generated will 1092 * be created with size generator. 1093 * 1094 * @param size integer used to determine the array size 1095 */ 1096 public static Generator<int[]> intArrays(Generator<Integer> size) { 1097 return new IntegerArrayGenerator(size); 1098 } 1099 1100 /** 1101 * Create a generator of integer arrays. Length values of arrays generated will 1102 * be created with size generator. 1103 * 1104 * @param size integer used to determine the array size 1105 * @param content generator for the integer array content 1106 */ 1107 public static Generator<int[]> intArrays(Generator<Integer> content, Generator<Integer> size) { 1108 return new IntegerArrayGenerator(content, size); 1109 } 1110 1111 /** 1112 * Create a generator of {@link Map maps}. 1113 * 1114 * <p> 1115 * This is a generator for simple maps where the values are not related to the 1116 * keys. 1117 * </p> 1118 * 1119 * @param keys {@link Generator} for the keys of the map 1120 * @param values {@link Generator} for the values of the map 1121 */ 1122 public static <K, V> Generator<Map<K, V>> maps(Generator<K> keys, Generator<V> values) { 1123 return new MapGenerator<>(keys, values); 1124 } 1125 1126 /** 1127 * Create a generator of {@link Map maps}. 1128 * 1129 * <p> 1130 * This is a generator for simple maps where the values are not related to the 1131 * keys. 1132 * </p> 1133 * 1134 * @param keys {@link Generator} for the keys of the map 1135 * @param values {@link Generator} for the values of the map 1136 * @param size integer used to determine the size of the generated map 1137 */ 1138 public static <K, V> Generator<Map<K, V>> maps(Generator<K> keys, Generator<V> values, Generator<Integer> size) { 1139 return new MapGenerator<>(keys, values, size); 1140 } 1141 1142 /** 1143 * Create a generator of maps from a given map. 1144 * 1145 * <p> 1146 * The entry set of the generated maps are subsets of the given map's entry set. 1147 * </p> 1148 * 1149 * @param supermap of the generated maps 1150 */ 1151 public static <K, V> Generator<Map<K, V>> maps(Map<K, V> supermap) { 1152 return new SubmapGenerator<>(supermap); 1153 } 1154 1155 /** 1156 * Create a generator of maps from a given map. 1157 * <p> 1158 * The entry set of the generated maps are subsets of the given map's entry set. 1159 * </p> 1160 * 1161 * @param supermap of the generated maps 1162 * @param sizes of the generated maps 1163 */ 1164 public static <K, V> Generator<Map<K, V>> maps(Map<K, V> supermap, Generator<Integer> sizes) { 1165 return new SubmapGenerator<>(supermap, sizes); 1166 } 1167 1168 /** 1169 * Create a deterministic generator which guarantees that all values from the 1170 * ensuredValues collection will be returned if enough calls to 1171 * {@link Generator#next()} are issued (i.e. ensuredValues.size() <= # of 1172 * runs). The order of values is undefined. 1173 * 1174 * @param <T> type of values return by the generator 1175 */ 1176 public static <T> StatefulGenerator<T> ensureValues(Iterable<T> ensuredValues) { 1177 return new EnsuredValuesGenerator<>(ensuredValues); 1178 } 1179 1180 /** 1181 * Create a deterministic generator which guarantees that all values from the 1182 * ensuredValues array will be returned if enough calls to 1183 * {@link Generator#next()} are issued (i.e. ensuredValues.size() <= # of 1184 * runs). The order of values is undefined. 1185 * 1186 * @param <T> type of values return by the generator 1187 */ 1188 @SafeVarargs 1189 public static <T> StatefulGenerator<T> ensureValues(T... content) { 1190 return ensureValues(asList(content)); 1191 } 1192 1193 /** 1194 * <p> 1195 * Create a deterministic generator which guarantees that all values from the 1196 * ensuredValues collection will be returned if enough calls to 1197 * {@link Generator#next()} are issued (i.e. ensuredValues.size() <= # of 1198 * runs). The order of values is undefined. 1199 * </p> 1200 * <p> 1201 * If all values of ensuredValues are generated calls to 1202 * {@link Generator#next()} will return values from the otherValues generator. 1203 * </p> 1204 * 1205 * @param <T> type of values return by the generator 1206 */ 1207 public static <T> StatefulGenerator<T> ensureValues(Iterable<T> ensuredValues, Generator<T> otherValues) { 1208 return new EnsuredValuesGenerator<>(ensuredValues, otherValues); 1209 } 1210 1211 /** 1212 * <p> 1213 * Create a generator which guarantees that all values from the ensuredValues 1214 * will be returned in a defined window when enough calls to 1215 * {@link Generator#next()} are issued. 1216 * </p> 1217 * <p> 1218 * The order of values is undefined. All other values in the window and after 1219 * the window are taken from the {@link Generator generator otherValues}. 1220 * </p> 1221 * 1222 * @param <T> type of values return by the generator 1223 * @param window After window number of calls to {@link Generator#next()} it is 1224 * guaranteed that all ensured values were returned. 1225 */ 1226 public static <T> StatefulGenerator<T> ensureValues(Iterable<T> ensuredValues, int window, 1227 Generator<T> otherValues) { 1228 return new EnsuredValuesGenerator<>(ensuredValues, window, otherValues); 1229 } 1230 1231 /** 1232 * <p> 1233 * Create a generator that ensures unique values. 1234 * </p> 1235 * <p> 1236 * The actual values are created with an arbitrary generator. 1237 * </p> 1238 * <p> 1239 * Note: unique generator depends on valid implementation of equals and hashCode 1240 * method of the content type generated. 1241 * </p> 1242 * 1243 * @param <T> type of values return by the generator 1244 * @param generator used to create the raw values. This generator can create 1245 * duplicate values 1246 * @param tries Number of tries to create a new unique value. After this 1247 * number of tries is exceeded the generation aborts with a 1248 * {@link GeneratorException}. 1249 * @return unique generator instance 1250 */ 1251 public static <T> StatefulGenerator<T> uniqueValues(Generator<T> generator, int tries) { 1252 return new UniqueValuesGenerator<>(generator, tries); 1253 } 1254 1255 /** 1256 * <p> 1257 * Create a generator that ensures unique values. 1258 * </p> 1259 * <p> 1260 * The actual values are created with an arbitrary generator. 1261 * </p> 1262 * <p> 1263 * Unique generator depends on the {@link Comparator} implementation to decide 1264 * if two instances are the same (i.e. when the comparator returns 0 for 1265 * {@link Comparator#compare(Object, Object)}). 1266 * </p> 1267 * 1268 * @param <T> type of values returned by the generator 1269 * @param generator used to create the raw values. This generator can create 1270 * duplicate values 1271 * @param comparator that decides if two values are of the same equivalence 1272 * class. 1273 * @param tries Number of tries to create a new unique value. After this 1274 * number of tries is exceeded the generation aborts with a 1275 * {@link GeneratorException}. 1276 * @return unique generator instance 1277 */ 1278 public static <T> StatefulGenerator<T> uniqueValues(Generator<T> generator, Comparator<? super T> comparator, 1279 int tries) { 1280 return new UniqueComparableValuesGenerator<>(generator, comparator, tries); 1281 } 1282 1283 /** 1284 * <p> 1285 * Create a generator that ensures unique values. 1286 * </p> 1287 * <p> 1288 * The actual values are created with an arbitrary generator. 1289 * </p> 1290 * <p> 1291 * Unique generator depends on the {@link Comparator} implementation to decide 1292 * if two instances are the same (i.e. when the comparator returns 0 for 1293 * {@link Comparator#compare(Object, Object)}). 1294 * </p> 1295 * 1296 * @param <T> type of values returned by the generator 1297 * @param generator used to create the raw values. This generator can create 1298 * duplicate values 1299 * @param comparator that decides if two values are of the same equivalence 1300 * class. 1301 * @return unique generator instance 1302 */ 1303 public static <T> StatefulGenerator<T> uniqueValues(Generator<T> generator, Comparator<? super T> comparator) { 1304 return uniqueValues(generator, comparator, DEFAULT_MAX_TRIES); 1305 } 1306 1307 /** 1308 * <p> 1309 * Create a generator that ensures unique values 1310 * </p> 1311 * <p> 1312 * The actual values are created with an arbitrary generator. 1313 * </p> 1314 * <p> 1315 * Note: unique generator depends on valid implementation of equals and hashCode 1316 * method of the content type generated. 1317 * </p> 1318 * 1319 * @param <T> type of values return by the generator 1320 * @param generator used to create the raw values. This generator can create 1321 * duplicate values 1322 * @return unique generator instance 1323 */ 1324 public static <T> StatefulGenerator<T> uniqueValues(Generator<T> generator) { 1325 return new UniqueValuesGenerator<>(generator, DEFAULT_MAX_TRIES); 1326 } 1327 1328 /** 1329 * Create a generator that omits a given value. 1330 * 1331 * @param generator used to create the raw values. 1332 * @param excluded value. This value will not be returned. 1333 */ 1334 public static <T> Generator<T> excludeValues(Generator<T> generator, T excluded) { 1335 return excludeValues(generator, singletonList(excluded)); 1336 } 1337 1338 /** 1339 * Create a generator that omits a given set of values. 1340 * 1341 * @param generator used to create the raw values. 1342 * @param excluded values. These values will not be returned. 1343 */ 1344 @SafeVarargs 1345 public static <T> Generator<T> excludeValues(Generator<T> generator, T... excluded) { 1346 return excludeValues(generator, asList(excluded)); 1347 } 1348 1349 /** 1350 * Create a generator that omits a given set of values. 1351 * 1352 * @param values of generator 1353 * @param excluded values. These values will not be returned. 1354 */ 1355 @SafeVarargs 1356 public static <T> Generator<T> excludeValues(Iterable<T> values, T... excluded) { 1357 return excludeValues(values, asList(excluded)); 1358 } 1359 1360 /** 1361 * Create a generator that omits a given set of values. 1362 * 1363 * @param values of generator 1364 * @param excluded values. These values will not be returned. 1365 */ 1366 public static <T> Generator<T> excludeValues(Iterable<T> values, Iterable<T> excluded) { 1367 return excludeValues(fixedValues(values), excluded); 1368 } 1369 1370 /** 1371 * Create a generator that omits a given set of values. 1372 * 1373 * @param generator used to create the raw values. 1374 * @param excluded values. These values will not be returned. 1375 */ 1376 public static <T> Generator<T> excludeValues(Generator<T> generator, Iterable<T> excluded) { 1377 return new ExcludingGenerator<>(generator, excluded, DEFAULT_MAX_TRIES); 1378 } 1379 1380 /** 1381 * A generator for a lists. The values in the lists are strictly increasing. 1382 * 1383 * <p> 1384 * For every element x in the list: x(n) < x(n+1). 1385 * </p> 1386 * 1387 * @param input values generator 1388 */ 1389 public static <T extends Comparable<T>> Generator<List<T>> strictlyOrdered(Generator<T> input) { 1390 return strictlyOrdered(input, 0, DEFAULT_COLLECTION_MAX_SIZE); 1391 } 1392 1393 /** 1394 * A generator for a lists. The values in the lists are strictly increasing. 1395 * 1396 * <p> 1397 * For every element x in the list: x(n) < x(n+1). 1398 * </p> 1399 * 1400 * @param input values generator 1401 * @param low minimum size of the lists 1402 * @param high maximum size of the lists 1403 */ 1404 public static <T extends Comparable<T>> Generator<List<T>> strictlyOrdered(Generator<T> input, int low, int high) { 1405 Comparator<T> natural = Comparable::compareTo; 1406 return strictlyOrdered(input, natural, new IntegerGenerator(low, high)); 1407 } 1408 1409 /** 1410 * A generator for a lists. The values in the lists are strictly increasing. 1411 * 1412 * <p> 1413 * For every element x in the list: x(n) < x(n+1). 1414 * </p> 1415 * 1416 * <p> 1417 * This {@link Generator} can be used to generate a list of strictly decreasing 1418 * values: 1419 * {@code Generators.strictlyOrdered(ts, Collections.<T> reverseOrder());} 1420 * </p> 1421 * 1422 * 1423 * @param input values generator 1424 * @param comparator that orders the values 1425 */ 1426 public static <T> Generator<List<T>> strictlyOrdered(Generator<T> input, Comparator<T> comparator) { 1427 return strictlyOrdered(input, comparator, new IntegerGenerator(0, DEFAULT_COLLECTION_MAX_SIZE)); 1428 } 1429 1430 /** 1431 * A generator for a lists. The values in the lists are strictly increasing. 1432 * <p> 1433 * For every element x in the list: x(n) < x(n+1). 1434 * </p> 1435 * 1436 * @param input values generator 1437 * @param comparator that orders the values 1438 * @param size of the resulting lists 1439 */ 1440 public static <T> Generator<List<T>> strictlyOrdered(Generator<T> input, Comparator<T> comparator, 1441 Generator<Integer> size) { 1442 return new StrictlyOrderedGenerator<>(input, comparator, size); 1443 } 1444}