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 java.util.Date; 020import java.util.concurrent.TimeUnit; 021 022import de.cuioss.test.generator.internal.net.java.quickcheck.ExtendibleGenerator; 023import de.cuioss.test.generator.internal.net.java.quickcheck.Generator; 024import de.cuioss.test.generator.internal.net.java.quickcheck.ObjectGenerator; 025import de.cuioss.test.generator.internal.net.java.quickcheck.generator.distribution.Distribution; 026 027/** 028 * {@link PrimitiveGenerators} contains factory methods for primitive value 029 * generators. These can be used to build custom test case generators. 030 * <p> 031 * The default distribution for generators is {@link Distribution#UNIFORM}. 032 * </p> 033 */ 034@SuppressWarnings({ "WeakerAccess", "unused" }) 035public class PrimitiveGenerators { 036 037 public static final int DEFAULT_STRING_MAX_LENGTH = Generators.DEFAULT_STRING_MAX_LENGTH; 038 039 /** 040 * Create a new string generator.<br> 041 * 042 * The characters are from the Basic Latin and Latin-1 Supplement unicode 043 * blocks. 044 */ 045 public static ExtendibleGenerator<Character, String> strings() { 046 return Generators.strings(); 047 } 048 049 /** 050 * Create a new string generator which generates strings of characters ranging 051 * from lo to hi. 052 * 053 * @param lo lower boundary character 054 * @param hi upper boundary character 055 */ 056 public static ExtendibleGenerator<Character, String> strings(char lo, char hi) { 057 return Generators.strings(lo, hi); 058 } 059 060 /** 061 * Create a new string generator which generates strings of characters from the 062 * given string. 063 */ 064 public static ExtendibleGenerator<Character, String> strings(String allowedCharacters) { 065 return Generators.strings(allowedCharacters); 066 } 067 068 /** 069 * Create a new string generator which generates strings of characters from the 070 * given string with a length between min and max. 071 */ 072 public static ExtendibleGenerator<Character, String> strings(String allowedCharacters, int min, int max) { 073 return Generators.strings(allowedCharacters, min, max); 074 } 075 076 /** 077 * Creates a new String genearator which generates strings whose length ranges 078 * from zero to given length. 079 */ 080 public static ExtendibleGenerator<Character, String> strings(int max) { 081 return Generators.strings(max); 082 } 083 084 /** 085 * Create a new string generator which generates strings of sizes ranging from 086 * loLength to hiLength. 087 * 088 * @param min lower size boundary 089 * @param max upper size boundary 090 */ 091 public static ExtendibleGenerator<Character, String> strings(int min, int max) { 092 return Generators.strings(min, max); 093 } 094 095 /** 096 * Create a new string generator which creates strings of characters generated 097 * by the given character generator with a length generated by the length 098 * generator. 099 * 100 */ 101 public static ExtendibleGenerator<Character, String> strings(Generator<Integer> length, 102 Generator<Character> characters) { 103 return Generators.strings(length, characters); 104 } 105 106 /** 107 * Create a new string generator which creates strings of characters generated 108 * by the given character generator. 109 * 110 */ 111 public static ExtendibleGenerator<Character, String> strings(Generator<Character> characterGenerator) { 112 return Generators.strings(characterGenerator); 113 } 114 115 /** 116 * Create a new string generator which creates strings of characters from a-z 117 * and A-Z. 118 */ 119 public static ExtendibleGenerator<Character, String> letterStrings() { 120 return Generators.letterStrings(); 121 } 122 123 /** 124 * Create a new string generator which creates strings with sizes ranging from 125 * loLengh to hiLength of characters from a-z and A-Z. 126 */ 127 public static ExtendibleGenerator<Character, String> letterStrings(int min, int max) { 128 return Generators.letterStrings(min, max); 129 } 130 131 /** 132 * Create a new string generator which creates strings of characters generated 133 * by {@link PrimitiveGenerators#basicLatinCharacters()} and 134 * {@link PrimitiveGenerators#latin1SupplementCharacters()}. 135 */ 136 public static ExtendibleGenerator<Character, String> printableStrings() { 137 return Generators.printableStrings(); 138 } 139 140 /** 141 * Create a new string generator for strings that are not empty. 142 */ 143 public static ExtendibleGenerator<Character, String> nonEmptyStrings() { 144 return Generators.nonEmptyStrings(); 145 } 146 147 /** 148 * Create a new string generator for substrings of a base string. 149 * 150 * <p> 151 * base.contains(generated string) will always be true. 152 * </p> 153 */ 154 public static Generator<String> substrings(String base) { 155 return Generators.substrings(base); 156 } 157 158 /** 159 * Create a new string generator for substrings of a base string. 160 * 161 * <p> 162 * base.contains(generated string) will always be true. 163 * </p> 164 * 165 * @param size of the generated string 166 */ 167 public static Generator<String> substrings(String base, int size) { 168 return Generators.substrings(base, size); 169 } 170 171 /** 172 * Create a new string generator for substrings of a base string. 173 * 174 * <p> 175 * base.contains(generated string) will always be true. 176 * </p> 177 * 178 * @param minSize is the minimum size of the generated string 179 * @param maxSize is the maximum size of the generated string 180 */ 181 public static Generator<String> substrings(String base, int minSize, int maxSize) { 182 return Generators.substrings(base, minSize, maxSize); 183 } 184 185 /** 186 * Create a new character generator which generates characters ranging from lo 187 * to hi. 188 */ 189 public static Generator<Character> characters(char lo, char hi) { 190 return Generators.characters(lo, hi); 191 } 192 193 /** 194 * Create a new character generator.<br> 195 * 196 * The characters are from the Basic Latin and Latin-1 Supplement unicode 197 * blocks. 198 */ 199 public static Generator<Character> characters() { 200 return Generators.characters(); 201 } 202 203 /** 204 * Create a new character generator which generates characters from the given 205 * character array. 206 */ 207 public static Generator<Character> characters(Character... chars) { 208 return Generators.characters(chars); 209 } 210 211 /** 212 * Create a new character generator which generates characters from the given 213 * string. 214 */ 215 public static Generator<Character> characters(String string) { 216 return Generators.characters(string); 217 } 218 219 /** 220 * Create a new character generator which generates characters from the given 221 * characters. 222 */ 223 public static Generator<Character> characters(Iterable<Character> chars) { 224 return Generators.characters(chars); 225 } 226 227 /** 228 * Create a new character generator which generates latin-1 supplement 229 * characters. 230 */ 231 public static Generator<Character> latin1SupplementCharacters() { 232 return Generators.latin1SupplementCharacters(); 233 } 234 235 /** 236 * Create a new character generator which generates latin characters. 237 */ 238 public static Generator<Character> basicLatinCharacters() { 239 return Generators.basicLatinCharacters(); 240 } 241 242 /** 243 * Create a new integer generator which creates integers ranging from 244 * {@link Integer#MIN_VALUE} to {@link Integer#MAX_VALUE}. 245 * 246 */ 247 public static Generator<Integer> integers() { 248 return Generators.integers(); 249 } 250 251 /** 252 * Create a new integer generator which creates integers that are at equal or 253 * greater than low. 254 */ 255 public static Generator<Integer> integers(int low) { 256 return Generators.integers(low); 257 } 258 259 /** 260 * Create a new integer generator which creates integers ranging from lo to hi. 261 */ 262 public static Generator<Integer> integers(int lo, int hi) { 263 return Generators.integers(lo, hi); 264 } 265 266 /** 267 * Create a new integer generator which creates integers ranging from {@code lo} 268 * to {@code hi} based on the given {@link Distribution}. 269 */ 270 public static Generator<Integer> integers(int lo, int hi, Distribution distribution) { 271 return Generators.integers(lo, hi, distribution); 272 } 273 274 /** 275 * Create a new integer generator which creates integers ranging from {@code 1} 276 * to {@link Integer#MAX_VALUE}. 277 */ 278 public static Generator<Integer> positiveIntegers() { 279 return Generators.positiveIntegers(); 280 } 281 282 /** 283 * Create a new integer generator which creates integers ranging from {@code 1} 284 * to {@code max} (which must be at least 1). 285 */ 286 public static Generator<Integer> positiveIntegers(int hi) { 287 return Generators.positiveIntegers(hi); 288 } 289 290 /** 291 * Create a new byte generator which creates byte values ranging from 292 * {@link Byte#MIN_VALUE} to {@link Byte#MAX_VALUE}. 293 */ 294 public static Generator<Byte> bytes() { 295 return Generators.bytes(); 296 } 297 298 /** 299 * Create a new byte generator which creates byte values ranging from lo to hi. 300 */ 301 public static Generator<Byte> bytes(byte lo, byte hi) { 302 return Generators.bytes(lo, hi); 303 } 304 305 /** 306 * Create a new integer generator which creates integers ranging from lo to hi 307 * based on the given {@link Distribution}. 308 */ 309 public static Generator<Byte> bytes(byte lo, byte hi, Distribution distribution) { 310 return Generators.bytes(lo, hi, distribution); 311 } 312 313 /** 314 * Create a new long generator which creates longs ranging from 315 * {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE}. 316 */ 317 public static Generator<Long> longs() { 318 return Generators.longs(); 319 } 320 321 /** 322 * Create a new long generator which creates longs ranging from lo to hi. 323 */ 324 public static Generator<Long> longs(long lo, long hi) { 325 return Generators.longs(lo, hi); 326 } 327 328 /** 329 * Create a new long generator which creates longs ranging from lo to hi based 330 * on the given {@link Distribution}. 331 */ 332 public static Generator<Long> longs(long lo, long hi, Distribution distribution) { 333 return Generators.longs(lo, hi, distribution); 334 } 335 336 /** 337 * Create a new long generator which creates long values ranging from 1 to 338 * {@link Long#MAX_VALUE}. 339 */ 340 public static Generator<Long> positiveLongs() { 341 return Generators.positiveLongs(); 342 } 343 344 /** 345 * Create a new long generator which creates long values ranging from 1 to hi. 346 */ 347 public static Generator<Long> positiveLongs(long hi) { 348 return Generators.positiveLongs(hi); 349 } 350 351 /** 352 * Create a new double generator which creates doubles ranging from 353 * {@link Double#MIN_VALUE} to {@link Double#MAX_VALUE}. 354 */ 355 public static Generator<Double> doubles() { 356 return Generators.doubles(); 357 } 358 359 /** 360 * Create a new double generator which creates doubles ranging from lo to hi. 361 */ 362 public static Generator<Double> doubles(double lo, double hi) { 363 return Generators.doubles(lo, hi); 364 } 365 366 /** 367 * Create a new double generator which creates doubles ranging from lo to hi 368 * based on the given {@link Distribution}. 369 */ 370 public static Generator<Double> doubles(double lo, double hi, Distribution distribution) { 371 return Generators.doubles(lo, hi, distribution); 372 } 373 374 /** 375 * Create a generator for boolean values. 376 */ 377 public static Generator<Boolean> booleans() { 378 return Generators.booleans(); 379 } 380 381 /** 382 * Create a generator for null values. 383 */ 384 public static <T> Generator<T> nulls() { 385 return Generators.nulls(); 386 } 387 388 /** 389 * Create a generator for date values. 390 */ 391 public static Generator<Date> dates() { 392 return Generators.dates(); 393 } 394 395 /** 396 * Create a generator for date values with the given precision. 397 */ 398 public static Generator<Date> dates(TimeUnit precision) { 399 return Generators.dates(precision); 400 } 401 402 /** 403 * Create a generator for date values from low to high. 404 */ 405 public static Generator<Date> dates(Date low, Date high) { 406 return Generators.dates(low, high); 407 } 408 409 /** 410 * Create a generator for date values from low to high. 411 */ 412 public static Generator<Date> dates(long low, long high) { 413 return Generators.dates(low, high); 414 } 415 416 /** 417 * Create a generator for date values from low to high with the given precision. 418 */ 419 public static Generator<Date> dates(Long low, Long high, TimeUnit precision) { 420 return Generators.dates(low, high, precision); 421 } 422 423 /** 424 * Create a generator for fixed value generator. 425 */ 426 public static <T> Generator<T> fixedValues(T value) { 427 return Generators.fixedValues(value); 428 } 429 430 /** 431 * Create a fixed value generator return values array. 432 */ 433 @SafeVarargs 434 public static <T> Generator<T> fixedValues(T... values) { 435 return Generators.fixedValues(values); 436 } 437 438 /** 439 * Create a fixed value generator return values. 440 */ 441 public static <T> Generator<T> fixedValues(Iterable<T> values) { 442 return Generators.fixedValues(values); 443 } 444 445 /** 446 * A cloning generator which uses object serialization to create clones of the 447 * prototype object. For each call a new copy of the prototype will be 448 * generated. 449 */ 450 public static <T> Generator<T> clonedValues(T prototype) { 451 return Generators.clonedValues(prototype); 452 } 453 454 /** 455 * Create a generator of enumeration values. 456 * 457 * @param <T> Type of enumerations 458 * @param enumClass class of enumeration 459 * @return generator of enum values 460 */ 461 public static <T extends Enum<T>> Generator<T> enumValues(Class<T> enumClass) { 462 return Generators.enumValues(enumClass); 463 } 464 465 /** 466 * Create a generator of enumeration values. 467 * 468 * @param <T> Type of enumerations 469 * @param enumClass class of enumeration 470 * @param excluded excluded values of enumeration 471 * @return generator of enum values 472 */ 473 @SafeVarargs 474 public static <T extends Enum<T>> Generator<T> enumValues(Class<T> enumClass, T... excluded) { 475 return Generators.enumValues(enumClass, excluded); 476 } 477 478 /** 479 * Create a generator of enumeration values. 480 * 481 * @param <T> Type of enumerations 482 * @param enumClass class of enumeration 483 * @param excludedValues excluded values of enumeration 484 * @return generator of enum values 485 */ 486 public static <T extends Enum<T>> Generator<T> enumValues(Class<T> enumClass, Iterable<T> excludedValues) { 487 return Generators.enumValues(enumClass, excludedValues); 488 } 489 490 /** 491 * Create a generator for {@link Object java.lang.Object} instances. 492 * <p> 493 * Note: every invocation of {@link Generator#next()} creates a new instance. 494 * </p> 495 */ 496 public static Generator<Object> objects() { 497 return Generators.objects(); 498 } 499 500 /** 501 * Create a generator from a {@link ObjectGenerator declarative object generator 502 * definition}. 503 */ 504 public static <T> ObjectGenerator<T> objects(Class<T> objectType) { 505 return Generators.objects(objectType); 506 } 507 508 /** 509 * Create a generator from a {@link ObjectGenerator declarative object generator 510 * definition}. 511 * <p> 512 * Default values will be used for all {@link ObjectGenerator#on(Object) 513 * undefined methods}. 514 * </p> 515 */ 516 public static <T> ObjectGenerator<T> defaultObjects(Class<T> objectType) { 517 return Generators.defaultObjects(objectType); 518 } 519}