From fb927e827aa3ef984edda10a2eba6648bcdef0c4 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Mon, 10 Jan 2022 14:37:56 -0600 Subject: [PATCH] Add test for c alignment --- test/c/ffi_test.c | 15 +++++++++++++++ test/clj/coffi/ffi_test.clj | 18 ++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/test/c/ffi_test.c b/test/c/ffi_test.c index 4d9d520..8235c1b 100644 --- a/test/c/ffi_test.c +++ b/test/c/ffi_test.c @@ -48,3 +48,18 @@ StringFactory get_downcall(int whichString) { return 0; } } + +typedef struct alignment_test { + char a; + double x; + float y; +} AlignmentTest; + +AlignmentTest get_struct() { + AlignmentTest ret = {}; + ret.a = 'x'; + ret.x = 3.14; + ret.y = 42.0; + + return ret; +} diff --git a/test/clj/coffi/ffi_test.clj b/test/clj/coffi/ffi_test.clj index 122e075..d7be985 100644 --- a/test/clj/coffi/ffi_test.clj +++ b/test/clj/coffi/ffi_test.clj @@ -1,8 +1,9 @@ (ns coffi.ffi-test (:require [clojure.test :as t] - [coffi.mem :as mem] - [coffi.ffi :as ffi])) + [coffi.ffi :as ffi] + [coffi.layout :as layout] + [coffi.mem :as mem])) (ffi/load-library "target/ffi_test.so") @@ -30,3 +31,16 @@ (t/is (= ((ffi/cfn "upcall_test" [[::ffi/fn [] ::mem/c-string]] ::mem/c-string) (fn [] "hello")) "hello"))) + +(mem/defalias ::alignment-test + (layout/with-c-layout + [::mem/struct + [[:a ::mem/char] + [:x ::mem/double] + [:y ::mem/float]]])) + +(t/deftest padding-matches + (t/is (= (dissoc ((ffi/cfn "get_struct" [] ::alignment-test)) ::layout/padding) + {:a \x + :x 3.14 + :y 42.0})))