001 package fj.test; 002 003 import fj.Effect; 004 import fj.F; 005 import fj.F2; 006 import fj.F3; 007 import fj.F4; 008 import fj.F5; 009 import fj.F6; 010 import fj.F7; 011 import fj.F8; 012 import fj.Function; 013 import static fj.Function.compose; 014 import static fj.P.p; 015 import fj.P1; 016 import fj.P2; 017 import fj.P3; 018 import fj.P4; 019 import fj.P5; 020 import fj.P6; 021 import fj.P7; 022 import fj.P8; 023 import fj.data.Array; 024 import fj.data.Either; 025 import static fj.data.Either.left; 026 import static fj.data.Either.right; 027 import static fj.data.Enumerator.charEnumerator; 028 import fj.data.List; 029 import static fj.data.List.asString; 030 import static fj.data.List.list; 031 import fj.data.Option; 032 import static fj.data.Option.some; 033 import fj.data.Stream; 034 import static fj.data.Stream.range; 035 import static fj.test.Gen.choose; 036 import static fj.test.Gen.elements; 037 import static fj.test.Gen.fail; 038 import static fj.test.Gen.frequency; 039 import static fj.test.Gen.listOf; 040 import static fj.test.Gen.oneOf; 041 import static fj.test.Gen.promote; 042 import static fj.test.Gen.sized; 043 import static fj.test.Gen.value; 044 045 import static java.lang.Math.abs; 046 import java.math.BigDecimal; 047 import java.math.BigInteger; 048 import java.sql.Time; 049 import java.sql.Timestamp; 050 import java.util.ArrayList; 051 import java.util.BitSet; 052 import java.util.Calendar; 053 import java.util.Date; 054 import java.util.EnumMap; 055 import java.util.EnumSet; 056 import java.util.GregorianCalendar; 057 import java.util.HashMap; 058 import java.util.HashSet; 059 import java.util.Hashtable; 060 import java.util.IdentityHashMap; 061 import java.util.LinkedHashMap; 062 import java.util.LinkedHashSet; 063 import java.util.LinkedList; 064 import java.util.Locale; 065 import static java.util.Locale.getAvailableLocales; 066 import java.util.PriorityQueue; 067 import java.util.Properties; 068 import java.util.Stack; 069 import java.util.TreeMap; 070 import java.util.TreeSet; 071 import java.util.Vector; 072 import java.util.WeakHashMap; 073 import static java.util.EnumSet.copyOf; 074 import java.util.concurrent.ArrayBlockingQueue; 075 import java.util.concurrent.ConcurrentHashMap; 076 import java.util.concurrent.ConcurrentLinkedQueue; 077 import java.util.concurrent.CopyOnWriteArrayList; 078 import java.util.concurrent.CopyOnWriteArraySet; 079 import java.util.concurrent.DelayQueue; 080 import java.util.concurrent.Delayed; 081 import java.util.concurrent.LinkedBlockingQueue; 082 import java.util.concurrent.PriorityBlockingQueue; 083 import java.util.concurrent.SynchronousQueue; 084 085 /** 086 * The type used to generate arbitrary values of the given type parameter (<code>A</code>). Common 087 * arbitrary implementations are provided. 088 * 089 * @version %build.number%<br> 090 * <ul> 091 * <li>$LastChangedRevision: 163 $</li> 092 * <li>$LastChangedDate: 2009-06-02 03:43:12 +1000 (Tue, 02 Jun 2009) $</li> 093 * <li>$LastChangedBy: runarorama $</li> 094 * </ul> 095 */ 096 public final class Arbitrary<A> { 097 /** 098 * The generator associated with this arbitrary. 099 */ 100 @SuppressWarnings({"PublicField"}) 101 public final Gen<A> gen; 102 103 private Arbitrary(final Gen<A> gen) { 104 this.gen = gen; 105 } 106 107 /** 108 * Constructs and arbitrary with the given generator. 109 * 110 * @param g The generator to construct an arbitrary with. 111 * @return A new arbitrary that uses the given generator. 112 */ 113 public static <A> Arbitrary<A> arbitrary(final Gen<A> g) { 114 return new Arbitrary<A>(g); 115 } 116 117 /** 118 * An arbitrary for functions. 119 * 120 * @param c The coarbitrary for the function domain. 121 * @param a The arbitrary for the function codomain. 122 * @return An arbitrary for functions. 123 */ 124 public static <A, B> Arbitrary<F<A, B>> arbF(final Coarbitrary<A> c, final Arbitrary<B> a) { 125 return arbitrary(promote(new F<A, Gen<B>>() { 126 public Gen<B> f(final A x) { 127 return c.coarbitrary(x, a.gen); 128 } 129 })); 130 } 131 132 /** 133 * An arbitrary for functions. 134 * 135 * @param a The arbitrary for the function codomain. 136 * @return An arbitrary for functions. 137 */ 138 public static <A, B> Arbitrary<F<A, B>> arbFInvariant(final Arbitrary<B> a) { 139 return arbitrary(a.gen.map(Function.<A, B>constant())); 140 } 141 142 /** 143 * An arbitrary for function-2. 144 * 145 * @param ca A coarbitrary for the part of the domain of the function. 146 * @param cb A coarbitrary for the part of the domain of the function. 147 * @param a An arbitrary for the codomain of the function. 148 * @return An arbitrary for function-2. 149 */ 150 public static <A, B, C> Arbitrary<F2<A, B, C>> arbF2(final Coarbitrary<A> ca, final Coarbitrary<B> cb, 151 final Arbitrary<C> a) { 152 return arbitrary(arbF(ca, arbF(cb, a)).gen.map(Function.<A, B, C>uncurryF2())); 153 } 154 155 /** 156 * An arbitrary for function-2. 157 * 158 * @param a The arbitrary for the function codomain. 159 * @return An arbitrary for function-2. 160 */ 161 public static <A, B, C> Arbitrary<F2<A, B, C>> arbF2Invariant(final Arbitrary<C> a) { 162 return arbitrary(a.gen.map( 163 compose(Function.<A, B, C>uncurryF2(), compose(Function.<A, F<B, C>>constant(), Function.<B, C>constant())))); 164 } 165 166 /** 167 * An arbitrary for function-3. 168 * 169 * @param ca A coarbitrary for the part of the domain of the function. 170 * @param cb A coarbitrary for the part of the domain of the function. 171 * @param cc A coarbitrary for the part of the domain of the function. 172 * @param a An arbitrary for the codomain of the function. 173 * @return An arbitrary for function-3. 174 */ 175 public static <A, B, C, D> Arbitrary<F3<A, B, C, D>> arbF3(final Coarbitrary<A> ca, final Coarbitrary<B> cb, 176 final Coarbitrary<C> cc, final Arbitrary<D> a) { 177 return arbitrary(arbF(ca, arbF(cb, arbF(cc, a))).gen.map(Function.<A, B, C, D>uncurryF3())); 178 } 179 180 /** 181 * An arbitrary for function-3. 182 * 183 * @param a The arbitrary for the function codomain. 184 * @return An arbitrary for function-3. 185 */ 186 public static <A, B, C, D> Arbitrary<F3<A, B, C, D>> arbF3Invariant(final Arbitrary<D> a) { 187 return arbitrary(a.gen.map(compose(Function.<A, B, C, D>uncurryF3(), compose(Function.<A, F<B, F<C, D>>>constant(), 188 compose( 189 Function.<B, F<C, D>>constant(), 190 Function.<C, D>constant()))))); 191 } 192 193 /** 194 * An arbitrary for function-4. 195 * 196 * @param ca A coarbitrary for the part of the domain of the function. 197 * @param cb A coarbitrary for the part of the domain of the function. 198 * @param cc A coarbitrary for the part of the domain of the function. 199 * @param cd A coarbitrary for the part of the domain of the function. 200 * @param a An arbitrary for the codomain of the function. 201 * @return An arbitrary for function-4. 202 */ 203 public static <A, B, C, D, E> Arbitrary<F4<A, B, C, D, E>> arbF4(final Coarbitrary<A> ca, final Coarbitrary<B> cb, 204 final Coarbitrary<C> cc, final Coarbitrary<D> cd, 205 final Arbitrary<E> a) { 206 return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, a)))).gen.map(Function.<A, B, C, D, E>uncurryF4())); 207 } 208 209 /** 210 * An arbitrary for function-4. 211 * 212 * @param a The arbitrary for the function codomain. 213 * @return An arbitrary for function-4. 214 */ 215 public static <A, B, C, D, E> Arbitrary<F4<A, B, C, D, E>> arbF4Invariant(final Arbitrary<E> a) { 216 return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E>uncurryF4(), 217 compose(Function.<A, F<B, F<C, F<D, E>>>>constant(), 218 compose(Function.<B, F<C, F<D, E>>>constant(), 219 compose(Function.<C, F<D, E>>constant(), 220 Function.<D, E>constant())))))); 221 } 222 223 /** 224 * An arbitrary for function-5. 225 * 226 * @param ca A coarbitrary for the part of the domain of the function. 227 * @param cb A coarbitrary for the part of the domain of the function. 228 * @param cc A coarbitrary for the part of the domain of the function. 229 * @param cd A coarbitrary for the part of the domain of the function. 230 * @param ce A coarbitrary for the part of the domain of the function. 231 * @param a An arbitrary for the codomain of the function. 232 * @return An arbitrary for function-5. 233 */ 234 public static <A, B, C, D, E, F$> Arbitrary<F5<A, B, C, D, E, F$>> arbF5(final Coarbitrary<A> ca, 235 final Coarbitrary<B> cb, 236 final Coarbitrary<C> cc, 237 final Coarbitrary<D> cd, 238 final Coarbitrary<E> ce, 239 final Arbitrary<F$> a) { 240 return arbitrary( 241 arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, a))))).gen.map(Function.<A, B, C, D, E, F$>uncurryF5())); 242 } 243 244 /** 245 * An arbitrary for function-5. 246 * 247 * @param a The arbitrary for the function codomain. 248 * @return An arbitrary for function-5. 249 */ 250 public static <A, B, C, D, E, F$> Arbitrary<F5<A, B, C, D, E, F$>> arbF5Invariant(final Arbitrary<F$> a) { 251 return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E, F$>uncurryF5(), 252 compose(Function.<A, F<B, F<C, F<D, F<E, F$>>>>>constant(), 253 compose(Function.<B, F<C, F<D, F<E, F$>>>>constant(), 254 compose(Function.<C, F<D, F<E, F$>>>constant(), 255 compose(Function.<D, F<E, F$>>constant(), 256 Function.<E, F$>constant()))))))); 257 } 258 259 /** 260 * An arbitrary for function-6. 261 * 262 * @param ca A coarbitrary for the part of the domain of the function. 263 * @param cb A coarbitrary for the part of the domain of the function. 264 * @param cc A coarbitrary for the part of the domain of the function. 265 * @param cd A coarbitrary for the part of the domain of the function. 266 * @param ce A coarbitrary for the part of the domain of the function. 267 * @param cf A coarbitrary for the part of the domain of the function. 268 * @param a An arbitrary for the codomain of the function. 269 * @return An arbitrary for function-6. 270 */ 271 public static <A, B, C, D, E, F$, G> Arbitrary<F6<A, B, C, D, E, F$, G>> arbF6(final Coarbitrary<A> ca, 272 final Coarbitrary<B> cb, 273 final Coarbitrary<C> cc, 274 final Coarbitrary<D> cd, 275 final Coarbitrary<E> ce, 276 final Coarbitrary<F$> cf, 277 final Arbitrary<G> a) { 278 return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, a)))))).gen.map( 279 Function.<A, B, C, D, E, F$, G>uncurryF6())); 280 } 281 282 /** 283 * An arbitrary for function-6. 284 * 285 * @param a The arbitrary for the function codomain. 286 * @return An arbitrary for function-6. 287 */ 288 public static <A, B, C, D, E, F$, G> Arbitrary<F6<A, B, C, D, E, F$, G>> arbF6Invariant(final Arbitrary<G> a) { 289 return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E, F$, G>uncurryF6(), 290 compose(Function.<A, F<B, F<C, F<D, F<E, F<F$, G>>>>>>constant(), 291 compose(Function.<B, F<C, F<D, F<E, F<F$, G>>>>>constant(), 292 compose(Function.<C, F<D, F<E, F<F$, G>>>>constant(), 293 compose(Function.<D, F<E, F<F$, G>>>constant(), 294 compose(Function.<E, F<F$, G>>constant(), 295 Function.<F$, G>constant())))))))); 296 } 297 298 /** 299 * An arbitrary for function-7. 300 * 301 * @param ca A coarbitrary for the part of the domain of the function. 302 * @param cb A coarbitrary for the part of the domain of the function. 303 * @param cc A coarbitrary for the part of the domain of the function. 304 * @param cd A coarbitrary for the part of the domain of the function. 305 * @param ce A coarbitrary for the part of the domain of the function. 306 * @param cf A coarbitrary for the part of the domain of the function. 307 * @param cg A coarbitrary for the part of the domain of the function. 308 * @param a An arbitrary for the codomain of the function. 309 * @return An arbitrary for function-7. 310 */ 311 public static <A, B, C, D, E, F$, G, H> Arbitrary<F7<A, B, C, D, E, F$, G, H>> arbF7(final Coarbitrary<A> ca, 312 final Coarbitrary<B> cb, 313 final Coarbitrary<C> cc, 314 final Coarbitrary<D> cd, 315 final Coarbitrary<E> ce, 316 final Coarbitrary<F$> cf, 317 final Coarbitrary<G> cg, 318 final Arbitrary<H> a) { 319 return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, arbF(cg, a))))))).gen.map( 320 Function.<A, B, C, D, E, F$, G, H>uncurryF7())); 321 } 322 323 /** 324 * An arbitrary for function-7. 325 * 326 * @param a The arbitrary for the function codomain. 327 * @return An arbitrary for function-7. 328 */ 329 public static <A, B, C, D, E, F$, G, H> Arbitrary<F7<A, B, C, D, E, F$, G, H>> arbF7Invariant(final Arbitrary<H> a) { 330 return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E, F$, G, H>uncurryF7(), 331 compose(Function.<A, F<B, F<C, F<D, F<E, F<F$, F<G, H>>>>>>>constant(), 332 compose(Function.<B, F<C, F<D, F<E, F<F$, F<G, H>>>>>>constant(), 333 compose(Function.<C, F<D, F<E, F<F$, F<G, H>>>>>constant(), 334 compose(Function.<D, F<E, F<F$, F<G, H>>>>constant(), 335 compose(Function.<E, F<F$, F<G, H>>>constant(), 336 compose(Function.<F$, F<G, H>>constant(), 337 Function.<G, H>constant()))))))))); 338 } 339 340 /** 341 * An arbitrary for function-8. 342 * 343 * @param ca A coarbitrary for the part of the domain of the function. 344 * @param cb A coarbitrary for the part of the domain of the function. 345 * @param cc A coarbitrary for the part of the domain of the function. 346 * @param cd A coarbitrary for the part of the domain of the function. 347 * @param ce A coarbitrary for the part of the domain of the function. 348 * @param cf A coarbitrary for the part of the domain of the function. 349 * @param cg A coarbitrary for the part of the domain of the function. 350 * @param ch A coarbitrary for the part of the domain of the function. 351 * @param a An arbitrary for the codomain of the function. 352 * @return An arbitrary for function-8. 353 */ 354 public static <A, B, C, D, E, F$, G, H, I> Arbitrary<F8<A, B, C, D, E, F$, G, H, I>> arbF8(final Coarbitrary<A> ca, 355 final Coarbitrary<B> cb, 356 final Coarbitrary<C> cc, 357 final Coarbitrary<D> cd, 358 final Coarbitrary<E> ce, 359 final Coarbitrary<F$> cf, 360 final Coarbitrary<G> cg, 361 final Coarbitrary<H> ch, 362 final Arbitrary<I> a) { 363 return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, arbF(cg, arbF(ch, a)))))))).gen.map( 364 Function.<A, B, C, D, E, F$, G, H, I>uncurryF8())); 365 } 366 367 /** 368 * An arbitrary for function-8. 369 * 370 * @param a The arbitrary for the function codomain. 371 * @return An arbitrary for function-8. 372 */ 373 public static <A, B, C, D, E, F$, G, H, I> Arbitrary<F8<A, B, C, D, E, F$, G, H, I>> arbF8Invariant( 374 final Arbitrary<I> a) { 375 return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E, F$, G, H, I>uncurryF8(), 376 compose(Function.<A, F<B, F<C, F<D, F<E, F<F$, F<G, F<H, I>>>>>>>>constant(), 377 compose(Function.<B, F<C, F<D, F<E, F<F$, F<G, F<H, I>>>>>>>constant(), 378 compose(Function.<C, F<D, F<E, F<F$, F<G, F<H, I>>>>>>constant(), 379 compose( 380 Function.<D, F<E, F<F$, F<G, F<H, I>>>>>constant(), 381 compose(Function.<E, F<F$, F<G, F<H, I>>>>constant(), 382 compose( 383 Function.<F$, F<G, F<H, I>>>constant(), 384 compose(Function.<G, F<H, I>>constant(), 385 Function.<H, I>constant())))))))))); 386 } 387 388 /** 389 * An arbitrary implementation for boolean values. 390 */ 391 public static final Arbitrary<Boolean> arbBoolean = arbitrary(elements(true, false)); 392 393 /** 394 * An arbitrary implementation for integer values. 395 */ 396 public static final Arbitrary<Integer> arbInteger = arbitrary(sized(new F<Integer, Gen<Integer>>() { 397 public Gen<Integer> f(final Integer i) { 398 return choose(-i, i); 399 } 400 })); 401 402 /** 403 * An arbitrary implementation for integer values that checks boundary values <code>(0, 1, -1, 404 * max, min, max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link 405 * #arbInteger} the remainder of the time (93%). 406 */ 407 public static final Arbitrary<Integer> arbIntegerBoundaries = arbitrary(sized(new F<Integer, Gen<Integer>>() { 408 @SuppressWarnings("unchecked") 409 public Gen<Integer> f(final Integer i) { 410 return frequency(list(p(1, value(0)), 411 p(1, value(1)), 412 p(1, value(-1)), 413 p(1, value(Integer.MAX_VALUE)), 414 p(1, value(Integer.MIN_VALUE)), 415 p(1, value(Integer.MAX_VALUE - 1)), 416 p(1, value(Integer.MIN_VALUE + 1)), 417 p(93, arbInteger.gen))); 418 } 419 })); 420 421 /** 422 * An arbitrary implementation for long values. 423 */ 424 public static final Arbitrary<Long> arbLong = 425 arbitrary(arbInteger.gen.bind(arbInteger.gen, new F<Integer, F<Integer, Long>>() { 426 public F<Integer, Long> f(final Integer i1) { 427 return new F<Integer, Long>() { 428 public Long f(final Integer i2) { 429 //No it isn't 430 //noinspection RedundantCast 431 return (long) i1 << 32L & i2; 432 } 433 }; 434 } 435 })); 436 437 /** 438 * An arbitrary implementation for long values that checks boundary values <code>(0, 1, -1, max, 439 * min, max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link #arbLong} 440 * the remainder of the time (93%). 441 */ 442 public static final Arbitrary<Long> arbLongBoundaries = arbitrary(sized(new F<Integer, Gen<Long>>() { 443 @SuppressWarnings("unchecked") 444 public Gen<Long> f(final Integer i) { 445 return frequency(list(p(1, value(0L)), 446 p(1, value(1L)), 447 p(1, value(-1L)), 448 p(1, value(Long.MAX_VALUE)), 449 p(1, value(Long.MIN_VALUE)), 450 p(1, value(Long.MAX_VALUE - 1L)), 451 p(1, value(Long.MIN_VALUE + 1L)), 452 p(93, arbLong.gen))); 453 } 454 })); 455 456 /** 457 * An arbitrary implementation for byte values. 458 */ 459 public static final Arbitrary<Byte> arbByte = arbitrary(arbInteger.gen.map(new F<Integer, Byte>() { 460 public Byte f(final Integer i) { 461 return (byte) i.intValue(); 462 } 463 })); 464 465 /** 466 * An arbitrary implementation for byte values that checks boundary values <code>(0, 1, -1, max, 467 * min, max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link #arbByte} 468 * the remainder of the time (93%). 469 */ 470 public static final Arbitrary<Byte> arbByteBoundaries = arbitrary(sized(new F<Integer, Gen<Byte>>() { 471 @SuppressWarnings("unchecked") 472 public Gen<Byte> f(final Integer i) { 473 return frequency(list(p(1, value((byte) 0)), 474 p(1, value((byte) 1)), 475 p(1, value((byte) -1)), 476 p(1, value(Byte.MAX_VALUE)), 477 p(1, value(Byte.MIN_VALUE)), 478 p(1, value((byte) (Byte.MAX_VALUE - 1))), 479 p(1, value((byte) (Byte.MIN_VALUE + 1))), 480 p(93, arbByte.gen))); 481 } 482 })); 483 484 /** 485 * An arbitrary implementation for short values. 486 */ 487 public static final Arbitrary<Short> arbShort = arbitrary(arbInteger.gen.map(new F<Integer, Short>() { 488 public Short f(final Integer i) { 489 return (short) i.intValue(); 490 } 491 })); 492 493 /** 494 * An arbitrary implementation for short values that checks boundary values <code>(0, 1, -1, max, 495 * min, max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link #arbShort} 496 * the remainder of the time (93%). 497 */ 498 public static final Arbitrary<Short> arbShortBoundaries = arbitrary(sized(new F<Integer, Gen<Short>>() { 499 @SuppressWarnings("unchecked") 500 public Gen<Short> f(final Integer i) { 501 return frequency(list(p(1, value((short) 0)), 502 p(1, value((short) 1)), 503 p(1, value((short) -1)), 504 p(1, value(Short.MAX_VALUE)), 505 p(1, value(Short.MIN_VALUE)), 506 p(1, value((short) (Short.MAX_VALUE - 1))), 507 p(1, value((short) (Short.MIN_VALUE + 1))), 508 p(93, arbShort.gen))); 509 } 510 })); 511 512 /** 513 * An arbitrary implementation for character values. 514 */ 515 public static final Arbitrary<Character> arbCharacter = arbitrary(choose(0, 65536).map(new F<Integer, Character>() { 516 public Character f(final Integer i) { 517 return (char) i.intValue(); 518 } 519 })); 520 521 /** 522 * An arbitrary implementation for character values that checks boundary values <code>(max, min, 523 * max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link #arbCharacter} 524 * the remainder of the time (96%). 525 */ 526 public static final Arbitrary<Character> arbCharacterBoundaries = arbitrary(sized(new F<Integer, Gen<Character>>() { 527 @SuppressWarnings("unchecked") 528 public Gen<Character> f(final Integer i) { 529 return frequency(list(p(1, value(Character.MIN_VALUE)), 530 p(1, value((char) (Character.MIN_VALUE + 1))), 531 p(1, value(Character.MAX_VALUE)), 532 p(1, value((char) (Character.MAX_VALUE - 1))), 533 p(95, arbCharacter.gen))); 534 } 535 })); 536 537 /** 538 * An arbitrary implementation for double values. 539 */ 540 public static final Arbitrary<Double> arbDouble = arbitrary(sized(new F<Integer, Gen<Double>>() { 541 public Gen<Double> f(final Integer i) { 542 return choose((double) -i, i); 543 } 544 })); 545 546 /** 547 * An arbitrary implementation for double values that checks boundary values <code>(0, 1, -1, max, 548 * min, min (normal), NaN, -infinity, infinity, max - 1)</code> with a frequency of 1% each then 549 * generates from {@link #arbDouble} the remainder of the time (91%). 550 */ 551 public static final Arbitrary<Double> arbDoubleBoundaries = arbitrary(sized(new F<Integer, Gen<Double>>() { 552 @SuppressWarnings("unchecked") 553 public Gen<Double> f(final Integer i) { 554 return frequency(list(p(1, value(0D)), 555 p(1, value(1D)), 556 p(1, value(-1D)), 557 p(1, value(Double.MAX_VALUE)), 558 p(1, value(Double.MIN_VALUE)), 559 p(1, value(Double.NaN)), 560 p(1, value(Double.NEGATIVE_INFINITY)), 561 p(1, value(Double.POSITIVE_INFINITY)), 562 p(1, value(Double.MAX_VALUE - 1D)), 563 p(91, arbDouble.gen))); 564 } 565 })); 566 567 /** 568 * An arbitrary implementation for float values. 569 */ 570 public static final Arbitrary<Float> arbFloat = arbitrary(arbDouble.gen.map(new F<Double, Float>() { 571 public Float f(final Double d) { 572 return (float) d.doubleValue(); 573 } 574 })); 575 576 /** 577 * An arbitrary implementation for float values that checks boundary values <code>(0, 1, -1, max, 578 * min, NaN, -infinity, infinity, max - 1)</code> with a frequency of 1% each then generates from 579 * {@link #arbFloat} the remainder of the time (91%). 580 */ 581 public static final Arbitrary<Float> arbFloatBoundaries = arbitrary(sized(new F<Integer, Gen<Float>>() { 582 @SuppressWarnings("unchecked") 583 public Gen<Float> f(final Integer i) { 584 return frequency(list(p(1, value(0F)), 585 p(1, value(1F)), 586 p(1, value(-1F)), 587 p(1, value(Float.MAX_VALUE)), 588 p(1, value(Float.MIN_VALUE)), 589 p(1, value(Float.NaN)), 590 p(1, value(Float.NEGATIVE_INFINITY)), 591 p(1, value(Float.POSITIVE_INFINITY)), 592 p(1, value(Float.MAX_VALUE - 1F)), 593 p(91, arbFloat.gen))); 594 } 595 })); 596 597 /** 598 * An arbitrary implementation for string values. 599 */ 600 public static final Arbitrary<String> arbString = 601 arbitrary(arbList(arbCharacter).gen.map(new F<List<Character>, String>() { 602 public String f(final List<Character> cs) { 603 return asString(cs); 604 } 605 })); 606 607 /** 608 * An arbitrary implementation for string values with characters in the US-ASCII range. 609 */ 610 public static final Arbitrary<String> arbUSASCIIString = 611 arbitrary(arbList(arbCharacter).gen.map(new F<List<Character>, String>() { 612 public String f(final List<Character> cs) { 613 return asString(cs.map(new F<Character, Character>() { 614 public Character f(final Character c) { 615 return (char) (c % 128); 616 } 617 })); 618 } 619 })); 620 621 /** 622 * An arbitrary implementation for string values with alpha-numeric characters. 623 */ 624 public static final Arbitrary<String> arbAlphaNumString = 625 arbitrary(arbList(arbitrary(elements(range(charEnumerator, 'a', 'z').append( 626 range(charEnumerator, 'A', 'Z')).append( 627 range(charEnumerator, '0', '9')).toArray().array(Character[].class)))).gen.map(asString())); 628 629 /** 630 * An arbitrary implementation for string buffer values. 631 */ 632 public static final Arbitrary<StringBuffer> arbStringBuffer = 633 arbitrary(arbString.gen.map(new F<String, StringBuffer>() { 634 public StringBuffer f(final String s) { 635 return new StringBuffer(s); 636 } 637 })); 638 639 /** 640 * An arbitrary implementation for string builder values. 641 */ 642 public static final Arbitrary<StringBuilder> arbStringBuilder = 643 arbitrary(arbString.gen.map(new F<String, StringBuilder>() { 644 public StringBuilder f(final String s) { 645 return new StringBuilder(s); 646 } 647 })); 648 649 /** 650 * Returns an arbitrary implementation for generators. 651 * 652 * @param aa an arbitrary implementation for the type over which the generator is defined. 653 * @return An arbitrary implementation for generators. 654 */ 655 public static <A> Arbitrary<Gen<A>> arbGen(final Arbitrary<A> aa) { 656 return arbitrary(sized(new F<Integer, Gen<Gen<A>>>() { 657 public Gen<Gen<A>> f(final Integer i) { 658 if (i == 0) 659 return fail(); 660 else 661 return aa.gen.map(new F<A, Gen<A>>() { 662 public Gen<A> f(final A a) { 663 return value(a); 664 } 665 }).resize(i - 1); 666 } 667 })); 668 } 669 670 /** 671 * Returns an arbitrary implementation for optional values. 672 * 673 * @param aa an arbitrary implementation for the type over which the optional value is defined. 674 * @return An arbitrary implementation for optional values. 675 */ 676 public static <A> Arbitrary<Option<A>> arbOption(final Arbitrary<A> aa) { 677 return arbitrary(sized(new F<Integer, Gen<Option<A>>>() { 678 public Gen<Option<A>> f(final Integer i) { 679 return i == 0 ? 680 value(Option.<A>none()) : 681 aa.gen.map(new F<A, Option<A>>() { 682 public Option<A> f(final A a) { 683 return some(a); 684 } 685 }).resize(i - 1); 686 } 687 })); 688 } 689 690 /** 691 * Returns an arbitrary implementation for the disjoint union. 692 * 693 * @param aa An arbitrary implementation for the type over which one side of the disjoint union is 694 * defined. 695 * @param ab An arbitrary implementation for the type over which one side of the disjoint union is 696 * defined. 697 * @return An arbitrary implementation for the disjoint union. 698 */ 699 @SuppressWarnings({"unchecked"}) 700 public static <A, B> Arbitrary<Either<A, B>> arbEither(final Arbitrary<A> aa, final Arbitrary<B> ab) { 701 final Gen<Either<A, B>> left = aa.gen.map(new F<A, Either<A, B>>() { 702 public Either<A, B> f(final A a) { 703 return left(a); 704 } 705 }); 706 final Gen<Either<A, B>> right = ab.gen.map(new F<B, Either<A, B>>() { 707 public Either<A, B> f(final B b) { 708 return right(b); 709 } 710 }); 711 return arbitrary(oneOf(list(left, right))); 712 } 713 714 /** 715 * Returns an arbitrary implementation for lists. 716 * 717 * @param aa An arbitrary implementation for the type over which the list is defined. 718 * @return An arbitrary implementation for lists. 719 */ 720 public static <A> Arbitrary<List<A>> arbList(final Arbitrary<A> aa) { 721 return arbitrary(listOf(aa.gen)); 722 } 723 724 /** 725 * Returns an arbitrary implementation for streams. 726 * 727 * @param aa An arbitrary implementation for the type over which the stream is defined. 728 * @return An arbitrary implementation for streams. 729 */ 730 public static <A> Arbitrary<Stream<A>> arbStream(final Arbitrary<A> aa) { 731 return arbitrary(arbList(aa).gen.map(new F<List<A>, Stream<A>>() { 732 public Stream<A> f(final List<A> as) { 733 return as.toStream(); 734 } 735 })); 736 } 737 738 /** 739 * Returns an arbitrary implementation for arrays. 740 * 741 * @param aa An arbitrary implementation for the type over which the array is defined. 742 * @return An arbitrary implementation for arrays. 743 */ 744 public static <A> Arbitrary<Array<A>> arbArray(final Arbitrary<A> aa) { 745 return arbitrary(arbList(aa).gen.map(new F<List<A>, Array<A>>() { 746 public Array<A> f(final List<A> as) { 747 return as.toArray(); 748 } 749 })); 750 } 751 752 /** 753 * Returns an arbitrary implementation for throwables. 754 * 755 * @param as An arbitrary used for the throwable message. 756 * @return An arbitrary implementation for throwables. 757 */ 758 public static Arbitrary<Throwable> arbThrowable(final Arbitrary<String> as) { 759 return arbitrary(as.gen.map(new F<String, Throwable>() { 760 public Throwable f(final String msg) { 761 //noinspection ThrowableInstanceNeverThrown 762 return new Throwable(msg); 763 } 764 })); 765 } 766 767 /** 768 * An arbitrary implementation for throwables. 769 */ 770 public static final Arbitrary<Throwable> arbThrowable = arbThrowable(arbString); 771 772 // BEGIN java.util 773 774 /** 775 * Returns an arbitrary implementation for array lists. 776 * 777 * @param aa An arbitrary implementation for the type over which the array list is defined. 778 * @return An arbitrary implementation for array lists. 779 */ 780 public static <A> Arbitrary<ArrayList<A>> arbArrayList(final Arbitrary<A> aa) { 781 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, ArrayList<A>>() { 782 public ArrayList<A> f(final Array<A> a) { 783 return new ArrayList<A>(a.toCollection()); 784 } 785 })); 786 } 787 788 /** 789 * An arbitrary implementation for bit sets. 790 */ 791 public static final Arbitrary<BitSet> arbBitSet = 792 arbitrary(arbList(arbBoolean).gen.map(new F<List<Boolean>, BitSet>() { 793 public BitSet f(final List<Boolean> bs) { 794 final BitSet s = new BitSet(bs.length()); 795 bs.zipIndex().foreach(new Effect<P2<Boolean, Integer>>() { 796 public void e(final P2<Boolean, Integer> bi) { 797 s.set(bi._2(), bi._1()); 798 } 799 }); 800 return s; 801 } 802 })); 803 804 /** 805 * An arbitrary implementation for calendars. 806 */ 807 public static final Arbitrary<Calendar> arbCalendar = arbitrary(arbLong.gen.map(new F<Long, Calendar>() { 808 public Calendar f(final Long i) { 809 final Calendar c = Calendar.getInstance(); 810 c.setTimeInMillis(i); 811 return c; 812 } 813 })); 814 815 /** 816 * An arbitrary implementation for dates. 817 */ 818 public static final Arbitrary<Date> arbDate = arbitrary(arbLong.gen.map(new F<Long, Date>() { 819 public Date f(final Long i) { 820 return new Date(i); 821 } 822 })); 823 824 /** 825 * Returns an arbitrary implementation for a Java enumeration. 826 * 827 * @param clazz The type of enum to return an arbtrary of. 828 * @return An arbitrary for instances of the supplied enum type. 829 */ 830 public static <A extends Enum<A>> Arbitrary<A> arbEnumValue(final Class<A> clazz) { 831 return arbitrary(Gen.elements(clazz.getEnumConstants())); 832 } 833 834 /** 835 * Returns an arbitrary implementation for enum maps. 836 * 837 * @param ak An arbitrary implementation for the type over which the enum map's keys are defined. 838 * @param av An arbitrary implementation for the type over which the enum map's values are 839 * defined. 840 * @return An arbitrary implementation for enum maps. 841 */ 842 public static <K extends Enum<K>, V> Arbitrary<EnumMap<K, V>> arbEnumMap(final Arbitrary<K> ak, 843 final Arbitrary<V> av) { 844 return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, EnumMap<K, V>>() { 845 @SuppressWarnings({"UseOfObsoleteCollectionType"}) 846 public EnumMap<K, V> f(final Hashtable<K, V> ht) { 847 return new EnumMap<K, V>(ht); 848 } 849 })); 850 } 851 852 /** 853 * Returns an arbitrary implementation for enum sets. 854 * 855 * @param aa An arbitrary implementation for the type over which the enum set is defined. 856 * @return An arbitrary implementation for enum sets. 857 */ 858 public static <A extends Enum<A>> Arbitrary<EnumSet<A>> arbEnumSet(final Arbitrary<A> aa) { 859 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, EnumSet<A>>() { 860 public EnumSet<A> f(final Array<A> a) { 861 return copyOf(a.toCollection()); 862 } 863 })); 864 } 865 866 /** 867 * An arbitrary implementation for gregorian calendars. 868 */ 869 public static final Arbitrary<GregorianCalendar> arbGregorianCalendar = 870 arbitrary(arbLong.gen.map(new F<Long, GregorianCalendar>() { 871 public GregorianCalendar f(final Long i) { 872 final GregorianCalendar c = new GregorianCalendar(); 873 c.setTimeInMillis(i); 874 return c; 875 } 876 })); 877 878 /** 879 * Returns an arbitrary implementation for hash maps. 880 * 881 * @param ak An arbitrary implementation for the type over which the hash map's keys are defined. 882 * @param av An arbitrary implementation for the type over which the hash map's values are 883 * defined. 884 * @return An arbitrary implementation for hash maps. 885 */ 886 public static <K, V> Arbitrary<HashMap<K, V>> arbHashMap(final Arbitrary<K> ak, final Arbitrary<V> av) { 887 return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, HashMap<K, V>>() { 888 @SuppressWarnings({"UseOfObsoleteCollectionType"}) 889 public HashMap<K, V> f(final Hashtable<K, V> ht) { 890 return new HashMap<K, V>(ht); 891 } 892 })); 893 } 894 895 /** 896 * Returns an arbitrary implementation for hash sets. 897 * 898 * @param aa An arbitrary implementation for the type over which the hash set is defined. 899 * @return An arbitrary implementation for hash sets. 900 */ 901 public static <A> Arbitrary<HashSet<A>> arbHashSet(final Arbitrary<A> aa) { 902 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, HashSet<A>>() { 903 public HashSet<A> f(final Array<A> a) { 904 return new HashSet<A>(a.toCollection()); 905 } 906 })); 907 } 908 909 /** 910 * Returns an arbitrary implementation for hash tables. 911 * 912 * @param ak An arbitrary implementation for the type over which the hash table's keys are 913 * defined. 914 * @param av An arbitrary implementation for the type over which the hash table's values are 915 * defined. 916 * @return An arbitrary implementation for hash tables. 917 */ 918 public static <K, V> Arbitrary<Hashtable<K, V>> arbHashtable(final Arbitrary<K> ak, final Arbitrary<V> av) { 919 return arbitrary(arbList(ak).gen.bind(arbList(av).gen, new F<List<K>, F<List<V>, Hashtable<K, V>>>() { 920 public F<List<V>, Hashtable<K, V>> f(final List<K> ks) { 921 return new F<List<V>, Hashtable<K, V>>() { 922 @SuppressWarnings({"UseOfObsoleteCollectionType"}) 923 public Hashtable<K, V> f(final List<V> vs) { 924 final Hashtable<K, V> t = new Hashtable<K, V>(); 925 926 ks.zip(vs).foreach(new Effect<P2<K, V>>() { 927 public void e(final P2<K, V> kv) { 928 t.put(kv._1(), kv._2()); 929 } 930 }); 931 932 return t; 933 } 934 }; 935 } 936 })); 937 } 938 939 /** 940 * Returns an arbitrary implementation for identity hash maps. 941 * 942 * @param ak An arbitrary implementation for the type over which the identity hash map's keys are 943 * defined. 944 * @param av An arbitrary implementation for the type over which the identity hash map's values 945 * are defined. 946 * @return An arbitrary implementation for identity hash maps. 947 */ 948 public static <K, V> Arbitrary<IdentityHashMap<K, V>> arbIdentityHashMap(final Arbitrary<K> ak, 949 final Arbitrary<V> av) { 950 return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, IdentityHashMap<K, V>>() { 951 @SuppressWarnings({"UseOfObsoleteCollectionType"}) 952 public IdentityHashMap<K, V> f(final Hashtable<K, V> ht) { 953 return new IdentityHashMap<K, V>(ht); 954 } 955 })); 956 } 957 958 /** 959 * Returns an arbitrary implementation for linked hash maps. 960 * 961 * @param ak An arbitrary implementation for the type over which the linked hash map's keys are 962 * defined. 963 * @param av An arbitrary implementation for the type over which the linked hash map's values are 964 * defined. 965 * @return An arbitrary implementation for linked hash maps. 966 */ 967 public static <K, V> Arbitrary<LinkedHashMap<K, V>> arbLinkedHashMap(final Arbitrary<K> ak, final Arbitrary<V> av) { 968 return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, LinkedHashMap<K, V>>() { 969 @SuppressWarnings({"UseOfObsoleteCollectionType"}) 970 public LinkedHashMap<K, V> f(final Hashtable<K, V> ht) { 971 return new LinkedHashMap<K, V>(ht); 972 } 973 })); 974 } 975 976 /** 977 * Returns an arbitrary implementation for hash sets. 978 * 979 * @param aa An arbitrary implementation for the type over which the hash set is defined. 980 * @return An arbitrary implementation for hash sets. 981 */ 982 public static <A> Arbitrary<LinkedHashSet<A>> arbLinkedHashSet(final Arbitrary<A> aa) { 983 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, LinkedHashSet<A>>() { 984 public LinkedHashSet<A> f(final Array<A> a) { 985 return new LinkedHashSet<A>(a.toCollection()); 986 } 987 })); 988 } 989 990 /** 991 * Returns an arbitrary implementation for linked lists. 992 * 993 * @param aa An arbitrary implementation for the type over which the linked list is defined. 994 * @return An arbitrary implementation for linked lists. 995 */ 996 public static <A> Arbitrary<LinkedList<A>> arbLinkedList(final Arbitrary<A> aa) { 997 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, LinkedList<A>>() { 998 public LinkedList<A> f(final Array<A> a) { 999 return new LinkedList<A>(a.toCollection()); 1000 } 1001 })); 1002 } 1003 1004 /** 1005 * Returns an arbitrary implementation for priority queues. 1006 * 1007 * @param aa An arbitrary implementation for the type over which the priority queue is defined. 1008 * @return An arbitrary implementation for priority queues. 1009 */ 1010 public static <A> Arbitrary<PriorityQueue<A>> arbPriorityQueue(final Arbitrary<A> aa) { 1011 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, PriorityQueue<A>>() { 1012 public PriorityQueue<A> f(final Array<A> a) { 1013 return new PriorityQueue<A>(a.toCollection()); 1014 } 1015 })); 1016 } 1017 1018 /** 1019 * An arbitrary implementation for properties. 1020 */ 1021 public static final Arbitrary<Properties> arbProperties = 1022 arbitrary(arbHashtable(arbString, arbString).gen.map(new F<Hashtable<String, String>, Properties>() { 1023 @SuppressWarnings({"UseOfObsoleteCollectionType"}) 1024 public Properties f(final Hashtable<String, String> ht) { 1025 final Properties p = new Properties(); 1026 1027 for (final String k : ht.keySet()) { 1028 p.setProperty(k, ht.get(k)); 1029 } 1030 1031 return p; 1032 } 1033 })); 1034 1035 /** 1036 * Returns an arbitrary implementation for stacks. 1037 * 1038 * @param aa An arbitrary implementation for the type over which the stack is defined. 1039 * @return An arbitrary implementation for stacks. 1040 */ 1041 @SuppressWarnings({"UseOfObsoleteCollectionType"}) 1042 public static <A> Arbitrary<Stack<A>> arbStack(final Arbitrary<A> aa) { 1043 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, Stack<A>>() { 1044 @SuppressWarnings({"UseOfObsoleteCollectionType"}) 1045 public Stack<A> f(final Array<A> a) { 1046 final Stack<A> s = new Stack<A>(); 1047 s.addAll(a.toCollection()); 1048 return s; 1049 } 1050 })); 1051 } 1052 1053 /** 1054 * Returns an arbitrary implementation for tree maps. 1055 * 1056 * @param ak An arbitrary implementation for the type over which the tree map's keys are defined. 1057 * @param av An arbitrary implementation for the type over which the tree map's values are 1058 * defined. 1059 * @return An arbitrary implementation for tree maps. 1060 */ 1061 public static <K, V> Arbitrary<TreeMap<K, V>> arbTreeMap(final Arbitrary<K> ak, final Arbitrary<V> av) { 1062 return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, TreeMap<K, V>>() { 1063 @SuppressWarnings({"UseOfObsoleteCollectionType"}) 1064 public TreeMap<K, V> f(final Hashtable<K, V> ht) { 1065 return new TreeMap<K, V>(ht); 1066 } 1067 })); 1068 } 1069 1070 /** 1071 * Returns an arbitrary implementation for tree sets. 1072 * 1073 * @param aa An arbitrary implementation for the type over which the tree set is defined. 1074 * @return An arbitrary implementation for tree sets. 1075 */ 1076 public static <A> Arbitrary<TreeSet<A>> arbTreeSet(final Arbitrary<A> aa) { 1077 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, TreeSet<A>>() { 1078 public TreeSet<A> f(final Array<A> a) { 1079 return new TreeSet<A>(a.toCollection()); 1080 } 1081 })); 1082 } 1083 1084 /** 1085 * Returns an arbitrary implementation for vectors. 1086 * 1087 * @param aa An arbitrary implementation for the type over which the vector is defined. 1088 * @return An arbitrary implementation for vectors. 1089 */ 1090 @SuppressWarnings({"UseOfObsoleteCollectionType"}) 1091 public static <A> Arbitrary<Vector<A>> arbVector(final Arbitrary<A> aa) { 1092 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, Vector<A>>() { 1093 @SuppressWarnings({"UseOfObsoleteCollectionType"}) 1094 public Vector<A> f(final Array<A> a) { 1095 return new Vector<A>(a.toCollection()); 1096 } 1097 })); 1098 } 1099 1100 /** 1101 * Returns an arbitrary implementation for weak hash maps. 1102 * 1103 * @param ak An arbitrary implementation for the type over which the weak hash map's keys are 1104 * defined. 1105 * @param av An arbitrary implementation for the type over which the weak hash map's values are 1106 * defined. 1107 * @return An arbitrary implementation for weak hash maps. 1108 */ 1109 public static <K, V> Arbitrary<WeakHashMap<K, V>> arbWeakHashMap(final Arbitrary<K> ak, final Arbitrary<V> av) { 1110 return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, WeakHashMap<K, V>>() { 1111 @SuppressWarnings({"UseOfObsoleteCollectionType"}) 1112 public WeakHashMap<K, V> f(final Hashtable<K, V> ht) { 1113 return new WeakHashMap<K, V>(ht); 1114 } 1115 })); 1116 } 1117 1118 // END java.util 1119 1120 // BEGIN java.util.concurrent 1121 1122 /** 1123 * Returns an arbitrary implementation for array blocking queues. 1124 * 1125 * @param aa An arbitrary implementation for the type over which the array blocking queue is 1126 * defined. 1127 * @return An arbitrary implementation for array blocking queues. 1128 */ 1129 public static <A> Arbitrary<ArrayBlockingQueue<A>> arbArrayBlockingQueue(final Arbitrary<A> aa) { 1130 return arbitrary(arbArray(aa).gen.bind(arbInteger.gen, arbBoolean.gen, 1131 new F<Array<A>, F<Integer, F<Boolean, ArrayBlockingQueue<A>>>>() { 1132 public F<Integer, F<Boolean, ArrayBlockingQueue<A>>> f(final Array<A> a) { 1133 return new F<Integer, F<Boolean, ArrayBlockingQueue<A>>>() { 1134 public F<Boolean, ArrayBlockingQueue<A>> f(final Integer capacity) { 1135 return new F<Boolean, ArrayBlockingQueue<A>>() { 1136 public ArrayBlockingQueue<A> f(final Boolean fair) { 1137 return new ArrayBlockingQueue<A>(a.length() + abs(capacity), 1138 fair, a.toCollection()); 1139 } 1140 }; 1141 } 1142 }; 1143 } 1144 })); 1145 } 1146 1147 /** 1148 * Returns an arbitrary implementation for concurrent hash maps. 1149 * 1150 * @param ak An arbitrary implementation for the type over which the concurrent hash map's keys 1151 * are defined. 1152 * @param av An arbitrary implementation for the type over which the concurrent hash map's values 1153 * are defined. 1154 * @return An arbitrary implementation for concurrent hash maps. 1155 */ 1156 public static <K, V> Arbitrary<ConcurrentHashMap<K, V>> arbConcurrentHashMap(final Arbitrary<K> ak, 1157 final Arbitrary<V> av) { 1158 return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, ConcurrentHashMap<K, V>>() { 1159 @SuppressWarnings({"UseOfObsoleteCollectionType"}) 1160 public ConcurrentHashMap<K, V> f(final Hashtable<K, V> ht) { 1161 return new ConcurrentHashMap<K, V>(ht); 1162 } 1163 })); 1164 } 1165 1166 /** 1167 * Returns an arbitrary implementation for concurrent linked queues. 1168 * 1169 * @param aa An arbitrary implementation for the type over which the concurrent linked queue is 1170 * defined. 1171 * @return An arbitrary implementation for concurrent linked queues. 1172 */ 1173 public static <A> Arbitrary<ConcurrentLinkedQueue<A>> arbConcurrentLinkedQueue(final Arbitrary<A> aa) { 1174 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, ConcurrentLinkedQueue<A>>() { 1175 public ConcurrentLinkedQueue<A> f(final Array<A> a) { 1176 return new ConcurrentLinkedQueue<A>(a.toCollection()); 1177 } 1178 })); 1179 } 1180 1181 /** 1182 * Returns an arbitrary implementation for copy-on-write array lists. 1183 * 1184 * @param aa An arbitrary implementation for the type over which the copy-on-write array list is 1185 * defined. 1186 * @return An arbitrary implementation for copy-on-write array lists. 1187 */ 1188 public static <A> Arbitrary<CopyOnWriteArrayList<A>> arbCopyOnWriteArrayList(final Arbitrary<A> aa) { 1189 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, CopyOnWriteArrayList<A>>() { 1190 public CopyOnWriteArrayList<A> f(final Array<A> a) { 1191 return new CopyOnWriteArrayList<A>(a.toCollection()); 1192 } 1193 })); 1194 } 1195 1196 /** 1197 * Returns an arbitrary implementation for copy-on-write array sets. 1198 * 1199 * @param aa An arbitrary implementation for the type over which the copy-on-write array set is 1200 * defined. 1201 * @return An arbitrary implementation for copy-on-write array sets. 1202 */ 1203 public static <A> Arbitrary<CopyOnWriteArraySet<A>> arbCopyOnWriteArraySet(final Arbitrary<A> aa) { 1204 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, CopyOnWriteArraySet<A>>() { 1205 public CopyOnWriteArraySet<A> f(final Array<A> a) { 1206 return new CopyOnWriteArraySet<A>(a.toCollection()); 1207 } 1208 })); 1209 } 1210 1211 /** 1212 * Returns an arbitrary implementation for delay queues. 1213 * 1214 * @param aa An arbitrary implementation for the type over which the delay queue is defined. 1215 * @return An arbitrary implementation for delay queues. 1216 */ 1217 public static <A extends Delayed> Arbitrary<DelayQueue<A>> arbDelayQueue(final Arbitrary<A> aa) { 1218 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, DelayQueue<A>>() { 1219 public DelayQueue<A> f(final Array<A> a) { 1220 return new DelayQueue<A>(a.toCollection()); 1221 } 1222 })); 1223 } 1224 1225 /** 1226 * Returns an arbitrary implementation for linked blocking queues. 1227 * 1228 * @param aa An arbitrary implementation for the type over which the linked blocking queue is 1229 * defined. 1230 * @return An arbitrary implementation for linked blocking queues. 1231 */ 1232 public static <A> Arbitrary<LinkedBlockingQueue<A>> arbLinkedBlockingQueue(final Arbitrary<A> aa) { 1233 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, LinkedBlockingQueue<A>>() { 1234 public LinkedBlockingQueue<A> f(final Array<A> a) { 1235 return new LinkedBlockingQueue<A>(a.toCollection()); 1236 } 1237 })); 1238 } 1239 1240 /** 1241 * Returns an arbitrary implementation for priority blocking queues. 1242 * 1243 * @param aa An arbitrary implementation for the type over which the priority blocking queue is 1244 * defined. 1245 * @return An arbitrary implementation for priority blocking queues. 1246 */ 1247 public static <A> Arbitrary<PriorityBlockingQueue<A>> arbPriorityBlockingQueue(final Arbitrary<A> aa) { 1248 return arbitrary(arbArray(aa).gen.map(new F<Array<A>, PriorityBlockingQueue<A>>() { 1249 public PriorityBlockingQueue<A> f(final Array<A> a) { 1250 return new PriorityBlockingQueue<A>(a.toCollection()); 1251 } 1252 })); 1253 } 1254 1255 /** 1256 * Returns an arbitrary implementation for priority blocking queues. 1257 * 1258 * @param aa An arbitrary implementation for the type over which the priority blocking queue is 1259 * defined. 1260 * @return An arbitrary implementation for priority blocking queues. 1261 */ 1262 public static <A> Arbitrary<SynchronousQueue<A>> arbSynchronousQueue(final Arbitrary<A> aa) { 1263 return arbitrary(arbArray(aa).gen.bind(arbBoolean.gen, new F<Array<A>, F<Boolean, SynchronousQueue<A>>>() { 1264 public F<Boolean, SynchronousQueue<A>> f(final Array<A> a) { 1265 return new F<Boolean, SynchronousQueue<A>>() { 1266 public SynchronousQueue<A> f(final Boolean fair) { 1267 final SynchronousQueue<A> q = new SynchronousQueue<A>(fair); 1268 q.addAll(a.toCollection()); 1269 return q; 1270 } 1271 }; 1272 } 1273 })); 1274 } 1275 1276 // END java.util.concurrent 1277 1278 // BEGIN java.sql 1279 1280 /** 1281 * An arbitrary implementation for SQL dates. 1282 */ 1283 public static final Arbitrary<java.sql.Date> arbSQLDate = arbitrary(arbLong.gen.map(new F<Long, java.sql.Date>() { 1284 public java.sql.Date f(final Long i) { 1285 return new java.sql.Date(i); 1286 } 1287 })); 1288 1289 /** 1290 * An arbitrary implementation for SQL times. 1291 */ 1292 public static final Arbitrary<Time> arbTime = arbitrary(arbLong.gen.map(new F<Long, Time>() { 1293 public Time f(final Long i) { 1294 return new Time(i); 1295 } 1296 })); 1297 1298 /** 1299 * An arbitrary implementation for SQL time stamps. 1300 */ 1301 public static final Arbitrary<Timestamp> arbTimestamp = arbitrary(arbLong.gen.map(new F<Long, Timestamp>() { 1302 public Timestamp f(final Long i) { 1303 return new Timestamp(i); 1304 } 1305 })); 1306 1307 // END java.sql 1308 1309 // BEGIN java.math 1310 1311 /** 1312 * An arbitrary implementation for big integers. 1313 */ 1314 public static final Arbitrary<BigInteger> arbBigInteger = 1315 arbitrary(arbArray(arbByte).gen.bind(arbByte.gen, new F<Array<Byte>, F<Byte, BigInteger>>() { 1316 public F<Byte, BigInteger> f(final Array<Byte> a) { 1317 return new F<Byte, BigInteger>() { 1318 public BigInteger f(final Byte b) { 1319 final byte[] x = new byte[a.length() + 1]; 1320 1321 for (int i = 0; i < a.array().length; i++) { 1322 x[i] = a.get(i); 1323 } 1324 1325 x[a.length()] = b; 1326 1327 return new BigInteger(x); 1328 } 1329 }; 1330 } 1331 })); 1332 1333 /** 1334 * An arbitrary implementation for big decimals. 1335 */ 1336 public static final Arbitrary<BigDecimal> arbBigDecimal = 1337 arbitrary(arbBigInteger.gen.map(new F<BigInteger, BigDecimal>() { 1338 public BigDecimal f(final BigInteger i) { 1339 return new BigDecimal(i); 1340 } 1341 })); 1342 1343 // END java.math 1344 1345 /** 1346 * An arbitrary implementation for locales. 1347 */ 1348 public static final Arbitrary<Locale> arbLocale = arbitrary(elements(getAvailableLocales())); 1349 1350 /** 1351 * Returns an arbitrary implementation for product-1 values. 1352 * 1353 * @param aa An arbitrary implementation for the type over which the product-1 is defined. 1354 * @return An arbitrary implementation for product-1 values. 1355 */ 1356 public static <A> Arbitrary<P1<A>> arbP1(final Arbitrary<A> aa) { 1357 return arbitrary(aa.gen.map(new F<A, P1<A>>() { 1358 public P1<A> f(final A a) { 1359 return p(a); 1360 } 1361 })); 1362 } 1363 1364 /** 1365 * Returns an arbitrary implementation for product-2 values. 1366 * 1367 * @param aa An arbitrary implementation for one of the types over which the product-2 is 1368 * defined. 1369 * @param ab An Arbitrary implementation for one of the types over which the product-2 is 1370 * defined. 1371 * @return An arbitrary implementation for product-2 values. 1372 */ 1373 public static <A, B> Arbitrary<P2<A, B>> arbP2(final Arbitrary<A> aa, final Arbitrary<B> ab) { 1374 return arbitrary(aa.gen.bind(ab.gen, new F<A, F<B, P2<A, B>>>() { 1375 public F<B, P2<A, B>> f(final A a) { 1376 return new F<B, P2<A, B>>() { 1377 public P2<A, B> f(final B b) { 1378 return p(a, b); 1379 } 1380 }; 1381 } 1382 })); 1383 } 1384 1385 /** 1386 * Returns an arbitrary implementation for product-3 values. 1387 * 1388 * @param aa An arbitrary implementation for one of the types over which the product-3 is 1389 * defined. 1390 * @param ab An Arbitrary implementation for one of the types over which the product-3 is 1391 * defined. 1392 * @param ac An arbitrary implementation for one of the types over which the product-3 is 1393 * defined. 1394 * @return An arbitrary implementation for product-3 values. 1395 */ 1396 public static <A, B, C> Arbitrary<P3<A, B, C>> arbP3(final Arbitrary<A> aa, final Arbitrary<B> ab, 1397 final Arbitrary<C> ac) { 1398 return arbitrary(aa.gen.bind(ab.gen, ac.gen, new F<A, F<B, F<C, P3<A, B, C>>>>() { 1399 public F<B, F<C, P3<A, B, C>>> f(final A a) { 1400 return new F<B, F<C, P3<A, B, C>>>() { 1401 public F<C, P3<A, B, C>> f(final B b) { 1402 return new F<C, P3<A, B, C>>() { 1403 public P3<A, B, C> f(final C c) { 1404 return p(a, b, c); 1405 } 1406 }; 1407 } 1408 }; 1409 } 1410 })); 1411 } 1412 1413 /** 1414 * Returns an arbitrary implementation for product-4 values. 1415 * 1416 * @param aa An arbitrary implementation for one of the types over which the product-4 is 1417 * defined. 1418 * @param ab An Arbitrary implementation for one of the types over which the product-4 is 1419 * defined. 1420 * @param ac An arbitrary implementation for one of the types over which the product-4 is 1421 * defined. 1422 * @param ad An arbitrary implementation for one of the types over which the product-4 is 1423 * defined. 1424 * @return An arbitrary implementation for product-4 values. 1425 */ 1426 public static <A, B, C, D> Arbitrary<P4<A, B, C, D>> arbP4(final Arbitrary<A> aa, final Arbitrary<B> ab, 1427 final Arbitrary<C> ac, final Arbitrary<D> ad) { 1428 return arbitrary(aa.gen.bind(ab.gen, ac.gen, ad.gen, new F<A, F<B, F<C, F<D, P4<A, B, C, D>>>>>() { 1429 public F<B, F<C, F<D, P4<A, B, C, D>>>> f(final A a) { 1430 return new F<B, F<C, F<D, P4<A, B, C, D>>>>() { 1431 public F<C, F<D, P4<A, B, C, D>>> f(final B b) { 1432 return new F<C, F<D, P4<A, B, C, D>>>() { 1433 public F<D, P4<A, B, C, D>> f(final C c) { 1434 return new F<D, P4<A, B, C, D>>() { 1435 public P4<A, B, C, D> f(final D d) { 1436 return p(a, b, c, d); 1437 } 1438 }; 1439 } 1440 }; 1441 } 1442 }; 1443 } 1444 })); 1445 } 1446 1447 /** 1448 * Returns an arbitrary implementation for product-5 values. 1449 * 1450 * @param aa An arbitrary implementation for one of the types over which the product-5 is 1451 * defined. 1452 * @param ab An Arbitrary implementation for one of the types over which the product-5 is 1453 * defined. 1454 * @param ac An arbitrary implementation for one of the types over which the product-5 is 1455 * defined. 1456 * @param ad An arbitrary implementation for one of the types over which the product-5 is 1457 * defined. 1458 * @param ae An arbitrary implementation for one of the types over which the product-5 is 1459 * defined. 1460 * @return An arbitrary implementation for product-5 values. 1461 */ 1462 public static <A, B, C, D, E> Arbitrary<P5<A, B, C, D, E>> arbP5(final Arbitrary<A> aa, final Arbitrary<B> ab, 1463 final Arbitrary<C> ac, final Arbitrary<D> ad, 1464 final Arbitrary<E> ae) { 1465 return arbitrary(aa.gen.bind(ab.gen, ac.gen, ad.gen, ae.gen, new F<A, F<B, F<C, F<D, F<E, P5<A, B, C, D, E>>>>>>() { 1466 public F<B, F<C, F<D, F<E, P5<A, B, C, D, E>>>>> f(final A a) { 1467 return new F<B, F<C, F<D, F<E, P5<A, B, C, D, E>>>>>() { 1468 public F<C, F<D, F<E, P5<A, B, C, D, E>>>> f(final B b) { 1469 return new F<C, F<D, F<E, P5<A, B, C, D, E>>>>() { 1470 public F<D, F<E, P5<A, B, C, D, E>>> f(final C c) { 1471 return new F<D, F<E, P5<A, B, C, D, E>>>() { 1472 public F<E, P5<A, B, C, D, E>> f(final D d) { 1473 return new F<E, P5<A, B, C, D, E>>() { 1474 public P5<A, B, C, D, E> f(final E e) { 1475 return p(a, b, c, d, e); 1476 } 1477 }; 1478 } 1479 }; 1480 } 1481 }; 1482 } 1483 }; 1484 } 1485 })); 1486 } 1487 1488 /** 1489 * Returns an arbitrary implementation for product-6 values. 1490 * 1491 * @param aa An arbitrary implementation for one of the types over which the product-6 is 1492 * defined. 1493 * @param ab An Arbitrary implementation for one of the types over which the product-6 is 1494 * defined. 1495 * @param ac An arbitrary implementation for one of the types over which the product-6 is 1496 * defined. 1497 * @param ad An arbitrary implementation for one of the types over which the product-6 is 1498 * defined. 1499 * @param ae An arbitrary implementation for one of the types over which the product-6 is 1500 * defined. 1501 * @param af An arbitrary implementation for one of the types over which the product-7 is 1502 * defined. 1503 * @return An arbitrary implementation for product-6 values. 1504 */ 1505 public static <A, B, C, D, E, F$> Arbitrary<P6<A, B, C, D, E, F$>> arbP6(final Arbitrary<A> aa, final Arbitrary<B> ab, 1506 final Arbitrary<C> ac, final Arbitrary<D> ad, 1507 final Arbitrary<E> ae, 1508 final Arbitrary<F$> af) { 1509 return arbitrary(aa.gen.bind(ab.gen, ac.gen, ad.gen, ae.gen, af.gen, 1510 new F<A, F<B, F<C, F<D, F<E, F<F$, P6<A, B, C, D, E, F$>>>>>>>() { 1511 public F<B, F<C, F<D, F<E, F<F$, P6<A, B, C, D, E, F$>>>>>> f(final A a) { 1512 return new F<B, F<C, F<D, F<E, F<F$, P6<A, B, C, D, E, F$>>>>>>() { 1513 public F<C, F<D, F<E, F<F$, P6<A, B, C, D, E, F$>>>>> f(final B b) { 1514 return new F<C, F<D, F<E, F<F$, P6<A, B, C, D, E, F$>>>>>() { 1515 public F<D, F<E, F<F$, P6<A, B, C, D, E, F$>>>> f(final C c) { 1516 return new F<D, F<E, F<F$, P6<A, B, C, D, E, F$>>>>() { 1517 public F<E, F<F$, P6<A, B, C, D, E, F$>>> f(final D d) { 1518 return new F<E, F<F$, P6<A, B, C, D, E, F$>>>() { 1519 public F<F$, P6<A, B, C, D, E, F$>> f(final E e) { 1520 return new F<F$, P6<A, B, C, D, E, F$>>() { 1521 public P6<A, B, C, D, E, F$> f(final F$ f) { 1522 return p(a, b, c, d, e, f); 1523 } 1524 }; 1525 } 1526 }; 1527 } 1528 }; 1529 } 1530 }; 1531 } 1532 }; 1533 } 1534 })); 1535 } 1536 1537 /** 1538 * Returns an arbitrary implementation for product-7 values. 1539 * 1540 * @param aa An arbitrary implementation for one of the types over which the product-7 is 1541 * defined. 1542 * @param ab An Arbitrary implementation for one of the types over which the product-7 is 1543 * defined. 1544 * @param ac An arbitrary implementation for one of the types over which the product-7 is 1545 * defined. 1546 * @param ad An arbitrary implementation for one of the types over which the product-7 is 1547 * defined. 1548 * @param ae An arbitrary implementation for one of the types over which the product-7 is 1549 * defined. 1550 * @param af An arbitrary implementation for one of the types over which the product-7 is 1551 * defined. 1552 * @param ag An arbitrary implementation for one of the types over which the product-8 is 1553 * defined. 1554 * @return An arbitrary implementation for product-7 values. 1555 */ 1556 public static <A, B, C, D, E, F$, G> Arbitrary<P7<A, B, C, D, E, F$, G>> arbP7(final Arbitrary<A> aa, 1557 final Arbitrary<B> ab, 1558 final Arbitrary<C> ac, 1559 final Arbitrary<D> ad, 1560 final Arbitrary<E> ae, 1561 final Arbitrary<F$> af, 1562 final Arbitrary<G> ag) { 1563 return arbitrary(aa.gen.bind(ab.gen, ac.gen, ad.gen, ae.gen, af.gen, ag.gen, 1564 new F<A, F<B, F<C, F<D, F<E, F<F$, F<G, P7<A, B, C, D, E, F$, G>>>>>>>>() { 1565 public F<B, F<C, F<D, F<E, F<F$, F<G, P7<A, B, C, D, E, F$, G>>>>>>> f(final A a) { 1566 return new F<B, F<C, F<D, F<E, F<F$, F<G, P7<A, B, C, D, E, F$, G>>>>>>>() { 1567 public F<C, F<D, F<E, F<F$, F<G, P7<A, B, C, D, E, F$, G>>>>>> f(final B b) { 1568 return new F<C, F<D, F<E, F<F$, F<G, P7<A, B, C, D, E, F$, G>>>>>>() { 1569 public F<D, F<E, F<F$, F<G, P7<A, B, C, D, E, F$, G>>>>> f(final C c) { 1570 return new F<D, F<E, F<F$, F<G, P7<A, B, C, D, E, F$, G>>>>>() { 1571 public F<E, F<F$, F<G, P7<A, B, C, D, E, F$, G>>>> f(final D d) { 1572 return new F<E, F<F$, F<G, P7<A, B, C, D, E, F$, G>>>>() { 1573 public F<F$, F<G, P7<A, B, C, D, E, F$, G>>> f(final E e) { 1574 return new F<F$, F<G, P7<A, B, C, D, E, F$, G>>>() { 1575 public F<G, P7<A, B, C, D, E, F$, G>> f(final F$ f) { 1576 return new F<G, P7<A, B, C, D, E, F$, G>>() { 1577 public P7<A, B, C, D, E, F$, G> f(final G g) { 1578 return p(a, b, c, d, e, f, g); 1579 } 1580 }; 1581 } 1582 }; 1583 } 1584 }; 1585 } 1586 }; 1587 } 1588 }; 1589 } 1590 }; 1591 } 1592 })); 1593 } 1594 1595 /** 1596 * Returns an arbitrary implementation for product-8 values. 1597 * 1598 * @param aa An arbitrary implementation for one of the types over which the product-8 is 1599 * defined. 1600 * @param ab An Arbitrary implementation for one of the types over which the product-8 is 1601 * defined. 1602 * @param ac An arbitrary implementation for one of the types over which the product-8 is 1603 * defined. 1604 * @param ad An arbitrary implementation for one of the types over which the product-8 is 1605 * defined. 1606 * @param ae An arbitrary implementation for one of the types over which the product-8 is 1607 * defined. 1608 * @param af An arbitrary implementation for one of the types over which the product-8 is 1609 * defined. 1610 * @param ag An arbitrary implementation for one of the types over which the product-8 is 1611 * defined. 1612 * @param ah An arbitrary implementation for one of the types over which the product-8 is 1613 * defined. 1614 * @return An arbitrary implementation for product-8 values. 1615 */ 1616 public static <A, B, C, D, E, F$, G, H> Arbitrary<P8<A, B, C, D, E, F$, G, H>> arbP8(final Arbitrary<A> aa, 1617 final Arbitrary<B> ab, 1618 final Arbitrary<C> ac, 1619 final Arbitrary<D> ad, 1620 final Arbitrary<E> ae, 1621 final Arbitrary<F$> af, 1622 final Arbitrary<G> ag, 1623 final Arbitrary<H> ah) { 1624 return arbitrary(aa.gen.bind(ab.gen, ac.gen, ad.gen, ae.gen, af.gen, ag.gen, ah.gen, 1625 new F<A, F<B, F<C, F<D, F<E, F<F$, F<G, F<H, P8<A, B, C, D, E, F$, G, H>>>>>>>>>() { 1626 public F<B, F<C, F<D, F<E, F<F$, F<G, F<H, P8<A, B, C, D, E, F$, G, H>>>>>>>> f( 1627 final A a) { 1628 return new F<B, F<C, F<D, F<E, F<F$, F<G, F<H, P8<A, B, C, D, E, F$, G, H>>>>>>>>() { 1629 public F<C, F<D, F<E, F<F$, F<G, F<H, P8<A, B, C, D, E, F$, G, H>>>>>>> f( 1630 final B b) { 1631 return new F<C, F<D, F<E, F<F$, F<G, F<H, P8<A, B, C, D, E, F$, G, H>>>>>>>() { 1632 public F<D, F<E, F<F$, F<G, F<H, P8<A, B, C, D, E, F$, G, H>>>>>> f( 1633 final C c) { 1634 return new F<D, F<E, F<F$, F<G, F<H, P8<A, B, C, D, E, F$, G, H>>>>>>() { 1635 public F<E, F<F$, F<G, F<H, P8<A, B, C, D, E, F$, G, H>>>>> f( 1636 final D d) { 1637 return new F<E, F<F$, F<G, F<H, P8<A, B, C, D, E, F$, G, H>>>>>() { 1638 public F<F$, F<G, F<H, P8<A, B, C, D, E, F$, G, H>>>> f(final E e) { 1639 return new F<F$, F<G, F<H, P8<A, B, C, D, E, F$, G, H>>>>() { 1640 public F<G, F<H, P8<A, B, C, D, E, F$, G, H>>> f(final F$ f) { 1641 return new F<G, F<H, P8<A, B, C, D, E, F$, G, H>>>() { 1642 public F<H, P8<A, B, C, D, E, F$, G, H>> f(final G g) { 1643 return new F<H, P8<A, B, C, D, E, F$, G, H>>() { 1644 public P8<A, B, C, D, E, F$, G, H> f(final H h) { 1645 return p(a, b, c, d, e, f, g, h); 1646 } 1647 }; 1648 } 1649 }; 1650 } 1651 }; 1652 } 1653 }; 1654 } 1655 }; 1656 } 1657 }; 1658 } 1659 }; 1660 } 1661 })); 1662 } 1663 }