From b90e2d4bc7af718729270ab81bd44c9f851154f4 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Mon, 18 Aug 2014 14:12:46 -0400 Subject: [PATCH] Add test for passing/getting packed structs with ffi. --- .../extern-fn-with-packed-struct/Makefile | 7 +++++ .../extern-fn-with-packed-struct/test.c | 9 ++++++ .../extern-fn-with-packed-struct/test.rs | 30 +++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 src/test/run-make/extern-fn-with-packed-struct/Makefile create mode 100644 src/test/run-make/extern-fn-with-packed-struct/test.c create mode 100644 src/test/run-make/extern-fn-with-packed-struct/test.rs diff --git a/src/test/run-make/extern-fn-with-packed-struct/Makefile b/src/test/run-make/extern-fn-with-packed-struct/Makefile new file mode 100644 index 000000000000..ea6971853fe9 --- /dev/null +++ b/src/test/run-make/extern-fn-with-packed-struct/Makefile @@ -0,0 +1,7 @@ +-include ../tools.mk + +all: + $(CC) -std=c99 test.c -c -o $(TMPDIR)/test.o + $(AR) rcs $(TMPDIR)/libtest.a $(TMPDIR)/test.o + $(RUSTC) test.rs -L $(TMPDIR) + $(call RUN,test) || exit 1 diff --git a/src/test/run-make/extern-fn-with-packed-struct/test.c b/src/test/run-make/extern-fn-with-packed-struct/test.c new file mode 100644 index 000000000000..1d3169404389 --- /dev/null +++ b/src/test/run-make/extern-fn-with-packed-struct/test.c @@ -0,0 +1,9 @@ +struct __attribute__((packed)) Foo { + char a; + short b; + char c; +}; + +struct Foo foo(struct Foo foo) { + return foo; +} diff --git a/src/test/run-make/extern-fn-with-packed-struct/test.rs b/src/test/run-make/extern-fn-with-packed-struct/test.rs new file mode 100644 index 000000000000..4b07b7f39f56 --- /dev/null +++ b/src/test/run-make/extern-fn-with-packed-struct/test.rs @@ -0,0 +1,30 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[packed] +#[deriving(PartialEq, Show)] +struct Foo { + a: i8, + b: i16, + c: i8 +} + +#[link(name = "test", kind = "static")] +extern { + fn foo(f: Foo) -> Foo; +} + +fn main() { + unsafe { + let a = Foo { a: 1, b: 2, c: 3 }; + let b = foo(a); + assert_eq!(a, b); + } +}