Carp/test/pointer.carp
Erik Svedäng 40d55562df
fix: Pointer str & prn (#1060)
* fix: Make `str` work for (Pointer a) types

* test: Make sure that the Pointer.str compiles

* fix: Remove test case, keep function around to make sure it compiles
2020-12-09 06:19:07 +01:00

53 lines
1.4 KiB
Plaintext

(load "Test.carp")
(use-all Test Pointer)
; we go to the middle of a chunk of 10 safe elements
(def x (add (Array.raw (the (Array Int) (Array.allocate 10))) 5l))
(def xa (to-long x))
(def w (width x))
(defn ref-to-ptr [r]
(the (Ptr a) (Unsafe.coerce (the (Ref a) r))))
;; Just make sure the 'str' for a Pointer compiles
;; (testing out its contents is platform specific)
(defn pointer-to-str []
(let [p (the (Ptr CChar) NULL)]
(str p)))
; these tests are sadly a little unsafe
(deftest test
(assert-equal test
4l
; we assume that the width of a char is 4
(width (Array.raw (the (Array Char) [])))
"Pointer.width works as expected"
)
(assert-equal test
(+ xa (* 3l w))
(to-long (add x 3l))
"Pointer.add works as expected"
)
(assert-equal test
(- xa (* 3l w))
(to-long (sub x 3l))
"Pointer.sub works as expected"
)
(assert-equal test
(+ xa w)
(to-long (inc x))
"Pointer.inc works as expected"
)
(assert-equal test
(- xa w)
(to-long (dec x))
"Pointer.dec works as expected"
)
(assert-equal test
(to-value (ref-to-ptr &123))
123
"Pointer.to-value works as expected"
)
)