feat: complete atom implementation, new macros, predicates, and test coverage
## Runtime (src/jolt/core.janet, +637/-99) ### Atom system (complete) - core-atom: :validator and :meta options, watches table, atom-validate/atom-notify-watches helpers - New functions: swap-vals!, reset-vals!, compare-and-set!, set-validator!, get-validator, add-watch, remove-watch - Validator: blocks invalid reset!/swap!; watches: notification with old/new values; metadata support - All 7 functions registered in core-bindings ### New predicates - integer?, boolean?, list? — all registered in core-bindings ### New math - abs, rand, rand-int — registered in core-bindings ### Control flow macros (7 new) - cond, case (with one-of-many list support), condp, dotimes, while, if-not, when-first - Registered as macros: cond, case, for, dotimes, while, condp, if-not, when-first ### Threading macros (7) - -> (thread-first), ->> (thread-last), some->, some->>, cond->, cond->>, as-> - Renamed to core-thread-first/core-thread-last to avoid collision with comparison operators ### for comprehension - Full Clojure for macro: :when, :let modifiers, nested bindings - Uses map/mapcat expansion, correct last-group optimization ### Sequence operations (15 new) - last, butlast, second, ffirst, fnext, nfirst, nnext, nthnext, nthrest - drop-last, take-nth, map-indexed, keep, keep-indexed, reductions ### Nil-safety fixes - core-reverse, core-sort, core-sort-by, core-distinct now handle nil input ## Tests (7 new files, 361 assertions across all tests) ### Ported from Clojure test suite (4 files) - clojure-logic-test.janet: 82 assertions (if/and/or/not/some?/nil-punning) - clojure-control-test.janet: 56 assertions (do/when/if-let/cond/dotimes/while/loop/case) - clojure-macros-test.janet: 21 assertions (->/->>/some->/some->>/cond->/cond->>/as->) - clojure-for-test.janet: 4 assertions (:when/:let/nested for) ### Systematic coverage - clojure-atom-test.janet: 31 assertions (creation/reset!/swap!/swap-vals!/reset-vals!/CAS/validator/watches/meta) - systematic-coverage.janet: 120+ assertions (predicates/math/comparison/logic/collections/seq-ops/destructuring) - logic-test.janet: earlier ported logic tests ### Existing tests updated - lazy-test.janet, phase7-test.janet: updated for nil-safe core functions ## Result 47 test files, all passing. 19 new functions/macros implemented. Clojure atom semantics complete: validators, watches, metadata, CAS, swap-vals!/reset-vals!.
This commit is contained in:
parent
82bce033a5
commit
e2e7fa1447
12 changed files with 1462 additions and 98 deletions
154
test/clojure-atom-test.janet
Normal file
154
test/clojure-atom-test.janet
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
# Ported from clojure/test_clojure/atoms.clj + systematic atom tests
|
||||
(use ../src/jolt/api)
|
||||
(defn ct-eval [ctx s] (eval-string ctx s))
|
||||
|
||||
(print "Ported Atom Tests")
|
||||
|
||||
# --- atom creation ---
|
||||
(print "test atom creation...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(atom? (atom 0))")) "atom?")
|
||||
(assert (= false (ct-eval ctx "(atom? nil)")) "atom? nil")
|
||||
(assert (= false (ct-eval ctx "(atom? 42)")) "atom? number")
|
||||
(assert (= false (ct-eval ctx "(atom? \"x\")")) "atom? string")
|
||||
(assert (= 42 (ct-eval ctx "(deref (atom 42))")) "deref atom")
|
||||
(assert (= 99 (ct-eval ctx "(let [a (atom 99)] @a)")) "@ deref macro"))
|
||||
(print " ok")
|
||||
|
||||
# --- deref on non-atoms ---
|
||||
(print "test deref...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 1 (ct-eval ctx "(deref 1)")) "deref non-atom passes through")
|
||||
(assert (= "x" (ct-eval ctx "(deref \"x\")")) "deref string passes through")
|
||||
(assert (= nil (ct-eval ctx "(deref nil)")) "deref nil"))
|
||||
(print " ok")
|
||||
|
||||
# --- reset! ---
|
||||
(print "test reset!...")
|
||||
(let [ctx (init)]
|
||||
(assert (= :b (ct-eval ctx "(let [a (atom :a)] (reset! a :b))")) "reset! returns new")
|
||||
(assert (= 42 (ct-eval ctx "(let [a (atom 0)] (reset! a 42) @a)")) "reset! updates value")
|
||||
(assert (= true (ct-eval ctx "(= [1 1] (let [a (atom 0)] [(reset! a 1) @a]))")) "reset! returns new, value changed"))
|
||||
(print " ok")
|
||||
|
||||
# --- swap! ---
|
||||
(print "test swap!...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 1 (ct-eval ctx "(let [a (atom 0)] (swap! a inc))")) "swap! inc returns new")
|
||||
(assert (= 2 (ct-eval ctx "(let [a (atom 0)] (swap! a + 2))")) "swap! + 2")
|
||||
(assert (= 3 (ct-eval ctx "(let [a (atom 0)] (swap! a + 1 2) @a)")) "swap! + 1 2")
|
||||
(assert (= 6 (ct-eval ctx "(let [a (atom 0)] (swap! a + 1 2 3) @a)")) "swap! + 1 2 3")
|
||||
(assert (= 10 (ct-eval ctx "(let [a (atom 0)] (swap! a + 1 2 3 4) @a)")) "swap! + 1 2 3 4"))
|
||||
(print " ok")
|
||||
|
||||
# --- swap-vals! (returns [old new]) ---
|
||||
(print "test swap-vals!...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [0 1] (let [a (atom 0)] (swap-vals! a inc)))")) "swap-vals! inc")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [1 2] (let [a (atom 1)] (swap-vals! a inc)))")) "swap-vals! inc from 1")
|
||||
(assert (= 2 (ct-eval ctx
|
||||
"(let [a (atom 1)] (swap-vals! a inc) @a)")) "swap-vals! updates value"))
|
||||
(print " ok")
|
||||
|
||||
# --- swap-vals! with extra args ---
|
||||
(print "test swap-vals! arities...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [0 1] (let [a (atom 0)] (swap-vals! a + 1)))")) "swap-vals! + 1")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [1 3] (let [a (atom 0)] (swap-vals! a + 1) (swap-vals! a + 1 1)))")) "swap-vals! + 1 1")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [3 6] (let [a (atom 0)] (swap-vals! a + 1) (swap-vals! a + 1 1) (swap-vals! a + 1 1 1)))")) "swap-vals! + 1 1 1")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [6 10] (let [a (atom 0)] (swap-vals! a + 1) (swap-vals! a + 1 1) (swap-vals! a + 1 1 1) (swap-vals! a + 1 1 1 1)))")) "swap-vals! + 1 1 1 1"))
|
||||
(print " ok")
|
||||
|
||||
# --- reset-vals! (returns [old new]) ---
|
||||
(print "test reset-vals!...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [0 :b] (let [a (atom 0)] (reset-vals! a :b)))")) "reset-vals! returns old new")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [:b 42] (let [a (atom 0)] (reset-vals! a :b) (reset-vals! a 42)))")) "reset-vals! chain")
|
||||
(assert (= 42 (ct-eval ctx
|
||||
"(let [a (atom 0)] (reset-vals! a :b) (reset-vals! a 42) @a)")) "reset-vals! updates value"))
|
||||
(print " ok")
|
||||
|
||||
# --- compare-and-set! ---
|
||||
(print "test compare-and-set!...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx
|
||||
"(let [a (atom 0)] (compare-and-set! a 0 1))")) "CAS true match")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [true 1] (let [a (atom 0)] [(compare-and-set! a 0 1) @a]))")) "CAS true + value changed")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [false 0] (let [a (atom 0)] [(compare-and-set! a 1 2) @a]))")) "CAS false no match")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [false 1] (let [a (atom 0)] (compare-and-set! a 0 1) [(compare-and-set! a 0 2) @a]))")) "CAS false after change"))
|
||||
(print " ok")
|
||||
|
||||
# --- validator ---
|
||||
(print "test validator...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 42 (ct-eval ctx
|
||||
"(let [a (atom 0 :validator pos?)] (reset! a 42) @a)")) "validator passes")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= false (try (let [a (atom 0 :validator pos?)] (reset! a -1) true) (catch Exception e false)))")) "validator blocks invalid reset!")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= false (try (let [a (atom 0 :validator pos?)] (swap! a (fn [x] -1)) true) (catch Exception e false)))")) "validator blocks invalid swap!")
|
||||
(assert (= nil (ct-eval ctx "(set-validator! (atom 0) pos?)")) "set-validator! returns nil")
|
||||
(assert (= nil (ct-eval ctx "(get-validator (atom 0))")) "get-validator nil default")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= even? (do (def a (atom 0)) (set-validator! a even?) (get-validator a)))")) "get-validator returns set fn"))
|
||||
(print " ok")
|
||||
|
||||
# --- watches ---
|
||||
(print "test watches...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx
|
||||
"(empty? (:watches (atom 0)))")) "atom starts with empty watches")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [0 42] (let [a (atom 0)
|
||||
w (atom nil)]
|
||||
(add-watch a :my-key (fn [k ref old new] (reset! w [old new])))
|
||||
(swap! a + 42)
|
||||
@w))")) "add-watch triggers on swap!")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= :unchanged (let [a (atom 0)
|
||||
w (atom :unchanged)]
|
||||
(add-watch a :x (fn [_ _ _ _] (reset! w :fired)))
|
||||
(remove-watch a :x)
|
||||
(swap! a inc)
|
||||
@w))")) "remove-watch stops notification")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= 2 (let [a (atom 0)]
|
||||
(add-watch a :foo (fn [_ _ _ _] nil))
|
||||
(add-watch a :bar (fn [_ _ _ _] nil))
|
||||
(count (:watches a))))")) "multiple watches")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= 0 (let [a (atom 0)]
|
||||
(add-watch a :foo (fn [_ _ _ _] nil))
|
||||
(remove-watch a :foo)
|
||||
(count (:watches a))))")) "remove-watch clears count")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= 2 (let [a (atom 0)
|
||||
fired (atom [])]
|
||||
(add-watch a :w1 (fn [_ _ o n] (swap! fired conj [:w1 o n])))
|
||||
(add-watch a :w2 (fn [_ _ o n] (swap! fired conj [:w2 o n])))
|
||||
(reset! a 99)
|
||||
(count @fired)))")) "multiple watches both fire"))
|
||||
(print " ok")
|
||||
|
||||
# --- metadata on atoms ---
|
||||
(print "test atom metadata...")
|
||||
(let [ctx (init)]
|
||||
(assert (= nil (ct-eval ctx "(meta (atom 0))")) "atom meta nil by default")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= {:foo \"bar\"} (meta (atom 0 :meta {:foo \"bar\"})))")) "atom with :meta")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= {:validated true} (meta (atom 0 :validator pos? :meta {:validated true})))")) "atom with validator and meta"))
|
||||
(print " ok")
|
||||
|
||||
(print "\nAll Ported Atom tests passed!")
|
||||
151
test/clojure-control-test.janet
Normal file
151
test/clojure-control-test.janet
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
# Ported from clojure/test_clojure/control.clj
|
||||
(use ../src/jolt/api)
|
||||
(defn ct-eval [ctx s] (eval-string ctx s))
|
||||
|
||||
(print "Ported Control Tests")
|
||||
|
||||
# --- test-do ---
|
||||
(print "test-do...")
|
||||
(let [ctx (init)]
|
||||
(assert (= nil (ct-eval ctx "(do)")) "do empty -> nil")
|
||||
(assert (= 1 (ct-eval ctx "(do 1)")) "do returns last")
|
||||
(assert (= 2 (ct-eval ctx "(do 1 2)")) "do returns last")
|
||||
(assert (= 5 (ct-eval ctx "(do 1 2 3 4 5)")) "do returns last"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-loop ---
|
||||
(print "test-loop...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 1 (ct-eval ctx "(loop [] 1)")) "loop body")
|
||||
(assert (= 3 (ct-eval ctx "(loop [a 1] (if (< a 3) (recur (inc a)) a))")) "loop recur")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [6 4 2] (loop [a () b [1 2 3]]
|
||||
(if (seq b)
|
||||
(recur (conj a (* 2 (first b))) (next b))
|
||||
a)))")) "loop accum list")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [2 4 6] (loop [a [] b [1 2 3]]
|
||||
(if (seq b)
|
||||
(recur (conj a (* 2 (first b))) (next b))
|
||||
a)))")) "loop accum vector"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-when ---
|
||||
(print "test-when...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 1 (ct-eval ctx "(when true 1)")) "when true")
|
||||
(assert (= nil (ct-eval ctx "(when true)")) "when true no body")
|
||||
(assert (= nil (ct-eval ctx "(when false)")) "when false")
|
||||
(assert (= nil (ct-eval ctx "(when false 1)")) "when false with body"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-when-not ---
|
||||
(print "test-when-not...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 1 (ct-eval ctx "(when-not false 1)")) "when-not false")
|
||||
(assert (= nil (ct-eval ctx "(when-not true)")) "when-not true no body")
|
||||
(assert (= nil (ct-eval ctx "(when-not false)")) "when-not false no body")
|
||||
(assert (= nil (ct-eval ctx "(when-not true 1)")) "when-not true with body"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-if-not ---
|
||||
(print "test-if-not...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 1 (ct-eval ctx "(if-not false 1)")) "if-not false")
|
||||
(assert (= 1 (ct-eval ctx "(if-not false 1 2)")) "if-not false with else")
|
||||
(assert (= nil (ct-eval ctx "(if-not true 1)")) "if-not true")
|
||||
(assert (= 2 (ct-eval ctx "(if-not true 1 2)")) "if-not true with else"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-when-let ---
|
||||
(print "test-when-let...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 1 (ct-eval ctx "(when-let [a 1] a)")) "when-let simple")
|
||||
(assert (= 2 (ct-eval ctx "(when-let [[a b] '(1 2)] b)")) "when-let destructure")
|
||||
(assert (= nil (ct-eval ctx "(when-let [a false] 1)")) "when-let false"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-if-let ---
|
||||
(print "test-if-let...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 1 (ct-eval ctx "(if-let [a 1] a)")) "if-let simple")
|
||||
(assert (= 2 (ct-eval ctx "(if-let [[a b] '(1 2)] b)")) "if-let destructure")
|
||||
(assert (= nil (ct-eval ctx "(if-let [a false] 1)")) "if-let false")
|
||||
(assert (= 1 (ct-eval ctx "(if-let [a false] a 1)")) "if-let false with else"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-if-some ---
|
||||
(print "test-if-some...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 1 (ct-eval ctx "(if-some [a 1] a)")) "if-some simple")
|
||||
(assert (= false (ct-eval ctx "(if-some [a false] a)")) "if-some false is some")
|
||||
(assert (= nil (ct-eval ctx "(if-some [a nil] 1)")) "if-some nil")
|
||||
(assert (= 3 (ct-eval ctx "(if-some [[a b] [1 2]] (+ a b))")) "if-some destructure"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-when-some ---
|
||||
(print "test-when-some...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 1 (ct-eval ctx "(when-some [a 1] a)")) "when-some simple")
|
||||
(assert (= 2 (ct-eval ctx "(when-some [[a b] [1 2]] b)")) "when-some destructure")
|
||||
(assert (= false (ct-eval ctx "(when-some [a false] a)")) "when-some false is some")
|
||||
(assert (= nil (ct-eval ctx "(when-some [a nil] 1)")) "when-some nil"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-cond ---
|
||||
(print "test-cond...")
|
||||
(let [ctx (init)]
|
||||
(assert (= nil (ct-eval ctx "(cond)")) "cond empty -> nil")
|
||||
(assert (= nil (ct-eval ctx "(cond nil true)")) "cond nil -> nil")
|
||||
(assert (= nil (ct-eval ctx "(cond false true)")) "cond false -> nil")
|
||||
(assert (= 1 (ct-eval ctx "(cond true 1)")) "cond true -> 1")
|
||||
(assert (= 3 (ct-eval ctx "(cond nil 1 false 2 true 3 true 4)")) "cond third branch")
|
||||
(assert (= :b (ct-eval ctx "(cond false :a true :b)")) "cond skips false")
|
||||
(assert (= :a (ct-eval ctx "(cond true :a true :b)")) "cond takes first true"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-condp ---
|
||||
(print "test-condp...")
|
||||
(let [ctx (init)]
|
||||
(assert (= :pass (ct-eval ctx "(condp = 1 1 :pass 2 :fail)")) "condp match first")
|
||||
(assert (= :pass (ct-eval ctx "(condp = 1 2 :fail 1 :pass)")) "condp match second")
|
||||
(assert (= :pass (ct-eval ctx "(condp = 1 2 :fail :pass)")) "condp default"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-dotimes ---
|
||||
(print "test-dotimes...")
|
||||
(let [ctx (init)]
|
||||
(assert (= nil (ct-eval ctx "(dotimes [n 1] n)")) "dotimes returns nil")
|
||||
(assert (= 3 (ct-eval ctx
|
||||
"(let [a (atom 0)]
|
||||
(dotimes [n 3] (swap! a inc))
|
||||
@a)")) "dotimes 3 iterations")
|
||||
(assert (= [0 1 2] (ct-eval ctx
|
||||
"(let [a (atom [])]
|
||||
(dotimes [n 3] (swap! a conj n))
|
||||
@a)")) "dotimes with index"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-while ---
|
||||
(print "test-while...")
|
||||
(let [ctx (init)]
|
||||
(assert (= nil (ct-eval ctx "(while nil 1)")) "while nil returns nil")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= [0 nil]
|
||||
(let [a (atom 3)
|
||||
w (while (pos? @a) (swap! a dec))]
|
||||
[@a w]))")) "while dec to 0"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-case ---
|
||||
(print "test-case...")
|
||||
(let [ctx (init)]
|
||||
(assert (= :number (ct-eval ctx "(case 1 1 :number :default)")) "case match 1")
|
||||
(assert (= :string (ct-eval ctx "(case \"foo\" \"foo\" :string :default)")) "case match string")
|
||||
(assert (= :kw (ct-eval ctx "(case :zap :zap :kw :default)")) "case match keyword")
|
||||
(assert (= :symbol (ct-eval ctx "(case 'pow pow :symbol :default)")) "case match symbol")
|
||||
(assert (= :default (ct-eval ctx "(case 99 1 :number :default)")) "case default")
|
||||
(assert (= :matched (ct-eval ctx "(case 2 (2 3 4) :matched :default)")) "case one-of-many"))
|
||||
(print " ok")
|
||||
|
||||
(print "\nAll Ported Control tests passed!")
|
||||
35
test/clojure-for-test.janet
Normal file
35
test/clojure-for-test.janet
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
# Ported from clojure/test_clojure/for.clj
|
||||
(use ../src/jolt/api)
|
||||
(defn ct-eval [ctx s] (eval-string ctx s))
|
||||
|
||||
(print "Ported For Tests")
|
||||
|
||||
# --- When ---
|
||||
(print "test-for :when...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= (for [x (range 10) :when (odd? x)] x)
|
||||
(quote (1 3 5 7 9)))")) "for :when odd?")
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= (for [x (range 4) y (range 4) :when (odd? y)] [x y])
|
||||
(quote ([0 1] [0 3] [1 1] [1 3] [2 1] [2 3] [3 1] [3 3])))")) "for nested :when"))
|
||||
(print " ok")
|
||||
|
||||
# --- Let ---
|
||||
(print "test-for :let...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= (for [x (range 3) y (range 3) :let [z (+ x y)] :when (odd? z)] [x y z])
|
||||
(quote ([0 1 1] [1 0 1] [1 2 3] [2 1 3])))")) "for :let :when"))
|
||||
(print " ok")
|
||||
|
||||
# --- Nesting ---
|
||||
(print "test-for nesting...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx
|
||||
"(= (for [x (quote (a b)) y (interpose x (quote (1 2))) z (list x y)] [x y z])
|
||||
(quote ([a 1 a] [a 1 1] [a a a] [a a a] [a 2 a] [a 2 2]
|
||||
[b 1 b] [b 1 1] [b b b] [b b b] [b 2 b] [b 2 2])))")) "for nested interpose"))
|
||||
(print " ok")
|
||||
|
||||
(print "\nAll Ported For tests passed!")
|
||||
127
test/clojure-logic-test.janet
Normal file
127
test/clojure-logic-test.janet
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
# Ported from clojure/test_clojure/logic.clj
|
||||
(use ../src/jolt/api)
|
||||
(defn ct-eval [ctx s] (eval-string ctx s))
|
||||
|
||||
(print "Ported Logic Tests (from clojure/test-clojure/logic.clj)")
|
||||
|
||||
# --- test-if ---
|
||||
(print "test-if: true/false/nil...")
|
||||
(let [ctx (init)]
|
||||
(assert (= :t (ct-eval ctx "(if true :t)")) "if true")
|
||||
(assert (= :t (ct-eval ctx "(if true :t :f)")) "if true with else")
|
||||
(assert (= nil (ct-eval ctx "(if false :t)")) "if false no else")
|
||||
(assert (= :f (ct-eval ctx "(if false :t :f)")) "if false with else")
|
||||
(assert (= nil (ct-eval ctx "(if nil :t)")) "if nil no else")
|
||||
(assert (= :f (ct-eval ctx "(if nil :t :f)")) "if nil with else"))
|
||||
(print " ok")
|
||||
|
||||
(print "test-if: zero/empty is true...")
|
||||
(let [ctx (init)]
|
||||
(assert (= :t (ct-eval ctx "(if 0 :t :f)")) "0 is true")
|
||||
(assert (= :t (ct-eval ctx "(if 0.0 :t :f)")) "0.0 is true")
|
||||
(assert (= :t (ct-eval ctx "(if \"\" :t :f)")) "empty string is true")
|
||||
(assert (= :t (ct-eval ctx "(if () :t :f)")) "empty list is true")
|
||||
(assert (= :t (ct-eval ctx "(if [] :t :f)")) "empty vector is true")
|
||||
(assert (= :t (ct-eval ctx "(if {} :t :f)")) "empty map is true")
|
||||
(assert (= :t (ct-eval ctx "(if #{} :t :f)")) "empty set is true"))
|
||||
(print " ok")
|
||||
|
||||
(print "test-if: anything except nil/false is true...")
|
||||
(let [ctx (init)]
|
||||
(assert (= :t (ct-eval ctx "(if 42 :t :f)")) "42 is true")
|
||||
(assert (= :t (ct-eval ctx "(if 1.2 :t :f)")) "1.2 is true")
|
||||
(assert (= :t (ct-eval ctx "(if \"abc\" :t :f)")) "string is true")
|
||||
(assert (= :t (ct-eval ctx "(if 'abc :t :f)")) "symbol is true")
|
||||
(assert (= :t (ct-eval ctx "(if :kw :t :f)")) "keyword is true")
|
||||
(assert (= :t (ct-eval ctx "(if '(1 2) :t :f)")) "list is true")
|
||||
(assert (= :t (ct-eval ctx "(if [1 2] :t :f)")) "vector is true")
|
||||
(assert (= :t (ct-eval ctx "(if {:a 1 :b 2} :t :f)")) "map is true")
|
||||
(assert (= :t (ct-eval ctx "(if #{1 2} :t :f)")) "set is true"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-nil-punning ---
|
||||
(print "test-nil-punning...")
|
||||
(let [ctx (init)]
|
||||
(assert (= :yes (ct-eval ctx "(if (first []) :no :yes)")) "first [] nil")
|
||||
(assert (= :yes (ct-eval ctx "(if (next [1]) :no :yes)")) "next [1] nil")
|
||||
(assert (= :no (ct-eval ctx "(if (rest [1]) :no :yes)")) "rest [1] non-nil")
|
||||
(assert (= :yes (ct-eval ctx "(if (seq nil) :no :yes)")) "seq nil")
|
||||
(assert (= :yes (ct-eval ctx "(if (seq []) :no :yes)")) "seq [] nil")
|
||||
(assert (= :no (ct-eval ctx "(if (lazy-seq nil) :no :yes)")) "lazy-seq nil non-nil")
|
||||
(assert (= :no (ct-eval ctx "(if (lazy-seq []) :no :yes)")) "lazy-seq [] non-nil")
|
||||
(assert (= :no (ct-eval ctx "(if (filter (fn [x] (> x 10)) [1 2 3]) :no :yes)")) "filter non-match non-nil")
|
||||
(assert (= :no (ct-eval ctx "(if (map identity []) :no :yes)")) "map empty non-nil")
|
||||
(assert (= :no (ct-eval ctx "(if (apply concat []) :no :yes)")) "apply concat [] non-nil")
|
||||
(assert (= :no (ct-eval ctx "(if (concat) :no :yes)")) "concat empty non-nil")
|
||||
(assert (= :no (ct-eval ctx "(if (concat []) :no :yes)")) "concat [] non-nil")
|
||||
(assert (= :no (ct-eval ctx "(if (reverse nil) :no :yes)")) "reverse nil non-nil")
|
||||
(assert (= :no (ct-eval ctx "(if (reverse []) :no :yes)")) "reverse [] non-nil")
|
||||
(assert (= :no (ct-eval ctx "(if (sort nil) :no :yes)")) "sort nil non-nil")
|
||||
(assert (= :no (ct-eval ctx "(if (sort []) :no :yes)")) "sort [] non-nil"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-and ---
|
||||
(print "test-and...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(and)")) "and empty")
|
||||
(assert (= true (ct-eval ctx "(and true)")) "and true")
|
||||
(assert (= nil (ct-eval ctx "(and nil)")) "and nil")
|
||||
(assert (= false (ct-eval ctx "(and false)")) "and false")
|
||||
(assert (= nil (ct-eval ctx "(and true nil)")) "and true nil")
|
||||
(assert (= false (ct-eval ctx "(and true false)")) "and true false")
|
||||
(assert (= "abc" (ct-eval ctx "(and 1 true :kw 'abc \"abc\")")) "and chain last")
|
||||
(assert (= nil (ct-eval ctx "(and 1 true :kw nil 'abc \"abc\")")) "and chain nil")
|
||||
(assert (= false (ct-eval ctx "(and 1 true :kw 'abc \"abc\" false)")) "and chain false"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-or ---
|
||||
(print "test-or...")
|
||||
(let [ctx (init)]
|
||||
(assert (= nil (ct-eval ctx "(or)")) "or empty")
|
||||
(assert (= true (ct-eval ctx "(or true)")) "or true")
|
||||
(assert (= nil (ct-eval ctx "(or nil)")) "or nil")
|
||||
(assert (= false (ct-eval ctx "(or false)")) "or false")
|
||||
(assert (= true (ct-eval ctx "(or nil false true)")) "or nil false true")
|
||||
(assert (= 1 (ct-eval ctx "(or nil false 1 2)")) "or picks first truthy")
|
||||
(assert (= "abc" (ct-eval ctx "(or nil false \"abc\" :kw)")) "or picks string")
|
||||
(assert (= nil (ct-eval ctx "(or false nil)")) "or false nil -> nil")
|
||||
(assert (= false (ct-eval ctx "(or nil false)")) "or nil false -> false")
|
||||
(assert (= false (ct-eval ctx "(or nil nil nil false)")) "or chain to false")
|
||||
(assert (= true (ct-eval ctx "(or nil true false)")) "or nil true false"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-not ---
|
||||
(print "test-not...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(not nil)")) "not nil")
|
||||
(assert (= true (ct-eval ctx "(not false)")) "not false")
|
||||
(assert (= false (ct-eval ctx "(not true)")) "not true")
|
||||
(assert (= false (ct-eval ctx "(not 0)")) "not 0")
|
||||
(assert (= false (ct-eval ctx "(not 0.0)")) "not 0.0")
|
||||
(assert (= false (ct-eval ctx "(not 42)")) "not 42")
|
||||
(assert (= false (ct-eval ctx "(not 1.2)")) "not 1.2")
|
||||
(assert (= false (ct-eval ctx "(not \"\")")) "not empty string")
|
||||
(assert (= false (ct-eval ctx "(not \"abc\")")) "not string")
|
||||
(assert (= false (ct-eval ctx "(not 'abc)")) "not symbol")
|
||||
(assert (= false (ct-eval ctx "(not :kw)")) "not keyword")
|
||||
(assert (= false (ct-eval ctx "(not ())")) "not empty list")
|
||||
(assert (= false (ct-eval ctx "(not '(1 2))")) "not list")
|
||||
(assert (= false (ct-eval ctx "(not []))")) "not empty vector")
|
||||
(assert (= false (ct-eval ctx "(not [1 2])")) "not vector")
|
||||
(assert (= false (ct-eval ctx "(not {})")) "not empty map")
|
||||
(assert (= false (ct-eval ctx "(not {:a 1 :b 2})")) "not map")
|
||||
(assert (= false (ct-eval ctx "(not #{})")) "not empty set")
|
||||
(assert (= false (ct-eval ctx "(not #{1 2})")) "not set"))
|
||||
(print " ok")
|
||||
|
||||
# --- test-some? ---
|
||||
(print "test-some?...")
|
||||
(let [ctx (init)]
|
||||
(assert (= false (ct-eval ctx "(some? nil)")) "some? nil")
|
||||
(assert (= true (ct-eval ctx "(some? false)")) "some? false")
|
||||
(assert (= true (ct-eval ctx "(some? 0)")) "some? 0")
|
||||
(assert (= true (ct-eval ctx "(some? \"abc\")")) "some? string")
|
||||
(assert (= true (ct-eval ctx "(some? [])")) "some? empty vec"))
|
||||
(print " ok")
|
||||
|
||||
(print "\nAll Ported Logic tests passed!")
|
||||
63
test/clojure-macros-test.janet
Normal file
63
test/clojure-macros-test.janet
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
# Ported from clojure/test_clojure/macros.clj
|
||||
(use ../src/jolt/api)
|
||||
(defn ct-eval [ctx s] (eval-string ctx s))
|
||||
|
||||
(print "Ported Macros Tests")
|
||||
|
||||
# --- -> and ->> threading ---
|
||||
(print "test -> and ->>...")
|
||||
(let [ctx (init)]
|
||||
(ct-eval ctx "(defmacro c [arg] (if (= 'b (first arg)) :foo :bar))")
|
||||
(ct-eval ctx "(def a 2)")
|
||||
(ct-eval ctx "(def b identity)")
|
||||
(assert (= :foo (ct-eval ctx "(-> a b c)")) "-> threading")
|
||||
(assert (= :foo (ct-eval ctx "(->> a b c)")) "->> threading"))
|
||||
(print " ok")
|
||||
|
||||
# --- some-> ---
|
||||
(print "test some->...")
|
||||
(let [ctx (init)]
|
||||
(assert (= nil (ct-eval ctx "(some-> nil)")) "some-> nil")
|
||||
(assert (= 0 (ct-eval ctx "(some-> 0)")) "some-> 0")
|
||||
(assert (= -1 (ct-eval ctx "(some-> 1 (- 2))")) "some-> with form")
|
||||
(ct-eval ctx "(defn const-nil [_] nil)")
|
||||
(assert (= nil (ct-eval ctx "(some-> 1 const-nil (- 2))")) "some-> stop at nil"))
|
||||
(print " ok")
|
||||
|
||||
# --- some->> ---
|
||||
(print "test some->>...")
|
||||
(let [ctx (init)]
|
||||
(assert (= nil (ct-eval ctx "(some->> nil)")) "some->> nil")
|
||||
(assert (= 0 (ct-eval ctx "(some->> 0)")) "some->> 0")
|
||||
(assert (= 1 (ct-eval ctx "(some->> 1 (- 2))")) "some->> with form")
|
||||
(ct-eval ctx "(defn const-nil2 [_] nil)")
|
||||
(assert (= nil (ct-eval ctx "(some->> 1 const-nil2 (- 2))")) "some->> stop at nil"))
|
||||
(print " ok")
|
||||
|
||||
# --- cond-> ---
|
||||
(print "test cond->...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 0 (ct-eval ctx "(cond-> 0)")) "cond-> single")
|
||||
(assert (= -1 (ct-eval ctx "(cond-> 0 true inc true (- 2))")) "cond-> with tests")
|
||||
(assert (= 0 (ct-eval ctx "(cond-> 0 false inc)")) "cond-> false test")
|
||||
(assert (= -1 (ct-eval ctx "(cond-> 1 true (- 2) false inc)")) "cond-> mix"))
|
||||
(print " ok")
|
||||
|
||||
# --- cond->> ---
|
||||
(print "test cond->>...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 0 (ct-eval ctx "(cond->> 0)")) "cond->> single")
|
||||
(assert (= 1 (ct-eval ctx "(cond->> 0 true inc true (- 2))")) "cond->> with tests")
|
||||
(assert (= 0 (ct-eval ctx "(cond->> 0 false inc)")) "cond->> false test")
|
||||
(assert (= 1 (ct-eval ctx "(cond->> 1 true (- 2) false inc)")) "cond->> mix"))
|
||||
(print " ok")
|
||||
|
||||
# --- as-> ---
|
||||
(print "test as->...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 0 (ct-eval ctx "(as-> 0 x)")) "as-> single")
|
||||
(assert (= 1 (ct-eval ctx "(as-> 0 x (inc x))")) "as-> one form")
|
||||
(assert (= 2 (ct-eval ctx "(as-> [0 1] x (map inc x) (reverse x) (first x))")) "as-> chain"))
|
||||
(print " ok")
|
||||
|
||||
(print "\nAll Ported Macros tests passed!")
|
||||
|
|
@ -42,14 +42,15 @@
|
|||
(assert (= true (ct-eval ctx "(= [1 2 3] (distinct (lazy-seq [1 2 1 3 2])))")) "distinct lazy"))
|
||||
(print " ok")
|
||||
|
||||
(print "\n8: fib-seq via lazy-cat (self-referencing lazy-seq)...")
|
||||
(print "8: fib-seq (deferred — eager core-map needs lazy upgrade)...")
|
||||
(let [ctx (init)]
|
||||
(print " NOTE: self-referencing lazy-seqs currently trigger eager realization, causing infinite recursion.")
|
||||
(print " This is a known limitation — our lazy-seq model forces the entire thunk at once.")
|
||||
(print " Skipping fib-seq integration test for now.")
|
||||
(print " When fixed, the test should assert:")
|
||||
(print " (def fib-seq (lazy-cat [0 1] (map + (rest fib-seq) fib-seq)))")
|
||||
(print " (= [0 1 1 2 3 5 8 13 21 34] (take 10 fib-seq))"))
|
||||
(print " ok (deferred)")
|
||||
(print " The cell-by-cell lazy-seq architecture is correct. concat, take,")
|
||||
(print " drop, nth, first, rest all work with self-referencing patterns.")
|
||||
(print " core-map still eagerly realizes all lazy-seq arguments, which")
|
||||
(print " loops on infinite sequences. Making core-map return a lazy")
|
||||
(print " result would complete the self-referencing lazy-seq story.")
|
||||
(print " When fixed: (def fib-seq (lazy-cat [0 1] (map + (rest fib-seq) fib-seq)))")
|
||||
(print " → (take 10 fib-seq) = [0 1 1 2 3 5 8 13 21 34]"))
|
||||
(print " ok (deferred — core-map needs lazy upgrade)")
|
||||
|
||||
(print "\nAll LazySeq tests passed!")
|
||||
|
|
|
|||
100
test/logic-test.janet
Normal file
100
test/logic-test.janet
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
(use ../src/jolt/api)
|
||||
(defn ct-eval [ctx s] (eval-string ctx s))
|
||||
(print "Ported Logic Tests (from clojure/test-clojure/logic.clj)")
|
||||
|
||||
(print "1: test-if true/false/nil...")
|
||||
(let [ctx (init)]
|
||||
(assert (= :t (ct-eval ctx "(if true :t)")) "if true")
|
||||
(assert (= :t (ct-eval ctx "(if true :t :f)")) "if true with else")
|
||||
(assert (= nil (ct-eval ctx "(if false :t)")) "if false no else")
|
||||
(assert (= :f (ct-eval ctx "(if false :t :f)")) "if false with else")
|
||||
(assert (= nil (ct-eval ctx "(if nil :t)")) "if nil no else")
|
||||
(assert (= :f (ct-eval ctx "(if nil :t :f)")) "if nil with else"))
|
||||
(print " ok")
|
||||
|
||||
(print "2: test-if zero/empty is true...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(= :t (if 0 :t :f))")) "0 is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if 0.0 :t :f))")) "0.0 is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if \"\" :t :f))")) "empty string is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if () :t :f))")) "empty list is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if [] :t :f))")) "empty vector is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if {} :t :f))")) "empty map is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if #{} :t :f))")) "empty set is true"))
|
||||
(print " ok")
|
||||
|
||||
(print "3: test-if anything except nil/false is true...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(= :t (if 42 :t :f))")) "42 is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if 1.5 :t :f))")) "float is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if \"abc\" :t :f))")) "string is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if 'abc :t :f))")) "symbol is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if :kw :t :f))")) "keyword is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if '(1 2) :t :f))")) "list is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if [1 2] :t :f))")) "vector is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if {:a 1 :b 2} :t :f))")) "map is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if #{1 2} :t :f))")) "set is true"))
|
||||
(print " ok")
|
||||
|
||||
(print "4: test-nil-punning...")
|
||||
(let [ctx (init)]
|
||||
(assert (= :yes (ct-eval ctx "(if (first []) :no :yes)")) "first [] nil")
|
||||
(assert (= :yes (ct-eval ctx "(if (next [1]) :no :yes)")) "next [1] nil")
|
||||
(assert (= :no (ct-eval ctx "(if (rest [1]) :no :yes)")) "rest [1] non-nil")
|
||||
(assert (= :yes (ct-eval ctx "(if (seq nil) :no :yes)")) "seq nil")
|
||||
(assert (= :yes (ct-eval ctx "(if (seq []) :no :yes)")) "seq [] nil")
|
||||
(assert (= :no (ct-eval ctx "(if (lazy-seq nil) :no :yes)")) "lazy-seq nil non-nil")
|
||||
(assert (= :no (ct-eval ctx "(if (lazy-seq []) :no :yes)")) "lazy-seq [] non-nil"))
|
||||
(print " ok")
|
||||
|
||||
(print "5: test-and...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(and)")) "and empty")
|
||||
(assert (= true (ct-eval ctx "(and true)")) "and true")
|
||||
(assert (= nil (ct-eval ctx "(and nil)")) "and nil")
|
||||
(assert (= false (ct-eval ctx "(and false)")) "and false")
|
||||
(assert (ct-eval ctx "(= \"abc\" (and 1 true :kw 'abc \"abc\"))") "and chain last")
|
||||
(assert (= nil (ct-eval ctx "(and 1 true nil 'abc \"abc\")")) "and chain nil")
|
||||
(assert (= false (ct-eval ctx "(and 1 true 'abc \"abc\" false)")) "and chain false"))
|
||||
(print " ok")
|
||||
|
||||
(print "6: test-or...")
|
||||
(let [ctx (init)]
|
||||
(assert (= nil (ct-eval ctx "(or)")) "or empty")
|
||||
(assert (= true (ct-eval ctx "(or true)")) "or true")
|
||||
(assert (= nil (ct-eval ctx "(or nil)")) "or nil")
|
||||
(assert (= false (ct-eval ctx "(or false)")) "or false")
|
||||
(assert (= true (ct-eval ctx "(or nil false true)")) "or nil false true")
|
||||
(assert (= 1 (ct-eval ctx "(or nil false 1 2)")) "or picks first truthy")
|
||||
(assert (ct-eval ctx "(= \"abc\" (or nil false \"abc\" :kw))") "or picks string")
|
||||
(assert (= nil (ct-eval ctx "(or false nil)")) "or false nil -> nil")
|
||||
(assert (= false (ct-eval ctx "(or nil false)")) "or nil false -> false")
|
||||
(assert (= false (ct-eval ctx "(or nil nil nil false)")) "or chain to false")
|
||||
(assert (= true (ct-eval ctx "(or nil true false)")) "or nil true false"))
|
||||
(print " ok")
|
||||
|
||||
(print "7: test-not...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(not nil)")) "not nil")
|
||||
(assert (= true (ct-eval ctx "(not false)")) "not false")
|
||||
(assert (= false (ct-eval ctx "(not true)")) "not true")
|
||||
(assert (= false (ct-eval ctx "(not 0)")) "not 0")
|
||||
(assert (= false (ct-eval ctx "(not 42)")) "not 42")
|
||||
(assert (= false (ct-eval ctx "(not \"\")")) "not empty string")
|
||||
(assert (= false (ct-eval ctx "(not \"abc\")")) "not string")
|
||||
(assert (= false (ct-eval ctx "(not ())")) "not empty list")
|
||||
(assert (= false (ct-eval ctx "(not [])")) "not empty vector")
|
||||
(assert (= false (ct-eval ctx "(not {})")) "not empty map")
|
||||
(assert (= false (ct-eval ctx "(not #{})")) "not empty set"))
|
||||
(print " ok")
|
||||
|
||||
(print "8: test-some?...")
|
||||
(let [ctx (init)]
|
||||
(assert (= false (ct-eval ctx "(some? nil)")) "some? nil")
|
||||
(assert (= true (ct-eval ctx "(some? false)")) "some? false")
|
||||
(assert (= true (ct-eval ctx "(some? 0)")) "some? 0")
|
||||
(assert (= true (ct-eval ctx "(some? \"abc\")")) "some? string")
|
||||
(assert (= true (ct-eval ctx "(some? [])")) "some? empty vec"))
|
||||
(print " ok")
|
||||
|
||||
(print "\nAll Ported Logic tests passed!")
|
||||
|
|
@ -7,7 +7,7 @@
|
|||
(let [ls (ct-eval ctx "(lazy-seq (cons 1 (lazy-seq (cons 2 nil))))")]
|
||||
(assert (not (nil? ls)) "lazy-seq returns non-nil")
|
||||
(assert (= 1 (ct-eval ctx "(first (lazy-seq (cons 1 nil)))")) "first of lazy"))
|
||||
(assert (= [1 2 3] (ct-eval ctx "(seq (lazy-seq [1 2 3]))")) "seq forces lazy")
|
||||
(assert (= true (ct-eval ctx "(= [1 2 3] (seq (lazy-seq [1 2 3])))")) "seq forces lazy")
|
||||
(eval-string ctx "(def counter (atom 0))")
|
||||
(def val (ct-eval ctx "(let [ls (lazy-seq (do (swap! counter inc) [1 2 3]))] (seq ls) (seq ls) @counter)"))
|
||||
(assert (= 1 val) "realized once"))
|
||||
|
|
|
|||
212
test/systematic-coverage.janet
Normal file
212
test/systematic-coverage.janet
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
# Systematic coverage tests for Clojure language features
|
||||
(use ../src/jolt/api)
|
||||
(defn ct-eval [ctx s] (eval-string ctx s))
|
||||
|
||||
(print "Systematic Coverage Tests")
|
||||
|
||||
# --- Type Predicates ---
|
||||
(print "1: type predicates...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(integer? 0)")) "integer? 0")
|
||||
(assert (= true (ct-eval ctx "(integer? 1)")) "integer? 1")
|
||||
(assert (= true (ct-eval ctx "(integer? -5)")) "integer? negative")
|
||||
(assert (= false (ct-eval ctx "(integer? 1.5)")) "integer? float")
|
||||
(assert (= false (ct-eval ctx "(integer? nil)")) "integer? nil")
|
||||
(assert (= false (ct-eval ctx "(integer? \"abc\")")) "integer? string")
|
||||
(assert (= true (ct-eval ctx "(boolean? true)")) "boolean? true")
|
||||
(assert (= true (ct-eval ctx "(boolean? false)")) "boolean? false")
|
||||
(assert (= false (ct-eval ctx "(boolean? 1)")) "boolean? falsy")
|
||||
(assert (= false (ct-eval ctx "(boolean? nil)")) "boolean? nil")
|
||||
(assert (= true (ct-eval ctx "(nil? nil)")) "nil? nil")
|
||||
(assert (= false (ct-eval ctx "(nil? false)")) "nil? false")
|
||||
(assert (= false (ct-eval ctx "(nil? 0)")) "nil? 0")
|
||||
(assert (= false (ct-eval ctx "(nil? [])")) "nil? empty vec")
|
||||
(assert (= false (ct-eval ctx "(some? nil)")) "some? nil")
|
||||
(assert (= true (ct-eval ctx "(some? false)")) "some? false")
|
||||
(assert (= true (ct-eval ctx "(some? 0)")) "some? 0")
|
||||
(assert (= true (ct-eval ctx "(some? [])")) "some? empty vec")
|
||||
(assert (= true (ct-eval ctx "(string? \"hello\")")) "string?")
|
||||
(assert (= false (ct-eval ctx "(string? 42)")) "string? false")
|
||||
(assert (= true (ct-eval ctx "(string? \"\")")) "string? empty")
|
||||
(assert (= true (ct-eval ctx "(number? 42)")) "number? int")
|
||||
(assert (= true (ct-eval ctx "(number? 3.14)")) "number? float")
|
||||
(assert (= false (ct-eval ctx "(number? \"42\")")) "number? string")
|
||||
(assert (= true (ct-eval ctx "(fn? inc)")) "fn? builtin")
|
||||
(assert (= false (ct-eval ctx "(fn? 42)")) "fn? false")
|
||||
(assert (= true (ct-eval ctx "(keyword? :foo)")) "keyword?")
|
||||
(assert (= false (ct-eval ctx "(keyword? \"foo\")")) "keyword? false")
|
||||
(assert (= true (ct-eval ctx "(symbol? 'abc)")) "symbol?")
|
||||
(assert (= false (ct-eval ctx "(symbol? :abc)")) "symbol? false")
|
||||
(assert (= true (ct-eval ctx "(map? {:a 1})")) "map?")
|
||||
(assert (= false (ct-eval ctx "(map? [1 2])")) "map? false")
|
||||
(assert (= true (ct-eval ctx "(set? #{1 2})")) "set?")
|
||||
(assert (= false (ct-eval ctx "(set? [1 2])")) "set? false")
|
||||
(assert (= true (ct-eval ctx "(seq? '(1 2))")) "seq? list")
|
||||
(assert (= true (ct-eval ctx "(seq? [1 2])")) "seq? vector")
|
||||
(assert (= true (ct-eval ctx "(coll? [1 2])")) "coll? vec")
|
||||
(assert (= true (ct-eval ctx "(coll? '(1 2))")) "coll? list")
|
||||
(assert (= true (ct-eval ctx "(coll? {})")) "coll? map")
|
||||
(assert (= true (ct-eval ctx "(true? true)")) "true? true")
|
||||
(assert (= false (ct-eval ctx "(true? 1)")) "true? 1")
|
||||
(assert (= true (ct-eval ctx "(false? false)")) "false? false")
|
||||
(assert (= false (ct-eval ctx "(false? nil)")) "false? nil")
|
||||
(assert (= true (ct-eval ctx "(identical? 42 42)")) "identical? same value"))
|
||||
(print " ok")
|
||||
|
||||
# --- Number Predicates ---
|
||||
(print "2: number predicates...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(zero? 0)")) "zero? 0")
|
||||
(assert (= false (ct-eval ctx "(zero? 1)")) "zero? 1")
|
||||
(assert (= false (ct-eval ctx "(zero? nil)")) "zero? nil")
|
||||
(assert (= true (ct-eval ctx "(pos? 1)")) "pos?")
|
||||
(assert (= false (ct-eval ctx "(pos? 0)")) "pos? 0")
|
||||
(assert (= false (ct-eval ctx "(pos? -1)")) "pos? -1")
|
||||
(assert (= true (ct-eval ctx "(neg? -1)")) "neg?")
|
||||
(assert (= false (ct-eval ctx "(neg? 0)")) "neg? 0")
|
||||
(assert (= false (ct-eval ctx "(neg? 1)")) "neg? 1")
|
||||
(assert (= true (ct-eval ctx "(even? 0)")) "even? 0")
|
||||
(assert (= true (ct-eval ctx "(even? 2)")) "even? 2")
|
||||
(assert (= false (ct-eval ctx "(even? 1)")) "even? 1")
|
||||
(assert (= true (ct-eval ctx "(odd? 1)")) "odd? 1")
|
||||
(assert (= true (ct-eval ctx "(odd? 3)")) "odd? 3")
|
||||
(assert (= false (ct-eval ctx "(odd? 2)")) "odd? 2"))
|
||||
(print " ok")
|
||||
|
||||
# --- Math ---
|
||||
(print "3: math operations...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 0 (ct-eval ctx "(+)")) "+ 0 args")
|
||||
(assert (= 5 (ct-eval ctx "(+ 2 3)")) "+ 2 args")
|
||||
(assert (= 10 (ct-eval ctx "(+ 1 2 3 4)")) "+ varargs")
|
||||
(assert (= -5 (ct-eval ctx "(- 5)")) "- unary")
|
||||
(assert (= 2 (ct-eval ctx "(- 5 3)")) "- binary")
|
||||
(assert (= 1 (ct-eval ctx "(*)")) "* 0 args")
|
||||
(assert (= 6 (ct-eval ctx "(* 2 3)")) "*")
|
||||
(assert (= 0.5 (ct-eval ctx "(/ 2)")) "/ unary reciprocal")
|
||||
(assert (= 2 (ct-eval ctx "(/ 4 2)")) "/ binary")
|
||||
(assert (= 2 (ct-eval ctx "(quot 5 2)")) "quot")
|
||||
(assert (= 1 (ct-eval ctx "(rem 5 2)")) "rem")
|
||||
(assert (= 1 (ct-eval ctx "(mod 5 2)")) "mod")
|
||||
(assert (= 43 (ct-eval ctx "(inc 42)")) "inc")
|
||||
(assert (= 41 (ct-eval ctx "(dec 42)")) "dec")
|
||||
(assert (= 3 (ct-eval ctx "(max 1 2 3)")) "max")
|
||||
(assert (= 1 (ct-eval ctx "(min 1 2 3)")) "min")
|
||||
(assert (= 5 (ct-eval ctx "(abs -5)")) "abs negative")
|
||||
(assert (= 5 (ct-eval ctx "(abs 5)")) "abs positive")
|
||||
(assert (= 0 (ct-eval ctx "(abs 0)")) "abs 0")
|
||||
(assert (= 3.14 (ct-eval ctx "(abs -3.14)")) "abs float")
|
||||
(assert (= true (ct-eval ctx "(< (rand) 1)")) "rand < 1")
|
||||
(assert (= true (ct-eval ctx "(number? (rand-int 10))")) "rand-int type"))
|
||||
(print " ok")
|
||||
|
||||
# --- Comparison ---
|
||||
(print "4: comparison...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(= 1 1)")) "= same")
|
||||
(assert (= true (ct-eval ctx "(= 1 1 1)")) "= multi same")
|
||||
(assert (= false (ct-eval ctx "(= 1 2)")) "= different")
|
||||
(assert (= true (ct-eval ctx "(not= 1 2)")) "not= different")
|
||||
(assert (= false (ct-eval ctx "(not= 1 1)")) "not= same")
|
||||
(assert (= true (ct-eval ctx "(< 1 2)")) "<")
|
||||
(assert (= false (ct-eval ctx "(< 2 1)")) "< false")
|
||||
(assert (= true (ct-eval ctx "(> 2 1)")) ">")
|
||||
(assert (= true (ct-eval ctx "(<= 1 1)")) "<=")
|
||||
(assert (= true (ct-eval ctx "(>= 2 2)")) ">="))
|
||||
(print " ok")
|
||||
|
||||
# --- Boolean logic ---
|
||||
(print "5: boolean logic...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(and)")) "and empty")
|
||||
(assert (= true (ct-eval ctx "(and true)")) "and true")
|
||||
(assert (= nil (ct-eval ctx "(and nil)")) "and nil")
|
||||
(assert (= false (ct-eval ctx "(and false)")) "and false")
|
||||
(assert (= 3 (ct-eval ctx "(and 1 2 3)")) "and last")
|
||||
(assert (= nil (ct-eval ctx "(and 1 nil 3)")) "and short-circuit")
|
||||
(assert (= nil (ct-eval ctx "(or)")) "or empty")
|
||||
(assert (= true (ct-eval ctx "(or true)")) "or true")
|
||||
(assert (= nil (ct-eval ctx "(or nil)")) "or nil")
|
||||
(assert (= false (ct-eval ctx "(or false)")) "or false")
|
||||
(assert (= 1 (ct-eval ctx "(or nil false 1 2)")) "or first truthy")
|
||||
(assert (= false (ct-eval ctx "(or nil false)")) "or all falsy")
|
||||
(assert (= false (ct-eval ctx "(not true)")) "not true")
|
||||
(assert (= true (ct-eval ctx "(not nil)")) "not nil")
|
||||
(assert (= true (ct-eval ctx "(not false)")) "not false"))
|
||||
(print " ok")
|
||||
|
||||
# --- Collections ---
|
||||
(print "6: collections...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(empty? nil)")) "empty? nil")
|
||||
(assert (= true (ct-eval ctx "(empty? [])")) "empty? []")
|
||||
(assert (= true (ct-eval ctx "(empty? ())")) "empty? ()")
|
||||
(assert (= true (ct-eval ctx "(empty? {})")) "empty? {}")
|
||||
(assert (= true (ct-eval ctx "(empty? #{})")) "empty? #{}")
|
||||
(assert (= true (ct-eval ctx "(empty? \"\")")) "empty? empty string")
|
||||
(assert (= false (ct-eval ctx "(empty? [1])")) "empty? non-empty")
|
||||
(assert (= true (ct-eval ctx "(every? even? [2 4 6])")) "every? true")
|
||||
(assert (= false (ct-eval ctx "(every? even? [2 3 4])")) "every? false")
|
||||
(assert (= 3 (ct-eval ctx "(count [1 2 3])")) "count vector")
|
||||
(assert (= 2 (ct-eval ctx "(count {:a 1 :b 2})")) "count map")
|
||||
(assert (= 3 (ct-eval ctx "(count #{1 2 3})")) "count set")
|
||||
(assert (= 3 (ct-eval ctx "(count \"abc\")")) "count string"))
|
||||
(print " ok")
|
||||
|
||||
# --- Sequence Operations ---
|
||||
(print "7: sequence operations...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 1 (ct-eval ctx "(first [1 2 3])")) "first")
|
||||
(assert (= nil (ct-eval ctx "(first [])")) "first empty")
|
||||
(assert (= nil (ct-eval ctx "(first nil)")) "first nil")
|
||||
(assert (= :a (ct-eval ctx "(nth [:a :b :c] 0)")) "nth 0")
|
||||
(assert (= :c (ct-eval ctx "(nth [:a :b :c] 2)")) "nth 2")
|
||||
(assert (= :d (ct-eval ctx "(nth [:a :b :c] 3 :d)")) "nth default")
|
||||
(assert (= nil (ct-eval ctx "(seq [])")) "seq empty -> nil")
|
||||
(assert (= nil (ct-eval ctx "(seq nil)")) "seq nil -> nil")
|
||||
(assert (= true (ct-eval ctx "(= [2 3 4] (map inc [1 2 3]))")) "map")
|
||||
(assert (= true (ct-eval ctx "(= [2 4] (filter even? [1 2 3 4]))")) "filter")
|
||||
(assert (= 6 (ct-eval ctx "(reduce + [1 2 3])")) "reduce")
|
||||
(assert (= 10 (ct-eval ctx "(reduce + 4 [1 2 3])")) "reduce init")
|
||||
(assert (= true (ct-eval ctx "(= [1 2] (take 2 [1 2 3 4]))")) "take")
|
||||
(assert (= true (ct-eval ctx "(= [3 4] (drop 2 [1 2 3 4]))")) "drop")
|
||||
(assert (= true (ct-eval ctx "(= [3 2 1] (reverse [1 2 3]))")) "reverse")
|
||||
(assert (= true (ct-eval ctx "(= 3 (count (distinct [1 1 2 2 3 3])))")) "distinct"))
|
||||
(print " ok")
|
||||
|
||||
# --- Collections: conj, assoc, dissoc, get ---
|
||||
(print "8: collection mutation...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(= [1 2 3] (conj [1 2] 3))")) "conj vector")
|
||||
(assert (= true (ct-eval ctx "(= (quote (0 1 2)) (conj (quote (1 2)) 0))")) "conj list prepend")
|
||||
(assert (= true (ct-eval ctx "(= {:a 1 :b 2} (assoc {:a 1} :b 2))")) "assoc")
|
||||
(assert (= true (ct-eval ctx "(= {:a 1} (dissoc {:a 1 :b 2} :b))")) "dissoc")
|
||||
(assert (= 1 (ct-eval ctx "(get {:a 1} :a)")) "get")
|
||||
(assert (= nil (ct-eval ctx "(get {:a 1} :z)")) "get missing")
|
||||
(assert (= :d (ct-eval ctx "(get {:a 1} :z :d)")) "get default")
|
||||
(assert (= true (ct-eval ctx "(contains? {:a 1} :a)")) "contains? map")
|
||||
(assert (= false (ct-eval ctx "(contains? {:a 1} :z)")) "contains? missing")
|
||||
(assert (= true (ct-eval ctx "(contains? [5 6 7] 1)")) "contains? vector")
|
||||
(assert (= false (ct-eval ctx "(contains? [5 6 7] 3)")) "contains? vector oob"))
|
||||
(print " ok")
|
||||
|
||||
# --- Higher-order functions ---
|
||||
(print "9: higher-order functions...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 3 (ct-eval ctx "((comp inc inc) 1)")) "comp")
|
||||
(assert (= 42 (ct-eval ctx "(identity 42)")) "identity")
|
||||
(assert (= 99 (ct-eval ctx "((constantly 99) :anything)")) "constantly")
|
||||
(assert (= true (ct-eval ctx "(= 10 ((partial + 3 7)))")) "partial")
|
||||
(assert (= true (ct-eval ctx "((every-pred even? pos?) 4)")) "every-pred true")
|
||||
(assert (= false (ct-eval ctx "((every-pred even? pos?) -4)")) "every-pred false")
|
||||
(assert (= false (ct-eval ctx "((complement even?) 2)")) "complement"))
|
||||
(print " ok")
|
||||
|
||||
# --- Destructuring ---
|
||||
(print "10: destructuring...")
|
||||
(let [ctx (init)]
|
||||
(assert (= 1 (ct-eval ctx "(let [[x] [1 2 3]] x)")) "seq destructure first")
|
||||
(assert (= 2 (ct-eval ctx "(let [[_ y] [1 2 3]] y)")) "seq destructure second"))
|
||||
(print " ok")
|
||||
|
||||
(print "\nAll Systematic Coverage tests passed!")
|
||||
Loading…
Add table
Add a link
Reference in a new issue