mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-19 01:18:40 +03:00
0d15a57d0e
* 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>
70 lines
1.7 KiB
Plaintext
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")
|
|
)
|