Skip to content

Commit

Permalink
Add test for safety checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenvandijk committed Jun 11, 2020
1 parent 25ace7c commit 8312d90
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/sci/safety_test.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(ns sci.safety-test
(:require
[clojure.test :as test :refer [deftest is]]
[sci.core :as sci :refer [eval-string]]
[sci.test-utils :as tu]))

(deftest iterate-max-test
(when-not tu/native?
(let [d (try (tu/eval* "(loop [i 1000] (if (zero? i) :done (recur (dec i))))" {:iterate-max 100})
(catch #?(:clj clojure.lang.ExceptionInfo :cljs ExceptionInfo) e
(ex-data e)))]
(is (= :sci.error/iterated-beyond-max (:type d)))))
(is (thrown-with-msg? #?(:clj clojure.lang.ExceptionInfo :cljs ExceptionInfo)
#"iteration"
(tu/eval* "(reduce (fn [_ _]) (range 1000))" {:iterate-max 100})))
(is (thrown-with-msg? #?(:clj clojure.lang.ExceptionInfo :cljs ExceptionInfo)
#"iteration"
(tu/eval* "((fn pow [x n] (case n 1 x, (* x (pow x (dec n))))) 2 10)" {:iterate-max 10})))
(is (thrown-with-msg? #?(:clj clojure.lang.ExceptionInfo :cljs ExceptionInfo)
#"iteration"
(tu/eval* "(loop [i 1000] (if (zero? i) :done (recur (dec i))))" {:iterate-max 100})))

(is (= :done (tu/eval* "(loop [i 10] (if (zero? i) :done (recur (dec i))))" {:iterate-max 100})))
(is (= :done (tu/eval* "(loop [i 1000] (if (zero? i) :done (recur (dec i))))" {})))

(is (= 1024 (tu/eval* "((fn pow [x n] (case n 1 x, (* x (pow x (dec n))))) 2 10)" {:iterate 100})))
(is (= 1024 (tu/eval* "((fn pow [x n] (case n 1 x, (* x (pow x (dec n))))) 2 10)" {}))))

0 comments on commit 8312d90

Please sign in to comment.