Skip to content

Commit

Permalink
fixed off-by-one errors in contract sampleing rate code
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBaranosky committed Apr 2, 2013
1 parent 9f9a3dd commit 9140410
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject org.clojars.runa/clj-schema "0.9.2"
(defproject org.clojars.runa/clj-schema "0.9.3"
:min-lein-version "2.0.0"
:description "Schemas for Clojure data. With validation and example data for tests"
:url "https://github.com/runa-dev/clj-schema"
Expand Down
6 changes: 3 additions & 3 deletions src/clj_schema/contracts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

(def-map-schema :loose ^:private contract-schema
[[:var] var?
(optional-path [:sampling-rate]) [:or nil fn? [integer? #(>= % 0) #(<= % 100)]]
(optional-path [:sampling-rate]) [:or nil fn? [number? #(>= % 0) #(<= % 100)]]
(optional-path [:input-schema]) Anything
(optional-path [:input-schema-on-failure]) [:or nil fn?]
(optional-path [:output-schema]) Anything
(optional-path [:output-schema-on-failure]) [:or nil fn?]])

(defn- check? [sampling-rate args]
(cond (not sampling-rate) true
(fn? sampling-rate) (>= (apply sampling-rate args) (rand-int 101))
:else (>= sampling-rate (rand-int 101))))
(fn? sampling-rate) (> (apply sampling-rate args) (rand 100))
:else (> sampling-rate (rand 100))))

(defn- schema-checker-fn [{:keys [var
sampling-rate
Expand Down
30 changes: 15 additions & 15 deletions test/clj_schema/contracts_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,12 @@

(deftest test-contract-sampling-rate
(add-contracts! flaky-contracts)
(with-redefs [rand-int (constantly 49)]
(with-redefs [rand (constantly 49.999)]
(is (thrown-with-msg? Exception #"Errors found in inputs"
(f "a" 'b 'c))))

(with-redefs [rand-int (constantly 50)]
(is (thrown-with-msg? Exception #"Errors found in inputs"
(f "a" 'b 'c))))

(with-redefs [rand-int (constantly 51)]
(f "a" 'b 'c)) ;; no exception!
(with-redefs [rand (constantly 50.0)]
(f "a" 'b 'c))

(remove-contracts! flaky-contracts))

Expand All @@ -110,20 +106,24 @@
(def fn-dependent-contracts
[{:var #'doubler
:sampling-rate (fn [n] n)
:input-schema String}])
:output-schema String}])

(deftest test-contract-with-function-sampling-rate
(add-contracts! fn-dependent-contracts)

(with-redefs [rand-int (constantly 49)]
(is (thrown-with-msg? Exception #"Errors found in inputs"
(with-redefs [rand (constantly 49.999)]
(is (thrown-with-msg? Exception #"Errors found in outputs"
(doubler 50))))

(with-redefs [rand-int (constantly 50)]
(is (thrown-with-msg? Exception #"Errors found in inputs"
(doubler 50))))

(with-redefs [rand-int (constantly 51)]
(with-redefs [rand (constantly 50.0)]
(doubler 50)) ;; no exception!

;; Lower-bound edge case
(with-redefs [rand (constantly 0.0)]
(doubler 0))

;; Upper-bound edge case
(with-redefs [rand (constantly 100.0)]
(doubler 100))

(remove-contracts! fn-dependent-contracts))

0 comments on commit 9140410

Please sign in to comment.