Codebase list test-check-clojure / 72ebee1
Add UUID generator Gary Fredericks 8 years ago
2 changed file(s) with 52 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
1010 (:refer-clojure :exclude [int vector list hash-map map keyword
1111 char boolean byte bytes sequence
1212 shuffle not-empty symbol namespace
13 set sorted-set])
13 set sorted-set uuid])
1414 (:require [#?(:clj clojure.core :cljs cljs.core) :as core]
1515 [clojure.test.check.random :as random]
1616 [clojure.test.check.rose-tree :as rose]
930930 (tuple int
931931 (such-that (complement zero?) int))))
932932
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
933968 (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]))
935970
936971 (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]))
938973
939974 (defn container-type
940975 [inner-type]
694694 (prop/for-all [[coll permutation] original-vector-and-permutation]
695695 (= (sort coll) (sort permutation))))
696696
697 ;; UUIDs
698 ;; ---------------------------------------------------------------------------
699
700 (defspec uuid-generates-uuids
701 (prop/for-all [uuid gen/uuid]
702 (and (instance? #?(:clj java.util.UUID :cljs cljs.core.UUID) uuid)
703 ;; check that we got the special fields right
704 (re-matches #"[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}"
705 (str uuid)))))
706
707 (deftest uuid-generates-distinct-values
708 (is (apply distinct?
709 (gen/sample gen/uuid 1000))))
710
697711 ;; vector can generate large vectors; regression for TCHECK-49
698712 ;; ---------------------------------------------------------------------------
699713