Carp/test/pointer.carp
Jorge Acereda 9b08b6df3c Merge
2020-05-11 16:10:35 +02:00

47 lines
1.2 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))))
; 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"
)
)