/core/src/main/java/fj/test/Arbitrary.java

https://github.com/rafalrusin/functionaljava · Java · 1188 lines · 666 code · 83 blank · 439 comment · 4 complexity · a531faac2a36d4c3916c9ad1d2907209 MD5 · raw file

  1. package fj.test;
  2. import fj.Effect;
  3. import fj.F;
  4. import fj.F2;
  5. import fj.F3;
  6. import fj.F4;
  7. import fj.F5;
  8. import fj.F6;
  9. import fj.F7;
  10. import fj.F8;
  11. import fj.Function;
  12. import static fj.Function.compose;
  13. import static fj.P.p;
  14. import fj.P1;
  15. import fj.P2;
  16. import fj.P3;
  17. import fj.P4;
  18. import fj.P5;
  19. import fj.P6;
  20. import fj.P7;
  21. import fj.P8;
  22. import fj.data.Array;
  23. import fj.data.Either;
  24. import static fj.data.Either.left;
  25. import static fj.data.Either.right;
  26. import static fj.data.Enumerator.charEnumerator;
  27. import fj.data.List;
  28. import static fj.data.List.asString;
  29. import static fj.data.List.list;
  30. import fj.data.Option;
  31. import static fj.data.Option.some;
  32. import fj.data.Stream;
  33. import static fj.data.Stream.range;
  34. import static fj.test.Gen.choose;
  35. import static fj.test.Gen.elements;
  36. import static fj.test.Gen.fail;
  37. import static fj.test.Gen.frequency;
  38. import static fj.test.Gen.listOf;
  39. import static fj.test.Gen.oneOf;
  40. import static fj.test.Gen.promote;
  41. import static fj.test.Gen.sized;
  42. import static fj.test.Gen.value;
  43. import static java.lang.Math.abs;
  44. import java.math.BigDecimal;
  45. import java.math.BigInteger;
  46. import java.sql.Time;
  47. import java.sql.Timestamp;
  48. import java.util.ArrayList;
  49. import java.util.BitSet;
  50. import java.util.Calendar;
  51. import java.util.Date;
  52. import java.util.EnumMap;
  53. import java.util.EnumSet;
  54. import java.util.GregorianCalendar;
  55. import java.util.HashMap;
  56. import java.util.HashSet;
  57. import java.util.Hashtable;
  58. import java.util.IdentityHashMap;
  59. import java.util.LinkedHashMap;
  60. import java.util.LinkedHashSet;
  61. import java.util.LinkedList;
  62. import java.util.Locale;
  63. import static java.util.Locale.getAvailableLocales;
  64. import java.util.PriorityQueue;
  65. import java.util.Properties;
  66. import java.util.Stack;
  67. import java.util.TreeMap;
  68. import java.util.TreeSet;
  69. import java.util.Vector;
  70. import java.util.WeakHashMap;
  71. import static java.util.EnumSet.copyOf;
  72. import java.util.concurrent.ArrayBlockingQueue;
  73. import java.util.concurrent.ConcurrentHashMap;
  74. import java.util.concurrent.ConcurrentLinkedQueue;
  75. import java.util.concurrent.CopyOnWriteArrayList;
  76. import java.util.concurrent.CopyOnWriteArraySet;
  77. import java.util.concurrent.DelayQueue;
  78. import java.util.concurrent.Delayed;
  79. import java.util.concurrent.LinkedBlockingQueue;
  80. import java.util.concurrent.PriorityBlockingQueue;
  81. import java.util.concurrent.SynchronousQueue;
  82. /**
  83. * The type used to generate arbitrary values of the given type parameter (<code>A</code>). Common
  84. * arbitrary implementations are provided.
  85. *
  86. * @version %build.number%
  87. */
  88. public final class Arbitrary<A> {
  89. /**
  90. * The generator associated with this arbitrary.
  91. */
  92. @SuppressWarnings({"PublicField"})
  93. public final Gen<A> gen;
  94. private Arbitrary(final Gen<A> gen) {
  95. this.gen = gen;
  96. }
  97. /**
  98. * Constructs and arbitrary with the given generator.
  99. *
  100. * @param g The generator to construct an arbitrary with.
  101. * @return A new arbitrary that uses the given generator.
  102. */
  103. public static <A> Arbitrary<A> arbitrary(final Gen<A> g) {
  104. return new Arbitrary<A>(g);
  105. }
  106. /**
  107. * An arbitrary for functions.
  108. *
  109. * @param c The coarbitrary for the function domain.
  110. * @param a The arbitrary for the function codomain.
  111. * @return An arbitrary for functions.
  112. */
  113. public static <A, B> Arbitrary<F<A, B>> arbF(final Coarbitrary<A> c, final Arbitrary<B> a) {
  114. return arbitrary(promote(new F<A, Gen<B>>() {
  115. public Gen<B> f(final A x) {
  116. return c.coarbitrary(x, a.gen);
  117. }
  118. }));
  119. }
  120. /**
  121. * An arbitrary for functions.
  122. *
  123. * @param a The arbitrary for the function codomain.
  124. * @return An arbitrary for functions.
  125. */
  126. public static <A, B> Arbitrary<F<A, B>> arbFInvariant(final Arbitrary<B> a) {
  127. return arbitrary(a.gen.map(Function.<A, B>constant()));
  128. }
  129. /**
  130. * An arbitrary for function-2.
  131. *
  132. * @param ca A coarbitrary for the part of the domain of the function.
  133. * @param cb A coarbitrary for the part of the domain of the function.
  134. * @param a An arbitrary for the codomain of the function.
  135. * @return An arbitrary for function-2.
  136. */
  137. public static <A, B, C> Arbitrary<F2<A, B, C>> arbF2(final Coarbitrary<A> ca, final Coarbitrary<B> cb,
  138. final Arbitrary<C> a) {
  139. return arbitrary(arbF(ca, arbF(cb, a)).gen.map(Function.<A, B, C>uncurryF2()));
  140. }
  141. /**
  142. * An arbitrary for function-2.
  143. *
  144. * @param a The arbitrary for the function codomain.
  145. * @return An arbitrary for function-2.
  146. */
  147. public static <A, B, C> Arbitrary<F2<A, B, C>> arbF2Invariant(final Arbitrary<C> a) {
  148. return arbitrary(a.gen.map(
  149. compose(Function.<A, B, C>uncurryF2(), compose(Function.<A, F<B, C>>constant(), Function.<B, C>constant()))));
  150. }
  151. /**
  152. * An arbitrary for function-3.
  153. *
  154. * @param ca A coarbitrary for the part of the domain of the function.
  155. * @param cb A coarbitrary for the part of the domain of the function.
  156. * @param cc A coarbitrary for the part of the domain of the function.
  157. * @param a An arbitrary for the codomain of the function.
  158. * @return An arbitrary for function-3.
  159. */
  160. public static <A, B, C, D> Arbitrary<F3<A, B, C, D>> arbF3(final Coarbitrary<A> ca, final Coarbitrary<B> cb,
  161. final Coarbitrary<C> cc, final Arbitrary<D> a) {
  162. return arbitrary(arbF(ca, arbF(cb, arbF(cc, a))).gen.map(Function.<A, B, C, D>uncurryF3()));
  163. }
  164. /**
  165. * An arbitrary for function-3.
  166. *
  167. * @param a The arbitrary for the function codomain.
  168. * @return An arbitrary for function-3.
  169. */
  170. public static <A, B, C, D> Arbitrary<F3<A, B, C, D>> arbF3Invariant(final Arbitrary<D> a) {
  171. return arbitrary(a.gen.map(compose(Function.<A, B, C, D>uncurryF3(), compose(Function.<A, F<B, F<C, D>>>constant(),
  172. compose(
  173. Function.<B, F<C, D>>constant(),
  174. Function.<C, D>constant())))));
  175. }
  176. /**
  177. * An arbitrary for function-4.
  178. *
  179. * @param ca A coarbitrary for the part of the domain of the function.
  180. * @param cb A coarbitrary for the part of the domain of the function.
  181. * @param cc A coarbitrary for the part of the domain of the function.
  182. * @param cd A coarbitrary for the part of the domain of the function.
  183. * @param a An arbitrary for the codomain of the function.
  184. * @return An arbitrary for function-4.
  185. */
  186. public static <A, B, C, D, E> Arbitrary<F4<A, B, C, D, E>> arbF4(final Coarbitrary<A> ca, final Coarbitrary<B> cb,
  187. final Coarbitrary<C> cc, final Coarbitrary<D> cd,
  188. final Arbitrary<E> a) {
  189. return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, a)))).gen.map(Function.<A, B, C, D, E>uncurryF4()));
  190. }
  191. /**
  192. * An arbitrary for function-4.
  193. *
  194. * @param a The arbitrary for the function codomain.
  195. * @return An arbitrary for function-4.
  196. */
  197. public static <A, B, C, D, E> Arbitrary<F4<A, B, C, D, E>> arbF4Invariant(final Arbitrary<E> a) {
  198. return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E>uncurryF4(),
  199. compose(Function.<A, F<B, F<C, F<D, E>>>>constant(),
  200. compose(Function.<B, F<C, F<D, E>>>constant(),
  201. compose(Function.<C, F<D, E>>constant(),
  202. Function.<D, E>constant()))))));
  203. }
  204. /**
  205. * An arbitrary for function-5.
  206. *
  207. * @param ca A coarbitrary for the part of the domain of the function.
  208. * @param cb A coarbitrary for the part of the domain of the function.
  209. * @param cc A coarbitrary for the part of the domain of the function.
  210. * @param cd A coarbitrary for the part of the domain of the function.
  211. * @param ce A coarbitrary for the part of the domain of the function.
  212. * @param a An arbitrary for the codomain of the function.
  213. * @return An arbitrary for function-5.
  214. */
  215. public static <A, B, C, D, E, F$> Arbitrary<F5<A, B, C, D, E, F$>> arbF5(final Coarbitrary<A> ca,
  216. final Coarbitrary<B> cb,
  217. final Coarbitrary<C> cc,
  218. final Coarbitrary<D> cd,
  219. final Coarbitrary<E> ce,
  220. final Arbitrary<F$> a) {
  221. return arbitrary(
  222. arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, a))))).gen.map(Function.<A, B, C, D, E, F$>uncurryF5()));
  223. }
  224. /**
  225. * An arbitrary for function-5.
  226. *
  227. * @param a The arbitrary for the function codomain.
  228. * @return An arbitrary for function-5.
  229. */
  230. public static <A, B, C, D, E, F$> Arbitrary<F5<A, B, C, D, E, F$>> arbF5Invariant(final Arbitrary<F$> a) {
  231. return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E, F$>uncurryF5(),
  232. compose(Function.<A, F<B, F<C, F<D, F<E, F$>>>>>constant(),
  233. compose(Function.<B, F<C, F<D, F<E, F$>>>>constant(),
  234. compose(Function.<C, F<D, F<E, F$>>>constant(),
  235. compose(Function.<D, F<E, F$>>constant(),
  236. Function.<E, F$>constant())))))));
  237. }
  238. /**
  239. * An arbitrary for function-6.
  240. *
  241. * @param ca A coarbitrary for the part of the domain of the function.
  242. * @param cb A coarbitrary for the part of the domain of the function.
  243. * @param cc A coarbitrary for the part of the domain of the function.
  244. * @param cd A coarbitrary for the part of the domain of the function.
  245. * @param ce A coarbitrary for the part of the domain of the function.
  246. * @param cf A coarbitrary for the part of the domain of the function.
  247. * @param a An arbitrary for the codomain of the function.
  248. * @return An arbitrary for function-6.
  249. */
  250. public static <A, B, C, D, E, F$, G> Arbitrary<F6<A, B, C, D, E, F$, G>> arbF6(final Coarbitrary<A> ca,
  251. final Coarbitrary<B> cb,
  252. final Coarbitrary<C> cc,
  253. final Coarbitrary<D> cd,
  254. final Coarbitrary<E> ce,
  255. final Coarbitrary<F$> cf,
  256. final Arbitrary<G> a) {
  257. return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, a)))))).gen.map(
  258. Function.<A, B, C, D, E, F$, G>uncurryF6()));
  259. }
  260. /**
  261. * An arbitrary for function-6.
  262. *
  263. * @param a The arbitrary for the function codomain.
  264. * @return An arbitrary for function-6.
  265. */
  266. public static <A, B, C, D, E, F$, G> Arbitrary<F6<A, B, C, D, E, F$, G>> arbF6Invariant(final Arbitrary<G> a) {
  267. return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E, F$, G>uncurryF6(),
  268. compose(Function.<A, F<B, F<C, F<D, F<E, F<F$, G>>>>>>constant(),
  269. compose(Function.<B, F<C, F<D, F<E, F<F$, G>>>>>constant(),
  270. compose(Function.<C, F<D, F<E, F<F$, G>>>>constant(),
  271. compose(Function.<D, F<E, F<F$, G>>>constant(),
  272. compose(Function.<E, F<F$, G>>constant(),
  273. Function.<F$, G>constant()))))))));
  274. }
  275. /**
  276. * An arbitrary for function-7.
  277. *
  278. * @param ca A coarbitrary for the part of the domain of the function.
  279. * @param cb A coarbitrary for the part of the domain of the function.
  280. * @param cc A coarbitrary for the part of the domain of the function.
  281. * @param cd A coarbitrary for the part of the domain of the function.
  282. * @param ce A coarbitrary for the part of the domain of the function.
  283. * @param cf A coarbitrary for the part of the domain of the function.
  284. * @param cg A coarbitrary for the part of the domain of the function.
  285. * @param a An arbitrary for the codomain of the function.
  286. * @return An arbitrary for function-7.
  287. */
  288. 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,
  289. final Coarbitrary<B> cb,
  290. final Coarbitrary<C> cc,
  291. final Coarbitrary<D> cd,
  292. final Coarbitrary<E> ce,
  293. final Coarbitrary<F$> cf,
  294. final Coarbitrary<G> cg,
  295. final Arbitrary<H> a) {
  296. return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, arbF(cg, a))))))).gen.map(
  297. Function.<A, B, C, D, E, F$, G, H>uncurryF7()));
  298. }
  299. /**
  300. * An arbitrary for function-7.
  301. *
  302. * @param a The arbitrary for the function codomain.
  303. * @return An arbitrary for function-7.
  304. */
  305. 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) {
  306. return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E, F$, G, H>uncurryF7(),
  307. compose(Function.<A, F<B, F<C, F<D, F<E, F<F$, F<G, H>>>>>>>constant(),
  308. compose(Function.<B, F<C, F<D, F<E, F<F$, F<G, H>>>>>>constant(),
  309. compose(Function.<C, F<D, F<E, F<F$, F<G, H>>>>>constant(),
  310. compose(Function.<D, F<E, F<F$, F<G, H>>>>constant(),
  311. compose(Function.<E, F<F$, F<G, H>>>constant(),
  312. compose(Function.<F$, F<G, H>>constant(),
  313. Function.<G, H>constant())))))))));
  314. }
  315. /**
  316. * An arbitrary for function-8.
  317. *
  318. * @param ca A coarbitrary for the part of the domain of the function.
  319. * @param cb A coarbitrary for the part of the domain of the function.
  320. * @param cc A coarbitrary for the part of the domain of the function.
  321. * @param cd A coarbitrary for the part of the domain of the function.
  322. * @param ce A coarbitrary for the part of the domain of the function.
  323. * @param cf A coarbitrary for the part of the domain of the function.
  324. * @param cg A coarbitrary for the part of the domain of the function.
  325. * @param ch A coarbitrary for the part of the domain of the function.
  326. * @param a An arbitrary for the codomain of the function.
  327. * @return An arbitrary for function-8.
  328. */
  329. 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,
  330. final Coarbitrary<B> cb,
  331. final Coarbitrary<C> cc,
  332. final Coarbitrary<D> cd,
  333. final Coarbitrary<E> ce,
  334. final Coarbitrary<F$> cf,
  335. final Coarbitrary<G> cg,
  336. final Coarbitrary<H> ch,
  337. final Arbitrary<I> a) {
  338. return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, arbF(cg, arbF(ch, a)))))))).gen.map(
  339. Function.<A, B, C, D, E, F$, G, H, I>uncurryF8()));
  340. }
  341. /**
  342. * An arbitrary for function-8.
  343. *
  344. * @param a The arbitrary for the function codomain.
  345. * @return An arbitrary for function-8.
  346. */
  347. public static <A, B, C, D, E, F$, G, H, I> Arbitrary<F8<A, B, C, D, E, F$, G, H, I>> arbF8Invariant(
  348. final Arbitrary<I> a) {
  349. return arbitrary(a.gen.map(compose(Function.<A, B, C, D, E, F$, G, H, I>uncurryF8(),
  350. compose(Function.<A, F<B, F<C, F<D, F<E, F<F$, F<G, F<H, I>>>>>>>>constant(),
  351. compose(Function.<B, F<C, F<D, F<E, F<F$, F<G, F<H, I>>>>>>>constant(),
  352. compose(Function.<C, F<D, F<E, F<F$, F<G, F<H, I>>>>>>constant(),
  353. compose(
  354. Function.<D, F<E, F<F$, F<G, F<H, I>>>>>constant(),
  355. compose(Function.<E, F<F$, F<G, F<H, I>>>>constant(),
  356. compose(
  357. Function.<F$, F<G, F<H, I>>>constant(),
  358. compose(Function.<G, F<H, I>>constant(),
  359. Function.<H, I>constant()))))))))));
  360. }
  361. /**
  362. * An arbitrary implementation for boolean values.
  363. */
  364. public static final Arbitrary<Boolean> arbBoolean = arbitrary(elements(true, false));
  365. /**
  366. * An arbitrary implementation for integer values.
  367. */
  368. public static final Arbitrary<Integer> arbInteger = arbitrary(sized(new F<Integer, Gen<Integer>>() {
  369. public Gen<Integer> f(final Integer i) {
  370. return choose(-i, i);
  371. }
  372. }));
  373. /**
  374. * An arbitrary implementation for integer values that checks boundary values <code>(0, 1, -1,
  375. * max, min, max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link
  376. * #arbInteger} the remainder of the time (93%).
  377. */
  378. public static final Arbitrary<Integer> arbIntegerBoundaries = arbitrary(sized(new F<Integer, Gen<Integer>>() {
  379. @SuppressWarnings("unchecked")
  380. public Gen<Integer> f(final Integer i) {
  381. return frequency(list(p(1, value(0)),
  382. p(1, value(1)),
  383. p(1, value(-1)),
  384. p(1, value(Integer.MAX_VALUE)),
  385. p(1, value(Integer.MIN_VALUE)),
  386. p(1, value(Integer.MAX_VALUE - 1)),
  387. p(1, value(Integer.MIN_VALUE + 1)),
  388. p(93, arbInteger.gen)));
  389. }
  390. }));
  391. /**
  392. * An arbitrary implementation for long values.
  393. */
  394. public static final Arbitrary<Long> arbLong =
  395. arbitrary(arbInteger.gen.bind(arbInteger.gen, new F<Integer, F<Integer, Long>>() {
  396. public F<Integer, Long> f(final Integer i1) {
  397. return new F<Integer, Long>() {
  398. public Long f(final Integer i2) {
  399. return (long) i1 << 32L & i2;
  400. }
  401. };
  402. }
  403. }));
  404. /**
  405. * An arbitrary implementation for long values that checks boundary values <code>(0, 1, -1, max,
  406. * min, max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link #arbLong}
  407. * the remainder of the time (93%).
  408. */
  409. public static final Arbitrary<Long> arbLongBoundaries = arbitrary(sized(new F<Integer, Gen<Long>>() {
  410. @SuppressWarnings("unchecked")
  411. public Gen<Long> f(final Integer i) {
  412. return frequency(list(p(1, value(0L)),
  413. p(1, value(1L)),
  414. p(1, value(-1L)),
  415. p(1, value(Long.MAX_VALUE)),
  416. p(1, value(Long.MIN_VALUE)),
  417. p(1, value(Long.MAX_VALUE - 1L)),
  418. p(1, value(Long.MIN_VALUE + 1L)),
  419. p(93, arbLong.gen)));
  420. }
  421. }));
  422. /**
  423. * An arbitrary implementation for byte values.
  424. */
  425. public static final Arbitrary<Byte> arbByte = arbitrary(arbInteger.gen.map(new F<Integer, Byte>() {
  426. public Byte f(final Integer i) {
  427. return (byte) i.intValue();
  428. }
  429. }));
  430. /**
  431. * An arbitrary implementation for byte values that checks boundary values <code>(0, 1, -1, max,
  432. * min, max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link #arbByte}
  433. * the remainder of the time (93%).
  434. */
  435. public static final Arbitrary<Byte> arbByteBoundaries = arbitrary(sized(new F<Integer, Gen<Byte>>() {
  436. @SuppressWarnings("unchecked")
  437. public Gen<Byte> f(final Integer i) {
  438. return frequency(list(p(1, value((byte) 0)),
  439. p(1, value((byte) 1)),
  440. p(1, value((byte) -1)),
  441. p(1, value(Byte.MAX_VALUE)),
  442. p(1, value(Byte.MIN_VALUE)),
  443. p(1, value((byte) (Byte.MAX_VALUE - 1))),
  444. p(1, value((byte) (Byte.MIN_VALUE + 1))),
  445. p(93, arbByte.gen)));
  446. }
  447. }));
  448. /**
  449. * An arbitrary implementation for short values.
  450. */
  451. public static final Arbitrary<Short> arbShort = arbitrary(arbInteger.gen.map(new F<Integer, Short>() {
  452. public Short f(final Integer i) {
  453. return (short) i.intValue();
  454. }
  455. }));
  456. /**
  457. * An arbitrary implementation for short values that checks boundary values <code>(0, 1, -1, max,
  458. * min, max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link #arbShort}
  459. * the remainder of the time (93%).
  460. */
  461. public static final Arbitrary<Short> arbShortBoundaries = arbitrary(sized(new F<Integer, Gen<Short>>() {
  462. @SuppressWarnings("unchecked")
  463. public Gen<Short> f(final Integer i) {
  464. return frequency(list(p(1, value((short) 0)),
  465. p(1, value((short) 1)),
  466. p(1, value((short) -1)),
  467. p(1, value(Short.MAX_VALUE)),
  468. p(1, value(Short.MIN_VALUE)),
  469. p(1, value((short) (Short.MAX_VALUE - 1))),
  470. p(1, value((short) (Short.MIN_VALUE + 1))),
  471. p(93, arbShort.gen)));
  472. }
  473. }));
  474. /**
  475. * An arbitrary implementation for character values.
  476. */
  477. public static final Arbitrary<Character> arbCharacter = arbitrary(choose(0, 65536).map(new F<Integer, Character>() {
  478. public Character f(final Integer i) {
  479. return (char) i.intValue();
  480. }
  481. }));
  482. /**
  483. * An arbitrary implementation for character values that checks boundary values <code>(max, min,
  484. * max - 1, min + 1)</code> with a frequency of 1% each then generates from {@link #arbCharacter}
  485. * the remainder of the time (96%).
  486. */
  487. public static final Arbitrary<Character> arbCharacterBoundaries = arbitrary(sized(new F<Integer, Gen<Character>>() {
  488. @SuppressWarnings("unchecked")
  489. public Gen<Character> f(final Integer i) {
  490. return frequency(list(p(1, value(Character.MIN_VALUE)),
  491. p(1, value((char) (Character.MIN_VALUE + 1))),
  492. p(1, value(Character.MAX_VALUE)),
  493. p(1, value((char) (Character.MAX_VALUE - 1))),
  494. p(95, arbCharacter.gen)));
  495. }
  496. }));
  497. /**
  498. * An arbitrary implementation for double values.
  499. */
  500. public static final Arbitrary<Double> arbDouble = arbitrary(sized(new F<Integer, Gen<Double>>() {
  501. public Gen<Double> f(final Integer i) {
  502. return choose((double) -i, i);
  503. }
  504. }));
  505. /**
  506. * An arbitrary implementation for double values that checks boundary values <code>(0, 1, -1, max,
  507. * min, min (normal), NaN, -infinity, infinity, max - 1)</code> with a frequency of 1% each then
  508. * generates from {@link #arbDouble} the remainder of the time (91%).
  509. */
  510. public static final Arbitrary<Double> arbDoubleBoundaries = arbitrary(sized(new F<Integer, Gen<Double>>() {
  511. @SuppressWarnings("unchecked")
  512. public Gen<Double> f(final Integer i) {
  513. return frequency(list(p(1, value(0D)),
  514. p(1, value(1D)),
  515. p(1, value(-1D)),
  516. p(1, value(Double.MAX_VALUE)),
  517. p(1, value(Double.MIN_VALUE)),
  518. p(1, value(Double.NaN)),
  519. p(1, value(Double.NEGATIVE_INFINITY)),
  520. p(1, value(Double.POSITIVE_INFINITY)),
  521. p(1, value(Double.MAX_VALUE - 1D)),
  522. p(91, arbDouble.gen)));
  523. }
  524. }));
  525. /**
  526. * An arbitrary implementation for float values.
  527. */
  528. public static final Arbitrary<Float> arbFloat = arbitrary(arbDouble.gen.map(new F<Double, Float>() {
  529. public Float f(final Double d) {
  530. return (float) d.doubleValue();
  531. }
  532. }));
  533. /**
  534. * An arbitrary implementation for float values that checks boundary values <code>(0, 1, -1, max,
  535. * min, NaN, -infinity, infinity, max - 1)</code> with a frequency of 1% each then generates from
  536. * {@link #arbFloat} the remainder of the time (91%).
  537. */
  538. public static final Arbitrary<Float> arbFloatBoundaries = arbitrary(sized(new F<Integer, Gen<Float>>() {
  539. @SuppressWarnings("unchecked")
  540. public Gen<Float> f(final Integer i) {
  541. return frequency(list(p(1, value(0F)),
  542. p(1, value(1F)),
  543. p(1, value(-1F)),
  544. p(1, value(Float.MAX_VALUE)),
  545. p(1, value(Float.MIN_VALUE)),
  546. p(1, value(Float.NaN)),
  547. p(1, value(Float.NEGATIVE_INFINITY)),
  548. p(1, value(Float.POSITIVE_INFINITY)),
  549. p(1, value(Float.MAX_VALUE - 1F)),
  550. p(91, arbFloat.gen)));
  551. }
  552. }));
  553. /**
  554. * An arbitrary implementation for string values.
  555. */
  556. public static final Arbitrary<String> arbString =
  557. arbitrary(arbList(arbCharacter).gen.map(new F<List<Character>, String>() {
  558. public String f(final List<Character> cs) {
  559. return asString(cs);
  560. }
  561. }));
  562. /**
  563. * An arbitrary implementation for string values with characters in the US-ASCII range.
  564. */
  565. public static final Arbitrary<String> arbUSASCIIString =
  566. arbitrary(arbList(arbCharacter).gen.map(new F<List<Character>, String>() {
  567. public String f(final List<Character> cs) {
  568. return asString(cs.map(new F<Character, Character>() {
  569. public Character f(final Character c) {
  570. return (char) (c % 128);
  571. }
  572. }));
  573. }
  574. }));
  575. /**
  576. * An arbitrary implementation for string values with alpha-numeric characters.
  577. */
  578. public static final Arbitrary<String> arbAlphaNumString =
  579. arbitrary(arbList(arbitrary(elements(range(charEnumerator, 'a', 'z').append(
  580. range(charEnumerator, 'A', 'Z')).append(
  581. range(charEnumerator, '0', '9')).toArray().array(Character[].class)))).gen.map(asString()));
  582. /**
  583. * An arbitrary implementation for string buffer values.
  584. */
  585. public static final Arbitrary<StringBuffer> arbStringBuffer =
  586. arbitrary(arbString.gen.map(new F<String, StringBuffer>() {
  587. public StringBuffer f(final String s) {
  588. return new StringBuffer(s);
  589. }
  590. }));
  591. /**
  592. * An arbitrary implementation for string builder values.
  593. */
  594. public static final Arbitrary<StringBuilder> arbStringBuilder =
  595. arbitrary(arbString.gen.map(new F<String, StringBuilder>() {
  596. public StringBuilder f(final String s) {
  597. return new StringBuilder(s);
  598. }
  599. }));
  600. /**
  601. * Returns an arbitrary implementation for generators.
  602. *
  603. * @param aa an arbitrary implementation for the type over which the generator is defined.
  604. * @return An arbitrary implementation for generators.
  605. */
  606. public static <A> Arbitrary<Gen<A>> arbGen(final Arbitrary<A> aa) {
  607. return arbitrary(sized(new F<Integer, Gen<Gen<A>>>() {
  608. @SuppressWarnings({"IfMayBeConditional"})
  609. public Gen<Gen<A>> f(final Integer i) {
  610. if (i == 0)
  611. return fail();
  612. else
  613. return aa.gen.map(new F<A, Gen<A>>() {
  614. public Gen<A> f(final A a) {
  615. return value(a);
  616. }
  617. }).resize(i - 1);
  618. }
  619. }));
  620. }
  621. /**
  622. * Returns an arbitrary implementation for optional values.
  623. *
  624. * @param aa an arbitrary implementation for the type over which the optional value is defined.
  625. * @return An arbitrary implementation for optional values.
  626. */
  627. public static <A> Arbitrary<Option<A>> arbOption(final Arbitrary<A> aa) {
  628. return arbitrary(sized(new F<Integer, Gen<Option<A>>>() {
  629. public Gen<Option<A>> f(final Integer i) {
  630. return i == 0 ?
  631. value(Option.<A>none()) :
  632. aa.gen.map(new F<A, Option<A>>() {
  633. public Option<A> f(final A a) {
  634. return some(a);
  635. }
  636. }).resize(i - 1);
  637. }
  638. }));
  639. }
  640. /**
  641. * Returns an arbitrary implementation for the disjoint union.
  642. *
  643. * @param aa An arbitrary implementation for the type over which one side of the disjoint union is
  644. * defined.
  645. * @param ab An arbitrary implementation for the type over which one side of the disjoint union is
  646. * defined.
  647. * @return An arbitrary implementation for the disjoint union.
  648. */
  649. @SuppressWarnings({"unchecked"})
  650. public static <A, B> Arbitrary<Either<A, B>> arbEither(final Arbitrary<A> aa, final Arbitrary<B> ab) {
  651. final Gen<Either<A, B>> left = aa.gen.map(new F<A, Either<A, B>>() {
  652. public Either<A, B> f(final A a) {
  653. return left(a);
  654. }
  655. });
  656. final Gen<Either<A, B>> right = ab.gen.map(new F<B, Either<A, B>>() {
  657. public Either<A, B> f(final B b) {
  658. return right(b);
  659. }
  660. });
  661. return arbitrary(oneOf(list(left, right)));
  662. }
  663. /**
  664. * Returns an arbitrary implementation for lists.
  665. *
  666. * @param aa An arbitrary implementation for the type over which the list is defined.
  667. * @return An arbitrary implementation for lists.
  668. */
  669. public static <A> Arbitrary<List<A>> arbList(final Arbitrary<A> aa) {
  670. return arbitrary(listOf(aa.gen));
  671. }
  672. /**
  673. * Returns an arbitrary implementation for streams.
  674. *
  675. * @param aa An arbitrary implementation for the type over which the stream is defined.
  676. * @return An arbitrary implementation for streams.
  677. */
  678. public static <A> Arbitrary<Stream<A>> arbStream(final Arbitrary<A> aa) {
  679. return arbitrary(arbList(aa).gen.map(new F<List<A>, Stream<A>>() {
  680. public Stream<A> f(final List<A> as) {
  681. return as.toStream();
  682. }
  683. }));
  684. }
  685. /**
  686. * Returns an arbitrary implementation for arrays.
  687. *
  688. * @param aa An arbitrary implementation for the type over which the array is defined.
  689. * @return An arbitrary implementation for arrays.
  690. */
  691. public static <A> Arbitrary<Array<A>> arbArray(final Arbitrary<A> aa) {
  692. return arbitrary(arbList(aa).gen.map(new F<List<A>, Array<A>>() {
  693. public Array<A> f(final List<A> as) {
  694. return as.toArray();
  695. }
  696. }));
  697. }
  698. /**
  699. * Returns an arbitrary implementation for throwables.
  700. *
  701. * @param as An arbitrary used for the throwable message.
  702. * @return An arbitrary implementation for throwables.
  703. */
  704. public static Arbitrary<Throwable> arbThrowable(final Arbitrary<String> as) {
  705. return arbitrary(as.gen.map(new F<String, Throwable>() {
  706. public Throwable f(final String msg) {
  707. return new Throwable(msg);
  708. }
  709. }));
  710. }
  711. /**
  712. * An arbitrary implementation for throwables.
  713. */
  714. public static final Arbitrary<Throwable> arbThrowable = arbThrowable(arbString);
  715. // BEGIN java.util
  716. /**
  717. * Returns an arbitrary implementation for array lists.
  718. *
  719. * @param aa An arbitrary implementation for the type over which the array list is defined.
  720. * @return An arbitrary implementation for array lists.
  721. */
  722. public static <A> Arbitrary<ArrayList<A>> arbArrayList(final Arbitrary<A> aa) {
  723. return arbitrary(arbArray(aa).gen.map(new F<Array<A>, ArrayList<A>>() {
  724. public ArrayList<A> f(final Array<A> a) {
  725. return new ArrayList<A>(a.toCollection());
  726. }
  727. }));
  728. }
  729. /**
  730. * An arbitrary implementation for bit sets.
  731. */
  732. public static final Arbitrary<BitSet> arbBitSet =
  733. arbitrary(arbList(arbBoolean).gen.map(new F<List<Boolean>, BitSet>() {
  734. public BitSet f(final List<Boolean> bs) {
  735. final BitSet s = new BitSet(bs.length());
  736. bs.zipIndex().foreach(new Effect<P2<Boolean, Integer>>() {
  737. public void e(final P2<Boolean, Integer> bi) {
  738. s.set(bi._2(), bi._1());
  739. }
  740. });
  741. return s;
  742. }
  743. }));
  744. /**
  745. * An arbitrary implementation for calendars.
  746. */
  747. public static final Arbitrary<Calendar> arbCalendar = arbitrary(arbLong.gen.map(new F<Long, Calendar>() {
  748. public Calendar f(final Long i) {
  749. final Calendar c = Calendar.getInstance();
  750. c.setTimeInMillis(i);
  751. return c;
  752. }
  753. }));
  754. /**
  755. * An arbitrary implementation for dates.
  756. */
  757. public static final Arbitrary<Date> arbDate = arbitrary(arbLong.gen.map(new F<Long, Date>() {
  758. public Date f(final Long i) {
  759. return new Date(i);
  760. }
  761. }));
  762. /**
  763. * Returns an arbitrary implementation for a Java enumeration.
  764. *
  765. * @param clazz The type of enum to return an arbtrary of.
  766. * @return An arbitrary for instances of the supplied enum type.
  767. */
  768. public static <A extends Enum<A>> Arbitrary<A> arbEnumValue(final Class<A> clazz) {
  769. return arbitrary(Gen.elements(clazz.getEnumConstants()));
  770. }
  771. /**
  772. * Returns an arbitrary implementation for enum maps.
  773. *
  774. * @param ak An arbitrary implementation for the type over which the enum map's keys are defined.
  775. * @param av An arbitrary implementation for the type over which the enum map's values are
  776. * defined.
  777. * @return An arbitrary implementation for enum maps.
  778. */
  779. public static <K extends Enum<K>, V> Arbitrary<EnumMap<K, V>> arbEnumMap(final Arbitrary<K> ak,
  780. final Arbitrary<V> av) {
  781. return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, EnumMap<K, V>>() {
  782. @SuppressWarnings({"UseOfObsoleteCollectionType"})
  783. public EnumMap<K, V> f(final Hashtable<K, V> ht) {
  784. return new EnumMap<K, V>(ht);
  785. }
  786. }));
  787. }
  788. /**
  789. * Returns an arbitrary implementation for enum sets.
  790. *
  791. * @param aa An arbitrary implementation for the type over which the enum set is defined.
  792. * @return An arbitrary implementation for enum sets.
  793. */
  794. public static <A extends Enum<A>> Arbitrary<EnumSet<A>> arbEnumSet(final Arbitrary<A> aa) {
  795. return arbitrary(arbArray(aa).gen.map(new F<Array<A>, EnumSet<A>>() {
  796. public EnumSet<A> f(final Array<A> a) {
  797. return copyOf(a.toCollection());
  798. }
  799. }));
  800. }
  801. /**
  802. * An arbitrary implementation for gregorian calendars.
  803. */
  804. public static final Arbitrary<GregorianCalendar> arbGregorianCalendar =
  805. arbitrary(arbLong.gen.map(new F<Long, GregorianCalendar>() {
  806. public GregorianCalendar f(final Long i) {
  807. final GregorianCalendar c = new GregorianCalendar();
  808. c.setTimeInMillis(i);
  809. return c;
  810. }
  811. }));
  812. /**
  813. * Returns an arbitrary implementation for hash maps.
  814. *
  815. * @param ak An arbitrary implementation for the type over which the hash map's keys are defined.
  816. * @param av An arbitrary implementation for the type over which the hash map's values are
  817. * defined.
  818. * @return An arbitrary implementation for hash maps.
  819. */
  820. public static <K, V> Arbitrary<HashMap<K, V>> arbHashMap(final Arbitrary<K> ak, final Arbitrary<V> av) {
  821. return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, HashMap<K, V>>() {
  822. @SuppressWarnings({"UseOfObsoleteCollectionType"})
  823. public HashMap<K, V> f(final Hashtable<K, V> ht) {
  824. return new HashMap<K, V>(ht);
  825. }
  826. }));
  827. }
  828. /**
  829. * Returns an arbitrary implementation for hash sets.
  830. *
  831. * @param aa An arbitrary implementation for the type over which the hash set is defined.
  832. * @return An arbitrary implementation for hash sets.
  833. */
  834. public static <A> Arbitrary<HashSet<A>> arbHashSet(final Arbitrary<A> aa) {
  835. return arbitrary(arbArray(aa).gen.map(new F<Array<A>, HashSet<A>>() {
  836. public HashSet<A> f(final Array<A> a) {
  837. return new HashSet<A>(a.toCollection());
  838. }
  839. }));
  840. }
  841. /**
  842. * Returns an arbitrary implementation for hash tables.
  843. *
  844. * @param ak An arbitrary implementation for the type over which the hash table's keys are
  845. * defined.
  846. * @param av An arbitrary implementation for the type over which the hash table's values are
  847. * defined.
  848. * @return An arbitrary implementation for hash tables.
  849. */
  850. public static <K, V> Arbitrary<Hashtable<K, V>> arbHashtable(final Arbitrary<K> ak, final Arbitrary<V> av) {
  851. return arbitrary(arbList(ak).gen.bind(arbList(av).gen, new F<List<K>, F<List<V>, Hashtable<K, V>>>() {
  852. public F<List<V>, Hashtable<K, V>> f(final List<K> ks) {
  853. return new F<List<V>, Hashtable<K, V>>() {
  854. @SuppressWarnings({"UseOfObsoleteCollectionType"})
  855. public Hashtable<K, V> f(final List<V> vs) {
  856. final Hashtable<K, V> t = new Hashtable<K, V>();
  857. ks.zip(vs).foreach(new Effect<P2<K, V>>() {
  858. public void e(final P2<K, V> kv) {
  859. t.put(kv._1(), kv._2());
  860. }
  861. });
  862. return t;
  863. }
  864. };
  865. }
  866. }));
  867. }
  868. /**
  869. * Returns an arbitrary implementation for identity hash maps.
  870. *
  871. * @param ak An arbitrary implementation for the type over which the identity hash map's keys are
  872. * defined.
  873. * @param av An arbitrary implementation for the type over which the identity hash map's values
  874. * are defined.
  875. * @return An arbitrary implementation for identity hash maps.
  876. */
  877. public static <K, V> Arbitrary<IdentityHashMap<K, V>> arbIdentityHashMap(final Arbitrary<K> ak,
  878. final Arbitrary<V> av) {
  879. return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, IdentityHashMap<K, V>>() {
  880. @SuppressWarnings({"UseOfObsoleteCollectionType"})
  881. public IdentityHashMap<K, V> f(final Hashtable<K, V> ht) {
  882. return new IdentityHashMap<K, V>(ht);
  883. }
  884. }));
  885. }
  886. /**
  887. * Returns an arbitrary implementation for linked hash maps.
  888. *
  889. * @param ak An arbitrary implementation for the type over which the linked hash map's keys are
  890. * defined.
  891. * @param av An arbitrary implementation for the type over which the linked hash map's values are
  892. * defined.
  893. * @return An arbitrary implementation for linked hash maps.
  894. */
  895. public static <K, V> Arbitrary<LinkedHashMap<K, V>> arbLinkedHashMap(final Arbitrary<K> ak, final Arbitrary<V> av) {
  896. return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, LinkedHashMap<K, V>>() {
  897. @SuppressWarnings({"UseOfObsoleteCollectionType"})
  898. public LinkedHashMap<K, V> f(final Hashtable<K, V> ht) {
  899. return new LinkedHashMap<K, V>(ht);
  900. }
  901. }));
  902. }
  903. /**
  904. * Returns an arbitrary implementation for hash sets.
  905. *
  906. * @param aa An arbitrary implementation for the type over which the hash set is defined.
  907. * @return An arbitrary implementation for hash sets.
  908. */
  909. public static <A> Arbitrary<LinkedHashSet<A>> arbLinkedHashSet(final Arbitrary<A> aa) {
  910. return arbitrary(arbArray(aa).gen.map(new F<Array<A>, LinkedHashSet<A>>() {
  911. public LinkedHashSet<A> f(final Array<A> a) {
  912. return new LinkedHashSet<A>(a.toCollection());
  913. }
  914. }));
  915. }
  916. /**
  917. * Returns an arbitrary implementation for linked lists.
  918. *
  919. * @param aa An arbitrary implementation for the type over which the linked list is defined.
  920. * @return An arbitrary implementation for linked lists.
  921. */
  922. public static <A> Arbitrary<LinkedList<A>> arbLinkedList(final Arbitrary<A> aa) {
  923. return arbitrary(arbArray(aa).gen.map(new F<Array<A>, LinkedList<A>>() {
  924. public LinkedList<A> f(final Array<A> a) {
  925. return new LinkedList<A>(a.toCollection());
  926. }
  927. }));
  928. }
  929. /**
  930. * Returns an arbitrary implementation for priority queues.
  931. *
  932. * @param aa An arbitrary implementation for the type over which the priority queue is defined.
  933. * @return An arbitrary implementation for priority queues.
  934. */
  935. public static <A> Arbitrary<PriorityQueue<A>> arbPriorityQueue(final Arbitrary<A> aa) {
  936. return arbitrary(arbArray(aa).gen.map(new F<Array<A>, PriorityQueue<A>>() {
  937. public PriorityQueue<A> f(final Array<A> a) {
  938. return new PriorityQueue<A>(a.toCollection());
  939. }
  940. }));
  941. }
  942. /**
  943. * An arbitrary implementation for properties.
  944. */
  945. public static final Arbitrary<Properties> arbProperties =
  946. arbitrary(arbHashtable(arbString, arbString).gen.map(new F<Hashtable<String, String>, Properties>() {
  947. @SuppressWarnings({"UseOfObsoleteCollectionType"})
  948. public Properties f(final Hashtable<String, String> ht) {
  949. final Properties p = new Properties();
  950. for (final String k : ht.keySet()) {
  951. p.setProperty(k, ht.get(k));
  952. }
  953. return p;
  954. }
  955. }));
  956. /**
  957. * Returns an arbitrary implementation for stacks.
  958. *
  959. * @param aa An arbitrary implementation for the type over which the stack is defined.
  960. * @return An arbitrary implementation for stacks.
  961. */
  962. public static <A> Arbitrary<Stack<A>> arbStack(final Arbitrary<A> aa) {
  963. return arbitrary(arbArray(aa).gen.map(new F<Array<A>, Stack<A>>() {
  964. public Stack<A> f(final Array<A> a) {
  965. final Stack<A> s = new Stack<A>();
  966. s.addAll(a.toCollection());
  967. return s;
  968. }
  969. }));
  970. }
  971. /**
  972. * Returns an arbitrary implementation for tree maps.
  973. *
  974. * @param ak An arbitrary implementation for the type over which the tree map's keys are defined.
  975. * @param av An arbitrary implementation for the type over which the tree map's values are
  976. * defined.
  977. * @return An arbitrary implementation for tree maps.
  978. */
  979. public static <K, V> Arbitrary<TreeMap<K, V>> arbTreeMap(final Arbitrary<K> ak, final Arbitrary<V> av) {
  980. return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, TreeMap<K, V>>() {
  981. @SuppressWarnings({"UseOfObsoleteCollectionType"})
  982. public TreeMap<K, V> f(final Hashtable<K, V> ht) {
  983. return new TreeMap<K, V>(ht);
  984. }
  985. }));
  986. }
  987. /**
  988. * Returns an arbitrary implementation for tree sets.
  989. *
  990. * @param aa An arbitrary implementation for the type over which the tree set is defined.
  991. * @return An arbitrary implementation for tree sets.
  992. */
  993. public static <A> Arbitrary<TreeSet<A>> arbTreeSet(final Arbitrary<A> aa) {
  994. return arbitrary(arbArray(aa).gen.map(new F<Array<A>, TreeSet<A>>() {
  995. public TreeSet<A> f(final Array<A> a) {
  996. return new TreeSet<A>(a.toCollection());
  997. }
  998. }));
  999. }
  1000. /**
  1001. * Returns an arbitrary implementation for vectors.
  1002. *
  1003. * @param aa An arbitrary implementation for the type over which the vector is defined.
  1004. * @return An arbitrary implementation for vectors.
  1005. */
  1006. @SuppressWarnings({"UseOfObsoleteCollectionType"})
  1007. public static <A> Arbitrary<Vector<A>> arbVector(final Arbitrary<A> aa) {
  1008. return arbitrary(arbArray(aa).gen.map(new F<Array<A>, Vector<A>>() {
  1009. @SuppressWarnings({"UseOfObsoleteCollectionType"})
  1010. public Vector<A> f(final Array<A> a) {
  1011. return new Vector<A>(a.toCollection());
  1012. }
  1013. }));
  1014. }
  1015. /**
  1016. * Returns an arbitrary implementation for weak hash maps.
  1017. *
  1018. * @param ak An arbitrary implementation for the type over which the weak hash map's keys are
  1019. * defined.
  1020. * @param av An arbitrary implementation for the type over which the weak hash map's values are
  1021. * defined.
  1022. * @return An arbitrary implementation for weak hash maps.
  1023. */
  1024. public static <K, V> Arbitrary<WeakHashMap<K, V>> arbWeakHashMap(final Arbitrary<K> ak, final Arbitrary<V> av) {
  1025. return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, WeakHashMap<K, V>>() {
  1026. @SuppressWarnings({"UseOfObsoleteCollectionType"})
  1027. public WeakHashMap<K, V> f(final Hashtable<K, V> ht) {
  1028. return new WeakHashMap<K, V>(ht);
  1029. }
  1030. }));
  1031. }
  1032. // END java.util
  1033. // BEGIN java.util.concurrent
  1034. /**
  1035. * Returns an arbitrary implementation for array blocking queues.
  1036. *
  1037. * @param aa An arbitrary implementation for the type over which the array blocking queue is
  1038. * defined.
  1039. * @return An arbitrary implementation for array blocking queues.
  1040. */
  1041. public static <A> Arbitrary<ArrayBlockingQueue<A>> arbArrayBlockingQueue(final Arbitrary<A> aa) {
  1042. return arbitrary(arbArray(aa).gen.bind(arbInteger.gen, arbBoolean.gen,
  1043. new F<Array<A>, F<Integer, F<Boolean, ArrayBlockingQueue<A>>>>() {
  1044. public F<Integer, F<Boolean, ArrayBlockingQueue<A>>> f(final Array<A> a) {
  1045. return new F<Integer, F<Boolean, ArrayBlockingQueue<A>>>() {
  1046. public F<Boolean, ArrayBlockingQueue<A>> f(final Integer capacity) {
  1047. return new F<Boolean, ArrayBlockingQueue<A>>() {
  1048. public ArrayBlockingQueue<A> f(final Boolean fair) {
  1049. return new ArrayBlockingQueue<A>(a.length() + abs(capacity),
  1050. fair, a.toCollection());
  1051. }
  1052. };
  1053. }
  1054. };
  1055. }
  1056. }));
  1057. }
  1058. /**
  1059. * Returns an arbitrary implementation for concurrent hash maps.
  1060. *
  1061. * @param ak An arbitrary implementation for the type over which the concurrent hash map's keys
  1062. * are defined.
  1063. * @param av An arbitrary implementation for the type over which the concurrent hash map's values
  1064. * are defined.
  1065. * @return An arbitrary implementation for concurrent hash maps.
  1066. */
  1067. public static <K, V> Arbitrary<ConcurrentHashMap<K, V>> arbConcurrentHashMap(final Arbitrary<K> ak,
  1068. final Arbitrary<V> av) {
  1069. return arbitrary(arbHashtable(ak, av).gen.map(new F<Hashtable<K, V>, ConcurrentHashMap<K, V>>() {
  1070. @SuppressWarnings({"UseOfObsoleteCollectionType"})
  1071. public ConcurrentHashMap<K, V> f(final Hashtable<K, V> ht) {
  1072. return new ConcurrentHashMap<K, V>(ht);
  1073. }
  1074. }));
  1075. }
  1076. /**
  1077. * Returns an arbitrary implementation for concurrent linked queues.
  1078. *
  1079. * @param aa An arbitrary implementation for the type over which the concurrent linked queue is
  1080. * defined.
  1081. * @return An arbitrary implementation for concurrent linked queues.
  1082. */
  1083. public static <A> Arbitrary<ConcurrentLinkedQueue<A>> arbConcurrentLinkedQueue(final Arbitrary<A> aa) {
  1084. return arbitrary(arbArray(aa).gen.map(new F<Array<A>, ConcurrentLinkedQueue<A>>() {
  1085. public ConcurrentLinkedQueue<A> f(final Array<A> a) {
  1086. return new ConcurrentLinkedQueue<A>(a.toCollection());
  1087. }
  1088. }));
  1089. }
  1090. /**
  1091. * Returns an arbitrary implementation for copy-on-write array lists.
  1092. *
  1093. * @param aa An arbitrary implementation for the type over which the copy-on-write array list is
  1094. * defined.
  1095. * @return An arbitrary implementation for copy-on-write array lists.
  1096. */
  1097. public static <A> Arbitrary<CopyOnWriteArrayList<A>> arbCopyOnWriteArrayList(final Arbitrary<A> aa) {
  1098. return arbitrary(arbArray(aa).gen.map(new F<Array<A>, CopyOnWriteArrayList<A>>() {
  1099. public CopyOnWriteArrayList<A> f(final Array<A> a) {
  1100. return new CopyOnWriteArrayList<A>(a.toCollection());
  1101. }
  1102. }));
  1103. }
  1104. /**
  1105. * Returns an arbitrary implementation for copy-on-write array se