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