9 | 9 |
(ns clojure.test.check.generators
|
10 | 10 |
(:refer-clojure :exclude [int vector list hash-map map keyword
|
11 | 11 |
char boolean byte bytes sequence
|
12 | |
shuffle not-empty symbol namespace])
|
|
12 |
shuffle not-empty symbol namespace
|
|
13 |
set sorted-set])
|
13 | 14 |
(:require [#?(:clj clojure.core :cljs cljs.core) :as core]
|
14 | 15 |
[clojure.test.check.random :as random]
|
15 | 16 |
[clojure.test.check.rose-tree :as rose]
|
|
504 | 505 |
"Generates byte-arrays."
|
505 | 506 |
(fmap core/byte-array (vector byte))))
|
506 | 507 |
|
507 | |
(defn map
|
508 | |
"Create a generator that generates maps, with keys chosen from
|
509 | |
`key-gen` and values chosen from `val-gen`."
|
510 | |
[key-gen val-gen]
|
511 | |
(let [input (vector (tuple key-gen val-gen))]
|
512 | |
(fmap #(into {} %) input)))
|
513 | |
|
514 | 508 |
(defn hash-map
|
515 | 509 |
"Like clojure.core/hash-map, except the values are generators.
|
516 | 510 |
Returns a generator that makes maps with the supplied keys and
|
|
528 | 522 |
"Value args to hash-map must be generators")
|
529 | 523 |
(fmap #(zipmap ks %)
|
530 | 524 |
(apply tuple vs))))
|
|
525 |
|
|
526 |
;; Collections of distinct elements
|
|
527 |
;; (has to be done in a low-level way (instead of with combinators)
|
|
528 |
;; and is subject to the same kind of failure as such-that)
|
|
529 |
;; ---------------------------------------------------------------------------
|
|
530 |
|
|
531 |
(defn ^:private transient-set-contains?
|
|
532 |
[s k]
|
|
533 |
#? (:clj
|
|
534 |
(.contains ^clojure.lang.ITransientSet s k)
|
|
535 |
:cljs
|
|
536 |
(some? (-lookup s k))))
|
|
537 |
|
|
538 |
(defn ^:private coll-distinct-by*
|
|
539 |
"Returns a rose tree."
|
|
540 |
[empty-coll key-fn shuffle-fn gen rng size num-elements max-tries]
|
|
541 |
{:pre [gen (:gen gen)]}
|
|
542 |
(loop [rose-trees (transient [])
|
|
543 |
s (transient #{})
|
|
544 |
rng rng
|
|
545 |
size size
|
|
546 |
tries 0]
|
|
547 |
(cond (= max-tries tries)
|
|
548 |
(throw (ex-info "Couldn't generate enough distinct elements!"
|
|
549 |
{:gen gen, :max-tries max-tries}))
|
|
550 |
|
|
551 |
(= (count rose-trees) num-elements)
|
|
552 |
(->> (persistent! rose-trees)
|
|
553 |
;; we shuffle the rose trees so that we aren't biased
|
|
554 |
;; toward generating "smaller" elements earlier in the
|
|
555 |
;; collection (only applies to ordered collections)
|
|
556 |
;;
|
|
557 |
;; shuffling the rose trees is more efficient than
|
|
558 |
;; (bind ... shuffle) because we only perform the
|
|
559 |
;; shuffling once and we have no need to shrink the
|
|
560 |
;; shufling.
|
|
561 |
(shuffle-fn rng)
|
|
562 |
(rose/shrink #(into empty-coll %&)))
|
|
563 |
|
|
564 |
:else
|
|
565 |
(let [[rng1 rng2] (random/split rng)
|
|
566 |
rose (call-gen gen rng1 size)
|
|
567 |
root (rose/root rose)
|
|
568 |
k (key-fn root)]
|
|
569 |
(if (transient-set-contains? s k)
|
|
570 |
(recur rose-trees s rng2 (inc size) (inc tries))
|
|
571 |
(recur (conj! rose-trees rose)
|
|
572 |
(conj! s k)
|
|
573 |
rng2
|
|
574 |
size
|
|
575 |
0))))))
|
|
576 |
|
|
577 |
(defn ^:private distinct-by?
|
|
578 |
"Like clojure.core/distinct? but takes a collection instead of varargs,
|
|
579 |
and returns true for empty collections."
|
|
580 |
[f coll]
|
|
581 |
(or (empty? coll)
|
|
582 |
(apply distinct? (core/map f coll))))
|
|
583 |
|
|
584 |
(defn ^:private the-shuffle-fn
|
|
585 |
"Returns a shuffled version of coll according to the rng.
|
|
586 |
|
|
587 |
Note that this is not a generator, it is just a utility function."
|
|
588 |
[rng coll]
|
|
589 |
(let [empty-coll (empty coll)
|
|
590 |
v (vec coll)
|
|
591 |
card (count coll)
|
|
592 |
dec-card (dec card)]
|
|
593 |
(into empty-coll
|
|
594 |
(first
|
|
595 |
(reduce (fn [[v rng] idx]
|
|
596 |
(let [[rng1 rng2] (random/split rng)
|
|
597 |
swap-idx (rand-range rng1 idx dec-card)]
|
|
598 |
[(swap v [idx swap-idx]) rng2]))
|
|
599 |
[v rng]
|
|
600 |
(range card))))))
|
|
601 |
|
|
602 |
(defn ^:private coll-distinct-by
|
|
603 |
[empty-coll key-fn allows-dupes? ordered? gen
|
|
604 |
{:keys [num-elements min-elements max-elements max-tries] :or {max-tries 10}}]
|
|
605 |
(let [shuffle-fn (if ordered?
|
|
606 |
the-shuffle-fn
|
|
607 |
(fn [_rng coll] coll))]
|
|
608 |
(if num-elements
|
|
609 |
(let [size-pred #(= num-elements (count %))]
|
|
610 |
(assert (and (nil? min-elements) (nil? max-elements)))
|
|
611 |
(make-gen
|
|
612 |
(fn [rng gen-size]
|
|
613 |
(rose/filter
|
|
614 |
(if allows-dupes?
|
|
615 |
;; is there a smarter way to do the shrinking than checking
|
|
616 |
;; the distinctness of the entire collection at each
|
|
617 |
;; step?
|
|
618 |
(every-pred size-pred #(distinct-by? key-fn %))
|
|
619 |
size-pred)
|
|
620 |
(coll-distinct-by* empty-coll key-fn shuffle-fn gen
|
|
621 |
rng gen-size num-elements max-tries)))))
|
|
622 |
(let [min-elements (or min-elements 0)
|
|
623 |
size-pred (if max-elements
|
|
624 |
#(<= min-elements (count %) max-elements)
|
|
625 |
#(<= min-elements (count %)))]
|
|
626 |
(gen-bind
|
|
627 |
(if max-elements
|
|
628 |
(choose min-elements max-elements)
|
|
629 |
(sized #(choose min-elements (+ min-elements %))))
|
|
630 |
(fn [num-elements-rose]
|
|
631 |
(let [num-elements (rose/root num-elements-rose)]
|
|
632 |
(make-gen
|
|
633 |
(fn [rng gen-size]
|
|
634 |
(rose/filter
|
|
635 |
(if allows-dupes?
|
|
636 |
;; same comment as above
|
|
637 |
(every-pred size-pred #(distinct-by? key-fn %))
|
|
638 |
size-pred)
|
|
639 |
(coll-distinct-by* empty-coll key-fn shuffle-fn gen
|
|
640 |
rng gen-size num-elements max-tries)))))))))))
|
|
641 |
|
|
642 |
|
|
643 |
;; I tried to reduce the duplication in these docstrings with a macro,
|
|
644 |
;; but couldn't make it work in cljs.
|
|
645 |
|
|
646 |
(defn vector-distinct
|
|
647 |
"Generates a vector of elements from the given generator, with the
|
|
648 |
guarantee that the elements will be distinct.
|
|
649 |
|
|
650 |
If the generator cannot or is unlikely to produce enough distinct
|
|
651 |
elements, this generator will fail in the same way as such-that.
|
|
652 |
|
|
653 |
Available options:
|
|
654 |
|
|
655 |
:num-elements the fixed size of generated vectors
|
|
656 |
:min-elements the min size of generated vectors
|
|
657 |
:max-elements the max size of generated vectors
|
|
658 |
:max-tries the number of times the generator will be tried before
|
|
659 |
failing when it does not produce distinct elements
|
|
660 |
(default 10)"
|
|
661 |
([gen] (vector-distinct gen {}))
|
|
662 |
([gen opts]
|
|
663 |
(assert (generator? gen) "First arg to vector-distinct must be a generator!")
|
|
664 |
(coll-distinct-by [] identity true true gen opts)))
|
|
665 |
|
|
666 |
(defn list-distinct
|
|
667 |
"Generates a list of elements from the given generator, with the
|
|
668 |
guarantee that the elements will be distinct.
|
|
669 |
|
|
670 |
If the generator cannot or is unlikely to produce enough distinct
|
|
671 |
elements, this generator will fail in the same way as such-that.
|
|
672 |
|
|
673 |
Available options:
|
|
674 |
|
|
675 |
:num-elements the fixed size of generated vectors
|
|
676 |
:min-elements the min size of generated vectors
|
|
677 |
:max-elements the max size of generated vectors
|
|
678 |
:max-tries the number of times the generator will be tried before
|
|
679 |
failing when it does not produce distinct elements
|
|
680 |
(default 10)"
|
|
681 |
([gen] (list-distinct gen {}))
|
|
682 |
([gen opts]
|
|
683 |
(assert (generator? gen) "First arg to list-distinct must be a generator!")
|
|
684 |
(coll-distinct-by () identity true true gen opts)))
|
|
685 |
|
|
686 |
(defn set
|
|
687 |
"Generates a set of elements from the given generator.
|
|
688 |
|
|
689 |
If the generator cannot or is unlikely to produce enough distinct
|
|
690 |
elements, this generator will fail in the same way as such-that.
|
|
691 |
|
|
692 |
Available options:
|
|
693 |
|
|
694 |
:num-elements the fixed size of generated vectors
|
|
695 |
:min-elements the min size of generated vectors
|
|
696 |
:max-elements the max size of generated vectors
|
|
697 |
:max-tries the number of times the generator will be tried before
|
|
698 |
failing when it does not produce distinct elements
|
|
699 |
(default 10)"
|
|
700 |
([gen] (set gen {}))
|
|
701 |
([gen opts]
|
|
702 |
(assert (generator? gen) "First arg to set must be a generator!")
|
|
703 |
(coll-distinct-by #{} identity false false gen opts)))
|
|
704 |
|
|
705 |
(defn sorted-set
|
|
706 |
"Generates a sorted set of elements from the given generator.
|
|
707 |
|
|
708 |
If the generator cannot or is unlikely to produce enough distinct
|
|
709 |
elements, this generator will fail in the same way as such-that.
|
|
710 |
|
|
711 |
Available options:
|
|
712 |
|
|
713 |
:num-elements the fixed size of generated vectors
|
|
714 |
:min-elements the min size of generated vectors
|
|
715 |
:max-elements the max size of generated vectors
|
|
716 |
:max-tries the number of times the generator will be tried before
|
|
717 |
failing when it does not produce distinct elements
|
|
718 |
(default 10)"
|
|
719 |
([gen] (sorted-set gen {}))
|
|
720 |
([gen opts]
|
|
721 |
(assert (generator? gen) "First arg to sorted-set must be a generator!")
|
|
722 |
(coll-distinct-by (core/sorted-set) identity false false gen opts)))
|
|
723 |
|
|
724 |
(defn map
|
|
725 |
"Create a generator that generates maps, with keys chosen from
|
|
726 |
`key-gen` and values chosen from `val-gen`.
|
|
727 |
|
|
728 |
If the key generator cannot or is unlikely to produce enough distinct
|
|
729 |
elements, this generator will fail in the same way as such-that.
|
|
730 |
|
|
731 |
Available options:
|
|
732 |
|
|
733 |
:num-elements the fixed size of generated vectors
|
|
734 |
:min-elements the min size of generated vectors
|
|
735 |
:max-elements the max size of generated vectors
|
|
736 |
:max-tries the number of times the generator will be tried before
|
|
737 |
failing when it does not produce distinct elements
|
|
738 |
(default 10)"
|
|
739 |
([key-gen val-gen] (map key-gen val-gen {}))
|
|
740 |
([key-gen val-gen opts]
|
|
741 |
(coll-distinct-by {} first false false (tuple key-gen val-gen) opts)))
|
|
742 |
|
|
743 |
;; Characters & Strings
|
|
744 |
;; ---------------------------------------------------------------------------
|
531 | 745 |
|
532 | 746 |
(def char
|
533 | 747 |
"Generates character from 0-255."
|