Carp/lisp/constraint_tests.carp

145 lines
5.3 KiB
Plaintext

;; CONSTRAINTS
(defn test-replace-in-list ()
(assert-eq (replace-in-list '(10 20 30) 20 "hej") '(10 "hej" 30)))
(test-replace-in-list)
(defn test-replace-from-right ()
(do
(assert-eq (replace-subst-from-right {:a :b :c :d} :b :e) {:a :e :c :d})
(assert-eq (replace-subst-from-right {:a :b :c :b} :b :e) {:a :e :c :e})
(assert-eq (replace-subst-from-right {:a (list :b :b) :c :d} :b :f) {:a (list :f :f) :c :d})))
(test-replace-from-right)
(defn test-constraint-solving-1 ()
(let [;;_ (println "\n- Constraint solving 1 -")
constraints (list {:a :int :b "t0"})
solution (solve-constraints constraints)
solution-backwards (solve-constraints (reverse constraints))]
(do
(assert-eq solution {"t0" :int})
(assert-eq solution solution-backwards))))
(defn test-constraint-solving-2 ()
(let [;;_ (println "\n- Constraint solving 2 -")
constraints (list {:a :int :b "t0"}
{:a "t1" :b "t0"})
solution (solve-constraints constraints)
;;_ (println "\n- Backwards -")
solution-backwards (solve-constraints (reverse constraints))]
(do
(assert-eq solution {"t0" :int "t1" :int})
(assert-eq solution solution-backwards))))
(defn test-constraint-solving-3 ()
(let [;;_ (println "\n- Constraint solving 3 -")
constraints (list {:a (list :bool :float) :b (list "t0" "t1")})
solution (solve-constraints constraints)
;;_ (println "\n- Backwards -")
solution-backwards (solve-constraints (reverse constraints))]
(do
(assert-eq solution {"t0" :bool "t1" :float})
(assert-eq solution solution-backwards))))
(defn test-constraint-solving-4 ()
(let [;;_ (println "\n- Constraint solving 4 -")
constraints (list {:a (list :ref "t0") :b (list :ref :string)})
solution (solve-constraints constraints)
;;_ (println "\n- Backwards -")
solution-backwards (solve-constraints (reverse constraints))]
(do
(assert-eq solution {"t0" :string})
(assert-eq solution solution-backwards))))
(defn test-constraint-solving-5 ()
(let [;;_ (println "\n- Constraint solving 5 -")
constraints (list {:a (list :ref "t0") :b "t1"}
{:a "t1" :b (list :ref :int)})
solution (solve-constraints constraints)
;;_ (println "\n- Backwards -")
solution-backwards (solve-constraints (reverse constraints))]
(do
(assert-eq solution {"t0" :int
"t1" (list :ref :int)})
(assert-eq solution solution-backwards))))
(defn test-constraint-solving-6 ()
(let [;;_ (println "\n- Constraint solving 6 -")
constraints (list {:a "t0" :b "t0"}
{:a "t0" :b "t1"}
{:a "t1" :b :float}
{:a "t1" :b "t1"})
solution (solve-constraints constraints)
;;_ (println "\n- Backwards -")
solution-backwards (solve-constraints (reverse constraints))]
(do
(assert-eq solution {"t0" :float
"t1" :float})
(assert-eq solution solution-backwards))))
(defn test-constraint-solving-7 ()
(let [;;_ (println "\n- Constraint solving 7 -")
constraints (list {:a "t0" :b "t1"}
{:a '(:Array "t1") :b '(:Array "t2")}
{:a "t2" :b :int})
solution (solve-constraints constraints)
;;_ (println "\n- Backwards -")
solution-backwards (solve-constraints (reverse constraints))]
(do
(assert-eq solution {"t0" :int
"t1" :int
"t2" :int})
(assert-eq solution solution-backwards))))
(defn test-constraint-solving-8 ()
(let [;;_ (println "\n- Constraint solving 8 -")
constraints (list
{:a '(:fn (:Array "t3")) :b "t2"}
{:a '(:fn (:Array "t1")) :b "t2"}
{:a "t3" :b :int}
)
solution (solve-constraints constraints)
;;_ (println "\n- Backwards -")
solution-backwards (solve-constraints (reverse constraints))]
(do
(assert-eq solution {
"t1" :int
"t2" '(:fn (:Array :int))
"t3" :int})
(assert-eq solution solution-backwards))))
(defn test-subst-in-nested-list []
(assert-eq
{"a" '(:foo (:goo :int))}
(replace-subst-from-right {"a" '(:foo (:goo "b"))} "b" :int)))
(defn test-constraint-solving-9 ()
(let [;;_ (println "\n- Constraint solving 8 -")
constraints (list
{:a "t3" :b :int}
{:a '(:fn (:Array "t3")) :b '(:fn "t2")}
)
solution (solve-constraints constraints)
;;_ (println "\n- Backwards -")
solution-backwards (solve-constraints (reverse constraints))
]
(do
(assert-eq solution {
"t2" '(:Array :int)
"t3" :int})
(assert-eq solution solution-backwards)
)))
(test-constraint-solving-1)
(test-constraint-solving-2)
(test-constraint-solving-3)
(test-constraint-solving-4)
(test-constraint-solving-5)
(test-constraint-solving-6)
(test-constraint-solving-7)
(test-constraint-solving-8)
(test-subst-in-nested-list)
(test-constraint-solving-9)