add ffi tests with structs

This commit is contained in:
Kristin Rutenkolk 2024-10-28 22:47:38 +01:00
parent 8fb300c5c9
commit a8fca25f7c
2 changed files with 48 additions and 0 deletions

View file

@ -78,3 +78,25 @@ void test_call_with_trailing_string_arg(int a, int b, char* text) {
return;
}
typedef struct complextype {
Point x;
char y;
int z[4];
char *w;
} ComplexType;
ComplexType complexTypeTest(ComplexType a) {
ComplexType ret = {};
ret.x = a.x;
ret.x.x++;
ret.x.y++;
ret.y = a.y-1;
ret.z[0] = a.z[0];
ret.z[1] = a.z[1];
ret.z[2] = a.z[2];
ret.z[3] = a.z[3];
ret.w = "hello from c";
return ret;
}

View file

@ -70,3 +70,29 @@
(catch Throwable _t
:err))
:ok)))
(mem/defstruct Point [::mem/float x ::mem/float y])
(t/deftest can-call-with-defstruct
(t/is (= {:x 2.0 :y 2.0}
((ffi/cfn "add_points" [::Point ::Point] ::Point) (Point. 1 2) (Point. 1 0)))))
(mem/defstruct AlignmentTest [::mem/char a ::mem/double x ::mem/float y])
(t/deftest padding-matches-defstruct
(t/is (= ((ffi/cfn "get_struct" [] ::AlignmentTest))
{:a \x
:x 3.14
:y 42.0})))
(mem/defstruct ComplexType [::Point x ::mem/byte y [::mem/array ::mem/int 4] z ::mem/c-string w])
(t/deftest can-call-with-complex-defstruct
(t/are [x y] (= x (y ((ffi/cfn "complexTypeTest" [::ComplexType] ::ComplexType)
(ComplexType. (Point. 2 3) 4 (int-array [5 6 7 8]) "hello from clojure"))))
{:x {:x 3.0 :y 4.0} :y 3 :w "hello from c"} #(dissoc % :z)
[5 6 7 8] (comp vec :z)))