Carp/lisp/ownership_tests.carp

328 lines
7.2 KiB
Plaintext

;; OWNERSHIP
(defn own-1 (s)
(eat-string s))
(defn test-own-1 ()
(do
(assert-eq '(:fn (:string) :void)
(sign own-1))
(assert-eq {:node :function,
:free (),
:body ()}
(ownership-analyze own-1))))
(test-own-1)
(defn own-2 (s)
(string-copy s))
(defn test-own-2 ()
(do
(assert-eq '(:fn ((:ref :string)) :string)
(sign own-2))
(assert-eq {:node :function,
:free '(),
:body ()}
(ownership-analyze own-2))))
(test-own-2)
(defn own-3 ()
(string-copy (ref (string-copy "CARP"))))
(defn test-own-3 ()
(do
(assert-eq '(:fn () :string)
(sign own-3))
(assert-eq {:node :function,
:free '({:name "_result_1" :type :string}),
:body ()}
(ownership-analyze own-3))))
(test-own-3)
(defn own-4 ()
(let [s (string-copy "CARP1")]
(string-copy "CARP2")))
(defn test-own-4 ()
(do
(assert-eq '(:fn () :string)
(sign own-4))
(assert-eq '{:node :function,
:free (),
:body {:node :let,
:free ({:name "s",
:type :string}),
:body ()}}
(ownership-analyze own-4))))
(test-own-4)
(defn own-5 ()
(let [s1 (string-copy "CARP1")
s2 (string-copy (ref s1))]
0))
(defn test-own-5 ()
(do
(assert-eq '(:fn () :int)
(sign own-5))
(assert-eq '{:node :function,
:free (),
:body {:node :let,
:free ({:name "s2",
:type :string}
{:name "s1",
:type :string}),
:body ()}}
(ownership-analyze own-5))))
(test-own-5)
(defn own-6 ()
(let [s1 (string-copy "CARP")]
(let [s2 (string-copy (ref s1))]
0)))
(defn test-own-6 ()
(do
(assert-eq '(:fn () :int)
(sign own-6))
(assert-eq '{:node :function,
:free (),
:body {:node :let,
:free ({:name "s1",
:type :string}),
:body {:node :let,
:free ({:name "s2",
:type :string}),
:body ()}}}
(ownership-analyze own-6))))
(test-own-6)
(defn own-7 ()
(let [s1 (string-copy "CARP")]
(do
(eat-string s1)
(eat-string s1))))
(defn test-own-7 ()
(assert-error error-given-away (ownership-analyze own-7)))
(test-own-7)
(defn own-8 ()
(let [s (string-copy "CARP1")]
(if true
s
(string-copy "CARP2"))))
(defn test-own-8 ()
(do
(assert-eq '(:fn () :string)
(sign own-8))
(assert-eq '{:node :function,
:free (),
:body {:node :let
:free ()
:body {:node :if,
:free-left (),
:free-right ({:name "s"
:type :string})
:body-left ()
:body-right ()}}}
(ownership-analyze own-8))))
(test-own-8)
(defn own-9 ()
(let [s (string-copy "CARP1")]
(if true
s
s)))
(defn test-own-9 ()
(do
(assert-eq '(:fn () :string)
(sign own-9))
(assert-eq '{:node :function,
:free (),
:body {:node :let
:free ()
:body {:node :if,
:free-left (),
:free-right ()
:body-left ()
:body-right ()}}}
(ownership-analyze own-9))))
(test-own-9)
(defn own-10 ()
(let [s (string-copy "CARP1")]
(if true
10
20)))
(defn test-own-10 ()
(do
(assert-eq '(:fn () :int)
(sign own-10))
(assert-eq '{:node :function,
:free (),
:body {:node :let
:free ({:name "s"
:type :string})
:body {:node :if,
:free-left (),
:free-right ()
:body-left ()
:body-right ()}}}
(ownership-analyze own-10))))
(test-own-10)
(defn own-11 ()
(let [s1 (string-copy "CARP1")
s2 (string-copy "CARP2")]
(if true
s1
s2)))
(defn test-own-11 ()
(do
(assert-eq '(:fn () :string)
(sign own-11))
(assert-eq '{:node :function,
:free (),
:body {:node :let
:free ()
:body {:node :if,
:free-left ({:name "s2"
:type :string}),
:free-right ({:name "s1"
:type :string})
:body-left ()
:body-right ()}}}
(ownership-analyze own-11))))
(test-own-11)
(defn own-12 (s1)
(let []
(if true
(itos (strlen (ref s1)))
s1)))
(defn test-own-12 ()
(do
(assert-eq '(:fn (:string) :string)
(sign own-12))
(assert-eq '{:node :function,
:free (),
:body {:node :let
:free ()
:body {:node :if,
:free-left ({:name "s1"
:type :string}),
:free-right ()
:body-left ()
:body-right ()}}}
(ownership-analyze own-12))))
(test-own-12)
(defn own-13 ()
(let [s (string-copy "CARP")
r (ref s)]
(do
(strlen r)
(eat-string s))))
(defn test-own-13 ()
(do
(assert-eq '(:fn () :void)
(sign own-13))
(assert-eq '{:node :function,
:free (),
:body {:node :let
:free ()
:body ()}}
(ownership-analyze own-13))))
(test-own-13)
(defn own-14 ()
(let [s (string-copy "CARP")
r (ref s)]
(do
(eat-string s)
(strlen r))))
(defn test-own-14 ()
(assert-error error-given-away (ownership-analyze own-14)))
(test-own-14)
(defn own-15 (s1)
(if true
(do (eat-string s1) (string-copy "eaten"))
s1))
(defn test-own-15 ()
(do
(assert-eq '(:fn (:string) :string)
(sign own-15))
(assert-eq '{:node :function,
:free (),
:body {:node :if,
:free-left (),
:free-right ()
:body-left ()
:body-right ()}}
(ownership-analyze own-15))))
(test-own-15)
;; This one is weird:
(defn own-string-weird (s)
(ref s))