10 | 10 |
(:refer-clojure :exclude [int vector list hash-map map keyword
|
11 | 11 |
char boolean byte bytes sequence
|
12 | 12 |
shuffle not-empty symbol namespace
|
13 | |
set sorted-set])
|
|
13 |
set sorted-set uuid])
|
14 | 14 |
(:require [#?(:clj clojure.core :cljs cljs.core) :as core]
|
15 | 15 |
[clojure.test.check.random :as random]
|
16 | 16 |
[clojure.test.check.rose-tree :as rose]
|
|
930 | 930 |
(tuple int
|
931 | 931 |
(such-that (complement zero?) int))))
|
932 | 932 |
|
|
933 |
(def uuid
|
|
934 |
"Generates a random type-4 UUID. Does not shrink."
|
|
935 |
(no-shrink
|
|
936 |
#?(:clj
|
|
937 |
;; this could be done with combinators, but doing it low-level
|
|
938 |
;; seems to be 10x faster
|
|
939 |
(make-gen
|
|
940 |
(fn [rng _size]
|
|
941 |
(let [[r1 r2] (random/split rng)
|
|
942 |
x1 (-> (random/rand-long r1)
|
|
943 |
(bit-and -45057)
|
|
944 |
(bit-or 0x4000))
|
|
945 |
x2 (-> (random/rand-long r2)
|
|
946 |
(bit-or -9223372036854775808)
|
|
947 |
(bit-and -4611686018427387905))]
|
|
948 |
(rose/make-rose
|
|
949 |
(java.util.UUID. x1 x2)
|
|
950 |
[]))))
|
|
951 |
|
|
952 |
:cljs
|
|
953 |
;; this could definitely be optimized so that it doesn't require
|
|
954 |
;; generating 31 numbers
|
|
955 |
(fmap (fn [nibbles]
|
|
956 |
(letfn [(hex [idx] (.toString (nibbles idx) 16))]
|
|
957 |
(let [rhex (-> (nibbles 15) (bit-and 3) (+ 8) (.toString 16))]
|
|
958 |
(core/uuid (str (hex 0) (hex 1) (hex 2) (hex 3)
|
|
959 |
(hex 4) (hex 5) (hex 6) (hex 7) "-"
|
|
960 |
(hex 8) (hex 9) (hex 10) (hex 11) "-"
|
|
961 |
"4" (hex 12) (hex 13) (hex 14) "-"
|
|
962 |
rhex (hex 16) (hex 17) (hex 18) "-"
|
|
963 |
(hex 19) (hex 20) (hex 21) (hex 22)
|
|
964 |
(hex 23) (hex 24) (hex 25) (hex 26)
|
|
965 |
(hex 27) (hex 28) (hex 29) (hex 30))))))
|
|
966 |
(vector (choose 0 15) 31)))))
|
|
967 |
|
933 | 968 |
(def simple-type
|
934 | |
(one-of [int char string ratio boolean keyword keyword-ns symbol symbol-ns]))
|
|
969 |
(one-of [int char string ratio boolean keyword keyword-ns symbol symbol-ns uuid]))
|
935 | 970 |
|
936 | 971 |
(def simple-type-printable
|
937 | |
(one-of [int char-ascii string-ascii ratio boolean keyword keyword-ns symbol symbol-ns]))
|
|
972 |
(one-of [int char-ascii string-ascii ratio boolean keyword keyword-ns symbol symbol-ns uuid]))
|
938 | 973 |
|
939 | 974 |
(defn container-type
|
940 | 975 |
[inner-type]
|