diff --git a/test/c/ffi_test.c b/test/c/ffi_test.c index 24e6959..c968e9c 100644 --- a/test/c/ffi_test.c +++ b/test/c/ffi_test.c @@ -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; +} + + diff --git a/test/clj/coffi/ffi_test.clj b/test/clj/coffi/ffi_test.clj index b462191..52be210 100644 --- a/test/clj/coffi/ffi_test.clj +++ b/test/clj/coffi/ffi_test.clj @@ -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))) + + +