-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
caumond
committed
May 21, 2024
1 parent
f9dcabd
commit 24c50b5
Showing
12 changed files
with
173 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
(ns automaton-core.utils.call-limit "Limit the number of calls of a function") | ||
|
||
(def ^:private n "Number of call per key" (atom {})) | ||
|
||
(defn allow-one-only-call | ||
"For key `k`, allow one call only of function `f`. | ||
Following calls will throw an exception." | ||
[f k] | ||
(if (pos? (get @n k 0)) | ||
(throw (ex-info (str "More than one call to " k " detected") {})) | ||
(do (swap! n update k (fnil inc 0)) (f)))) | ||
|
||
(defn remove-fn-call | ||
"Removes a call so next `allow-one-only-call` will be successful. | ||
Use it when the previous call scope of `allow-one-only-call` is lost. Especially useful for tests." | ||
[k] | ||
(when (pos? (get @n k 0)) (swap! n update k dec))) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
(ns automaton-core.utils.numbers | ||
"Helpers for manipulating numbers in the same way between clj and cljs") | ||
|
||
(defn check-val-in-range | ||
"Returns `nil` if `val` is in the range `[min;max[`. | ||
Otherwise, returns `val` itself." | ||
[min max val] | ||
(cond | ||
(> min val) val | ||
(<= max val) val | ||
:else nil)) | ||
|
||
|
||
(defn check-vals-in-range | ||
"Check if `vals` are between the range of integers (i.e. `[min;max[`. | ||
Returns the value if it exceeds the range." | ||
[min max vals] | ||
(filter (partial check-val-in-range min max) vals)) |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
(ns automaton-core.utils.numbers-test | ||
(:require | ||
[automaton-core.utils.numbers :as sut] | ||
#?@(:cljs [[cljs.test :refer-macros [testing is deftest]]] | ||
:clj [[clojure.test :refer [testing is deftest]]]))) | ||
|
||
(deftest check-val-in-range-test | ||
(testing "In the range integer are accepted" | ||
(is (nil? (sut/check-val-in-range 1 100 10))) | ||
(is (nil? (sut/check-val-in-range 10 100 10)))) | ||
(testing "In the range floats are accepted" | ||
(is (nil? (sut/check-val-in-range 1.0 100.0 10.0))) | ||
(is (nil? (sut/check-val-in-range 10.0 100.0 10.0)))) | ||
(testing "Out of range integer are accepted" | ||
(is (= -1 (sut/check-val-in-range 0 100 -1))) | ||
(is (= 100 (sut/check-val-in-range 1 100 100))) | ||
(is (= 0 (sut/check-val-in-range 1 100 0)))) | ||
(testing "Out of range floats are accepted" | ||
(is (= 100.0 (sut/check-val-in-range 1.0 100.0 100.0))) | ||
(is (= 0.0 (sut/check-val-in-range 1.0 100.0 0.0))) | ||
(is (= 110.0 (sut/check-val-in-range 10.0 100.0 110.0))))) | ||
|
||
|
||
(deftest check-vals-in-range-test | ||
(testing "In the range integer are accepted" | ||
(is (empty? (sut/check-vals-in-range 1 100 [10 1 99]))))) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
;; Last generated version, note a failed push consume a number | ||
{:version "1.2.1"} | ||
{:version "1.2.2"} |