forked from carp-lang/Carp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointer.carp
52 lines (47 loc) · 1.39 KB
/
pointer.carp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(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"
)
)