Carp/test/short_circuiting.carp
guberathome 0d15a57d0e
(and) and (or) now handle any number of parameters (#1251)
* feat: generalized (and) and (or) to handle any number of parameters

* feat!: removed (and*) and (or*) macros

* chore: worked around compiler issue for unit test

* fix: unit test in ./test/macro.carp

Co-authored-by: guberatsie <gunnar.bernhardt@siemens.com>
2021-06-20 21:44:04 +02:00

70 lines
1.7 KiB
Plaintext

(load "Test.carp")
(use Test)
(def g 0) ;; Global variable used to detect if too many forms are evaluated.
(defn inc-true []
(do (update! g inc)
true))
(defn inc-false []
(do (update! g inc)
false))
(defn short-circuit-and []
(do (set! g 0)
(ignore (and (inc-false) (inc-true))) ;; Only the first form should be evaluated.
g))
(defn short-circuit-or []
(do (set! g 0)
(ignore (or (inc-true) (inc-false))) ;; Only the first form should be evaluated.
g))
(defn eval-both-expr-in-and []
(do (set! g 0)
(ignore (and (inc-true) (inc-true))) ;; Both forms evaluate.
g))
(defn eval-both-expr-in-or []
(do (set! g 0)
(ignore (or (inc-false) (inc-false))) ;; Both forms evaluate.
g))
(defn vararg-and []
(do (set! g 0)
(ignore (and (inc-true) (inc-true) (inc-false) (inc-true) (inc-true))) ;; 3 expressions will evaluate.
g))
(defn vararg-or []
(do (set! g 0)
(ignore (or (inc-false) (inc-false) (inc-true) (inc-false) (inc-false))) ;; 3 expressions will evaluate.
g))
(deftest test
(assert-equal test
1
(short-circuit-and)
"'and' short circuits")
(assert-equal test
1
(short-circuit-or)
"'or' short circuits")
(assert-equal test
2
(eval-both-expr-in-and)
"'and' can evaluate second expr")
(assert-equal test
2
(eval-both-expr-in-or)
"'or' can evaluate second expr")
(assert-equal test
3
(vararg-and)
"vararg-'and' works")
(assert-equal test
3
(vararg-or)
"vararg-'or' works")
)