Created
October 16, 2017 00:06
-
-
Save acthp/d7ec06acfb341e295705bc2433057852 to your computer and use it in GitHub Desktop.
test.check shrinking of 2d matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns matrix-test.core-test | |
(:require [clojure.test :refer :all] | |
[clojure.test.check :as tc] | |
[clojure.test.check.generators :as gen] | |
[clojure.test.check.properties :as prop] | |
[matrix-test.core :refer :all])) | |
(def element gen/int) | |
(defn matrix-size [[w h]] | |
(apply gen/tuple (repeat h (apply gen/tuple (repeat w element))))) | |
; generate a matrix by binding the size | |
(def matrix | |
(gen/bind (gen/tuple gen/s-pos-int gen/s-pos-int) matrix-size)) | |
; | |
; | |
(defn new-row [m] | |
(apply gen/tuple (repeat (count (first m)) element))) | |
(defn new-col [m] | |
(apply gen/tuple (repeat (count m) element))) | |
(defn add-row [m] | |
(gen/fmap #(conj m %) (new-row m))) | |
(defn add-col [m] | |
(gen/fmap #(mapv conj m %) (new-col m))) | |
(defn matrix-inc [m] | |
(gen/bind (gen/frequency [[10 (gen/return :ret)] [45 (gen/return :row)] [45 (gen/return :col)]]) | |
(fn [op] | |
(case op | |
:ret (gen/return m) | |
:row (gen/bind (add-row m) matrix-inc) | |
:col (gen/bind (add-col m) matrix-inc))))) | |
; generate a matrix by incrementally adding rows and columns | |
(def matrix2 | |
(gen/bind (gen/fmap #(-> [[%]]) element) matrix-inc)) | |
; | |
; | |
; coerce failure on an unusual condition | |
(defn matrix-ok [m] | |
(< (count (filter #(= % 4) (map last m))) 3)) | |
;(gen/sample (matrix-size [2 3])) | |
;(gen/sample matrix) | |
;(gen/sample matrix2) | |
;(tc/quick-check 1000 (prop/for-all [m matrix] (matrix-ok m))) | |
;(tc/quick-check 1000 (prop/for-all [m matrix2] (matrix-ok m))) |
(tc/quick-check 1000 (prop/for-all [m matrix] (not-any? #{97} (apply concat m))))
shrinks to
[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 97 0]
[0 0 0 0 0 0 0]]
and (tc/quick-check 1000 (prop/for-all [m matrix2] (not-any? #{97} (apply concat m))))
shrinks to
[[0 0]
[0 0]
[0 0]
[0 0]
[0 0]
[0 0]
[0 0]
[0 0]
[0 0]
[0 0]
[0 0]
[0 0]
[0 0]
[0 0]
[0 0]
[0 0]
[0 0]
[0 0]
[97 0]
[0 0]
[0 0]
[0 0]
[0 0]
[0 0]]
whereas ideally it would shrink to [[97]]
.
yeah, it's not ideal, just better, at least for the conditions I've tested.
oh okay. I don't know what your goal was then.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
matrix2 will usually, but not always, shrink better than matrix